Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //http://www.youtube.com/user/thecplusplusguy
- //render only the perimiter of SDL_Rect
- #include <SDL/SDL.h>
- #include <iostream>
- #include <cmath>
- void setPixel(int x,int y,int r,int g,int b)
- {
- SDL_Surface* screen=SDL_GetVideoSurface();
- if(x>=0 && x<screen->w && y>=0 && y<=screen->h)
- ((Uint32*)screen->pixels)[y*screen->pitch/4+x]=SDL_MapRGB(screen->format,r,g,b);
- }
- void verticalLine(int y1,int y2,int x)
- {
- for(int i=y1;i<y2;i++)
- setPixel(x,i,255,255,255);
- }
- void horizontalLine(int x1,int x2,int y)
- {
- for(int i=x1;i<x2;i++)
- setPixel(i,y,255,255,255);
- }
- void renderPerimiter(SDL_Rect* rec)
- {
- verticalLine(rec->y,rec->y+rec->h,rec->x);
- verticalLine(rec->y,rec->y+rec->h,rec->x+rec->w);
- horizontalLine(rec->x,rec->x+rec->w,rec->y);
- horizontalLine(rec->x,rec->x+rec->w,rec->y+rec->h);
- }
- int main(int argc, char** argv)
- {
- SDL_Init(SDL_INIT_EVERYTHING);
- SDL_Surface *screen;
- screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
- // screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE|SDL_FULLSCREEN);
- bool running = true;
- const int FPS = 30;
- Uint32 start;
- SDL_Rect rect;
- rect.x = 10;
- rect.y = 10;
- rect.w = 20;
- rect.h = 20;
- Uint32 color = SDL_MapRGB(screen->format, 0,0,0);
- Uint32 color2 = SDL_MapRGB(screen->format, 0,0,0);
- int x=0,y=0;
- while(running) {
- start = SDL_GetTicks();
- SDL_Event event;
- while(SDL_PollEvent(&event)) {
- switch(event.type) {
- case SDL_QUIT:
- running = false;
- break;
- }
- }
- //logic && render
- SDL_FillRect(screen, &screen->clip_rect, color);
- //SDL_FillRect(screen, &rect, color2);
- renderPerimiter(&rect);
- SDL_Flip(screen);
- if(1000/FPS > SDL_GetTicks()-start) {
- SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
- }
- }
- SDL_Quit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement