]> Shamusworld >> Repos - virtualjaguar/blob - src/video.cpp
New video subsystem supports 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 "types.h"
8 #include "tom.h"
9 #include "sdlemu_opengl.h"                                                      // For testing only... ;-)
10 #include "video.h"
11
12 // External global variables
13
14 extern SDL_Surface * surface, * mainSurface;
15 extern Uint32 mainSurfaceFlags;
16 extern int16 * backbuffer;
17 extern bool useOpenGL;                                                          // Testing, testing... :-)
18 extern bool fullscreen;
19
20 //
21 // Render the backbuffer to the primary screen surface
22 //
23 void RenderBackbuffer(void)
24 {
25         if (SDL_MUSTLOCK(surface))
26                 while (SDL_LockSurface(surface) < 0)
27                         SDL_Delay(10);
28
29         memcpy(surface->pixels, backbuffer, tom_getVideoModeWidth() * tom_getVideoModeHeight() * 2);
30
31         if (SDL_MUSTLOCK(surface))
32                 SDL_UnlockSurface(surface);
33
34         if (useOpenGL)
35                 sdlemu_draw_texture(mainSurface, surface, 1/*1=GL_QUADS*/);
36         else
37         {
38                 SDL_Rect rect = { 0, 0, surface->w, surface->h };
39                 SDL_BlitSurface(surface, &rect, mainSurface, &rect);
40                 SDL_Flip(mainSurface);
41     }
42 }
43
44 //
45 // Resize the main SDL screen & backbuffer
46 //
47 void ResizeScreen(uint32 width, uint32 height)
48 {
49         char window_title[256];
50
51         SDL_FreeSurface(surface);
52         surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height,
53                 16, 0x7C00, 0x03E0, 0x001F, 0);
54         if (surface == NULL)
55         {
56                 WriteLog("TOM: Could not create primary SDL surface: %s", SDL_GetError());
57                 exit(1);
58         }
59
60         sprintf(window_title, "Virtual Jaguar (%i x %i)", (int)width, (int)height);
61 //Hmm.
62 /*      mainSurface = SDL_SetVideoMode(tom_width, tom_height, 16, mainSurfaceFlags);
63
64         if (mainSurface == NULL)
65         {
66                 WriteLog("Joystick: SDL is unable to set the video mode: %s\n", SDL_GetError());
67                 exit(1);
68         }//*/
69
70         SDL_WM_SetCaption(window_title, window_title);
71
72 //This seems to work well for resizing...
73         if (useOpenGL)
74                 sdlemu_resize_texture(surface, mainSurface);
75 }
76
77 //
78 // Return the screen's pitch
79 //
80 uint32 GetSDLScreenPitch(void)
81 {
82 //      extern SDL_Surface * surface;
83
84         return surface->pitch;
85 }
86
87 //
88 // Fullscreen <-> window switching
89 //
90 //NOTE: This does *NOT* work with OpenGL rendering! !!! FIX !!!
91 void ToggleFullscreen(void)
92 {
93         fullscreen = !fullscreen;
94         mainSurfaceFlags &= ~SDL_FULLSCREEN;
95
96         if (fullscreen)
97                 mainSurfaceFlags |= SDL_FULLSCREEN;
98
99         mainSurface = SDL_SetVideoMode(tom_width, tom_height, 16, mainSurfaceFlags);
100
101         if (mainSurface == NULL)
102         {
103                 WriteLog("Joystick: SDL is unable to set the video mode: %s\n", SDL_GetError());
104                 exit(1);
105         }
106
107         SDL_WM_SetCaption("Virtual Jaguar", "Virtual Jaguar");
108 }