Advertisement
thecplusplusguy

SDL tutorial 17

Jun 22nd, 2012
1,455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. #include <SDL/SDL.h>
  2.  
  3. //void putpixel(SDL_Surface* screen, int x, int y)
  4. //{
  5. //  Uint8* pixels = (Uint8*)screen->pixels;
  6. //  Uint8* pixel = pixels + y*screen->pitch + x; // offset of pointer
  7. //  *pixel = SDL_MapRGB(screen->format,0xff,0xff,0xff);
  8. //}
  9.  
  10. // 32 Bit (not yet working correctly)
  11. void putpixel(SDL_Surface* screen, int x, int y)
  12. {
  13.     Uint32* pixels = (Uint32*)screen->pixels;
  14.     Uint32* pixel = pixels + y*screen->pitch/4 + x; // offset of pointer
  15.     *pixel = SDL_MapRGB(screen->format,0xff,0xff,0xff);
  16. }
  17.  
  18. int main(int argc, char** argv)
  19. {
  20.     SDL_Init(SDL_INIT_EVERYTHING);
  21.     SDL_Surface *screen;
  22. //  screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE);
  23.     screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
  24.     bool running = true;
  25.     const int FPS = 30;
  26.     Uint32 start;
  27.     for(int i = 0; i < 100; i += 1) {
  28.         putpixel(screen,i,10);
  29.     }
  30.     putpixel(screen,20,40);
  31.     while(running) {
  32.         start = SDL_GetTicks();
  33.         SDL_Event event;
  34.         while(SDL_PollEvent(&event)) {
  35.             switch(event.type) {
  36.                 case SDL_QUIT:
  37.                     running = false;
  38.                     break;
  39.             }
  40.         }
  41.  
  42.         //logic
  43.  
  44.         //render
  45.         SDL_Flip(screen);
  46.         if(1000/FPS > SDL_GetTicks()-start) {
  47.             SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
  48.         }
  49.     }
  50.     SDL_Quit();
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement