2 // VIDEO.CPP: SDL/local hardware specific video routines
10 #include "sdlemu_opengl.h"
14 // External global variables
16 //shouldn't these exist here??? Prolly.
17 //And now, they do! :-)
18 SDL_Surface * surface, * mainSurface;
19 SDL_Joystick * joystick;
20 Uint32 mainSurfaceFlags;
24 // One of the reasons why OpenGL is slower then normal SDL rendering, is because
25 // the data is being pumped into the buffer every frame with a overflow as result.
26 // So, we going tot render every 1 frame instead of every 0 frame.
28 // [Shamus] This isn't the case. OpenGL is slower because 60 frames a second is a
29 // lot of data to pump through the system. In any case, frameskip is probably
30 // a good idea for now, since most systems are probably too slow to run at
31 // 60 FPS. But doing so will have some nasty side effects in some games.
32 // You have been warned!
34 int frame_ticker = vjs.frameSkip;
37 // Create SDL/OpenGL surfaces
41 // Get proper info about the platform we're running on...
42 const SDL_VideoInfo * info = SDL_GetVideoInfo();
46 WriteLog("VJ: SDL is unable to get the video info: %s\n", SDL_GetError());
52 // Initializing SDL attributes with OpenGL
53 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
54 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
55 mainSurfaceFlags = SDL_OPENGL;
60 if (info->hw_available)
61 mainSurfaceFlags = SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF;
64 mainSurfaceFlags |= SDL_HWACCEL;
68 mainSurfaceFlags |= SDL_FULLSCREEN;
70 /* if (!vjs.useOpenGL)
71 // mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH, VIRTUAL_SCREEN_HEIGHT_NTSC, 16, mainSurfaceFlags);
72 mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH,
73 (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL),
74 16, mainSurfaceFlags);
76 // When OpenGL is used, we're going to use a standard resolution of 640x480.
77 // This way we have good scaling functionality and when the screen is resized
78 // because of the NTSC <-> PAL resize, we only have to re-create the texture
79 // instead of initializing the entire OpenGL texture en screens.
80 mainSurface = SDL_SetVideoMode(640, 480, 16, mainSurfaceFlags);//*/
83 // mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH, VIRTUAL_SCREEN_HEIGHT_NTSC, 16, mainSurfaceFlags);
84 mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH,
85 (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL),
86 32, mainSurfaceFlags);
88 // When OpenGL is used, we're going to use a standard resolution of 640x480.
89 // This way we have good scaling functionality and when the screen is resized
90 // because of the NTSC <-> PAL resize, we only have to re-create the texture
91 // instead of initializing the entire OpenGL texture en screens.
92 mainSurface = SDL_SetVideoMode(640, 480, 32, mainSurfaceFlags);//*/
94 if (mainSurface == NULL)
96 WriteLog("VJ: SDL is unable to set the video mode: %s\n", SDL_GetError());
100 SDL_WM_SetCaption("Virtual Jaguar", "Virtual Jaguar");
102 // Create the primary SDL display (16 BPP, 5/5/5 RGB format)
103 /* surface = SDL_CreateRGBSurface(SDL_SWSURFACE, VIRTUAL_SCREEN_WIDTH,
104 (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL),
105 16, 0x7C00, 0x03E0, 0x001F, 0);//*/
107 uint32 vsWidth = VIRTUAL_SCREEN_WIDTH;
109 if (vjs.renderType == RT_TV)
112 // surface = SDL_CreateRGBSurface(SDL_SWSURFACE, VIRTUAL_SCREEN_WIDTH,
113 surface = SDL_CreateRGBSurface(SDL_SWSURFACE, vsWidth,
114 (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL), 32,
115 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
116 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
118 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);
123 WriteLog("VJ: Could not create primary SDL surface: %s\n", SDL_GetError());
128 // Let us setup OpenGL and our rendering texture. We give the src (surface) and the
129 // dst (mainSurface) display as well as the automatic bpp selection as options so that
130 // our texture is automaticly created :)
131 sdlemu_init_opengl(surface, mainSurface, 1 /*method*/,
132 vjs.glFilter /*texture type (linear, nearest)*/,
133 0 /* Automatic bpp selection based upon src */);
135 // Initialize Joystick support under SDL
139 if (SDL_NumJoysticks() <= 0)
141 vjs.useJoystick = false;
142 printf("VJ: No joystick(s) or joypad(s) detected on your system. Using keyboard...\n");
146 if ((joystick = SDL_JoystickOpen(vjs.joyport)) == 0)
148 vjs.useJoystick = false;
149 printf("VJ: Unable to open a Joystick on port: %d\n", (int)vjs.joyport);
152 printf("VJ: Using: %s\n", SDL_JoystickName(vjs.joyport));
156 // Set up the backbuffer
157 //To be safe, this should be 1280 * 625 * 2...
158 // backbuffer = (int16 *)malloc(845 * 525 * sizeof(int16));
159 /* backbuffer = (int16 *)malloc(1280 * 625 * sizeof(int16));
160 // memset(backbuffer, 0x44, VIRTUAL_SCREEN_WIDTH * VIRTUAL_SCREEN_HEIGHT_NTSC * sizeof(int16));
161 memset(backbuffer, 0x44, VIRTUAL_SCREEN_WIDTH *
162 (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL)
163 * sizeof(int16));//*/
165 backbuffer = (uint32 *)malloc(1280 * 625 * sizeof(uint32));
166 // memset(backbuffer, 0x44, VIRTUAL_SCREEN_WIDTH * VIRTUAL_SCREEN_HEIGHT_NTSC * sizeof(int16));
167 memset(backbuffer, 0x44, VIRTUAL_SCREEN_WIDTH *
168 (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL)
175 // Free various SDL components
180 sdlemu_close_opengl();
182 SDL_JoystickClose(joystick);
183 SDL_FreeSurface(surface);
188 // Render the backbuffer to the primary screen surface
190 void RenderBackbuffer(void)
192 if (SDL_MUSTLOCK(surface))
193 while (SDL_LockSurface(surface) < 0)
196 // memcpy(surface->pixels, backbuffer, tom_getVideoModeWidth() * tom_getVideoModeHeight() * 2);
197 if (vjs.renderType == RT_NORMAL)
198 memcpy(surface->pixels, backbuffer, tom_getVideoModeWidth() * tom_getVideoModeHeight() * 4);
199 else if (vjs.renderType == RT_TV)
200 memcpy(surface->pixels, backbuffer, 1280 * tom_getVideoModeHeight() * 4);
202 if (SDL_MUSTLOCK(surface))
203 SDL_UnlockSurface(surface);
207 // One of the reasons why OpenGL is slower then normal SDL rendering, is because
208 // the data is being pumped into the buffer every frame with a overflow as result.
209 // So, we going to render every 1 fps instead of every 0 fps.
210 // [Shamus] This is isn't why it's slower--see top of file for explanation... ;-)
211 if (frame_ticker == 0)
213 sdlemu_draw_texture(mainSurface, surface, 1/*1=GL_QUADS*/);
214 frame_ticker = vjs.frameSkip; // Reset frame_ticker
221 SDL_Rect rect = { 0, 0, surface->w, surface->h };
222 SDL_BlitSurface(surface, &rect, mainSurface, &rect);
223 SDL_Flip(mainSurface);
228 // Resize the main SDL screen & backbuffer
230 void ResizeScreen(uint32 width, uint32 height)
232 char window_title[256];
234 SDL_FreeSurface(surface);
235 // surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 16, 0x7C00, 0x03E0, 0x001F, 0);
237 surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,
238 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
239 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
241 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);
246 WriteLog("Video: Could not create primary SDL surface: %s", SDL_GetError());
247 //This is just crappy. We shouldn't exit this way--it leaves all kinds of memory leaks
248 //as well as screwing up SDL... !!! FIX !!!
254 // Recreate the texture because of the NTSC <-> PAL screen resize.
255 sdlemu_create_texture(surface, mainSurface, vjs.glFilter, 0);
259 mainSurface = SDL_SetVideoMode(width, height, 32, mainSurfaceFlags);
261 if (mainSurface == NULL)
263 WriteLog("Video: SDL is unable to set the video mode: %s\n", SDL_GetError());
268 sprintf(window_title, "Virtual Jaguar (%i x %i)", (int)width, (int)height);
269 SDL_WM_SetCaption(window_title, window_title);
273 // Return the screen's width in pixels
275 uint32 GetSDLScreenWidthInPixels(void)
277 return surface->pitch / 4; // Pitch / 4 since we're in 32BPP mode
281 // Fullscreen <-> window switching
283 void ToggleFullscreen(void)
285 //NOTE: This does *NOT* work with OpenGL rendering! !!! FIX !!!
287 return; // Until we can fix it...
289 vjs.fullscreen = !vjs.fullscreen;
290 mainSurfaceFlags &= ~SDL_FULLSCREEN;
293 mainSurfaceFlags |= SDL_FULLSCREEN;
295 mainSurface = SDL_SetVideoMode(tom_width, tom_height, 32, mainSurfaceFlags);
297 if (mainSurface == NULL)
299 WriteLog("Video: SDL is unable to set the video mode: %s\n", SDL_GetError());
303 SDL_WM_SetCaption("Virtual Jaguar", "Virtual Jaguar");