comparison render_sdl.c @ 487:c08a4efeee7f opengl

Update opengl branch from default. Fix build breakage unrelated to merge
author Mike Pavone <pavone@retrodev.com>
date Sat, 26 Oct 2013 22:38:47 -0700
parents 7696d824489d db5880d8ea03
children 32f053ad9b02
comparison
equal deleted inserted replaced
449:7696d824489d 487:c08a4efeee7f
1 /*
2 Copyright 2013 Michael Pavone
3 This file is part of BlastEm.
4 BlastEm is free software distributed under the terms of the GNU General Public License version 3 or greater. See COPYING for full license text.
5 */
1 #include <stdlib.h> 6 #include <stdlib.h>
2 #include <stdio.h> 7 #include <stdio.h>
3 #include "render.h" 8 #include "render.h"
4 #include "blastem.h" 9 #include "blastem.h"
5 #include "io.h" 10 #include "io.h"
6 11
7 #ifndef DISABLE_OPENGL 12 #ifndef DISABLE_OPENGL
13 #define GL_GLEXT_PROTOTYPES
8 #include <GL/gl.h> 14 #include <GL/gl.h>
15 #include <GL/glext.h>
9 #endif 16 #endif
10 17
11 SDL_Surface *screen; 18 SDL_Surface *screen;
12 uint8_t render_dbg = 0; 19 uint8_t render_dbg = 0;
13 uint8_t debug_pal = 0; 20 uint8_t debug_pal = 0;
76 } 83 }
77 84
78 SDL_Joystick * joysticks[MAX_JOYSTICKS]; 85 SDL_Joystick * joysticks[MAX_JOYSTICKS];
79 int num_joysticks; 86 int num_joysticks;
80 87
88 int render_num_joysticks()
89 {
90 return num_joysticks;
91 }
92
81 uint32_t render_map_color(uint8_t r, uint8_t g, uint8_t b) 93 uint32_t render_map_color(uint8_t r, uint8_t g, uint8_t b)
82 { 94 {
83 if (render_gl) { 95 if (render_gl) {
84 return b << 24 | g << 16 | r << 8 | 255; 96 return b << 24 | g << 16 | r << 8 | 255;
85 } else { 97 } else {
111 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 123 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
112 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 124 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
113 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 125 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
114 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 126 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
115 if (i < 2) { 127 if (i < 2) {
116 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8​, 512, 256, 0, GL_BGRA, GL_UNSIGNED_BYTE, i ? context->evenbuf : context->oddbuf); 128 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 512, 256, 0, GL_BGRA, GL_UNSIGNED_BYTE, i ? context->evenbuf : context->oddbuf);
117 } else { 129 } else {
118 uint32_t blank = 255; 130 uint32_t blank = 255;
119 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0, GL_BGRA, GL_UNSIGNED_BYTE, &blank); 131 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0, GL_BGRA, GL_UNSIGNED_BYTE, &blank);
120 } 132 }
121 } 133 }
133 uint8_t render_depth() 145 uint8_t render_depth()
134 { 146 {
135 return screen->format->BytesPerPixel * 8; 147 return screen->format->BytesPerPixel * 8;
136 } 148 }
137 149
138 void render_init(int width, int height, char * title, uint32_t fps, uint8_t use_gl) 150 char * caption = NULL;
151
152 void render_init(int width, int height, char * title, uint32_t fps, uint8_t fullscreen, uint8_t use_gl)
139 { 153 {
140 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) < 0) { 154 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) < 0) {
141 fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); 155 fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
142 exit(1); 156 exit(1);
143 } 157 }
144 atexit(SDL_Quit); 158 atexit(SDL_Quit);
145 atexit(render_close_audio); 159 atexit(render_close_audio);
146 printf("width: %d, height: %d\n", width, height); 160 printf("width: %d, height: %d\n", width, height);
147 uint32_t flags 161 uint32_t flags = SDL_ANYFORMAT;
148 #ifndef DISABLE_OPENGL 162 #ifndef DISABLE_OPENGL
149 if (use_gl) 163 if (use_gl)
150 { 164 {
151 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); 165 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
152 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5); 166 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
156 flags = SDL_OPENGL; 170 flags = SDL_OPENGL;
157 } else { 171 } else {
158 #else 172 #else
159 { 173 {
160 #endif 174 #endif
161 flags = SDL_SWSURFACE | SDL_ANYFORMAT; 175 if (fullscreen) {
176 flags |= SDL_FULLSCREEN | SDL_HWSURFACE | SDL_DOUBLEBUF;
177 } else {
178 flags |= SDL_SWSURFACE;
179 }
162 } 180 }
163 screen = SDL_SetVideoMode(width, height, 32, flags); 181 screen = SDL_SetVideoMode(width, height, 32, flags);
164 if (!screen) { 182 if (!screen) {
165 fprintf(stderr, "Unable to get SDL surface: %s\n", SDL_GetError()); 183 fprintf(stderr, "Unable to get SDL surface: %s\n", SDL_GetError());
166 exit(1); 184 exit(1);
172 #ifndef DISABLE_OPENGL 190 #ifndef DISABLE_OPENGL
173 //TODO: Fallback to plain SDL if OpenGL 2.0 not available 191 //TODO: Fallback to plain SDL if OpenGL 2.0 not available
174 render_gl = use_gl; 192 render_gl = use_gl;
175 #endif 193 #endif
176 SDL_WM_SetCaption(title, title); 194 SDL_WM_SetCaption(title, title);
195 caption = title;
177 min_delay = 0; 196 min_delay = 0;
178 for (int i = 0; i < 100; i++) { 197 for (int i = 0; i < 100; i++) {
179 uint32_t start = SDL_GetTicks(); 198 uint32_t start = SDL_GetTicks();
180 SDL_Delay(1); 199 SDL_Delay(1);
181 uint32_t delay = SDL_GetTicks()-start; 200 uint32_t delay = SDL_GetTicks()-start;
194 psg_cond = SDL_CreateCond(); 213 psg_cond = SDL_CreateCond();
195 ym_cond = SDL_CreateCond(); 214 ym_cond = SDL_CreateCond();
196 audio_ready = SDL_CreateCond(); 215 audio_ready = SDL_CreateCond();
197 216
198 SDL_AudioSpec desired, actual; 217 SDL_AudioSpec desired, actual;
199 desired.freq = 48000; 218 char * rate_str = tern_find_ptr(config, "audiorate");
219 int rate = rate_str ? atoi(rate_str) : 0;
220 if (!rate) {
221 rate = 48000;
222 }
223 desired.freq = rate;
200 desired.format = AUDIO_S16SYS; 224 desired.format = AUDIO_S16SYS;
201 desired.channels = 2; 225 desired.channels = 2;
202 desired.samples = 2048;//1024; 226 char * samples_str = tern_find_ptr(config, "audiobuffer");
227 int samples = samples_str ? atoi(samples_str) : 0;
228 if (!samples) {
229 samples = 512;
230 }
231 printf("config says: %d\n", samples);
232 desired.samples = samples*2;
203 desired.callback = audio_callback; 233 desired.callback = audio_callback;
204 desired.userdata = NULL; 234 desired.userdata = NULL;
205 235
206 if (SDL_OpenAudio(&desired, &actual) < 0) { 236 if (SDL_OpenAudio(&desired, &actual) < 0) {
207 fprintf(stderr, "Unable to open SDL audio: %s\n", SDL_GetError()); 237 fprintf(stderr, "Unable to open SDL audio: %s\n", SDL_GetError());
296 } 326 }
297 } 327 }
298 if ( SDL_MUSTLOCK(screen) ) { 328 if ( SDL_MUSTLOCK(screen) ) {
299 SDL_UnlockSurface(screen); 329 SDL_UnlockSurface(screen);
300 } 330 }
301 SDL_UpdateRect(screen, 0, 0, screen->clip_rect.w, screen->clip_rect.h); 331 //SDL_UpdateRect(screen, 0, 0, screen->clip_rect.w, screen->clip_rect.h);
332 SDL_Flip(screen);
302 if (context->regs[REG_MODE_4] & BIT_INTERLACE) 333 if (context->regs[REG_MODE_4] & BIT_INTERLACE)
303 { 334 {
304 context->framebuf = context->framebuf == context->oddbuf ? context->evenbuf : context->oddbuf; 335 context->framebuf = context->framebuf == context->oddbuf ? context->evenbuf : context->oddbuf;
305 } 336 }
306 } 337 }
383 exit(0); 414 exit(0);
384 } 415 }
385 return 0; 416 return 0;
386 } 417 }
387 418
419 char * fps_caption = NULL;
420
388 uint32_t frame_counter = 0; 421 uint32_t frame_counter = 0;
389 uint32_t start = 0; 422 uint32_t start = 0;
390 int wait_render_frame(vdp_context * context, int frame_limit) 423 int wait_render_frame(vdp_context * context, int frame_limit)
391 { 424 {
392 SDL_Event event; 425 SDL_Event event;
409 } 442 }
410 render_context(context); 443 render_context(context);
411 444
412 445
413 //TODO: Figure out why this causes segfaults 446 //TODO: Figure out why this causes segfaults
414 /*frame_counter++; 447 frame_counter++;
415 if ((last_frame - start) > 1000) { 448 if ((last_frame - start) > 1000) {
416 if (start && (last_frame-start)) { 449 if (start && (last_frame-start)) {
417 printf("\r%f fps", ((float)frame_counter) / (((float)(last_frame-start)) / 1000.0)); 450 if (!fps_caption) {
451 fps_caption = malloc(strlen(caption) + strlen(" - 1000.1 fps") + 1);
452 }
453 sprintf(fps_caption, "%s - %.1f fps", caption, ((float)frame_counter) / (((float)(last_frame-start)) / 1000.0));
454 SDL_WM_SetCaption(fps_caption, caption);
418 fflush(stdout); 455 fflush(stdout);
419 } 456 }
420 start = last_frame; 457 start = last_frame;
421 frame_counter = 0; 458 frame_counter = 0;
422 }*/ 459 }
423 return ret; 460 return ret;
424 } 461 }
425 462
426 void process_events() 463 void process_events()
427 { 464 {