1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
Screen::Screen(int _width, int _height, bool _is_fullscreen, std::string title) :
is_fullscreen(_is_fullscreen), width(_width), height(_height), window(nullptr),
renderer(nullptr), noise(nullptr), noise_interval(5), noise_res(64), noise_alpha(25) {
srand(time(nullptr));
if(SDL_Init(SDL_INIT_VIDEO) == -1)
throw std::runtime_error(std::string("Error during video initialization. SDL 2.0 says: ") + SDL_GetError());
window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_SHOWN | (is_fullscreen * SDL_WINDOW_FULLSCREEN));
if(window == nullptr)
throw std::runtime_error(std::string("Error during window initialization. SDL 2.0 says: ") + SDL_GetError());
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if(renderer == nullptr)
throw std::runtime_error(std::string("Error during renderer initialization. SDL 2.0 says: ") + SDL_GetError());
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
SDL_Surface *noise_surface = SDL_CreateRGBSurface(0, noise_res + 64, noise_res + 64, 32, 0, 0, 0, 0xff);
if(noise_surface == nullptr)
throw std::runtime_error("Error generating snow.");
Uint32 *p = (Uint32*)noise_surface->pixels;
int max = (noise_res + 64) * (noise_res + 64);
for(int i = 0; i < max; ++i, ++p) {
int temp_color = rand() % 255;
// for black and white noise *p = SDL_MapRGB(noise_surface->format, temp_color, temp_color, temp_color);
*p = SDL_MapRGB(noise_surface->format, rand() % 255, rand() % 255, rand() % 255);
}
noise = SDL_CreateTextureFromSurface(renderer, noise_surface);
SDL_FreeSurface(noise_surface);
noise_rect = {0, 0, noise_res, noise_res};
noise_time = SDL_GetTicks();
}
|