]> Shamusworld >> Repos - virtualjaguar/blob - src/video.cpp
Changed to allow no sound system init
[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 Uint32 mainSurfaceFlags;
18 //int16 * backbuffer;
19 uint32 * backbuffer;
20 SDL_Joystick * joystick;
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 //24BPP
105         surface = SDL_CreateRGBSurface(SDL_SWSURFACE, VIRTUAL_SCREEN_WIDTH,
106                 (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL), 32,
107 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
108                  0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
109 #else
110                  0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);
111 #endif//*/
112
113         if (surface == NULL)
114         {
115                 WriteLog("VJ: Could not create primary SDL surface: %s\n", SDL_GetError());
116                 return false;
117         }
118
119         if (vjs.useOpenGL)
120                 // Let us setup OpenGL and our rendering texture. We give the src (surface) and the
121                 // dst (mainSurface) display as well as the automatic bpp selection as options so that
122                 // our texture is automaticly created :)
123                 sdlemu_init_opengl(surface, mainSurface, 1 /*method*/,
124                         vjs.glFilter /*texture type (linear, nearest)*/,
125                         0 /* Automatic bpp selection based upon src */);
126
127         // Initialize Joystick support under SDL
128
129         if (vjs.useJoystick)
130         {
131                 if (SDL_NumJoysticks() <= 0)
132                 {
133                         vjs.useJoystick = false;
134                         printf("VJ: No joystick(s) or joypad(s) detected on your system. Using keyboard...\n");
135                 }
136                 else
137                 {
138                         if ((joystick = SDL_JoystickOpen(vjs.joyport)) == 0)
139                         {
140                                 vjs.useJoystick = false;
141                                 printf("VJ: Unable to open a Joystick on port: %d\n", (int)vjs.joyport);
142                         }
143                         else
144                                 printf("VJ: Using: %s\n", SDL_JoystickName(vjs.joyport));
145                 }
146         }
147
148         // Set up the backbuffer
149 //To be safe, this should be 1280 * 625 * 2...
150 //      backbuffer = (int16 *)malloc(845 * 525 * sizeof(int16));
151 /*      backbuffer = (int16 *)malloc(1280 * 625 * sizeof(int16));
152 //      memset(backbuffer, 0x44, VIRTUAL_SCREEN_WIDTH * VIRTUAL_SCREEN_HEIGHT_NTSC * sizeof(int16));
153         memset(backbuffer, 0x44, VIRTUAL_SCREEN_WIDTH *
154                 (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL)
155                 * sizeof(int16));//*/
156 //24BPP
157         backbuffer = (uint32 *)malloc(1280 * 625 * sizeof(uint32));
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(uint32));
162
163         return true;
164 }
165
166 //
167 // Free various SDL components
168 //
169 void VideoDone(void)
170 {
171         if (vjs.useOpenGL)
172                 sdlemu_close_opengl();
173
174         SDL_JoystickClose(joystick);
175         SDL_FreeSurface(surface);
176         free(backbuffer);
177 }
178
179 //
180 // Render the backbuffer to the primary screen surface
181 //
182 void RenderBackbuffer(void)
183 {
184         if (SDL_MUSTLOCK(surface))
185                 while (SDL_LockSurface(surface) < 0)
186                         SDL_Delay(10);
187
188 //      memcpy(surface->pixels, backbuffer, tom_getVideoModeWidth() * tom_getVideoModeHeight() * 2);
189         memcpy(surface->pixels, backbuffer, tom_getVideoModeWidth() * tom_getVideoModeHeight() * 4);
190
191         if (SDL_MUSTLOCK(surface))
192                 SDL_UnlockSurface(surface);
193
194         if (vjs.useOpenGL)
195         {
196                 // One of the reasons why OpenGL is slower then normal SDL rendering, is because
197                 // the data is being pumped into the buffer every frame with a overflow as result.
198                 // So, we going to render every 1 fps instead of every 0 fps.
199                 // [Shamus] This is isn't why it's slower--see top of file for explanation... ;-)
200                 if (frame_ticker == 0)
201                 {
202                         sdlemu_draw_texture(mainSurface, surface, 1/*1=GL_QUADS*/);
203                         frame_ticker = vjs.frameSkip;           // Reset frame_ticker
204                 }
205                 else
206                         frame_ticker--;
207     }  
208         else
209         {
210                 SDL_Rect rect = { 0, 0, surface->w, surface->h };
211                 SDL_BlitSurface(surface, &rect, mainSurface, &rect);
212                 SDL_Flip(mainSurface);
213     }
214 }
215
216 //
217 // Resize the main SDL screen & backbuffer
218 //
219 void ResizeScreen(uint32 width, uint32 height)
220 {
221         char window_title[256];
222
223         SDL_FreeSurface(surface);
224 //      surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 16, 0x7C00, 0x03E0, 0x001F, 0);
225 //24BPP
226         surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,
227 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
228                  0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
229 #else
230                  0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);
231 #endif//*/
232
233         if (surface == NULL)
234         {
235                 WriteLog("Video: Could not create primary SDL surface: %s", SDL_GetError());
236 //This is just crappy. We shouldn't exit this way--it leaves all kinds of memory leaks
237 //as well as screwing up SDL... !!! FIX !!!
238                 exit(1);
239         }
240
241         if (vjs.useOpenGL)
242         {
243                 // Recreate the texture because of the NTSC <-> PAL screen resize.
244                 sdlemu_create_texture(surface, mainSurface, vjs.glFilter, 0);
245         }
246         else
247         {
248                 mainSurface = SDL_SetVideoMode(width, height, 16, mainSurfaceFlags);
249
250                 if (mainSurface == NULL)
251                 {
252                         WriteLog("Video: SDL is unable to set the video mode: %s\n", SDL_GetError());
253                         exit(1);
254                 }
255         }
256
257         sprintf(window_title, "Virtual Jaguar (%i x %i)", (int)width, (int)height);
258         SDL_WM_SetCaption(window_title, window_title);
259 }
260
261 //
262 // Return the screen's pitch
263 //
264 uint32 GetSDLScreenPitch(void)
265 {
266         return surface->pitch;
267 }
268
269 //
270 // Return the screen's width in pixels
271 //
272 uint32 GetSDLScreenWidthInPixels(void)
273 {
274         return surface->pitch / 4;                                              // Pitch / 4 since we're in 32BPP mode
275 }
276
277 //
278 // Fullscreen <-> window switching
279 //
280 void ToggleFullscreen(void)
281 {
282 //NOTE: This does *NOT* work with OpenGL rendering! !!! FIX !!!
283         if (vjs.useOpenGL)
284                 return;                                                                         // Until we can fix it...
285
286         vjs.fullscreen = !vjs.fullscreen;
287         mainSurfaceFlags &= ~SDL_FULLSCREEN;
288
289         if (vjs.fullscreen)
290                 mainSurfaceFlags |= SDL_FULLSCREEN;
291
292         mainSurface = SDL_SetVideoMode(tom_width, tom_height, 16, mainSurfaceFlags);
293
294         if (mainSurface == NULL)
295         {
296                 WriteLog("Video: SDL is unable to set the video mode: %s\n", SDL_GetError());
297                 exit(1);
298         }
299
300         SDL_WM_SetCaption("Virtual Jaguar", "Virtual Jaguar");
301 }