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.
27 // Create SDL/OpenGL surfaces
31 // Get proper info about the platform we're running on...
32 const SDL_VideoInfo * info = SDL_GetVideoInfo();
36 WriteLog("VJ: SDL is unable to get the video info: %s\n", SDL_GetError());
42 // Initializing SDL attributes with OpenGL
43 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
44 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
45 mainSurfaceFlags = SDL_OPENGL;
50 if (info->hw_available)
51 mainSurfaceFlags = SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF;
54 mainSurfaceFlags |= SDL_HWACCEL;
58 mainSurfaceFlags |= SDL_FULLSCREEN;
61 // mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH, VIRTUAL_SCREEN_HEIGHT_NTSC, 16, mainSurfaceFlags);
62 mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH,
63 (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL),
64 16, mainSurfaceFlags);
66 // When OpenGL is used, we're going to use a standard resolution of 640x480.
67 // This way we have good scaling functionality and when the screen is resized
68 // because of the NTSC <-> PAL resize, we only have to re-create the texture
69 // instead of initializing the entire OpenGL texture en screens.
70 mainSurface = SDL_SetVideoMode(640, 480, 16, mainSurfaceFlags);
72 if (mainSurface == NULL)
74 WriteLog("VJ: SDL is unable to set the video mode: %s\n", SDL_GetError());
78 SDL_WM_SetCaption("Virtual Jaguar", "Virtual Jaguar");
80 // Create the primary SDL display (16 BPP, 5/5/5 RGB format)
81 surface = SDL_CreateRGBSurface(SDL_SWSURFACE, VIRTUAL_SCREEN_WIDTH,
82 (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL),
83 16, 0x7C00, 0x03E0, 0x001F, 0);
87 WriteLog("VJ: Could not create primary SDL surface: %s\n", SDL_GetError());
92 // Let us setup OpenGL and our rendering texture. We give the src (surface) and the
93 // dst (mainSurface) display as well as the automatic bpp selection as options so that
94 // our texture is automaticly created :)
95 sdlemu_init_opengl(surface, mainSurface, 1 /*method*/,
96 vjs.glFilter /*texture type (linear, nearest)*/,
97 NULL /* Automatic bpp selection based upon src */);
100 // Initialize Joystick support under SDL
103 if (SDL_NumJoysticks() <= 0)
105 vjs.useJoystick = false;
106 printf("VJ: No joystick(s) or joypad(s) detected on your system. Using keyboard...\n");
110 if ((joystick = SDL_JoystickOpen(vjs.joyport)) == 0)
112 vjs.useJoystick = false;
113 printf("VJ: Unable to open a Joystick on port: %d\n", (int)vjs.joyport);
116 printf("VJ: Using: %s\n", SDL_JoystickName(vjs.joyport));
120 // Set up the backbuffer
121 //To be safe, this should be 1280 * 625 * 2...
122 // backbuffer = (int16 *)malloc(845 * 525 * sizeof(int16));
123 backbuffer = (int16 *)malloc(1280 * 625 * sizeof(int16));
124 // memset(backbuffer, 0x44, VIRTUAL_SCREEN_WIDTH * VIRTUAL_SCREEN_HEIGHT_NTSC * sizeof(int16));
125 memset(backbuffer, 0x44, VIRTUAL_SCREEN_WIDTH *
126 (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL)
133 // Free various SDL components
138 sdlemu_close_opengl();
140 SDL_JoystickClose(joystick);
141 SDL_FreeSurface(surface);
146 // Render the backbuffer to the primary screen surface
148 void RenderBackbuffer(void)
150 if (SDL_MUSTLOCK(surface))
151 while (SDL_LockSurface(surface) < 0)
154 memcpy(surface->pixels, backbuffer, tom_getVideoModeWidth() * tom_getVideoModeHeight() * 2);
156 if (SDL_MUSTLOCK(surface))
157 SDL_UnlockSurface(surface);
160 // One of the reasons why OpenGL is slower then normal SDL rendering, is because
161 // the data is being pumped into the buffer every frame with a overflow as result.
162 // So, we going tot render every 1 fps instead of every 0 fps.
163 if ( frame_ticker != 0 ) {
164 sdlemu_draw_texture(mainSurface, surface, 1/*1=GL_QUADS*/);
165 frame_ticker = 0; // Reset frame_ticker to 0 otherwise we won't get
166 // backrendering "frameskip".
168 frame_ticker = frame_ticker + 1;
172 SDL_Rect rect = { 0, 0, surface->w, surface->h };
173 SDL_BlitSurface(surface, &rect, mainSurface, &rect);
174 SDL_Flip(mainSurface);
179 // Resize the main SDL screen & backbuffer
181 void ResizeScreen(uint32 width, uint32 height)
183 char window_title[256];
185 SDL_FreeSurface(surface);
186 surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 16, 0x7C00, 0x03E0, 0x001F, 0);
190 WriteLog("Video: Could not create primary SDL surface: %s", SDL_GetError());
191 //This is just crappy. We shouldn't exit this way--it leaves all kinds of memory leaks
192 //as well as screwing up SDL... !!! FIX !!!
198 // Recreate the texture because of the NTSC <-> PAL screen resize.
199 sdlemu_create_texture( surface, mainSurface, vjs.glFilter , NULL);
203 mainSurface = SDL_SetVideoMode(width, height, 16, mainSurfaceFlags);
205 if (mainSurface == NULL)
207 WriteLog("Video: SDL is unable to set the video mode: %s\n", SDL_GetError());
212 sprintf(window_title, "Virtual Jaguar (%i x %i)", (int)width, (int)height);
213 SDL_WM_SetCaption(window_title, window_title);
217 // Return the screen's pitch
219 uint32 GetSDLScreenPitch(void)
221 return surface->pitch;
225 // Fullscreen <-> window switching
227 void ToggleFullscreen(void)
229 //NOTE: This does *NOT* work with OpenGL rendering! !!! FIX !!!
231 return; // Until we can fix it...
233 vjs.fullscreen = !vjs.fullscreen;
234 mainSurfaceFlags &= ~SDL_FULLSCREEN;
237 mainSurfaceFlags |= SDL_FULLSCREEN;
239 mainSurface = SDL_SetVideoMode(tom_width, tom_height, 16, mainSurfaceFlags);
241 if (mainSurface == NULL)
243 WriteLog("Video: SDL is unable to set the video mode: %s\n", SDL_GetError());
247 SDL_WM_SetCaption("Virtual Jaguar", "Virtual Jaguar");