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