parik

RGB Noise

Mar 2nd, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <SDL/SDL.h>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. #define FPS 30
  6.  
  7. void
  8. generateRGB_noise(SDL_Surface* surface)
  9. {
  10.   //fills all pixels in surface with random colors.
  11.   Uint32* pixels = (Uint32*) surface->pixels;
  12.   for(int x = 0; x < surface->w; ++x)
  13.     {
  14.       for(int y = 0; y < surface->h; ++y)
  15.     {
  16.       Uint8 r = rand() % 256;
  17.       Uint8 g = rand() % 256;
  18.       Uint8 b = rand() % 256;
  19.  
  20.      
  21.       pixels[( y * surface->w ) + x] = SDL_MapRGB(surface->format, r, g, b);
  22.     }
  23.     }
  24.   surface->pixels = pixels;
  25. }
  26.  
  27. int
  28. main(int argc , char** argv)
  29. {
  30.   //initialise window and screen
  31.   SDL_Init(SDL_INIT_VIDEO);
  32.   SDL_Surface* window;
  33.   window = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
  34.  
  35.   srand(time(NULL)); //set rand seed
  36.  
  37.   bool active = true;
  38.   while(active)
  39.     {
  40.       SDL_Event event;
  41.       while(SDL_PollEvent(&event))
  42.     {
  43.       if(event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE)
  44.         {
  45.           //quit on pressing escape
  46.           active = false;
  47.         }
  48.     }
  49.       generateRGB_noise(window);
  50.       SDL_Flip(window);
  51.  
  52.       SDL_Delay(1000 / FPS); //to not fry up cpu
  53.     }
  54.   SDL_Quit();
  55. }
Add Comment
Please, Sign In to add comment