2 // VIDEO.CPP: SDL/local hardware specific video routines
8 #include "sdlemu_opengl.h"
12 // External global variables
14 //shouldn't these exist here??? Prolly.
15 //And now, they do! :-)
16 SDL_Surface * surface, * mainSurface;
17 Uint32 mainSurfaceFlags;
20 SDL_Joystick * joystick;
22 // One of the reasons why OpenGL is slower then normal SDL rendering, is because
23 // the data is being pumped into the buffer every frame with a overflow as result.
24 // So, we going tot render every 1 frame instead of every 0 frame.
26 // [Shamus] This isn't the case. OpenGL is slower because 60 frames a second is a
27 // lot of data to pump through the system. In any case, frameskip is probably
28 // a good idea for now, since most systems are probably too slow to run at
29 // 60 FPS. But doing so will have some nasty side effects in some games.
30 // You have been warned!
32 int frame_ticker = vjs.frameSkip;
35 // Create SDL/OpenGL surfaces
39 // Get proper info about the platform we're running on...
40 const SDL_VideoInfo * info = SDL_GetVideoInfo();
44 WriteLog("VJ: SDL is unable to get the video info: %s\n", SDL_GetError());
50 // Initializing SDL attributes with OpenGL
51 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
52 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
53 mainSurfaceFlags = SDL_OPENGL;
58 if (info->hw_available)
59 mainSurfaceFlags = SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF;
62 mainSurfaceFlags |= SDL_HWACCEL;
66 mainSurfaceFlags |= SDL_FULLSCREEN;
68 /* if (!vjs.useOpenGL)
69 // mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH, VIRTUAL_SCREEN_HEIGHT_NTSC, 16, mainSurfaceFlags);
70 mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH,
71 (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL),
72 16, mainSurfaceFlags);
74 // When OpenGL is used, we're going to use a standard resolution of 640x480.
75 // This way we have good scaling functionality and when the screen is resized
76 // because of the NTSC <-> PAL resize, we only have to re-create the texture
77 // instead of initializing the entire OpenGL texture en screens.
78 mainSurface = SDL_SetVideoMode(640, 480, 16, mainSurfaceFlags);//*/
81 // mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH, VIRTUAL_SCREEN_HEIGHT_NTSC, 16, mainSurfaceFlags);
82 mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH,
83 (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL),
84 32, mainSurfaceFlags);
86 // When OpenGL is used, we're going to use a standard resolution of 640x480.
87 // This way we have good scaling functionality and when the screen is resized
88 // because of the NTSC <-> PAL resize, we only have to re-create the texture
89 // instead of initializing the entire OpenGL texture en screens.
90 mainSurface = SDL_SetVideoMode(640, 480, 32, mainSurfaceFlags);//*/
92 if (mainSurface == NULL)
94 WriteLog("VJ: SDL is unable to set the video mode: %s\n", SDL_GetError());
98 SDL_WM_SetCaption("Virtual Jaguar", "Virtual Jaguar");
100 // Create the primary SDL display (16 BPP, 5/5/5 RGB format)
101 /* surface = SDL_CreateRGBSurface(SDL_SWSURFACE, VIRTUAL_SCREEN_WIDTH,
102 (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL),
103 16, 0x7C00, 0x03E0, 0x001F, 0);//*/
105 surface = SDL_CreateRGBSurface(SDL_SWSURFACE, VIRTUAL_SCREEN_WIDTH,
106 (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL), 32,
107 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
108 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
110 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);
115 WriteLog("VJ: Could not create primary SDL surface: %s\n", SDL_GetError());
120 // Let us setup OpenGL and our rendering texture. We give the src (surface) and the
121 // dst (mainSurface) display as well as the automatic bpp selection as options so that
122 // our texture is automaticly created :)
123 sdlemu_init_opengl(surface, mainSurface, 1 /*method*/,
124 vjs.glFilter /*texture type (linear, nearest)*/,
125 0 /* Automatic bpp selection based upon src */);
127 // Initialize Joystick support under SDL
131 if (SDL_NumJoysticks() <= 0)
133 vjs.useJoystick = false;
134 printf("VJ: No joystick(s) or joypad(s) detected on your system. Using keyboard...\n");
138 if ((joystick = SDL_JoystickOpen(vjs.joyport)) == 0)
140 vjs.useJoystick = false;
141 printf("VJ: Unable to open a Joystick on port: %d\n", (int)vjs.joyport);
144 printf("VJ: Using: %s\n", SDL_JoystickName(vjs.joyport));
148 // Set up the backbuffer
149 //To be safe, this should be 1280 * 625 * 2...
150 // backbuffer = (int16 *)malloc(845 * 525 * sizeof(int16));
151 /* backbuffer = (int16 *)malloc(1280 * 625 * sizeof(int16));
152 // memset(backbuffer, 0x44, VIRTUAL_SCREEN_WIDTH * VIRTUAL_SCREEN_HEIGHT_NTSC * sizeof(int16));
153 memset(backbuffer, 0x44, VIRTUAL_SCREEN_WIDTH *
154 (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL)
155 * sizeof(int16));//*/
157 backbuffer = (uint32 *)malloc(1280 * 625 * sizeof(uint32));
158 // memset(backbuffer, 0x44, VIRTUAL_SCREEN_WIDTH * VIRTUAL_SCREEN_HEIGHT_NTSC * sizeof(int16));
159 memset(backbuffer, 0x44, VIRTUAL_SCREEN_WIDTH *
160 (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL)
167 // Free various SDL components
172 sdlemu_close_opengl();
174 SDL_JoystickClose(joystick);
175 SDL_FreeSurface(surface);
180 // Render the backbuffer to the primary screen surface
182 void RenderBackbuffer(void)
184 if (SDL_MUSTLOCK(surface))
185 while (SDL_LockSurface(surface) < 0)
188 // memcpy(surface->pixels, backbuffer, tom_getVideoModeWidth() * tom_getVideoModeHeight() * 2);
189 memcpy(surface->pixels, backbuffer, tom_getVideoModeWidth() * tom_getVideoModeHeight() * 4);
191 if (SDL_MUSTLOCK(surface))
192 SDL_UnlockSurface(surface);
196 // One of the reasons why OpenGL is slower then normal SDL rendering, is because
197 // the data is being pumped into the buffer every frame with a overflow as result.
198 // So, we going to render every 1 fps instead of every 0 fps.
199 // [Shamus] This is isn't why it's slower--see top of file for explanation... ;-)
200 if (frame_ticker == 0)
202 sdlemu_draw_texture(mainSurface, surface, 1/*1=GL_QUADS*/);
203 frame_ticker = vjs.frameSkip; // Reset frame_ticker
210 SDL_Rect rect = { 0, 0, surface->w, surface->h };
211 SDL_BlitSurface(surface, &rect, mainSurface, &rect);
212 SDL_Flip(mainSurface);
217 // Resize the main SDL screen & backbuffer
219 void ResizeScreen(uint32 width, uint32 height)
221 char window_title[256];
223 SDL_FreeSurface(surface);
224 // surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 16, 0x7C00, 0x03E0, 0x001F, 0);
226 surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,
227 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
228 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
230 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);
235 WriteLog("Video: Could not create primary SDL surface: %s", SDL_GetError());
236 //This is just crappy. We shouldn't exit this way--it leaves all kinds of memory leaks
237 //as well as screwing up SDL... !!! FIX !!!
243 // Recreate the texture because of the NTSC <-> PAL screen resize.
244 sdlemu_create_texture(surface, mainSurface, vjs.glFilter, 0);
248 mainSurface = SDL_SetVideoMode(width, height, 16, mainSurfaceFlags);
250 if (mainSurface == NULL)
252 WriteLog("Video: SDL is unable to set the video mode: %s\n", SDL_GetError());
257 sprintf(window_title, "Virtual Jaguar (%i x %i)", (int)width, (int)height);
258 SDL_WM_SetCaption(window_title, window_title);
262 // Return the screen's pitch
264 uint32 GetSDLScreenPitch(void)
266 return surface->pitch;
270 // Return the screen's width in pixels
272 uint32 GetSDLScreenWidthInPixels(void)
274 return surface->pitch / 4; // Pitch / 4 since we're in 32BPP mode
278 // Fullscreen <-> window switching
280 void ToggleFullscreen(void)
282 //NOTE: This does *NOT* work with OpenGL rendering! !!! FIX !!!
284 return; // Until we can fix it...
286 vjs.fullscreen = !vjs.fullscreen;
287 mainSurfaceFlags &= ~SDL_FULLSCREEN;
290 mainSurfaceFlags |= SDL_FULLSCREEN;
292 mainSurface = SDL_SetVideoMode(tom_width, tom_height, 16, mainSurfaceFlags);
294 if (mainSurface == NULL)
296 WriteLog("Video: SDL is unable to set the video mode: %s\n", SDL_GetError());
300 SDL_WM_SetCaption("Virtual Jaguar", "Virtual Jaguar");