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 //And now, they do! :-)
17 SDL_Surface * surface, * mainSurface;
18 Uint32 mainSurfaceFlags;
20 SDL_Joystick * joystick;
23 // Prime SDL and create surfaces
28 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE) < 0)
30 WriteLog("VJ: Could not initialize the SDL library: %s", SDL_GetError());
34 // Get proper info about the platform we're running on...
35 const SDL_VideoInfo * info = SDL_GetVideoInfo();
39 WriteLog("VJ: SDL is unable to get the video info: %s\n", SDL_GetError());
45 mainSurfaceFlags = SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF | SDL_OPENGL;
46 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
50 if (info->hw_available)
51 mainSurfaceFlags = SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF;
54 mainSurfaceFlags |= SDL_HWACCEL;
58 mainSurfaceFlags |= SDL_FULLSCREEN;
61 mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH, VIRTUAL_SCREEN_HEIGHT_NTSC, 16, mainSurfaceFlags);
63 mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH * 2, VIRTUAL_SCREEN_HEIGHT_NTSC * 2, 16, mainSurfaceFlags);
65 if (mainSurface == NULL)
67 WriteLog("VJ: SDL is unable to set the video mode: %s\n", SDL_GetError());
71 SDL_WM_SetCaption("Virtual Jaguar", "Virtual Jaguar");
73 // Create the primary SDL display (16 BPP, 5/5/5 RGB format)
74 surface = SDL_CreateRGBSurface(SDL_SWSURFACE, VIRTUAL_SCREEN_WIDTH, VIRTUAL_SCREEN_HEIGHT_NTSC,
75 16, 0x7C00, 0x03E0, 0x001F, 0);
79 WriteLog("VJ: Could not create primary SDL surface: %s\n", SDL_GetError());
84 //Should make another setting here, for either linear or nearest (instead of just picking one)
86 sdlemu_init_opengl(surface, 1/*method*/, 2/*size*/, vjs.glFilter/*texture type (linear, nearest)*/);
88 // Initialize Joystick support under SDL
91 if (SDL_NumJoysticks() <= 0)
93 vjs.useJoystick = false;
94 printf("VJ: No joystick(s) or joypad(s) detected on your system. Using keyboard...\n");
98 if ((joystick = SDL_JoystickOpen(vjs.joyport)) == 0)
100 vjs.useJoystick = false;
101 printf("VJ: Unable to open a Joystick on port: %d\n", (int)vjs.joyport);
104 printf("VJ: Using: %s\n", SDL_JoystickName(vjs.joyport));
108 // Set up the backbuffer
109 //To be safe, this should be 1280 * 625 * 2...
110 // backbuffer = (int16 *)malloc(845 * 525 * sizeof(int16));
111 backbuffer = (int16 *)malloc(1280 * 625 * sizeof(int16));
112 memset(backbuffer, 0x44, VIRTUAL_SCREEN_WIDTH * VIRTUAL_SCREEN_HEIGHT_NTSC * sizeof(int16));
118 // Free various SDL components
123 sdlemu_close_opengl();
125 SDL_JoystickClose(joystick);
126 SDL_FreeSurface(surface);
127 SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER);
134 // Render the backbuffer to the primary screen surface
136 void RenderBackbuffer(void)
138 if (SDL_MUSTLOCK(surface))
139 while (SDL_LockSurface(surface) < 0)
142 memcpy(surface->pixels, backbuffer, tom_getVideoModeWidth() * tom_getVideoModeHeight() * 2);
144 if (SDL_MUSTLOCK(surface))
145 SDL_UnlockSurface(surface);
148 sdlemu_draw_texture(mainSurface, surface, 1/*1=GL_QUADS*/);
151 SDL_Rect rect = { 0, 0, surface->w, surface->h };
152 SDL_BlitSurface(surface, &rect, mainSurface, &rect);
153 SDL_Flip(mainSurface);
158 // Resize the main SDL screen & backbuffer
160 void ResizeScreen(uint32 width, uint32 height)
162 char window_title[256];
164 SDL_FreeSurface(surface);
165 surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 16,
166 0x7C00, 0x03E0, 0x001F, 0);
170 WriteLog("Video: Could not create primary SDL surface: %s", SDL_GetError());
175 // This seems to work well for resizing (i.e., changes in the pixel width)...
176 sdlemu_resize_texture(surface, mainSurface, vjs.glFilter);
179 mainSurface = SDL_SetVideoMode(width, height, 16, mainSurfaceFlags);
181 if (mainSurface == NULL)
183 WriteLog("Video: SDL is unable to set the video mode: %s\n", SDL_GetError());
188 sprintf(window_title, "Virtual Jaguar (%i x %i)", (int)width, (int)height);
189 SDL_WM_SetCaption(window_title, window_title);
191 // This seems to work well for resizing (i.e., changes in the pixel width)...
192 // if (vjs.useOpenGL)
193 // sdlemu_resize_texture(surface, mainSurface);
197 // Return the screen's pitch
199 uint32 GetSDLScreenPitch(void)
201 return surface->pitch;
205 // Fullscreen <-> window switching
207 //NOTE: This does *NOT* work with OpenGL rendering! !!! FIX !!!
208 void ToggleFullscreen(void)
210 vjs.fullscreen = !vjs.fullscreen;
211 mainSurfaceFlags &= ~SDL_FULLSCREEN;
214 mainSurfaceFlags |= SDL_FULLSCREEN;
216 mainSurface = SDL_SetVideoMode(tom_width, tom_height, 16, mainSurfaceFlags);
218 if (mainSurface == NULL)
220 WriteLog("Video: SDL is unable to set the video mode: %s\n", SDL_GetError());
224 SDL_WM_SetCaption("Virtual Jaguar", "Virtual Jaguar");