]> Shamusworld >> Repos - virtualjaguar/blob - src/video.cpp
Added disclaimer to Makefile, until we can fix the Qt compilation problems.
[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 // For now, we don't do anything here...
244 #if 0
245         SDL_FreeSurface(surface);
246         surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, RMASK, GMASK, BMASK, AMASK);
247
248         if (surface == NULL)
249         {
250                 WriteLog("Video: Could not create primary SDL surface: %s", SDL_GetError());
251 //This is just crappy. We shouldn't exit this way--it leaves all kinds of memory leaks
252 //as well as screwing up SDL... !!! FIX !!!
253 //              exit(1);
254                 // OK, this is cleaner. We can't continue if there is no surface created!
255 #warning "!!! FIX !!! (finished = true)"
256 //              finished = true;
257         }
258
259         if (vjs.useOpenGL)
260         {
261                 // Recreate the texture because of the NTSC <-> PAL screen resize.
262 //Not sure why this is here...
263 //Is it because of the resized surface up above?
264                 sdlemu_create_texture(surface, mainSurface, vjs.glFilter, 0);
265         }
266         else
267         {
268                 mainSurface = SDL_SetVideoMode(width, height, 32, mainSurfaceFlags);
269
270                 if (mainSurface == NULL)
271                 {
272                         WriteLog("Video: SDL is unable to set the video mode: %s\n", SDL_GetError());
273 // Don't exit because we can't resize!
274 //                      exit(1);
275                 }
276         }
277
278         char window_title[64];
279
280         sprintf(window_title, "Virtual Jaguar (%i x %i)", (int)width, (int)height);
281         SDL_WM_SetCaption((vjs.useOpenGL ? "Virtual Jaguar (OpenGL)" : window_title), "Virtual Jaguar");
282 #endif
283 }
284
285 //
286 // Return the screen's width in pixels
287 //
288 uint32 GetSDLScreenWidthInPixels(void)
289 {
290         return surface->pitch / 4;                                              // Pitch / 4 since we're in 32BPP mode
291 }
292
293 //
294 // Fullscreen <-> window switching
295 //
296 void ToggleFullscreen(void)
297 {
298         // Set our internal variable, then toggle the SDL flag
299         vjs.fullscreen = !vjs.fullscreen;
300         mainSurfaceFlags ^= SDL_FULLSCREEN;
301 //      mainSurfaceFlags = (vjs.fullscreen ? mainSurfaceFlags | SDL_FULLSCREEN :
302 //              mainSurfaceFlags & ~SDL_FULLSCREEN);
303
304 //      mainSurfaceFlags &= ~SDL_FULLSCREEN;
305
306 //      if (vjs.fullscreen)
307 //              mainSurfaceFlags |= SDL_FULLSCREEN;
308
309         if (vjs.useOpenGL)
310         {
311                 // When OpenGL is used, we're going to use a standard resolution of 640x480.
312                 // This way we have good scaling functionality and when the screen is resized
313                 // because of the NTSC <-> PAL resize, we only have to re-create the texture
314                 // instead of initializing the entire OpenGL texture en screens.
315                 mainSurface = SDL_SetVideoMode(640, 480, 32, mainSurfaceFlags);
316
317                 // Reset viewport, etc.
318                 glViewport(0, 0, mainSurface->w, mainSurface->h);
319                 glMatrixMode(GL_PROJECTION);
320                 glPushMatrix();
321                 glLoadIdentity();
322                 glOrtho(0.0, (GLdouble)mainSurface->w, (GLdouble)mainSurface->h, 0.0, 0.0, 1.0);
323                 glMatrixMode(GL_MODELVIEW);
324                 glPushMatrix();
325                 glLoadIdentity();
326         }
327         else
328                 mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH,
329                         (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL),
330                         32, mainSurfaceFlags);
331
332         if (mainSurface == NULL)
333         {
334                 WriteLog("Video: SDL was unable to switch the video to %s: %s\n", (vjs.fullscreen ? "fullscreen" : "windowed"), SDL_GetError());
335 // Shouldn't exit because we can't switch! BAD!!!
336 //              exit(1);
337                 return;
338         }
339
340         SDL_WM_SetCaption((vjs.useOpenGL ? "Virtual Jaguar (OpenGL)" : "Virtual Jaguar"), "Virtual Jaguar");
341
342         return;
343 }