2 // VIDEO.CPP: SDL/local hardware specific video routines
9 #include "sdlemu_opengl.h"
13 // External global variables
15 //shouldn't these exist here??? Prolly.
16 extern SDL_Surface * surface, * mainSurface;
17 extern Uint32 mainSurfaceFlags;
18 extern int16 * backbuffer;
19 extern SDL_Joystick * joystick;
22 // Prime SDL and create surfaces
27 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE) < 0)
29 WriteLog("VJ: Could not initialize the SDL library: %s", SDL_GetError());
33 // Get proper info about the platform we're running on...
34 const SDL_VideoInfo * info = SDL_GetVideoInfo();
38 WriteLog("VJ: SDL is unable to get the video info: %s\n", SDL_GetError());
44 mainSurfaceFlags = SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF | SDL_OPENGL;
45 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
49 if (info->hw_available)
50 mainSurfaceFlags = SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF;
53 mainSurfaceFlags |= SDL_HWACCEL;
57 mainSurfaceFlags |= SDL_FULLSCREEN;
60 mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH, VIRTUAL_SCREEN_HEIGHT, 16, mainSurfaceFlags);
62 mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH * 2, VIRTUAL_SCREEN_HEIGHT * 2, 16, mainSurfaceFlags);
64 if (mainSurface == NULL)
66 WriteLog("VJ: SDL is unable to set the video mode: %s\n", SDL_GetError());
70 SDL_WM_SetCaption("Virtual Jaguar", "Virtual Jaguar");
72 // Create the primary SDL display (16 BPP, 5/5/5 RGB format)
73 surface = SDL_CreateRGBSurface(SDL_SWSURFACE, VIRTUAL_SCREEN_WIDTH, VIRTUAL_SCREEN_HEIGHT,
74 16, 0x7C00, 0x03E0, 0x001F, 0);
78 WriteLog("VJ: Could not create primary SDL surface: %s\n", SDL_GetError());
83 sdlemu_init_opengl(surface, 1/*method*/, 2/*size*/, 0/*texture type (linear, nearest)*/);
85 // Initialize Joystick support under SDL
88 if (SDL_NumJoysticks() <= 0)
90 vjs.useJoystick = false;
91 printf("VJ: No joystick(s) or joypad(s) detected on your system. Using keyboard...\n");
95 if ((joystick = SDL_JoystickOpen(vjs.joyport)) == 0)
97 vjs.useJoystick = false;
98 printf("VJ: Unable to open a Joystick on port: %d\n", (int)vjs.joyport);
101 printf("VJ: Using: %s\n", SDL_JoystickName(vjs.joyport));
109 // Free various SDL components
114 sdlemu_close_opengl();
116 SDL_JoystickClose(joystick);
117 SDL_FreeSurface(surface);
118 SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER);
123 // Render the backbuffer to the primary screen surface
125 void RenderBackbuffer(void)
127 if (SDL_MUSTLOCK(surface))
128 while (SDL_LockSurface(surface) < 0)
131 memcpy(surface->pixels, backbuffer, tom_getVideoModeWidth() * tom_getVideoModeHeight() * 2);
133 if (SDL_MUSTLOCK(surface))
134 SDL_UnlockSurface(surface);
137 sdlemu_draw_texture(mainSurface, surface, 1/*1=GL_QUADS*/);
140 SDL_Rect rect = { 0, 0, surface->w, surface->h };
141 SDL_BlitSurface(surface, &rect, mainSurface, &rect);
142 SDL_Flip(mainSurface);
147 // Resize the main SDL screen & backbuffer
149 void ResizeScreen(uint32 width, uint32 height)
151 char window_title[256];
153 SDL_FreeSurface(surface);
154 surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 16,
155 0x7C00, 0x03E0, 0x001F, 0);
159 WriteLog("Video: Could not create primary SDL surface: %s", SDL_GetError());
163 sprintf(window_title, "Virtual Jaguar (%i x %i)", (int)width, (int)height);
167 mainSurface = SDL_SetVideoMode(width, height, 16, mainSurfaceFlags);
169 if (mainSurface == NULL)
171 WriteLog("Video: SDL is unable to set the video mode: %s\n", SDL_GetError());
176 SDL_WM_SetCaption(window_title, window_title);
178 // This seems to work well for resizing (i.e., changes in the pixel width)...
180 sdlemu_resize_texture(surface, mainSurface);
184 // Return the screen's pitch
186 uint32 GetSDLScreenPitch(void)
188 return surface->pitch;
192 // Fullscreen <-> window switching
194 //NOTE: This does *NOT* work with OpenGL rendering! !!! FIX !!!
195 void ToggleFullscreen(void)
197 vjs.fullscreen = !vjs.fullscreen;
198 mainSurfaceFlags &= ~SDL_FULLSCREEN;
201 mainSurfaceFlags |= SDL_FULLSCREEN;
203 mainSurface = SDL_SetVideoMode(tom_width, tom_height, 16, mainSurfaceFlags);
205 if (mainSurface == NULL)
207 WriteLog("Video: SDL is unable to set the video mode: %s\n", SDL_GetError());
211 SDL_WM_SetCaption("Virtual Jaguar", "Virtual Jaguar");