Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages | Examples

dblbuffer.cpp

A demo application using "doublebuffering". It also shows hot to use ParaGUI without the built-in eventloop (using a custom one).
/* Simple program: Move N sprites around on the screen as fast as possible */ #include "pgapplication.h" #include "pgwidget.h" #include "pglog.h" #include "pgbutton.h" #include "pglabel.h" #include "pgcheckbutton.h" #include "pgtheme.h" #include <ctime> #define NUM_SPRITES 100 #define MAX_SPEED 1 SDL_Surface *sprite; int numsprites; SDL_Rect *sprite_rects; SDL_Rect *positions; SDL_Rect *velocities; int sprites_visible; int done = 0; int bForeground = 0; bool handle_quit() { done = 1; return true; } bool handle_toggle() { bForeground = 1-bForeground; return true; } int LoadSprite(SDL_Surface *screen, char *file) { /* Load the sprite image */ sprite = PG_FileArchive::LoadSurface(file, false); if ( sprite == NULL ) { PG_LogMSG( "Couldn't load %s: %s", file, SDL_GetError()); return(-1); } /* Set transparent pixel as the pixel at (0,0) */ if ( sprite->format->palette ) { SDL_SetColorKey(sprite, (SDL_SRCCOLORKEY|SDL_RLEACCEL), *(Uint8 *)sprite->pixels); } /* We're ready to roll. :) */ return(0); } void MoveSprites(SDL_Surface *screen, Uint32 background) { int i, nupdates; SDL_Rect area, *position, *velocity; nupdates = 0; SDL_SetAlpha(sprite, 0, 0); /* Move the sprite, bounce at the wall, and draw */ for ( i=0; i<numsprites; ++i ) { position = &positions[i]; velocity = &velocities[i]; position->x += velocity->x; if ( (position->x < 0) || (position->x >= screen->w) ) { velocity->x = -velocity->x; position->x += velocity->x; } position->y += velocity->y; if ( (position->y < 0) || (position->y >= screen->h) ) { velocity->y = -velocity->y; position->y += velocity->y; } /* Blit the sprite onto the screen */ area = *position; SDL_BlitSurface(sprite, NULL, screen, &area); sprite_rects[nupdates++] = area; } sprites_visible = 1; } /* This is a way of telling whether or not to use hardware surfaces */ Uint32 FastestFlags(Uint32 flags, int width, int height, int bpp) { const SDL_VideoInfo *info; /* Hardware acceleration is only used in fullscreen mode */ flags |= SDL_FULLSCREEN; /* Check for various video capabilities */ info = SDL_GetVideoInfo(); if ( info->blit_hw_CC && info->blit_fill ) { /* We use accelerated colorkeying and color filling */ flags |= SDL_HWSURFACE; } /* If we have enough video memory, and will use accelerated blits directly to it, then use page flipping. */ if ( (flags & SDL_HWSURFACE) == SDL_HWSURFACE ) { /* Direct hardware blitting without double-buffering causes really bad flickering. */ if ( info->video_mem*1024 > (Uint32)(height*width*bpp/8) ) { flags |= SDL_DOUBLEBUF; } else { flags &= ~SDL_HWSURFACE; } } /* Return the flags */ return(flags); } int main(int argc, char *argv[]) { SDL_Surface *screen; Uint8 *mem; int width, height; Uint8 video_bpp; Uint32 videoflags; Uint32 background; int i; SDL_Event event; Uint32 then, now, frames; PG_Application app; numsprites = NUM_SPRITES; videoflags = SDL_SWSURFACE|SDL_DOUBLEBUF | SDL_ANYFORMAT; width = 640; height = 480; video_bpp = 16; while ( argc > 1 ) { --argc; if ( strcmp(argv[argc-1], "-width") == 0 ) { width = atoi(argv[argc]); --argc; } else if ( strcmp(argv[argc-1], "-height") == 0 ) { height = atoi(argv[argc]); --argc; } else if ( strcmp(argv[argc-1], "-bpp") == 0 ) { video_bpp = atoi(argv[argc]); videoflags &= ~SDL_ANYFORMAT; --argc; } else if ( strcmp(argv[argc], "-fast") == 0 ) { videoflags = FastestFlags(videoflags, width, height, video_bpp); } else if ( strcmp(argv[argc], "-hw") == 0 ) { videoflags ^= SDL_HWSURFACE; } else if ( strcmp(argv[argc], "-flip") == 0 ) { videoflags ^= SDL_DOUBLEBUF; } else if ( strcmp(argv[argc], "-fullscreen") == 0 ) { videoflags ^= SDL_FULLSCREEN; } else if ( isdigit(argv[argc][0]) ) { numsprites = atoi(argv[argc]); } else { PG_LogMSG( "Usage: %s [-bpp N] [-hw] [-flip] [-fast] [-fullscreen] [numsprites]", argv[0]); exit(1); } } /* Set video mode */ if(!app.InitScreen(width, height, video_bpp, videoflags)) { PG_LogERR("Couldn't set %dx%d video mode: %s", width, height, SDL_GetError()); exit(2); } PG_LogConsole::SetConsoleKey((SDLKey)0); app.SetBulkMode(true); app.LoadTheme("default"); app.LoadLayout("dblbuffer.xml"); app.SetCursor(app.GetTheme()->FindSurface("Pointer", "Pointer", "normal")); app.ShowCursor(PG_Application::SOFTWARE); // get a pointer to the "quit" button PG_Button* btn = app.GetWidget<PG_Button>("quit"); btn->sigClick.connect(slot(handle_quit)); // get the checkbutton PG_CheckButton* toggle = app.GetWidget<PG_CheckButton>("toggle"); toggle->sigClick.connect(slot(handle_toggle)); // get the label PG_Label* fps = app.GetWidget<PG_Label>("fps"); screen = app.GetScreen(); /* Load the sprite */ if ( LoadSprite(screen, "icon.bmp") < 0 ) { exit(1); } /* Allocate memory for the sprite info */ mem = (Uint8 *)malloc(4*sizeof(SDL_Rect)*numsprites); if ( mem == NULL ) { PG_LogMSG("Out of memory!"); exit(2); } sprite_rects = (SDL_Rect *)mem; positions = sprite_rects; sprite_rects += numsprites; velocities = sprite_rects; sprite_rects += numsprites; srand(time(NULL)); for ( i=0; i<numsprites; ++i ) { positions[i].x = rand()%screen->w; positions[i].y = rand()%screen->h; positions[i].w = sprite->w; positions[i].h = sprite->h; velocities[i].x = 0; velocities[i].y = 0; while ( ! velocities[i].x && ! velocities[i].y ) { velocities[i].x = (rand()%(MAX_SPEED*2+1))-MAX_SPEED; velocities[i].y = (rand()%(MAX_SPEED*2+1))-MAX_SPEED; } } background = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00); /* Print out information about our surfaces */ PG_LogMSG("Screen is at %d bits per pixel",screen->format->BitsPerPixel); if ( (screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) { PG_LogMSG("Screen is in video memory"); } else { PG_LogMSG("Screen is in system memory"); } if ( (screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) { PG_LogMSG("Screen has double-buffering enabled"); } if ( (sprite->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) { PG_LogMSG("Sprite is in video memory"); } else { PG_LogMSG("Sprite is in system memory"); } /* Run a sample blit to trigger blit acceleration */ { SDL_Rect dst; dst.x = 0; dst.y = 0; dst.w = sprite->w; dst.h = sprite->h; SDL_BlitSurface(sprite, NULL, screen, &dst); SDL_FillRect(screen, &dst, background); } if ( (sprite->flags & SDL_HWACCEL) == SDL_HWACCEL ) { PG_LogMSG("Sprite blit uses hardware acceleration"); } if ( (sprite->flags & SDL_RLEACCEL) == SDL_RLEACCEL ) { PG_LogMSG("Sprite blit uses RLE acceleration"); } /* Loop, blitting sprites and waiting for a keystroke */ frames = 0; then = SDL_GetTicks(); done = 0; sprites_visible = 0; while ( !done ) { /* Check for events */ ++frames; while ( SDL_PollEvent(&event) ) { app.PumpIntoEventQueue(&event); } now = SDL_GetTicks(); if ( now > then+1000 ) { fps->SetTextFormat("%2.2f FPS", ((double)frames*1000)/(now-then)); if((now-then) > 1000) { then = now; frames=0; } } /* Erase all the sprites if necessary */ if ( sprites_visible ) { SDL_FillRect(screen, NULL, background); } if(bForeground) { PG_Widget::BulkBlit(); MoveSprites(screen, background); } else { MoveSprites(screen, background); PG_Widget::BulkBlit(); } /* Update the screen! */ SDL_Flip(screen); } free(mem); /* Print out some timing information */ now = SDL_GetTicks(); if ( now > then ) { PG_LogMSG("%2.2f frames per second", ((double)frames*1000)/(now-then)); } return(0); }
00001 00002 /* Simple program: Move N sprites around on the screen as fast as possible */ 00003 00004 #include "pgapplication.h" 00005 #include "pgwidget.h" 00006 #include "pglog.h" 00007 #include "pgbutton.h" 00008 #include "pglabel.h" 00009 #include "pgcheckbutton.h" 00010 #include "pgtheme.h" 00011 00012 #include <ctime> 00013 00014 #define NUM_SPRITES 100 00015 #define MAX_SPEED 1 00016 00017 SDL_Surface *sprite; 00018 int numsprites; 00019 SDL_Rect *sprite_rects; 00020 SDL_Rect *positions; 00021 SDL_Rect *velocities; 00022 int sprites_visible; 00023 int done = 0; 00024 int bForeground = 0; 00025 00026 bool handle_quit() { 00027 done = 1; 00028 return true; 00029 } 00030 00031 bool handle_toggle() { 00032 bForeground = 1-bForeground; 00033 return true; 00034 } 00035 00036 int LoadSprite(SDL_Surface *screen, char *file) 00037 { 00038 /* Load the sprite image */ 00039 sprite = PG_FileArchive::LoadSurface(file, false); 00040 if ( sprite == NULL ) { 00041 PG_LogMSG( "Couldn't load %s: %s", file, SDL_GetError()); 00042 return(-1); 00043 } 00044 00045 /* Set transparent pixel as the pixel at (0,0) */ 00046 if ( sprite->format->palette ) { 00047 SDL_SetColorKey(sprite, (SDL_SRCCOLORKEY|SDL_RLEACCEL), 00048 *(Uint8 *)sprite->pixels); 00049 } 00050 00051 /* We're ready to roll. :) */ 00052 return(0); 00053 } 00054 00055 void MoveSprites(SDL_Surface *screen, Uint32 background) 00056 { 00057 int i, nupdates; 00058 SDL_Rect area, *position, *velocity; 00059 00060 nupdates = 0; 00061 00062 SDL_SetAlpha(sprite, 0, 0); 00063 00064 /* Move the sprite, bounce at the wall, and draw */ 00065 for ( i=0; i<numsprites; ++i ) { 00066 position = &positions[i]; 00067 velocity = &velocities[i]; 00068 position->x += velocity->x; 00069 if ( (position->x < 0) || (position->x >= screen->w) ) { 00070 velocity->x = -velocity->x; 00071 position->x += velocity->x; 00072 } 00073 position->y += velocity->y; 00074 if ( (position->y < 0) || (position->y >= screen->h) ) { 00075 velocity->y = -velocity->y; 00076 position->y += velocity->y; 00077 } 00078 00079 /* Blit the sprite onto the screen */ 00080 area = *position; 00081 SDL_BlitSurface(sprite, NULL, screen, &area); 00082 sprite_rects[nupdates++] = area; 00083 } 00084 00085 sprites_visible = 1; 00086 } 00087 00088 /* This is a way of telling whether or not to use hardware surfaces */ 00089 Uint32 FastestFlags(Uint32 flags, int width, int height, int bpp) 00090 { 00091 const SDL_VideoInfo *info; 00092 00093 /* Hardware acceleration is only used in fullscreen mode */ 00094 flags |= SDL_FULLSCREEN; 00095 00096 /* Check for various video capabilities */ 00097 info = SDL_GetVideoInfo(); 00098 if ( info->blit_hw_CC && info->blit_fill ) { 00099 /* We use accelerated colorkeying and color filling */ 00100 flags |= SDL_HWSURFACE; 00101 } 00102 /* If we have enough video memory, and will use accelerated 00103 blits directly to it, then use page flipping. 00104 */ 00105 if ( (flags & SDL_HWSURFACE) == SDL_HWSURFACE ) { 00106 /* Direct hardware blitting without double-buffering 00107 causes really bad flickering. 00108 */ 00109 if ( info->video_mem*1024 > (Uint32)(height*width*bpp/8) ) { 00110 flags |= SDL_DOUBLEBUF; 00111 } else { 00112 flags &= ~SDL_HWSURFACE; 00113 } 00114 } 00115 00116 /* Return the flags */ 00117 return(flags); 00118 } 00119 00120 int main(int argc, char *argv[]) 00121 { 00122 SDL_Surface *screen; 00123 Uint8 *mem; 00124 int width, height; 00125 Uint8 video_bpp; 00126 Uint32 videoflags; 00127 Uint32 background; 00128 int i; 00129 SDL_Event event; 00130 Uint32 then, now, frames; 00131 00132 PG_Application app; 00133 00134 numsprites = NUM_SPRITES; 00135 videoflags = SDL_SWSURFACE|SDL_DOUBLEBUF | SDL_ANYFORMAT; 00136 width = 640; 00137 height = 480; 00138 video_bpp = 16; 00139 while ( argc > 1 ) { 00140 --argc; 00141 if ( strcmp(argv[argc-1], "-width") == 0 ) { 00142 width = atoi(argv[argc]); 00143 --argc; 00144 } else 00145 if ( strcmp(argv[argc-1], "-height") == 0 ) { 00146 height = atoi(argv[argc]); 00147 --argc; 00148 } else 00149 if ( strcmp(argv[argc-1], "-bpp") == 0 ) { 00150 video_bpp = atoi(argv[argc]); 00151 videoflags &= ~SDL_ANYFORMAT; 00152 --argc; 00153 } else 00154 if ( strcmp(argv[argc], "-fast") == 0 ) { 00155 videoflags = FastestFlags(videoflags, width, height, video_bpp); 00156 } else 00157 if ( strcmp(argv[argc], "-hw") == 0 ) { 00158 videoflags ^= SDL_HWSURFACE; 00159 } else 00160 if ( strcmp(argv[argc], "-flip") == 0 ) { 00161 videoflags ^= SDL_DOUBLEBUF; 00162 } else 00163 if ( strcmp(argv[argc], "-fullscreen") == 0 ) { 00164 videoflags ^= SDL_FULLSCREEN; 00165 } else 00166 if ( isdigit(argv[argc][0]) ) { 00167 numsprites = atoi(argv[argc]); 00168 } else { 00169 PG_LogMSG( 00170 "Usage: %s [-bpp N] [-hw] [-flip] [-fast] [-fullscreen] [numsprites]", 00171 argv[0]); 00172 exit(1); 00173 } 00174 } 00175 00176 /* Set video mode */ 00177 if(!app.InitScreen(width, height, video_bpp, videoflags)) { 00178 PG_LogERR("Couldn't set %dx%d video mode: %s", 00179 width, height, SDL_GetError()); 00180 exit(2); 00181 } 00182 00183 PG_LogConsole::SetConsoleKey((SDLKey)0); 00184 00185 app.SetBulkMode(true); 00186 app.LoadTheme("default"); 00187 00188 app.LoadLayout("dblbuffer.xml"); 00189 app.SetCursor(app.GetTheme()->FindSurface("Pointer", "Pointer", "normal")); 00190 app.ShowCursor(PG_Application::SOFTWARE); 00191 00192 // get a pointer to the "quit" button 00193 PG_Button* btn = app.GetWidget<PG_Button>("quit"); 00194 btn->sigClick.connect(slot(handle_quit)); 00195 00196 // get the checkbutton 00197 PG_CheckButton* toggle = app.GetWidget<PG_CheckButton>("toggle"); 00198 toggle->sigClick.connect(slot(handle_toggle)); 00199 00200 // get the label 00201 PG_Label* fps = app.GetWidget<PG_Label>("fps"); 00202 00203 screen = app.GetScreen(); 00204 00205 /* Load the sprite */ 00206 if ( LoadSprite(screen, "icon.bmp") < 0 ) { 00207 exit(1); 00208 } 00209 00210 /* Allocate memory for the sprite info */ 00211 mem = (Uint8 *)malloc(4*sizeof(SDL_Rect)*numsprites); 00212 if ( mem == NULL ) { 00213 PG_LogMSG("Out of memory!"); 00214 exit(2); 00215 } 00216 sprite_rects = (SDL_Rect *)mem; 00217 positions = sprite_rects; 00218 sprite_rects += numsprites; 00219 velocities = sprite_rects; 00220 sprite_rects += numsprites; 00221 srand(time(NULL)); 00222 for ( i=0; i<numsprites; ++i ) { 00223 positions[i].x = rand()%screen->w; 00224 positions[i].y = rand()%screen->h; 00225 positions[i].w = sprite->w; 00226 positions[i].h = sprite->h; 00227 velocities[i].x = 0; 00228 velocities[i].y = 0; 00229 while ( ! velocities[i].x && ! velocities[i].y ) { 00230 velocities[i].x = (rand()%(MAX_SPEED*2+1))-MAX_SPEED; 00231 velocities[i].y = (rand()%(MAX_SPEED*2+1))-MAX_SPEED; 00232 } 00233 } 00234 background = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00); 00235 00236 /* Print out information about our surfaces */ 00237 PG_LogMSG("Screen is at %d bits per pixel",screen->format->BitsPerPixel); 00238 if ( (screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) { 00239 PG_LogMSG("Screen is in video memory"); 00240 } else { 00241 PG_LogMSG("Screen is in system memory"); 00242 } 00243 if ( (screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) { 00244 PG_LogMSG("Screen has double-buffering enabled"); 00245 } 00246 if ( (sprite->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) { 00247 PG_LogMSG("Sprite is in video memory"); 00248 } else { 00249 PG_LogMSG("Sprite is in system memory"); 00250 } 00251 /* Run a sample blit to trigger blit acceleration */ 00252 { SDL_Rect dst; 00253 dst.x = 0; 00254 dst.y = 0; 00255 dst.w = sprite->w; 00256 dst.h = sprite->h; 00257 SDL_BlitSurface(sprite, NULL, screen, &dst); 00258 SDL_FillRect(screen, &dst, background); 00259 } 00260 if ( (sprite->flags & SDL_HWACCEL) == SDL_HWACCEL ) { 00261 PG_LogMSG("Sprite blit uses hardware acceleration"); 00262 } 00263 if ( (sprite->flags & SDL_RLEACCEL) == SDL_RLEACCEL ) { 00264 PG_LogMSG("Sprite blit uses RLE acceleration"); 00265 } 00266 00267 /* Loop, blitting sprites and waiting for a keystroke */ 00268 frames = 0; 00269 then = SDL_GetTicks(); 00270 done = 0; 00271 sprites_visible = 0; 00272 00273 while ( !done ) { 00274 /* Check for events */ 00275 ++frames; 00276 00277 while ( SDL_PollEvent(&event) ) { 00278 app.PumpIntoEventQueue(&event); 00279 } 00280 now = SDL_GetTicks(); 00281 if ( now > then+1000 ) { 00282 fps->SetTextFormat("%2.2f FPS", ((double)frames*1000)/(now-then)); 00283 00284 if((now-then) > 1000) { 00285 then = now; 00286 frames=0; 00287 } 00288 } 00289 00290 /* Erase all the sprites if necessary */ 00291 if ( sprites_visible ) { 00292 SDL_FillRect(screen, NULL, background); 00293 } 00294 00295 if(bForeground) { 00296 PG_Widget::BulkBlit(); 00297 MoveSprites(screen, background); 00298 } 00299 else { 00300 MoveSprites(screen, background); 00301 PG_Widget::BulkBlit(); 00302 } 00303 00304 /* Update the screen! */ 00305 SDL_Flip(screen); 00306 } 00307 free(mem); 00308 00309 /* Print out some timing information */ 00310 now = SDL_GetTicks(); 00311 if ( now > then ) { 00312 PG_LogMSG("%2.2f frames per second", 00313 ((double)frames*1000)/(now-then)); 00314 } 00315 return(0); 00316 }


The ParaGUI Project - Alexander Pipelka