]> Shamusworld >> Repos - stargem2/blob - src/video.cpp
da960c8c61b8b86aed4389c81b127acf0fce259b
[stargem2] / 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 "SDL.h"
10 #include <string.h>     // Why??? (for memset, etc... Lazy!) Dunno why, but this just strikes me as wrong...
11 #include <malloc.h>
12 #include "sdlemu_opengl.h"
13 #include "log.h"
14 #include "settings.h"
15 #include "icon.h"
16
17 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
18 #define MASK_R 0xFF000000
19 #define MASK_G 0x00FF0000
20 #define MASK_B 0x0000FF00
21 #define MASK_A 0x000000FF
22 #else
23 #define MASK_R 0x000000FF
24 #define MASK_G 0x0000FF00
25 #define MASK_B 0x00FF0000
26 #define MASK_A 0xFF000000
27 #endif
28
29 //#define TEST_ALPHA_BLENDING
30
31 // Exported global variables (actually, these are LOCAL global variables, EXPORTED...)
32
33 SDL_Surface * surface, * mainSurface, * someAlphaSurface;
34 Uint32 mainSurfaceFlags;
35 uint32 * scrBuffer = NULL;
36 SDL_Joystick * joystick;
37
38 //
39 // Prime SDL and create surfaces
40 //
41 bool InitVideo(void)
42 {
43         // Set up SDL library
44         if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE) < 0)
45         {
46                 WriteLog("Video: Could not initialize the SDL library: %s\n", SDL_GetError());
47                 return false;
48         }
49
50         // Get proper info about the platform we're running on...
51         const SDL_VideoInfo * info = SDL_GetVideoInfo();
52
53         if (!info)
54         {
55                 WriteLog("Video: SDL is unable to get the video info: %s\n", SDL_GetError());
56                 return false;
57         }
58
59         WriteLog("Video: Hardware is%s available...\n", (info->hw_available ? "" : " NOT"));
60         WriteLog("Video: Hardware blit is%s available...\n", (info->blit_hw ? "" : " NOT"));
61
62         if (settings.useOpenGL)
63         {
64                 mainSurfaceFlags = SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF | SDL_OPENGL;
65                 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
66 //We want 32BPP, so force the issue...
67                 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
68                 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
69                 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
70         }
71         else
72         {
73                 mainSurfaceFlags = SDL_DOUBLEBUF;
74
75                 if (info->hw_available)
76                         mainSurfaceFlags = SDL_HWSURFACE | SDL_HWPALETTE;
77
78                 if (info->blit_hw)
79                         mainSurfaceFlags |= SDL_HWACCEL;
80         }
81
82         if (settings.fullscreen)
83                 mainSurfaceFlags |= SDL_FULLSCREEN;
84
85         // Create the primary SDL display (32 BPP)
86         if (settings.useOpenGL)
87                 mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH * 2, VIRTUAL_SCREEN_HEIGHT * 2, 32, mainSurfaceFlags);
88         else
89                 mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH, VIRTUAL_SCREEN_HEIGHT, 32, mainSurfaceFlags);
90
91         if (mainSurface == NULL)
92         {
93                 WriteLog("Video: SDL is unable to set the video mode: %s\n", SDL_GetError());
94                 return false;
95         }
96
97         // Set icon (mainly for Win32 target--though seems to work under KDE as well...!)
98         SDL_Surface * iconSurf = SDL_CreateRGBSurfaceFrom(icon, 32, 32, 32, 128,
99                 MASK_R, MASK_G, MASK_B, MASK_A);
100         SDL_WM_SetIcon(iconSurf, NULL);
101         SDL_FreeSurface(iconSurf);
102
103         SDL_WM_SetCaption("StarGem2", "StarGem2");
104
105         // Create the secondary SDL display (32 BPP) that we use directly
106         surface = SDL_CreateRGBSurface(SDL_SWSURFACE, VIRTUAL_SCREEN_WIDTH, VIRTUAL_SCREEN_HEIGHT, 32, MASK_R, MASK_G, MASK_B, MASK_A);
107 /*WriteLog("Video: Created secondary surface with attributes:\n\n");
108 WriteLog("\tWidth, height: %u x %u\n", surface->w, surface->h);
109 WriteLog("\t        Pitch: %u\n", surface->pitch);
110 WriteLog("\t      Palette: %08X\n", surface->format->palette);
111 WriteLog("\t          BPP: %u\n", surface->format->BitsPerPixel);
112 WriteLog("\t      BytesPP: %u\n", surface->format->BytesPerPixel);
113 WriteLog("\t        RMask: %08X\n", surface->format->Rmask);
114 WriteLog("\t        GMask: %08X\n", surface->format->Gmask);
115 WriteLog("\t        BMask: %08X\n", surface->format->Bmask);
116 WriteLog("\t        AMask: %08X\n", surface->format->Amask);
117 WriteLog("\n");//*/
118
119         if (surface == NULL)
120         {
121                 WriteLog("Video: Could not create secondary SDL surface: %s\n", SDL_GetError());
122                 return false;
123         }
124
125         if (settings.useOpenGL)
126                 sdlemu_init_opengl(surface, mainSurface, 1 /*method*/,
127                         settings.glFilter /*texture type (linear, nearest)*/,
128                         0 /* Automatic bpp selection based upon src */);
129
130         // Initialize Joystick support under SDL
131 /*      if (settings.useJoystick)
132         {
133                 if (SDL_NumJoysticks() <= 0)
134                 {
135                         settings.useJoystick = false;
136                         WriteLog("Video: No joystick(s) or joypad(s) detected on your system. Using keyboard...\n");
137                 }
138                 else
139                 {
140                         if ((joystick = SDL_JoystickOpen(settings.joyport)) == 0)
141                         {
142                                 settings.useJoystick = false;
143                                 WriteLog("Video: Unable to open a Joystick on port: %d\n", (int)settings.joyport);
144                         }
145                         else
146                                 WriteLog("Video: Using: %s\n", SDL_JoystickName(settings.joyport));
147                 }
148         }//*/
149
150         // Set up the scrBuffer
151         scrBuffer = (uint32 *)surface->pixels;  // Kludge--And shouldn't have to lock since it's a software surface...
152 //needed? Dunno. Mebbe an SDL function instead?
153 //      memset(scrBuffer, 0x00, VIRTUAL_SCREEN_WIDTH * VIRTUAL_SCREEN_HEIGHT * sizeof(uint32));
154
155         if (settings.fullscreen)
156                 SDL_ShowCursor(SDL_DISABLE);
157
158 #ifdef TEST_ALPHA_BLENDING
159 //Here's some code to test alpha blending...
160 //Well whaddya know, it works. :-)
161         someAlphaSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, 30, 30, 32,
162                 MASK_R, MASK_G, MASK_B, MASK_A);
163
164         for(int i=0; i<30; i++)
165         {
166                 for(int j=0; j<30; j++)
167                 {
168                         uint32 color = (uint32)(((double)(i * j) / (29.0 * 29.0)) * 255.0);
169                         color = (color << 24) | 0x00FF00FF;
170                         ((uint32 *)someAlphaSurface->pixels)[(j * 30) + i] = color;
171                 }
172         }
173 //End test code
174 #endif
175
176         WriteLog("Video: Successfully initialized.\n");
177         return true;
178 }
179
180 //
181 // Free various SDL components
182 //
183 void VideoDone(void)
184 {
185         if (settings.useOpenGL)
186                 sdlemu_close_opengl();
187
188 //      SDL_JoystickClose(joystick);
189         SDL_FreeSurface(surface);
190         SDL_Quit();
191 }
192
193 //
194 // Render the backbuffer to the primary screen surface
195 //
196 void RenderScreenBuffer(void)
197 {
198 //WriteLog("Video: Blitting a %u x %u surface to the main surface...\n", surface->w, surface->h);
199 //Don't need this crapola--why have a separate buffer just to copy it to THIS
200 //buffer in order to copy it to the main screen? That's what *I* thought!
201 /*      if (SDL_MUSTLOCK(surface))
202                 while (SDL_LockSurface(surface) < 0)
203                         SDL_Delay(10);
204
205         memcpy(surface->pixels, scrBuffer, VIRTUAL_SCREEN_WIDTH * VIRTUAL_SCREEN_HEIGHT * sizeof(uint32));
206
207         if (SDL_MUSTLOCK(surface))
208                 SDL_UnlockSurface(surface);//*/
209 #ifdef TEST_ALPHA_BLENDING
210 SDL_Rect dstRect = { 100, 100, 30, 30 };
211 SDL_BlitSurface(someAlphaSurface, NULL, surface, &dstRect);
212 #endif
213
214         if (settings.useOpenGL)
215                 sdlemu_draw_texture(mainSurface, surface, 1/*1=GL_QUADS*/);
216         else
217         {
218                 SDL_BlitSurface(surface, NULL, mainSurface, NULL);
219                 SDL_Flip(mainSurface);
220     }
221 }
222
223 #if 0
224 //
225 // Fullscreen <-> window switching
226 //
227 //NOTE: This does *NOT* work with OpenGL rendering! !!! FIX !!!
228 void ToggleFullscreen(void)
229 {
230         vjs.fullscreen = !vjs.fullscreen;
231         mainSurfaceFlags &= ~SDL_FULLSCREEN;
232
233         if (vjs.fullscreen)
234                 mainSurfaceFlags |= SDL_FULLSCREEN;
235
236         mainSurface = SDL_SetVideoMode(tom_width, tom_height, 16, mainSurfaceFlags);
237
238         if (mainSurface == NULL)
239         {
240                 WriteLog("Video: SDL is unable to set the video mode: %s\n", SDL_GetError());
241                 exit(1);
242         }
243
244         SDL_WM_SetCaption("Virtual Jaguar", "Virtual Jaguar");
245 }
246 #endif