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