]> Shamusworld >> Repos - virtualjaguar/blob - src/video.cpp
Fixed fullscreen<-->windowed mode swith in OpenGL mode, phase one of
[virtualjaguar] / src / video.cpp
1 //
2 // VIDEO.CPP: SDL/local hardware specific video routines
3 //
4 // by James L. Hammons
5 //
6
7 #include "video.h"
8
9 #include "gui.h"                                                                // For "finished"
10 #include "log.h"
11 #include "tom.h"
12 #include "sdlemu_opengl.h"
13 #include "settings.h"
14
15 // External global variables
16
17 //shouldn't these exist here??? Prolly.
18 //And now, they do! :-)
19 SDL_Surface * surface, * mainSurface;
20 SDL_Joystick * joystick;
21 Uint32 mainSurfaceFlags;
22 //int16 * backbuffer;
23 uint32 * backbuffer;
24
25 // One of the reasons why OpenGL is slower then normal SDL rendering, is because
26 // the data is being pumped into the buffer every frame with a overflow as result.
27 // So, we going tot render every 1 frame instead of every 0 frame.
28
29 // [Shamus] This isn't the case. OpenGL is slower because 60 frames a second is a
30 //          lot of data to pump through the system. In any case, frameskip is probably
31 //          a good idea for now, since most systems are probably too slow to run at
32 //          60 FPS. But doing so will have some nasty side effects in some games.
33 //          You have been warned!
34
35 int frame_ticker = 0;
36
37 //
38 // Create SDL/OpenGL surfaces
39 //
40 bool InitVideo(void)
41 {
42         // Get proper info about the platform we're running on...
43         const SDL_VideoInfo * info = SDL_GetVideoInfo();
44
45         if (!info)
46         {
47                 WriteLog("VJ: SDL is unable to get the video info: %s\n", SDL_GetError());
48                 return false;
49         }
50
51         if (vjs.useOpenGL)
52         {
53             // Initializing SDL attributes with OpenGL
54             SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
55         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
56                 mainSurfaceFlags = SDL_OPENGL;
57
58         }
59         else
60         {
61                 if (info->hw_available)
62                         mainSurfaceFlags = SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF;
63
64                 if (info->blit_hw)
65                         mainSurfaceFlags |= SDL_HWACCEL;
66         }
67
68         if (vjs.fullscreen)
69                 mainSurfaceFlags |= SDL_FULLSCREEN;
70
71 /*      if (!vjs.useOpenGL)
72 //              mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH, VIRTUAL_SCREEN_HEIGHT_NTSC, 16, mainSurfaceFlags);
73                 mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH,
74                         (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL),
75                         16, mainSurfaceFlags);
76         else
77                 // When OpenGL is used, we're going to use a standard resolution of 640x480.
78                 // This way we have good scaling functionality and when the screen is resized
79                 // because of the NTSC <-> PAL resize, we only have to re-create the texture
80                 // instead of initializing the entire OpenGL texture en screens.
81                 mainSurface = SDL_SetVideoMode(640, 480, 16, mainSurfaceFlags);//*/
82 //24BPP
83         if (!vjs.useOpenGL)
84 //              mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH, VIRTUAL_SCREEN_HEIGHT_NTSC, 16, mainSurfaceFlags);
85                 mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH,
86                         (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL),
87                         32, mainSurfaceFlags);
88         else
89                 // When OpenGL is used, we're going to use a standard resolution of 640x480.
90                 // This way we have good scaling functionality and when the screen is resized
91                 // because of the NTSC <-> PAL resize, we only have to re-create the texture
92                 // instead of initializing the entire OpenGL texture en screens.
93                 mainSurface = SDL_SetVideoMode(640, 480, 32, mainSurfaceFlags);//*/
94
95         if (mainSurface == NULL)
96         {
97                 WriteLog("VJ: SDL is unable to set the video mode: %s\n", SDL_GetError());
98                 return false;
99         }
100
101         SDL_WM_SetCaption("Virtual Jaguar", "Virtual Jaguar");
102
103         // Create the primary SDL display (16 BPP, 5/5/5 RGB format)
104 /*      surface = SDL_CreateRGBSurface(SDL_SWSURFACE, VIRTUAL_SCREEN_WIDTH,
105                 (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL),
106                 16, 0x7C00, 0x03E0, 0x001F, 0);//*/
107
108         uint32 vsWidth = (vjs.renderType == RT_TV ? 1280 : VIRTUAL_SCREEN_WIDTH),
109                 vsHeight = (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL);
110
111 //      if (vjs.renderType == RT_TV)
112 //              vsWidth = 1280;
113 //24BPP
114 //      surface = SDL_CreateRGBSurface(SDL_SWSURFACE, VIRTUAL_SCREEN_WIDTH,
115         surface = SDL_CreateRGBSurface(SDL_SWSURFACE, vsWidth, vsHeight, 32,
116 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
117                  0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
118 #else
119                  0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);
120 #endif//*/
121
122         if (surface == NULL)
123         {
124                 WriteLog("VJ: Could not create primary SDL surface: %s\n", SDL_GetError());
125                 return false;
126         }
127
128         if (vjs.useOpenGL)
129                 // Let us setup OpenGL and our rendering texture. We give the src (surface) and the
130                 // dst (mainSurface) display as well as the automatic bpp selection as options so that
131                 // our texture is automatically created :)
132                 sdlemu_init_opengl(surface, mainSurface, 1 /*method*/,
133                         vjs.glFilter /*texture type (linear, nearest)*/,
134                         0 /* Automatic bpp selection based upon src */);
135
136         // Initialize Joystick support under SDL
137
138         if (vjs.useJoystick)
139         {
140                 if (SDL_NumJoysticks() <= 0)
141                 {
142                         vjs.useJoystick = false;
143                         printf("VJ: No joystick(s) or joypad(s) detected on your system. Using keyboard...\n");
144                 }
145                 else
146                 {
147                         if ((joystick = SDL_JoystickOpen(vjs.joyport)) == 0)
148                         {
149                                 vjs.useJoystick = false;
150                                 printf("VJ: Unable to open a Joystick on port: %d\n", (int)vjs.joyport);
151                         }
152                         else
153                                 printf("VJ: Using: %s\n", SDL_JoystickName(vjs.joyport));
154                 }
155         }
156
157         // Set up the backbuffer
158         // To be safe, this should be 1280 * 625 * 2...
159         backbuffer = (uint32 *)malloc(1280 * 625 * sizeof(uint32));
160         memset(backbuffer, 0x44, VIRTUAL_SCREEN_WIDTH *
161                 (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL)
162                 * sizeof(uint32));
163
164         return true;
165 }
166
167 //
168 // Free various SDL components
169 //
170 void VideoDone(void)
171 {
172         if (vjs.useOpenGL)
173                 sdlemu_close_opengl();
174
175         SDL_JoystickClose(joystick);
176         SDL_FreeSurface(surface);
177         free(backbuffer);
178 }
179
180 //
181 // Render the backbuffer to the primary screen surface
182 //
183 void RenderBackbuffer(void)
184 {
185         // Handle frameskip *before* we do any heavy lifting here...
186
187         if (frame_ticker > 0)
188         {
189                 frame_ticker--;
190                 return;
191         }
192
193         frame_ticker = vjs.frameSkip;                           // Reset frame_ticker
194
195         if (SDL_MUSTLOCK(surface))
196                 while (SDL_LockSurface(surface) < 0)
197                         SDL_Delay(10);
198
199 //      memcpy(surface->pixels, backbuffer, tom_getVideoModeWidth() * tom_getVideoModeHeight() * 2);
200 // This memcpy is expensive--do some profiling to see what the impact is!
201         if (vjs.renderType == RT_NORMAL)
202                 memcpy(surface->pixels, backbuffer, tom_getVideoModeWidth() * tom_getVideoModeHeight() * 4);
203         else if (vjs.renderType == RT_TV)
204                 memcpy(surface->pixels, backbuffer, 1280 * tom_getVideoModeHeight() * 4);
205
206         if (SDL_MUSTLOCK(surface))
207                 SDL_UnlockSurface(surface);
208
209         if (vjs.useOpenGL)
210                 // One of the reasons why OpenGL is slower then normal SDL rendering, is because
211                 // the data is being pumped into the buffer every frame with a overflow as result.
212                 // So, we going to render every 1 fps instead of every 0 fps.
213                 // [Shamus] This is isn't why it's slower--see top of file for explanation... ;-)
214 //The problem lies in this function...
215                 sdlemu_draw_texture(mainSurface, surface, 1/*1=GL_QUADS*/);
216         else
217         {
218                 SDL_Rect rect = { 0, 0, surface->w, surface->h };
219                 SDL_BlitSurface(surface, &rect, mainSurface, &rect);
220                 SDL_Flip(mainSurface);
221     }
222 }
223
224 //
225 // Resize the main SDL screen & backbuffer
226 //
227 void ResizeScreen(uint32 width, uint32 height)
228 {
229         SDL_FreeSurface(surface);
230         surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,
231 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
232                  0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
233 #else
234                  0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);
235 #endif//*/
236
237         if (surface == NULL)
238         {
239                 WriteLog("Video: Could not create primary SDL surface: %s", SDL_GetError());
240 //This is just crappy. We shouldn't exit this way--it leaves all kinds of memory leaks
241 //as well as screwing up SDL... !!! FIX !!!
242 //              exit(1);
243                 // OK, this is cleaner. We can't continue if there is no surface created!
244                 finished = true;
245         }
246
247         if (vjs.useOpenGL)
248         {
249                 // Recreate the texture because of the NTSC <-> PAL screen resize.
250 //Not sure why this is here...
251 //Is it because of the resized surface up above?
252                 sdlemu_create_texture(surface, mainSurface, vjs.glFilter, 0);
253         }
254         else
255         {
256                 mainSurface = SDL_SetVideoMode(width, height, 32, mainSurfaceFlags);
257
258                 if (mainSurface == NULL)
259                 {
260                         WriteLog("Video: SDL is unable to set the video mode: %s\n", SDL_GetError());
261 // Don't exit because we can't resize!
262 //                      exit(1);
263                 }
264         }
265
266         char window_title[64];
267
268         sprintf(window_title, "Virtual Jaguar (%i x %i)", (int)width, (int)height);
269         SDL_WM_SetCaption((vjs.useOpenGL ? "Virtual Jaguar (OpenGL)" : window_title), "Virtual Jaguar");
270 }
271
272 //
273 // Return the screen's width in pixels
274 //
275 uint32 GetSDLScreenWidthInPixels(void)
276 {
277         return surface->pitch / 4;                                              // Pitch / 4 since we're in 32BPP mode
278 }
279
280 //
281 // Fullscreen <-> window switching
282 //
283 void ToggleFullscreen(void)
284 {
285         vjs.fullscreen = !vjs.fullscreen;
286         mainSurfaceFlags &= ~SDL_FULLSCREEN;
287
288         if (vjs.fullscreen)
289                 mainSurfaceFlags |= SDL_FULLSCREEN;
290
291         if (vjs.useOpenGL)
292         {
293                 // When OpenGL is used, we're going to use a standard resolution of 640x480.
294                 // This way we have good scaling functionality and when the screen is resized
295                 // because of the NTSC <-> PAL resize, we only have to re-create the texture
296                 // instead of initializing the entire OpenGL texture en screens.
297                 mainSurface = SDL_SetVideoMode(640, 480, 32, mainSurfaceFlags);//*/
298
299                 // Reset viewport, etc.
300                 glViewport(0, 0, mainSurface->w, mainSurface->h);
301                 glMatrixMode(GL_PROJECTION);
302                 glPushMatrix();
303                 glLoadIdentity();
304                 glOrtho(0.0, (GLdouble)mainSurface->w, (GLdouble)mainSurface->h, 0.0, 0.0, 1.0);
305                 glMatrixMode(GL_MODELVIEW);
306                 glPushMatrix();
307                 glLoadIdentity();
308         }
309         else
310                 mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH,
311                         (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL),
312                         32, mainSurfaceFlags);
313
314         if (mainSurface == NULL)
315         {
316                 WriteLog("Video: SDL was unable to switch the video to %s: %s\n", (vjs.fullscreen ? "fullscreen" : "windowed"), SDL_GetError());
317 // Shouldn't exit because we can't switch! BAD!!!
318 //              exit(1);
319                 return;
320         }
321
322         SDL_WM_SetCaption((vjs.useOpenGL ? "Virtual Jaguar (OpenGL)" : "Virtual Jaguar"), "Virtual Jaguar");
323
324         return;
325 }