]> Shamusworld >> Repos - virtualjaguar/blob - src/video.cpp
e7ed63ff344ce222afc475d8c40b3d01e6de4a89
[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 VideoInit(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         else
59         {
60                 if (info->hw_available)
61                         mainSurfaceFlags = SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF;
62
63                 if (info->blit_hw)
64                         mainSurfaceFlags |= SDL_HWACCEL;
65         }
66
67         if (vjs.fullscreen)
68                 mainSurfaceFlags |= SDL_FULLSCREEN;
69
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);
75         else
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);//*/
81 //24BPP
82         if (!vjs.useOpenGL)
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);
87         else
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);//*/
93
94         if (mainSurface == NULL)
95         {
96                 WriteLog("VJ: SDL is unable to set the video mode: %s\n", SDL_GetError());
97                 return false;
98         }
99
100         SDL_WM_SetCaption("Virtual Jaguar", "Virtual Jaguar");
101
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);//*/
106
107         uint32 vsWidth = (vjs.renderType == RT_TV ? 1280 : VIRTUAL_SCREEN_WIDTH),
108                 vsHeight = (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL);
109
110 //      if (vjs.renderType == RT_TV)
111 //              vsWidth = 1280;
112 //24BPP
113 //      surface = SDL_CreateRGBSurface(SDL_SWSURFACE, VIRTUAL_SCREEN_WIDTH,
114         surface = SDL_CreateRGBSurface(SDL_SWSURFACE, vsWidth, vsHeight, 32,
115 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
116                  0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
117 #else
118                  0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);
119 #endif//*/
120
121         if (surface == NULL)
122         {
123                 WriteLog("VJ: Could not create primary SDL surface: %s\n", SDL_GetError());
124                 return false;
125         }
126
127         if (vjs.useOpenGL)
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 automatically created :)
131                 sdlemu_init_opengl(surface, mainSurface, 1 /*method*/,
132                         vjs.glFilter /*texture type (linear, nearest)*/,
133                         0 /* Automatic bpp selection based upon src */);
134
135         // Initialize Joystick support under SDL
136
137         if (vjs.useJoystick)
138         {
139                 if (SDL_NumJoysticks() <= 0)
140                 {
141                         vjs.useJoystick = false;
142                         printf("VJ: No joystick(s) or joypad(s) detected on your system. Using keyboard...\n");
143                 }
144                 else
145                 {
146                         if ((joystick = SDL_JoystickOpen(vjs.joyport)) == 0)
147                         {
148                                 vjs.useJoystick = false;
149                                 printf("VJ: Unable to open a Joystick on port: %d\n", (int)vjs.joyport);
150                         }
151                         else
152                                 printf("VJ: Using: %s\n", SDL_JoystickName(vjs.joyport));
153                 }
154         }
155
156         // Set up the backbuffer
157         // To be safe, this should be 1280 * 625 * 2...
158         backbuffer = (uint32 *)malloc(1280 * 625 * sizeof(uint32));
159         memset(backbuffer, 0x44, VIRTUAL_SCREEN_WIDTH *
160                 (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL)
161                 * sizeof(uint32));
162
163         return true;
164 }
165
166 //
167 // Free various SDL components
168 //
169 void VideoDone(void)
170 {
171         if (vjs.useOpenGL)
172                 sdlemu_close_opengl();
173
174         SDL_JoystickClose(joystick);
175         SDL_FreeSurface(surface);
176         free(backbuffer);
177 }
178
179 //
180 // Render the backbuffer to the primary screen surface
181 //
182 void RenderBackbuffer(void)
183 {
184         // Handle frameskip *before* we do any heavy lifting here...
185
186         if (frame_ticker > 0)
187         {
188                 frame_ticker--;
189                 return;
190         }
191
192         frame_ticker = vjs.frameSkip;                           // Reset frame_ticker
193
194         if (SDL_MUSTLOCK(surface))
195                 while (SDL_LockSurface(surface) < 0)
196                         SDL_Delay(10);
197
198 //      memcpy(surface->pixels, backbuffer, tom_getVideoModeWidth() * tom_getVideoModeHeight() * 2);
199 // This memcpy is expensive--do some profiling to see what the impact is!
200         if (vjs.renderType == RT_NORMAL)
201                 memcpy(surface->pixels, backbuffer, TOMGetVideoModeWidth() * TOMGetVideoModeHeight() * 4);
202         else if (vjs.renderType == RT_TV)
203                 memcpy(surface->pixels, backbuffer, 1280 * TOMGetVideoModeHeight() * 4);
204
205         if (SDL_MUSTLOCK(surface))
206                 SDL_UnlockSurface(surface);
207
208         if (vjs.useOpenGL)
209                 // One of the reasons why OpenGL is slower then normal SDL rendering, is because
210                 // the data is being pumped into the buffer every frame with a overflow as result.
211                 // So, we going to render every 1 fps instead of every 0 fps.
212                 // [Shamus] This is isn't why it's slower--see top of file for explanation... ;-)
213 //The problem lies in this function...
214                 sdlemu_draw_texture(mainSurface, surface, 1/*1=GL_QUADS*/);
215         else
216         {
217                 SDL_Rect rect = { 0, 0, surface->w, surface->h };
218                 SDL_BlitSurface(surface, &rect, mainSurface, &rect);
219                 SDL_Flip(mainSurface);
220     }
221 }
222
223 //
224 // Resize the main SDL screen & backbuffer
225 //
226 void ResizeScreen(uint32 width, uint32 height)
227 {
228         SDL_FreeSurface(surface);
229         surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,
230 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
231                  0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
232 #else
233                  0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);
234 #endif//*/
235
236         if (surface == NULL)
237         {
238                 WriteLog("Video: Could not create primary SDL surface: %s", SDL_GetError());
239 //This is just crappy. We shouldn't exit this way--it leaves all kinds of memory leaks
240 //as well as screwing up SDL... !!! FIX !!!
241 //              exit(1);
242                 // OK, this is cleaner. We can't continue if there is no surface created!
243                 finished = true;
244         }
245
246         if (vjs.useOpenGL)
247         {
248                 // Recreate the texture because of the NTSC <-> PAL screen resize.
249 //Not sure why this is here...
250 //Is it because of the resized surface up above?
251                 sdlemu_create_texture(surface, mainSurface, vjs.glFilter, 0);
252         }
253         else
254         {
255                 mainSurface = SDL_SetVideoMode(width, height, 32, mainSurfaceFlags);
256
257                 if (mainSurface == NULL)
258                 {
259                         WriteLog("Video: SDL is unable to set the video mode: %s\n", SDL_GetError());
260 // Don't exit because we can't resize!
261 //                      exit(1);
262                 }
263         }
264
265         char window_title[64];
266
267         sprintf(window_title, "Virtual Jaguar (%i x %i)", (int)width, (int)height);
268         SDL_WM_SetCaption((vjs.useOpenGL ? "Virtual Jaguar (OpenGL)" : window_title), "Virtual Jaguar");
269 }
270
271 //
272 // Return the screen's width in pixels
273 //
274 uint32 GetSDLScreenWidthInPixels(void)
275 {
276         return surface->pitch / 4;                                              // Pitch / 4 since we're in 32BPP mode
277 }
278
279 //
280 // Fullscreen <-> window switching
281 //
282 void ToggleFullscreen(void)
283 {
284         // Set our internal variable, then toggle the SDL flag
285         vjs.fullscreen = !vjs.fullscreen;
286         mainSurfaceFlags ^= SDL_FULLSCREEN;
287 //      mainSurfaceFlags = (vjs.fullscreen ? mainSurfaceFlags | SDL_FULLSCREEN :
288 //              mainSurfaceFlags & ~SDL_FULLSCREEN);
289
290 //      mainSurfaceFlags &= ~SDL_FULLSCREEN;
291
292 //      if (vjs.fullscreen)
293 //              mainSurfaceFlags |= SDL_FULLSCREEN;
294
295         if (vjs.useOpenGL)
296         {
297                 // When OpenGL is used, we're going to use a standard resolution of 640x480.
298                 // This way we have good scaling functionality and when the screen is resized
299                 // because of the NTSC <-> PAL resize, we only have to re-create the texture
300                 // instead of initializing the entire OpenGL texture en screens.
301                 mainSurface = SDL_SetVideoMode(640, 480, 32, mainSurfaceFlags);
302
303                 // Reset viewport, etc.
304                 glViewport(0, 0, mainSurface->w, mainSurface->h);
305                 glMatrixMode(GL_PROJECTION);
306                 glPushMatrix();
307                 glLoadIdentity();
308                 glOrtho(0.0, (GLdouble)mainSurface->w, (GLdouble)mainSurface->h, 0.0, 0.0, 1.0);
309                 glMatrixMode(GL_MODELVIEW);
310                 glPushMatrix();
311                 glLoadIdentity();
312         }
313         else
314                 mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH,
315                         (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL),
316                         32, mainSurfaceFlags);
317
318         if (mainSurface == NULL)
319         {
320                 WriteLog("Video: SDL was unable to switch the video to %s: %s\n", (vjs.fullscreen ? "fullscreen" : "windowed"), SDL_GetError());
321 // Shouldn't exit because we can't switch! BAD!!!
322 //              exit(1);
323                 return;
324         }
325
326         SDL_WM_SetCaption((vjs.useOpenGL ? "Virtual Jaguar (OpenGL)" : "Virtual Jaguar"), "Virtual Jaguar");
327
328         return;
329 }