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