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