]> Shamusworld >> Repos - virtualjaguar/blob - src/video.cpp
4507363cf99c23dd941d0d70a1c1e05d598c272a
[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 "tom.h"
10 #include "sdlemu_opengl.h"
11 #include "settings.h"
12 #include "log.h"
13
14 // External global variables
15
16 //shouldn't these exist here??? Prolly.
17 //And now, they do! :-)
18 SDL_Surface * surface, * mainSurface;
19 SDL_Joystick * joystick;
20 Uint32 mainSurfaceFlags;
21 //int16 * backbuffer;
22 uint32 * backbuffer;
23
24 // One of the reasons why OpenGL is slower then normal SDL rendering, is because
25 // the data is being pumped into the buffer every frame with a overflow as result.
26 // So, we going tot render every 1 frame instead of every 0 frame.
27
28 // [Shamus] This isn't the case. OpenGL is slower because 60 frames a second is a
29 //          lot of data to pump through the system. In any case, frameskip is probably
30 //          a good idea for now, since most systems are probably too slow to run at
31 //          60 FPS. But doing so will have some nasty side effects in some games.
32 //          You have been warned!
33
34 int frame_ticker = vjs.frameSkip;
35
36 //
37 // Create SDL/OpenGL surfaces
38 //
39 bool InitVideo(void)
40 {
41         // Get proper info about the platform we're running on...
42         const SDL_VideoInfo * info = SDL_GetVideoInfo();
43
44         if (!info)
45         {
46                 WriteLog("VJ: SDL is unable to get the video info: %s\n", SDL_GetError());
47                 return false;
48         }
49
50         if (vjs.useOpenGL)
51         {
52             // Initializing SDL attributes with OpenGL
53             SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
54         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
55                 mainSurfaceFlags = SDL_OPENGL;
56                 
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 = VIRTUAL_SCREEN_WIDTH;
108
109         if (vjs.renderType == RT_TV)
110                 vsWidth = 1280;
111 //24BPP
112 //      surface = SDL_CreateRGBSurface(SDL_SWSURFACE, VIRTUAL_SCREEN_WIDTH,
113         surface = SDL_CreateRGBSurface(SDL_SWSURFACE, vsWidth,
114                 (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL), 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 automaticly 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 = (int16 *)malloc(845 * 525 * sizeof(int16));
159 /*      backbuffer = (int16 *)malloc(1280 * 625 * sizeof(int16));
160 //      memset(backbuffer, 0x44, VIRTUAL_SCREEN_WIDTH * VIRTUAL_SCREEN_HEIGHT_NTSC * sizeof(int16));
161         memset(backbuffer, 0x44, VIRTUAL_SCREEN_WIDTH *
162                 (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL)
163                 * sizeof(int16));//*/
164 //24BPP
165         backbuffer = (uint32 *)malloc(1280 * 625 * sizeof(uint32));
166 //      memset(backbuffer, 0x44, VIRTUAL_SCREEN_WIDTH * VIRTUAL_SCREEN_HEIGHT_NTSC * sizeof(int16));
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         if (SDL_MUSTLOCK(surface))
193                 while (SDL_LockSurface(surface) < 0)
194                         SDL_Delay(10);
195
196 //      memcpy(surface->pixels, backbuffer, tom_getVideoModeWidth() * tom_getVideoModeHeight() * 2);
197         if (vjs.renderType == RT_NORMAL)
198                 memcpy(surface->pixels, backbuffer, tom_getVideoModeWidth() * tom_getVideoModeHeight() * 4);
199         else if (vjs.renderType == RT_TV)
200                 memcpy(surface->pixels, backbuffer, 1280 * tom_getVideoModeHeight() * 4);
201
202         if (SDL_MUSTLOCK(surface))
203                 SDL_UnlockSurface(surface);
204
205         if (vjs.useOpenGL)
206         {
207                 // One of the reasons why OpenGL is slower then normal SDL rendering, is because
208                 // the data is being pumped into the buffer every frame with a overflow as result.
209                 // So, we going to render every 1 fps instead of every 0 fps.
210                 // [Shamus] This is isn't why it's slower--see top of file for explanation... ;-)
211                 if (frame_ticker == 0)
212                 {
213                         sdlemu_draw_texture(mainSurface, surface, 1/*1=GL_QUADS*/);
214                         frame_ticker = vjs.frameSkip;           // Reset frame_ticker
215                 }
216                 else
217                         frame_ticker--;
218     }  
219         else
220         {
221                 SDL_Rect rect = { 0, 0, surface->w, surface->h };
222                 SDL_BlitSurface(surface, &rect, mainSurface, &rect);
223                 SDL_Flip(mainSurface);
224     }
225 }
226
227 //
228 // Resize the main SDL screen & backbuffer
229 //
230 void ResizeScreen(uint32 width, uint32 height)
231 {
232         char window_title[256];
233
234         SDL_FreeSurface(surface);
235 //      surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 16, 0x7C00, 0x03E0, 0x001F, 0);
236 //24BPP
237         surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,
238 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
239                  0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
240 #else
241                  0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);
242 #endif//*/
243
244         if (surface == NULL)
245         {
246                 WriteLog("Video: Could not create primary SDL surface: %s", SDL_GetError());
247 //This is just crappy. We shouldn't exit this way--it leaves all kinds of memory leaks
248 //as well as screwing up SDL... !!! FIX !!!
249                 exit(1);
250         }
251
252         if (vjs.useOpenGL)
253         {
254                 // Recreate the texture because of the NTSC <-> PAL screen resize.
255                 sdlemu_create_texture(surface, mainSurface, vjs.glFilter, 0);
256         }
257         else
258         {
259                 mainSurface = SDL_SetVideoMode(width, height, 32, mainSurfaceFlags);
260
261                 if (mainSurface == NULL)
262                 {
263                         WriteLog("Video: SDL is unable to set the video mode: %s\n", SDL_GetError());
264                         exit(1);
265                 }
266         }
267
268         sprintf(window_title, "Virtual Jaguar (%i x %i)", (int)width, (int)height);
269         SDL_WM_SetCaption(window_title, window_title);
270 }
271
272 //
273 // Return the screen's width in pixels
274 //
275 uint32 GetSDLScreenWidthInPixels(void)
276 {
277         return surface->pitch / 4;                                              // Pitch / 4 since we're in 32BPP mode
278 }
279
280 //
281 // Fullscreen <-> window switching
282 //
283 void ToggleFullscreen(void)
284 {
285 //NOTE: This does *NOT* work with OpenGL rendering! !!! FIX !!!
286         if (vjs.useOpenGL)
287                 return;                                                                         // Until we can fix it...
288
289         vjs.fullscreen = !vjs.fullscreen;
290         mainSurfaceFlags &= ~SDL_FULLSCREEN;
291
292         if (vjs.fullscreen)
293                 mainSurfaceFlags |= SDL_FULLSCREEN;
294
295         mainSurface = SDL_SetVideoMode(tom_width, tom_height, 32, mainSurfaceFlags);
296
297         if (mainSurface == NULL)
298         {
299                 WriteLog("Video: SDL is unable to set the video mode: %s\n", SDL_GetError());
300                 exit(1);
301         }
302
303         SDL_WM_SetCaption("Virtual Jaguar", "Virtual Jaguar");
304 }