2 // VIDEO.CPP: SDL/local hardware specific video routines
5 // (C) 2010 Underground Software
7 // JLH = James L. Hammons <jlhamm@acm.org>
10 // --- ---------- -------------------------------------------------------------
11 // JLH 01/16/2010 Created this log ;-)
16 //#include "gui.h" // For "finished"
19 #include "sdlemu_opengl.h"
22 // External global variables
24 //shouldn't these exist here??? Prolly.
25 //And now, they do! :-)
26 SDL_Surface * surface, * mainSurface;
27 SDL_Joystick * joystick1;
28 Uint32 mainSurfaceFlags;
32 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
33 #define RMASK 0xFF000000
34 #define GMASK 0x00FF0000
35 #define BMASK 0x0000FF00
36 #define AMASK 0x000000FF
38 #define RMASK 0x000000FF
39 #define GMASK 0x0000FF00
40 #define BMASK 0x00FF0000
41 #define AMASK 0xFF000000
45 // One of the reasons why OpenGL is slower then normal SDL rendering, is because
46 // the data is being pumped into the buffer every frame with a overflow as result.
47 // So, we going tot render every 1 frame instead of every 0 frame.
49 // [Shamus] This isn't the case. OpenGL is slower because 60 frames a second is a
50 // lot of data to pump through the system. In any case, frameskip is probably
51 // a good idea for now, since most systems are probably too slow to run at
52 // 60 FPS. But doing so will have some nasty side effects in some games.
53 // You have been warned!
58 // Create SDL/OpenGL surfaces
62 // Get proper info about the platform we're running on...
63 const SDL_VideoInfo * info = SDL_GetVideoInfo();
67 WriteLog("VJ: SDL is unable to get the video info: %s\n", SDL_GetError());
73 // Initializing SDL attributes with OpenGL
74 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
75 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
76 mainSurfaceFlags = SDL_OPENGL;
80 if (info->hw_available)
81 mainSurfaceFlags = SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF;
84 mainSurfaceFlags |= SDL_HWACCEL;
88 mainSurfaceFlags |= SDL_FULLSCREEN;
90 /* if (!vjs.useOpenGL)
91 // mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH, VIRTUAL_SCREEN_HEIGHT_NTSC, 16, mainSurfaceFlags);
92 mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH,
93 (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL),
94 16, mainSurfaceFlags);
96 // When OpenGL is used, we're going to use a standard resolution of 640x480.
97 // This way we have good scaling functionality and when the screen is resized
98 // because of the NTSC <-> PAL resize, we only have to re-create the texture
99 // instead of initializing the entire OpenGL texture en screens.
100 mainSurface = SDL_SetVideoMode(640, 480, 16, mainSurfaceFlags);//*/
103 // mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH, VIRTUAL_SCREEN_HEIGHT_NTSC, 16, mainSurfaceFlags);
104 mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH,
105 (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL),
106 32, mainSurfaceFlags);
108 // When OpenGL is used, we're going to use a standard resolution of 640x480.
109 // This way we have good scaling functionality and when the screen is resized
110 // because of the NTSC <-> PAL resize, we only have to re-create the texture
111 // instead of initializing the entire OpenGL texture en screens.
112 mainSurface = SDL_SetVideoMode(640, 480, 32, mainSurfaceFlags);//*/
114 if (mainSurface == NULL)
116 WriteLog("VJ: SDL is unable to set the video mode: %s\n", SDL_GetError());
120 SDL_WM_SetCaption("Virtual Jaguar", "Virtual Jaguar");
122 // Create the primary SDL display (16 BPP, 5/5/5 RGB format)
123 /* surface = SDL_CreateRGBSurface(SDL_SWSURFACE, VIRTUAL_SCREEN_WIDTH,
124 (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL),
125 16, 0x7C00, 0x03E0, 0x001F, 0);//*/
127 uint32 vsWidth = (vjs.renderType == RT_TV ? 1280 : VIRTUAL_SCREEN_WIDTH),
128 vsHeight = (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL);
130 // if (vjs.renderType == RT_TV)
133 // surface = SDL_CreateRGBSurface(SDL_SWSURFACE, VIRTUAL_SCREEN_WIDTH,
134 surface = SDL_CreateRGBSurface(SDL_SWSURFACE, vsWidth, vsHeight, 32, RMASK, GMASK, BMASK, AMASK);
138 WriteLog("VJ: Could not create primary SDL surface: %s\n", SDL_GetError());
143 // Let us setup OpenGL and our rendering texture. We give the src (surface) and the
144 // dst (mainSurface) display as well as the automatic bpp selection as options so that
145 // our texture is automatically created :)
146 sdlemu_init_opengl(surface, mainSurface, 1 /*method*/,
147 vjs.glFilter /*texture type (linear, nearest)*/,
148 0 /* Automatic bpp selection based upon src */);
150 // Initialize Joystick support under SDL
154 if (SDL_NumJoysticks() <= 0)
156 vjs.useJoystick = false;
157 printf("VJ: No joystick(s) or joypad(s) detected on your system. Using keyboard...\n");
161 if ((joystick1 = SDL_JoystickOpen(vjs.joyport)) == 0)
163 vjs.useJoystick = false;
164 printf("VJ: Unable to open a Joystick on port: %d\n", (int)vjs.joyport);
167 printf("VJ: Using: %s\n", SDL_JoystickName(vjs.joyport));
171 // Set up the backbuffer
172 // To be safe, this should be 1280 * 625 * 2...
173 backbuffer = (uint32 *)malloc(1280 * 625 * sizeof(uint32));
174 memset(backbuffer, 0x44, VIRTUAL_SCREEN_WIDTH *
175 (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL)
182 // Free various SDL components
187 sdlemu_close_opengl();
189 SDL_JoystickClose(joystick1);
190 SDL_FreeSurface(surface);
195 // Render the backbuffer to the primary screen surface
197 void RenderBackbuffer(void)
199 // Handle frameskip *before* we do any heavy lifting here...
201 if (frame_ticker > 0)
207 frame_ticker = vjs.frameSkip; // Reset frame_ticker
209 if (SDL_MUSTLOCK(surface))
210 while (SDL_LockSurface(surface) < 0)
213 // memcpy(surface->pixels, backbuffer, tom_getVideoModeWidth() * tom_getVideoModeHeight() * 2);
214 // This memcpy is expensive--do some profiling to see what the impact is!
215 if (vjs.renderType == RT_NORMAL)
216 memcpy(surface->pixels, backbuffer, TOMGetVideoModeWidth() * TOMGetVideoModeHeight() * 4);
217 else if (vjs.renderType == RT_TV)
218 memcpy(surface->pixels, backbuffer, 1280 * TOMGetVideoModeHeight() * 4);
220 if (SDL_MUSTLOCK(surface))
221 SDL_UnlockSurface(surface);
224 // One of the reasons why OpenGL is slower then normal SDL rendering, is because
225 // the data is being pumped into the buffer every frame with a overflow as result.
226 // So, we going to render every 1 fps instead of every 0 fps.
227 // [Shamus] This is isn't why it's slower--see top of file for explanation... ;-)
228 //The problem lies in this function...
229 sdlemu_draw_texture(mainSurface, surface, 1/*1=GL_QUADS*/);
232 SDL_Rect rect = { 0, 0, surface->w, surface->h };
233 SDL_BlitSurface(surface, &rect, mainSurface, &rect);
234 SDL_Flip(mainSurface);
239 // Resize the main SDL screen & backbuffer
241 void ResizeScreen(uint32 width, uint32 height)
243 SDL_FreeSurface(surface);
244 surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, RMASK, GMASK, BMASK, AMASK);
248 WriteLog("Video: Could not create primary SDL surface: %s", SDL_GetError());
249 //This is just crappy. We shouldn't exit this way--it leaves all kinds of memory leaks
250 //as well as screwing up SDL... !!! FIX !!!
252 // OK, this is cleaner. We can't continue if there is no surface created!
253 #warning "!!! FIX !!! (finished = true)"
259 // Recreate the texture because of the NTSC <-> PAL screen resize.
260 //Not sure why this is here...
261 //Is it because of the resized surface up above?
262 sdlemu_create_texture(surface, mainSurface, vjs.glFilter, 0);
266 mainSurface = SDL_SetVideoMode(width, height, 32, mainSurfaceFlags);
268 if (mainSurface == NULL)
270 WriteLog("Video: SDL is unable to set the video mode: %s\n", SDL_GetError());
271 // Don't exit because we can't resize!
276 char window_title[64];
278 sprintf(window_title, "Virtual Jaguar (%i x %i)", (int)width, (int)height);
279 SDL_WM_SetCaption((vjs.useOpenGL ? "Virtual Jaguar (OpenGL)" : window_title), "Virtual Jaguar");
283 // Return the screen's width in pixels
285 uint32 GetSDLScreenWidthInPixels(void)
287 return surface->pitch / 4; // Pitch / 4 since we're in 32BPP mode
291 // Fullscreen <-> window switching
293 void ToggleFullscreen(void)
295 // Set our internal variable, then toggle the SDL flag
296 vjs.fullscreen = !vjs.fullscreen;
297 mainSurfaceFlags ^= SDL_FULLSCREEN;
298 // mainSurfaceFlags = (vjs.fullscreen ? mainSurfaceFlags | SDL_FULLSCREEN :
299 // mainSurfaceFlags & ~SDL_FULLSCREEN);
301 // mainSurfaceFlags &= ~SDL_FULLSCREEN;
303 // if (vjs.fullscreen)
304 // mainSurfaceFlags |= SDL_FULLSCREEN;
308 // When OpenGL is used, we're going to use a standard resolution of 640x480.
309 // This way we have good scaling functionality and when the screen is resized
310 // because of the NTSC <-> PAL resize, we only have to re-create the texture
311 // instead of initializing the entire OpenGL texture en screens.
312 mainSurface = SDL_SetVideoMode(640, 480, 32, mainSurfaceFlags);
314 // Reset viewport, etc.
315 glViewport(0, 0, mainSurface->w, mainSurface->h);
316 glMatrixMode(GL_PROJECTION);
319 glOrtho(0.0, (GLdouble)mainSurface->w, (GLdouble)mainSurface->h, 0.0, 0.0, 1.0);
320 glMatrixMode(GL_MODELVIEW);
325 mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH,
326 (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL),
327 32, mainSurfaceFlags);
329 if (mainSurface == NULL)
331 WriteLog("Video: SDL was unable to switch the video to %s: %s\n", (vjs.fullscreen ? "fullscreen" : "windowed"), SDL_GetError());
332 // Shouldn't exit because we can't switch! BAD!!!
337 SDL_WM_SetCaption((vjs.useOpenGL ? "Virtual Jaguar (OpenGL)" : "Virtual Jaguar"), "Virtual Jaguar");