Advertisement
thecplusplusguy

SDL render perimiter

May 11th, 2013
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. //http://www.youtube.com/user/thecplusplusguy
  2. //render only the perimiter of SDL_Rect
  3. #include <SDL/SDL.h>
  4. #include <iostream>
  5. #include <cmath>
  6.  
  7. void setPixel(int x,int y,int r,int g,int b)
  8. {
  9.     SDL_Surface* screen=SDL_GetVideoSurface();
  10.     if(x>=0 && x<screen->w && y>=0 && y<=screen->h)
  11.         ((Uint32*)screen->pixels)[y*screen->pitch/4+x]=SDL_MapRGB(screen->format,r,g,b);
  12. }
  13.  
  14. void verticalLine(int y1,int y2,int x)
  15. {
  16.     for(int i=y1;i<y2;i++)
  17.         setPixel(x,i,255,255,255);
  18. }
  19.  
  20. void horizontalLine(int x1,int x2,int y)
  21. {
  22.     for(int i=x1;i<x2;i++)
  23.         setPixel(i,y,255,255,255);
  24. }
  25.  
  26. void renderPerimiter(SDL_Rect* rec)
  27. {
  28.     verticalLine(rec->y,rec->y+rec->h,rec->x);
  29.     verticalLine(rec->y,rec->y+rec->h,rec->x+rec->w);
  30.     horizontalLine(rec->x,rec->x+rec->w,rec->y);
  31.     horizontalLine(rec->x,rec->x+rec->w,rec->y+rec->h);
  32. }
  33.  
  34.  
  35.  
  36. int main(int argc, char** argv)
  37. {
  38.     SDL_Init(SDL_INIT_EVERYTHING);
  39.     SDL_Surface *screen;
  40.     screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
  41. //  screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE|SDL_FULLSCREEN);
  42.     bool running = true;
  43.     const int FPS = 30;
  44.     Uint32 start;
  45.     SDL_Rect rect;
  46.     rect.x = 10;
  47.     rect.y = 10;
  48.     rect.w = 20;
  49.     rect.h = 20;
  50.     Uint32 color = SDL_MapRGB(screen->format, 0,0,0);
  51.     Uint32 color2 = SDL_MapRGB(screen->format, 0,0,0);
  52.     int x=0,y=0;
  53.     while(running) {
  54.         start = SDL_GetTicks();
  55.         SDL_Event event;
  56.         while(SDL_PollEvent(&event)) {
  57.             switch(event.type) {
  58.                 case SDL_QUIT:
  59.                     running = false;
  60.                     break;
  61.             }
  62.         }
  63.         //logic && render
  64.  
  65.         SDL_FillRect(screen, &screen->clip_rect, color);
  66.         //SDL_FillRect(screen, &rect, color2);
  67.         renderPerimiter(&rect);
  68.         SDL_Flip(screen);
  69.  
  70.         if(1000/FPS > SDL_GetTicks()-start) {
  71.             SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
  72.         }
  73.     }
  74.     SDL_Quit();
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement