]> icculus.org git repositories - btb/d2x.git/blob - arch/ogl/sdlgl.c
fix crash/hang when using -nojoystick in linux version (d1x r1.5)
[btb/d2x.git] / arch / ogl / sdlgl.c
1 /* $Id: sdlgl.c,v 1.11 2004-05-20 02:04:28 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         if (gl_initialized){
73                 if (x==curx && y==cury && curfull==ogl_fullscreen)
74                         return 0;
75 #ifdef __linux__ // Windows, at least, seems to need to reload every time.
76                 if (ogl_fullscreen || curfull)
77 #endif
78                         ogl_smash_texture_list_internal();//if we are or were fullscreen, changing vid mode will invalidate current textures
79         }
80         SDL_WM_SetCaption(DESCENT_VERSION, "Descent II");
81
82 #ifdef SDL_IMAGE
83         {
84 #include "descent.xpm"
85                 SDL_WM_SetIcon(IMG_ReadXPMFromArray(pixmap), NULL);
86         }
87 #endif
88
89         if (!SDL_SetVideoMode(x,y, 16, SDL_OPENGL | (ogl_fullscreen?SDL_FULLSCREEN:0))) {
90            Error("Could not set %dx%dx16 opengl video mode\n",x,y);
91         }
92         SDL_ShowCursor(0);
93
94         curx=x;cury=y;curfull=ogl_fullscreen;
95         gl_initialized=1;
96
97         return 0;
98 }
99
100 void ogl_destroy_window(void){
101         if (gl_initialized){
102                 ogl_smash_texture_list_internal();
103                 SDL_ShowCursor(1);
104                 //gl_initialized=0;
105                 //well..SDL doesn't really let you kill the window.. so we just need to wait for sdl_quit
106         }
107         return;
108 }
109
110 void ogl_init(void){
111         int t;
112         if ((t=FindArg("-gl_red")))
113                 SDL_GL_SetAttribute( SDL_GL_RED_SIZE, atoi(Args[t+1]) );
114         if ((t=FindArg("-gl_green")))
115                 SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, atoi(Args[t+1]) );
116         if ((t=FindArg("-gl_blue")))
117                 SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, atoi(Args[t+1]) );
118         if ((t=FindArg("-gl_alpha")))
119                 SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, atoi(Args[t+1]) );
120         if ((t=FindArg("-gl_buffer")))
121                 SDL_GL_SetAttribute( SDL_GL_BUFFER_SIZE, atoi(Args[t+1]) );
122 //      SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
123 //      SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
124 //      SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
125 //      SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8 );
126
127
128         SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,0);
129         SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE,0);
130         SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE,0);
131         SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE,0);
132         SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE,0);
133         SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE,0);
134         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
135
136         init_gammaramp();
137 }
138
139 void ogl_close(void){
140 #if 0  // shouldn't really be necessary...
141         if (ogl_fullscreen){
142                 ogl_fullscreen=0;
143                 ogl_do_fullscreen_internal();
144         }
145 #endif
146         ogl_destroy_window();
147 }