comparison 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
comparison
equal deleted inserted replaced
53:44e661913a51 54:3b79cbcf6846
3 #include <stdio.h> 3 #include <stdio.h>
4 #include "render.h" 4 #include "render.h"
5 5
6 SDL_Surface *screen; 6 SDL_Surface *screen;
7 uint8_t render_dbg = 0; 7 uint8_t render_dbg = 0;
8
9 uint32_t last_frame = 0;
8 10
9 void render_init(int width, int height) 11 void render_init(int width, int height)
10 { 12 {
11 if (SDL_Init(SDL_INIT_VIDEO) < 0) { 13 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
12 fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); 14 fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
29 { 31 {
30 uint8_t *buf_8; 32 uint8_t *buf_8;
31 uint16_t *buf_16; 33 uint16_t *buf_16;
32 uint32_t *buf_32; 34 uint32_t *buf_32;
33 uint8_t b,g,r; 35 uint8_t b,g,r;
36 last_frame = SDL_GetTicks();
34 if (SDL_MUSTLOCK(screen)) { 37 if (SDL_MUSTLOCK(screen)) {
35 if (SDL_LockSurface(screen) < 0) { 38 if (SDL_LockSurface(screen) < 0) {
36 return; 39 return;
37 } 40 }
38 } 41 }
41 if (repeat_x > repeat_y) { 44 if (repeat_x > repeat_y) {
42 repeat_x = repeat_y; 45 repeat_x = repeat_y;
43 } else { 46 } else {
44 repeat_y = repeat_x; 47 repeat_y = repeat_x;
45 } 48 }
46 printf("w: %d, h: %d, repeat_x: %d, repeat_y: %d\n", screen->clip_rect.w, screen->clip_rect.h, repeat_x, repeat_y);
47 switch (screen->format->BytesPerPixel) { 49 switch (screen->format->BytesPerPixel) {
48 case 2: 50 case 2:
49 buf_16 = (uint16_t *)screen->pixels; 51 buf_16 = (uint16_t *)screen->pixels;
50 for (int y = 0; y < 240; y++) { 52 for (int y = 0; y < 240; y++) {
51 for (int i = 0; i < repeat_y; i++,buf_16 += screen->pitch/2) { 53 for (int i = 0; i < repeat_y; i++,buf_16 += screen->pitch/2) {
149 return; 151 return;
150 } 152 }
151 } 153 }
152 } 154 }
153 155
156 #define FRAME_DELAY 16
157 #define MIN_DELAY 10
158
159 void wait_render_frame(vdp_context * context)
160 {
161 SDL_Event event;
162 while(SDL_PollEvent(&event)) {
163 switch (event.type) {
164 case SDL_KEYDOWN:
165 //TODO: Update emulated gamepads
166 if (event.key.keysym.sym == SDLK_LEFTBRACKET) {
167 render_dbg = !render_dbg;
168 render_context(context);
169 }
170 break;
171 case SDL_QUIT:
172 exit(0);
173 }
174 }
175 //TODO: Adjust frame delay so we actually get 60 FPS rather than 62.5 FPS
176 uint32_t current = SDL_GetTicks();
177 uint32_t desired = last_frame + FRAME_DELAY;
178 if (current < desired) {
179 uint32_t delay = last_frame + FRAME_DELAY - current;
180 //TODO: Calculate MIN_DELAY at runtime
181 if (delay > MIN_DELAY) {
182 SDL_Delay((delay/MIN_DELAY)*MIN_DELAY);
183 }
184 while ((desired) < SDL_GetTicks()) {
185 }
186 }
187 render_context(context);
188 }
189
190