]> icculus.org git repositories - btb/d2x.git/blob - arch/ogl/sdlgl.c
make Mac OS 9 Voodoo display textures
[btb/d2x.git] / arch / ogl / sdlgl.c
1 /* $Id: sdlgl.c,v 1.12 2004-05-22 22:43:50 btb Exp $ */
2 /*
3  *
4  * Graphics functions for SDL-GL.
5  *
6  *
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include <conf.h>
11 #endif
12
13 #include <SDL.h>
14 #ifdef SDL_IMAGE
15 #include <SDL_image.h>
16 #endif
17
18 #include "internal.h"
19 #include "vers_id.h"
20 #include "error.h"
21 #include "u_mem.h"
22 #include "args.h"
23
24 static int curx=-1,cury=-1,curfull=0;
25
26 void ogl_do_fullscreen_internal(void){
27         ogl_init_window(curx,cury);
28 }
29
30 static Uint16 gammaramp[512];
31
32 static void init_gammaramp(void)
33 {
34         int i;
35
36         for (i = 0; i < 256; ++i)
37                 gammaramp[i] = i * 256;
38         for (i = 256; i < 512; ++i)
39                 gammaramp[i] = 0xffff;
40 }
41
42 int ogl_setbrightness_internal(void)
43 {
44         return SDL_SetGammaRamp(gammaramp + ogl_brightness_r * 4,
45                                 gammaramp + ogl_brightness_g * 4,
46                                 gammaramp + ogl_brightness_b * 4
47                                                         );
48 }
49
50 // maybe we might add a real gamma setting (as opposed to brightness setting)
51 // however, SDL_SetGamma seems to call SetGammaRamp internally, so we would need
52 // to modify our own gamma ramp instead.
53 //int ogl_setgamma_internal(void)
54 //{
55 //      float gamma = 1 + gr_palette_realgamma / 8.0;
56 //      return SDL_SetGamma(gamma, gamma, gamma);
57 //}
58
59 void ogl_swap_buffers_internal(void)
60 {
61         SDL_GL_SwapBuffers();
62 }
63
64
65 int ogl_check_mode(int x, int y)
66 {
67         return !SDL_VideoModeOK(x, y, 16, SDL_OPENGL | (ogl_fullscreen?SDL_FULLSCREEN:0));
68 }
69
70
71 int ogl_init_window(int x, int y)
72 {
73         int bpp = FindArg("-gl_16bpp") ? 16 : 32;
74
75         if (gl_initialized){
76                 if (x==curx && y==cury && curfull==ogl_fullscreen)
77                         return 0;
78 #ifdef __linux__ // Windows, at least, seems to need to reload every time.
79                 if (ogl_fullscreen || curfull)
80 #endif
81                         ogl_smash_texture_list_internal();//if we are or were fullscreen, changing vid mode will invalidate current textures
82         }
83         SDL_WM_SetCaption(DESCENT_VERSION, "Descent II");
84
85 #ifdef SDL_IMAGE
86         {
87 #include "descent.xpm"
88                 SDL_WM_SetIcon(IMG_ReadXPMFromArray(pixmap), NULL);
89         }
90 #endif
91
92         if (!SDL_SetVideoMode(x, y, bpp, SDL_OPENGL | (ogl_fullscreen ? SDL_FULLSCREEN : 0)))
93         {
94                 Error("Could not set %dx%dx%d opengl video mode\n", x, y, bpp);
95         }
96         SDL_ShowCursor(0);
97
98         curx=x;cury=y;curfull=ogl_fullscreen;
99         gl_initialized=1;
100
101         return 0;
102 }
103
104 void ogl_destroy_window(void){
105         if (gl_initialized){
106                 ogl_smash_texture_list_internal();
107                 SDL_ShowCursor(1);
108                 //gl_initialized=0;
109                 //well..SDL doesn't really let you kill the window.. so we just need to wait for sdl_quit
110         }
111         return;
112 }
113
114 void ogl_init(void){
115         int t;
116         if ((t=FindArg("-gl_red")))
117                 SDL_GL_SetAttribute( SDL_GL_RED_SIZE, atoi(Args[t+1]) );
118         if ((t=FindArg("-gl_green")))
119                 SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, atoi(Args[t+1]) );
120         if ((t=FindArg("-gl_blue")))
121                 SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, atoi(Args[t+1]) );
122         if ((t=FindArg("-gl_alpha")))
123                 SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, atoi(Args[t+1]) );
124         if ((t=FindArg("-gl_buffer")))
125                 SDL_GL_SetAttribute( SDL_GL_BUFFER_SIZE, atoi(Args[t+1]) );
126 //      SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
127 //      SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
128 //      SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
129 //      SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8 );
130
131
132         SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,0);
133         SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE,0);
134         SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE,0);
135         SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE,0);
136         SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE,0);
137         SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE,0);
138         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
139
140         init_gammaramp();
141 }
142
143 void ogl_close(void){
144 #if 0  // shouldn't really be necessary...
145         if (ogl_fullscreen){
146                 ogl_fullscreen=0;
147                 ogl_do_fullscreen_internal();
148         }
149 #endif
150         ogl_destroy_window();
151 }