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