diff render_sdl.c @ 54:3b79cbcf6846

Get Flavio's color bar demo kind of sort of working
author Mike Pavone <pavone@retrodev.com>
date Tue, 18 Dec 2012 02:16:42 -0800
parents 3fc57e1a2c56
children 8317f174d916
line wrap: on
line diff
--- a/render_sdl.c	Sun Dec 16 22:25:29 2012 -0800
+++ b/render_sdl.c	Tue Dec 18 02:16:42 2012 -0800
@@ -6,6 +6,8 @@
 SDL_Surface *screen;
 uint8_t render_dbg = 0;
 
+uint32_t last_frame = 0;
+
 void render_init(int width, int height)
 {
 	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
@@ -31,6 +33,7 @@
 	uint16_t *buf_16;
 	uint32_t *buf_32; 
 	uint8_t b,g,r;
+	last_frame = SDL_GetTicks();
 	if (SDL_MUSTLOCK(screen)) {
 		if (SDL_LockSurface(screen) < 0) {
 			return;
@@ -43,7 +46,6 @@
     } else {
     	repeat_y = repeat_x;
     }
-    printf("w: %d, h: %d, repeat_x: %d, repeat_y: %d\n", screen->clip_rect.w, screen->clip_rect.h, repeat_x, repeat_y);
     switch (screen->format->BytesPerPixel) {
     case 2:
         buf_16 = (uint16_t *)screen->pixels;
@@ -151,3 +153,38 @@
 	}
 }
 
+#define FRAME_DELAY 16
+#define MIN_DELAY 10
+
+void wait_render_frame(vdp_context * context)
+{
+	SDL_Event event;
+	while(SDL_PollEvent(&event)) {
+		switch (event.type) {
+		case SDL_KEYDOWN:
+			//TODO: Update emulated gamepads
+			if (event.key.keysym.sym == SDLK_LEFTBRACKET) {
+				render_dbg = !render_dbg;
+				render_context(context);
+			}
+			break;
+		case SDL_QUIT:
+			exit(0);
+		}
+	}
+	//TODO: Adjust frame delay so we actually get 60 FPS rather than 62.5 FPS
+	uint32_t current = SDL_GetTicks();
+	uint32_t desired = last_frame + FRAME_DELAY;
+	if (current < desired) {
+		uint32_t delay = last_frame + FRAME_DELAY - current;
+		//TODO: Calculate MIN_DELAY at runtime
+		if (delay > MIN_DELAY) {
+			SDL_Delay((delay/MIN_DELAY)*MIN_DELAY);
+		}
+		while ((desired) < SDL_GetTicks()) {
+		}
+	}
+	render_context(context);
+}
+
+