comparison render_sdl.c @ 486:db5880d8ea03

Add an FPS counter to the title bar
author Mike Pavone <pavone@retrodev.com>
date Thu, 03 Oct 2013 21:22:05 -0700
parents 140af5509ce7
children c08a4efeee7f
comparison
equal deleted inserted replaced
485:b449af228c63 486:db5880d8ea03
91 uint8_t render_depth() 91 uint8_t render_depth()
92 { 92 {
93 return screen->format->BytesPerPixel * 8; 93 return screen->format->BytesPerPixel * 8;
94 } 94 }
95 95
96 char * caption = NULL;
97
96 void render_init(int width, int height, char * title, uint32_t fps, uint8_t fullscreen) 98 void render_init(int width, int height, char * title, uint32_t fps, uint8_t fullscreen)
97 { 99 {
98 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) < 0) { 100 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) < 0) {
99 fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); 101 fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
100 exit(1); 102 exit(1);
116 if (screen->format->BytesPerPixel != 2 && screen->format->BytesPerPixel != 4) { 118 if (screen->format->BytesPerPixel != 2 && screen->format->BytesPerPixel != 4) {
117 fprintf(stderr, "BlastEm requires a 16-bit or 32-bit surface, SDL returned a %d-bit surface\n", screen->format->BytesPerPixel * 8); 119 fprintf(stderr, "BlastEm requires a 16-bit or 32-bit surface, SDL returned a %d-bit surface\n", screen->format->BytesPerPixel * 8);
118 exit(1); 120 exit(1);
119 } 121 }
120 SDL_WM_SetCaption(title, title); 122 SDL_WM_SetCaption(title, title);
123 caption = title;
121 min_delay = 0; 124 min_delay = 0;
122 for (int i = 0; i < 100; i++) { 125 for (int i = 0; i < 100; i++) {
123 uint32_t start = SDL_GetTicks(); 126 uint32_t start = SDL_GetTicks();
124 SDL_Delay(1); 127 SDL_Delay(1);
125 uint32_t delay = SDL_GetTicks()-start; 128 uint32_t delay = SDL_GetTicks()-start;
324 exit(0); 327 exit(0);
325 } 328 }
326 return 0; 329 return 0;
327 } 330 }
328 331
332 char * fps_caption = NULL;
333
329 uint32_t frame_counter = 0; 334 uint32_t frame_counter = 0;
330 uint32_t start = 0; 335 uint32_t start = 0;
331 int wait_render_frame(vdp_context * context, int frame_limit) 336 int wait_render_frame(vdp_context * context, int frame_limit)
332 { 337 {
333 SDL_Event event; 338 SDL_Event event;
347 while ((desired) >= SDL_GetTicks()) { 352 while ((desired) >= SDL_GetTicks()) {
348 } 353 }
349 } 354 }
350 } 355 }
351 render_context(context); 356 render_context(context);
352 357
353 358
354 //TODO: Figure out why this causes segfaults 359 //TODO: Figure out why this causes segfaults
355 /*frame_counter++; 360 frame_counter++;
356 if ((last_frame - start) > 1000) { 361 if ((last_frame - start) > 1000) {
357 if (start && (last_frame-start)) { 362 if (start && (last_frame-start)) {
358 printf("\r%f fps", ((float)frame_counter) / (((float)(last_frame-start)) / 1000.0)); 363 if (!fps_caption) {
364 fps_caption = malloc(strlen(caption) + strlen(" - 1000.1 fps") + 1);
365 }
366 sprintf(fps_caption, "%s - %.1f fps", caption, ((float)frame_counter) / (((float)(last_frame-start)) / 1000.0));
367 SDL_WM_SetCaption(fps_caption, caption);
359 fflush(stdout); 368 fflush(stdout);
360 } 369 }
361 start = last_frame; 370 start = last_frame;
362 frame_counter = 0; 371 frame_counter = 0;
363 }*/ 372 }
364 return ret; 373 return ret;
365 } 374 }
366 375
367 void process_events() 376 void process_events()
368 { 377 {