]> icculus.org git repositories - btb/d2x.git/blob - arch/svgalib/gr.c
use timer_delay instead of d_delay
[btb/d2x.git] / arch / svgalib / gr.c
1 /*
2  * $Source: /cvs/cvsroot/d2x/arch/svgalib/gr.c,v $
3  * $Revision: 1.1 $
4  * $Author: bradleyb $
5  * $Date: 2001-10-25 08:25:34 $
6  *
7  * SVGALib video functions
8  *
9  * $Log: not supported by cvs2svn $
10  * Revision 1.2  2001/01/29 13:47:52  bradleyb
11  * Fixed build, some minor cleanups.
12  *
13  */
14
15 #ifdef HAVE_CONFIG_H
16 #include <conf.h>
17 #endif
18
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <vga.h>
23 #include <vgagl.h>
24 #include "gr.h"
25 #include "grdef.h"
26 #include "palette.h"
27 #include "u_mem.h"
28 #include "error.h"
29 #ifdef SVGALIB_INPUT
30 #include <vgamouse.h>
31 #endif
32
33 #include "gamefont.h"
34
35 int gr_installed = 0;
36 int usebuffer;
37
38 extern void mouse_handler (int button, int dx, int dy, int dz, int drx, int dry, int drz);
39
40 GraphicsContext *physicalscreen, *screenbuffer;
41
42 void gr_update()
43 {
44         if (usebuffer)
45                 gl_copyscreen(physicalscreen);
46 }
47
48 int gr_set_mode(u_int32_t mode)
49 {
50         unsigned int w, h;
51         char vgamode[16];
52         vga_modeinfo *modeinfo;
53         int modenum, rowsize;
54         void *framebuffer;
55         
56 #ifdef NOGRAPH
57         return 0;
58 #endif
59         if (mode<=0)
60                 return 0;
61
62         w=SM_W(mode);
63         h=SM_H(mode);
64
65         gr_palette_clear();
66
67         sprintf(vgamode, "G%dx%dx256", w, h);
68         modenum = vga_getmodenumber(vgamode);   
69         vga_setmode(modenum);
70 #ifdef SVGALIB_INPUT
71         mouse_seteventhandler(mouse_handler);
72 #endif
73         modeinfo = vga_getmodeinfo(modenum);
74
75         if (modeinfo->flags & CAPABLE_LINEAR)
76         {
77                 usebuffer = 0;
78
79                 vga_setlinearaddressing();
80
81                 // Set up physical screen only
82                 gl_setcontextvga(modenum);
83                 physicalscreen = gl_allocatecontext();
84                 gl_getcontext(physicalscreen);
85                 screenbuffer = physicalscreen;
86
87                 framebuffer = physicalscreen->vbuf;
88                 rowsize = physicalscreen->bytewidth;
89         }
90         else
91         {
92                 usebuffer = 1;
93
94                 // Set up the physical screen
95                 gl_setcontextvga(modenum);
96                 physicalscreen = gl_allocatecontext();
97                 gl_getcontext(physicalscreen);
98
99                 // Set up the virtual screen
100                 gl_setcontextvgavirtual(modenum);
101                 screenbuffer = gl_allocatecontext();
102                 gl_getcontext(screenbuffer);
103
104                 framebuffer = screenbuffer->vbuf;
105                 rowsize = screenbuffer->bytewidth;
106         }
107
108         memset(grd_curscreen, 0, sizeof(grs_screen));
109         grd_curscreen->sc_mode = mode;
110         grd_curscreen->sc_w = w;
111         grd_curscreen->sc_h = h;
112         grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
113         grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
114         grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
115         grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
116         grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
117         grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = rowsize;
118         grd_curscreen->sc_canvas.cv_bitmap.bm_type = BM_LINEAR;
119         grd_curscreen->sc_canvas.cv_bitmap.bm_data = framebuffer;
120         gr_set_current_canvas(NULL);
121         
122         //gamefont_choose_game_font(w,h);
123         
124         return 0;
125 }
126
127 int gr_init(void)
128 {
129         int retcode;
130         int mode = SM(320,200);
131
132         // Only do this function once!
133         if (gr_installed==1)
134                 return -1;
135         MALLOC(grd_curscreen,grs_screen, 1);
136         memset(grd_curscreen, 0, sizeof(grs_screen));
137         
138         vga_init();
139
140         if ((retcode=gr_set_mode(mode)))
141                 return retcode;
142         
143         grd_curscreen->sc_canvas.cv_color = 0;
144         grd_curscreen->sc_canvas.cv_drawmode = 0;
145         grd_curscreen->sc_canvas.cv_font = NULL;
146         grd_curscreen->sc_canvas.cv_font_fg_color = 0;
147         grd_curscreen->sc_canvas.cv_font_bg_color = 0;
148         gr_set_current_canvas( &grd_curscreen->sc_canvas );
149
150         gr_installed = 1;
151         atexit(gr_close);
152         return 0;
153 }
154
155 void gr_close ()
156 {
157         if (gr_installed==1)
158         {
159                 gr_installed = 0;
160                 free(grd_curscreen);
161                 gl_freecontext(screenbuffer);
162                 gl_freecontext(physicalscreen);
163         }
164 }
165
166 // Palette functions follow.
167
168 static int last_r=0, last_g=0, last_b=0;
169
170 void gr_palette_clear ()
171 {
172         int colors[768];
173
174         memset (colors, 0, 768 * sizeof(int));
175         vga_setpalvec (0, 256, colors);
176
177         gr_palette_faded_out = 1;
178 }
179
180
181 void gr_palette_step_up (int r, int g, int b)
182 {
183         int i = 0;
184         ubyte *p = gr_palette;
185         int temp;
186
187         int colors[768];
188
189         if (gr_palette_faded_out) return;
190
191         if ((r==last_r) && (g==last_g) && (b==last_b)) return;
192
193         last_r = r;
194         last_g = g;
195         last_b = b;
196
197         while (i < 768)
198         {
199                 temp = (int)(*p++) + r + gr_palette_gamma;
200                 if (temp<0) temp=0;
201                 else if (temp>63) temp=63;
202                 colors[i++] = temp;
203                 temp = (int)(*p++) + g + gr_palette_gamma;
204                 if (temp<0) temp=0;
205                 else if (temp>63) temp=63;
206                 colors[i++] = temp;
207                 temp = (int)(*p++) + b + gr_palette_gamma;
208                 if (temp<0) temp=0;
209                 else if (temp>63) temp=63;
210                 colors[i++] = temp;
211         }
212         vga_setpalvec (0, 256, colors);
213 }
214
215 //added on 980913 by adb to fix palette problems
216 // need a min without side effects...
217 #undef min
218 static inline int min(int x, int y) { return x < y ? x : y; }
219 //end changes by adb
220
221 void gr_palette_load (ubyte *pal)       
222 {
223         int i;
224         int colors[768];
225
226         for (i = 0; i < 768; i++)
227         {
228                 gr_current_pal[i] = pal[i];
229                 if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
230                 colors[i] = (min(gr_current_pal[i] + gr_palette_gamma, 63));
231         }
232
233         vga_setpalvec (0, 256, colors);
234
235         gr_palette_faded_out = 0;
236         init_computed_colors();
237 }
238
239
240
241 int gr_palette_fade_out (ubyte *pal, int nsteps, int allow_keys)
242 {
243         int i, j;
244         ubyte c;
245         fix fade_palette[768];
246         fix fade_palette_delta[768];
247
248         int fade_colors[768];
249
250         if (gr_palette_faded_out) return 0;
251
252         if (pal==NULL) pal=gr_current_pal;
253
254         for (i=0; i<768; i++)
255         {
256                 gr_current_pal[i] = pal[i];
257                 fade_palette[i] = i2f(pal[i]);
258                 fade_palette_delta[i] = fade_palette[i] / nsteps;
259         }
260
261         for (j=0; j<nsteps; j++)
262         {
263                 for (i = 0; i < 768; i++)
264                 {
265                         fade_palette[i] -= fade_palette_delta[i];
266                         if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma))
267                                 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
268                         c = f2i(fade_palette[i]);
269                         if (c > 63) c = 63;
270                         fade_colors[i] = c;
271                 }
272                 vga_setpalvec (0, 256, fade_colors);
273         }
274
275         gr_palette_faded_out = 1;
276         return 0;
277 }
278
279
280
281 int gr_palette_fade_in (ubyte *pal, int nsteps, int allow_keys)
282 {
283         int i, j;
284         ubyte c;
285         fix fade_palette[768];
286         fix fade_palette_delta[768];
287
288         int fade_colors[768];
289
290         if (!gr_palette_faded_out) return 0;
291
292         for (i=0; i<768; i++)
293         {
294                 gr_current_pal[i] = pal[i];
295                 fade_palette[i] = 0;
296                 fade_palette_delta[i] = i2f(pal[i] + gr_palette_gamma) / nsteps;
297         }
298
299         for (j=0; j<nsteps; j++ )
300         {
301                 for (i = 0; i < 768; i++ )
302                 {
303                         fade_palette[i] += fade_palette_delta[i];
304                         if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma))
305                                 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
306                         c = f2i(fade_palette[i]);
307                         if (c > 63) c = 63;
308                         fade_colors[i] = c;
309                 }
310                 vga_setpalvec (0, 256, fade_colors);
311         }
312
313         gr_palette_faded_out = 0;
314         return 0;
315 }
316
317
318
319 void gr_palette_read (ubyte *pal)
320 {
321         int colors[768];
322         int i;
323
324         vga_getpalvec (0, 256, colors);
325
326         for (i = 0; i < 768; i++)
327                 pal[i] = colors[i];
328 }