Guest User

Untitled

a guest
Jun 25th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. // main.cpp
  2.  
  3. #include <stdio.h>
  4. #include <SDL.h>
  5.  
  6. #define WIDTH 640
  7. #define HEIGHT 480
  8. #define BPP 4
  9. #define DEPTH 32
  10.  
  11. void SetPixel(SDL_Surface *screen, int x, int y, Uint8 r, Uint8 g, Uint8 b)
  12. {
  13.     int ytimesw = y*screen->pitch/BPP;
  14.     Uint32 *pixmem32 = (Uint32*) screen->pixels + ytimesw + x;
  15.     *pixmem32 = SDL_MapRGB( screen->format, r, g, b );
  16. }
  17.  
  18. void ClearSurface(SDL_Surface* surf, Uint8 r, Uint8 g, Uint8 b)
  19. {
  20.     int w = surf->w;
  21.     int h = surf->h;
  22.     for(int y=0 ; y < h ; y++)
  23.     {
  24.         for(int x=0 ; x < w ; x++)
  25.         {
  26.             SetPixel(surf, x, y, r, g ,b);
  27.         }
  28.     }
  29. }
  30.  
  31. void DrawScreen(SDL_Surface* screen)
  32. {
  33.     for(int y=0 ; y < HEIGHT ; y++)
  34.     {
  35.         for(int x=0 ; x < WIDTH ; x++)
  36.         {
  37.             float fr = 255.0f * float(x) / float(WIDTH-1);
  38.             float fg = 255.0f * float(y) / float(HEIGHT-1);
  39.             SetPixel(screen, x, y, (Uint8)fr, (Uint8)fg, 0x00);
  40.         }
  41.     }
  42. }
  43.  
  44.  
  45. int main(int argc, char* argv[])
  46. {
  47.     SDL_Surface *screen;
  48.     SDL_Event evt;
  49.  
  50.     int keypress = 0;
  51.     int h=0;
  52.  
  53.     if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1;
  54.    
  55.     if (!(screen = SDL_SetVideoMode(WIDTH, HEIGHT, DEPTH, SDL_HWSURFACE)))
  56.     {
  57.         SDL_Quit();
  58.         return 1;
  59.     }
  60.  
  61.     while(!keypress)
  62.     {
  63.         bool bSkip = false;
  64.         if(SDL_MUSTLOCK(screen))
  65.             if(SDL_LockSurface(screen) < 0)
  66.                 bSkip = true;
  67.         if(!bSkip)
  68.         {
  69.             DrawScreen(screen);
  70.  
  71.             if(SDL_MUSTLOCK(screen))
  72.                 SDL_UnlockSurface(screen);
  73.  
  74.             SDL_Flip(screen);
  75.         }
  76.  
  77.         while(SDL_PollEvent(&evt))
  78.         {      
  79.             switch (evt.type)
  80.             {
  81.             case SDL_QUIT:
  82.                 keypress = 1;
  83.                 break;
  84.             //case SDL_KEYDOWN:
  85.             //  keypress = 1;
  86.             //  break;
  87.             default:
  88.                 break;
  89.             }
  90.         }
  91.     }
  92.  
  93.     SDL_Quit();
  94.  
  95.     return 0;
  96. }
Add Comment
Please, Sign In to add comment