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;
19 SDL_Joystick * joystick;
21 // One of the reasons why OpenGL is slower then normal SDL rendering, is because
22 // the data is being pumped into the buffer every frame with a overflow as result.
23 // So, we going tot render every 1 frame instead of every 0 frame.
25 // [Shamus] This isn't the case. OpenGL is slower because 60 frames a second is a
26 // lot of data to pump through the system. In any case, frameskip is probably
27 // a good idea for now, since most systems are probably too slow to run at
28 // 60 FPS. But doing so will have some nasty side effects in some games.
29 // You have been warned!
31 int frame_ticker = vjs.frameSkip;
34 // Create SDL/OpenGL surfaces
38 // Get proper info about the platform we're running on...
39 const SDL_VideoInfo * info = SDL_GetVideoInfo();
43 WriteLog("VJ: SDL is unable to get the video info: %s\n", SDL_GetError());
49 // Initializing SDL attributes with OpenGL
50 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
51 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
52 mainSurfaceFlags = SDL_OPENGL;
57 if (info->hw_available)
58 mainSurfaceFlags = SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF;
61 mainSurfaceFlags |= SDL_HWACCEL;
65 mainSurfaceFlags |= SDL_FULLSCREEN;
68 // mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH, VIRTUAL_SCREEN_HEIGHT_NTSC, 16, mainSurfaceFlags);
69 mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH,
70 (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL),
71 16, mainSurfaceFlags);
73 // When OpenGL is used, we're going to use a standard resolution of 640x480.
74 // This way we have good scaling functionality and when the screen is resized
75 // because of the NTSC <-> PAL resize, we only have to re-create the texture
76 // instead of initializing the entire OpenGL texture en screens.
77 mainSurface = SDL_SetVideoMode(640, 480, 16, mainSurfaceFlags);
79 if (mainSurface == NULL)
81 WriteLog("VJ: SDL is unable to set the video mode: %s\n", SDL_GetError());
85 SDL_WM_SetCaption("Virtual Jaguar", "Virtual Jaguar");
87 // Create the primary SDL display (16 BPP, 5/5/5 RGB format)
88 surface = SDL_CreateRGBSurface(SDL_SWSURFACE, VIRTUAL_SCREEN_WIDTH,
89 (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL),
90 16, 0x7C00, 0x03E0, 0x001F, 0);
94 WriteLog("VJ: Could not create primary SDL surface: %s\n", SDL_GetError());
99 // Let us setup OpenGL and our rendering texture. We give the src (surface) and the
100 // dst (mainSurface) display as well as the automatic bpp selection as options so that
101 // our texture is automaticly created :)
102 sdlemu_init_opengl(surface, mainSurface, 1 /*method*/,
103 vjs.glFilter /*texture type (linear, nearest)*/,
104 0 /* Automatic bpp selection based upon src */);
106 // Initialize Joystick support under SDL
110 if (SDL_NumJoysticks() <= 0)
112 vjs.useJoystick = false;
113 printf("VJ: No joystick(s) or joypad(s) detected on your system. Using keyboard...\n");
117 if ((joystick = SDL_JoystickOpen(vjs.joyport)) == 0)
119 vjs.useJoystick = false;
120 printf("VJ: Unable to open a Joystick on port: %d\n", (int)vjs.joyport);
123 printf("VJ: Using: %s\n", SDL_JoystickName(vjs.joyport));
127 // Set up the backbuffer
128 //To be safe, this should be 1280 * 625 * 2...
129 // backbuffer = (int16 *)malloc(845 * 525 * sizeof(int16));
130 backbuffer = (int16 *)malloc(1280 * 625 * sizeof(int16));
131 // memset(backbuffer, 0x44, VIRTUAL_SCREEN_WIDTH * VIRTUAL_SCREEN_HEIGHT_NTSC * sizeof(int16));
132 memset(backbuffer, 0x44, VIRTUAL_SCREEN_WIDTH *
133 (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL)
140 // Free various SDL components
145 sdlemu_close_opengl();
147 SDL_JoystickClose(joystick);
148 SDL_FreeSurface(surface);
153 // Render the backbuffer to the primary screen surface
155 void RenderBackbuffer(void)
157 if (SDL_MUSTLOCK(surface))
158 while (SDL_LockSurface(surface) < 0)
161 memcpy(surface->pixels, backbuffer, tom_getVideoModeWidth() * tom_getVideoModeHeight() * 2);
163 if (SDL_MUSTLOCK(surface))
164 SDL_UnlockSurface(surface);
168 // One of the reasons why OpenGL is slower then normal SDL rendering, is because
169 // the data is being pumped into the buffer every frame with a overflow as result.
170 // So, we going to render every 1 fps instead of every 0 fps.
171 // [Shamus] This is isn't why it's slower--see top of file for explanation... ;-)
172 if (frame_ticker == 0)
174 sdlemu_draw_texture(mainSurface, surface, 1/*1=GL_QUADS*/);
175 frame_ticker = vjs.frameSkip; // Reset frame_ticker
182 SDL_Rect rect = { 0, 0, surface->w, surface->h };
183 SDL_BlitSurface(surface, &rect, mainSurface, &rect);
184 SDL_Flip(mainSurface);
189 // Resize the main SDL screen & backbuffer
191 void ResizeScreen(uint32 width, uint32 height)
193 char window_title[256];
195 SDL_FreeSurface(surface);
196 surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 16, 0x7C00, 0x03E0, 0x001F, 0);
200 WriteLog("Video: Could not create primary SDL surface: %s", SDL_GetError());
201 //This is just crappy. We shouldn't exit this way--it leaves all kinds of memory leaks
202 //as well as screwing up SDL... !!! FIX !!!
208 // Recreate the texture because of the NTSC <-> PAL screen resize.
209 sdlemu_create_texture(surface, mainSurface, vjs.glFilter, 0);
213 mainSurface = SDL_SetVideoMode(width, height, 16, mainSurfaceFlags);
215 if (mainSurface == NULL)
217 WriteLog("Video: SDL is unable to set the video mode: %s\n", SDL_GetError());
222 sprintf(window_title, "Virtual Jaguar (%i x %i)", (int)width, (int)height);
223 SDL_WM_SetCaption(window_title, window_title);
227 // Return the screen's pitch
229 uint32 GetSDLScreenPitch(void)
231 return surface->pitch;
235 // Fullscreen <-> window switching
237 void ToggleFullscreen(void)
239 //NOTE: This does *NOT* work with OpenGL rendering! !!! FIX !!!
241 return; // Until we can fix it...
243 vjs.fullscreen = !vjs.fullscreen;
244 mainSurfaceFlags &= ~SDL_FULLSCREEN;
247 mainSurfaceFlags |= SDL_FULLSCREEN;
249 mainSurface = SDL_SetVideoMode(tom_width, tom_height, 16, mainSurfaceFlags);
251 if (mainSurface == NULL)
253 WriteLog("Video: SDL is unable to set the video mode: %s\n", SDL_GetError());
257 SDL_WM_SetCaption("Virtual Jaguar", "Virtual Jaguar");