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