]> icculus.org git repositories - btb/d2x.git/blob - video/sdl_gr.c
Fixed build, some minor cleanups.
[btb/d2x.git] / video / sdl_gr.c
1 /*
2  * $Source: /cvs/cvsroot/d2x/video/sdl_gr.c,v $
3  * $Revision: 1.3 $
4  * $Author: bradleyb $
5  * $Date: 2001-01-29 13:47:52 $
6  *
7  * SDL video functions.
8  *
9  * $Log: not supported by cvs2svn $
10  */
11
12 #ifdef HAVE_CONFIG_H
13 #include <conf.h>
14 #endif
15
16 #include <stdlib.h>
17 #include <string.h>
18 #include <SDL/SDL.h>
19 #include "gr.h"
20 #include "grdef.h"
21 #include "palette.h"
22 #include "u_mem.h"
23 #include "error.h"
24 //added on 9/30/98 by Matt Mueller to set the title bar.  Woohoo!
25 #include "vers_id.h"
26 //end addition -MM
27
28 #include "gamefont.h"
29
30 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
31 #include "args.h"
32
33 int sdl_video_flags = SDL_SWSURFACE | SDL_HWPALETTE;
34 char checkvidmodeok=0;
35 //end addition -MM
36
37 SDL_Surface *screen;
38
39 int gr_installed = 0;
40
41 //added 05/19/99 Matt Mueller - locking stuff
42 #ifdef GR_LOCK
43 #include "checker.h"
44 #ifdef TEST_GR_LOCK
45 int gr_testlocklevel=0;
46 #endif
47 inline void gr_dolock(const char *file,int line) {
48         gr_dotestlock();
49         if ( gr_testlocklevel==1 && SDL_MUSTLOCK(screen) ) {
50 #ifdef __CHECKER__
51                 chcksetwritable(screen.pixels,screen->w*screen->h*screen->format->BytesPerPixel);
52 #endif
53                 if ( SDL_LockSurface(screen) < 0 )Error("could not lock screen (%s:%i)\n",file,line);
54         }
55 }
56 inline void gr_dounlock(void) {
57         gr_dotestunlock();
58         if (gr_testlocklevel==0 && SDL_MUSTLOCK(screen) ) {
59                 SDL_UnlockSurface(screen);
60 #ifdef __CHECKER__
61                 chcksetunwritable(screen.pixels,screen->w*screen->h*screen->format->BytesPerPixel);
62 #endif
63         }
64 }
65 #endif
66 //end addition -MM
67
68 void gr_palette_clear(); // Function prototype for gr_init;
69
70
71 void gr_update()
72 {
73         //added 05/19/99 Matt Mueller - locking stuff
74 //
75 //gr_testunlock();
76         //end addition -MM
77  SDL_UpdateRect(screen,0,0,0,0);
78 }
79
80 extern int VGA_current_mode; // DPH: kludge - remove at all costs
81
82 int gr_set_mode(u_int32_t mode)
83 {
84         int w,h;
85 #ifdef NOGRAPH
86         return 0;
87 #endif
88
89         if (mode<=0)
90                 return 0;
91
92         w=SM_W(mode);
93         h=SM_H(mode);
94         VGA_current_mode = mode;
95         
96         if (screen != NULL) gr_palette_clear();
97
98 //added on 11/06/98 by Matt Mueller to set the title bar. (moved from below)
99 //sekmu: might wanna copy this litte blurb to one of the text files or something
100 //we want to set it here so that X window manager "Style" type commands work
101 //for example, in fvwm2 or fvwm95:
102 //Style "D1X*"  NoTitle, NoHandles, BorderWidth 0
103 //if you can't use -fullscreen like me (crashes X), this is a big help in
104 //getting the window centered correctly (if you use SmartPlacement)
105         SDL_WM_SetCaption("D2x", "Descent II");
106 //end addition -MM
107
108 //edited 10/05/98 by Matt Mueller - make fullscreen mode optional
109           // changed by adb on 980913: added SDL_HWPALETTE (should be option?)
110         // changed by someone on 980923 to add SDL_FULLSCREEN
111         if(!checkvidmodeok || SDL_VideoModeOK(w,h,8,sdl_video_flags)){
112           screen = SDL_SetVideoMode(w, h, 8, sdl_video_flags);
113         } else {
114           screen=NULL;
115         }
116         // end changes by someone
117         // end changes by adb
118 //end edit -MM
119         if (screen == NULL) {
120            Error("Could not set %dx%dx8 video mode\n",w,h);
121            exit(1);
122         }
123         memset( grd_curscreen, 0, sizeof(grs_screen));
124         grd_curscreen->sc_mode = mode;
125         grd_curscreen->sc_w = w;
126         grd_curscreen->sc_h = h;
127         grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
128         grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
129         grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
130         grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
131         grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
132         grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = screen->pitch;
133         grd_curscreen->sc_canvas.cv_bitmap.bm_type = BM_LINEAR;
134         grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)screen->pixels;
135         gr_set_current_canvas(NULL);
136         //gr_enable_default_palette_loading();
137
138 //added on 9/30/98 by Matt Mueller to hide the mouse if its over the game window
139         SDL_ShowCursor(0);
140 //end addition -MM
141 //--moved up--added on 9/30/98 by Matt Mueller to set the title bar.  Woohoo!
142 //--moved up--  SDL_WM_SetCaption(DESCENT_VERSION " " D1X_DATE, NULL);
143 //--moved up--end addition -MM
144
145 //      gamefont_choose_game_font(w,h);
146         return 0;
147 }
148
149
150
151 int gr_check_fullscreen(void){
152         return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
153 }
154 int gr_toggle_fullscreen(void){
155         sdl_video_flags^=SDL_FULLSCREEN;
156         grd_curscreen->sc_mode=0;//hack to get it to reset screen mode
157         return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
158 }
159
160 int gr_init(void)
161 {
162  int retcode;
163  int mode = SM(640,480);
164         // Only do this function once!
165         if (gr_installed==1)
166                 return -1;
167
168         if (SDL_Init(SDL_INIT_VIDEO) < 0)
169         {
170                 Error("SDL library video initialisation failed: %s.",SDL_GetError());
171         }
172         MALLOC( grd_curscreen,grs_screen,1 );
173         memset( grd_curscreen, 0, sizeof(grs_screen));
174
175 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
176         if (FindArg("-fullscreen"))
177              sdl_video_flags|=SDL_FULLSCREEN;
178 //end addition -MM
179         //added 05/19/99 Matt Mueller - make HW surface optional
180         if (FindArg("-hwsurface"))
181              sdl_video_flags|=SDL_HWSURFACE;
182         //end addition -MM
183         if (FindArg("-nosdlvidmodecheck"))
184                 checkvidmodeok=0;
185         
186         // Set the mode.
187         if ((retcode=gr_set_mode(mode)))
188         {
189                 return retcode;
190         }
191         grd_curscreen->sc_canvas.cv_color = 0;
192         grd_curscreen->sc_canvas.cv_drawmode = 0;
193         grd_curscreen->sc_canvas.cv_font = NULL;
194         grd_curscreen->sc_canvas.cv_font_fg_color = 0;
195         grd_curscreen->sc_canvas.cv_font_bg_color = 0;
196         gr_set_current_canvas( &grd_curscreen->sc_canvas );
197
198         gr_installed = 1;
199         // added on 980913 by adb to add cleanup
200         atexit(gr_close);
201         // end changes by adb
202
203         return 0;
204 }
205
206 void gr_close()
207 {
208         if (gr_installed==1)
209         {
210                 gr_installed = 0;
211                 d_free(grd_curscreen);
212         }
213 }
214
215 // Palette functions follow.
216
217 static int last_r=0, last_g=0, last_b=0;
218
219 void gr_palette_clear()
220 {
221  SDL_Palette *palette;
222  SDL_Color colors[256];
223  int ncolors;
224
225  palette = screen->format->palette;
226
227  if (palette == NULL) {
228     return; // Display is not palettised
229  }
230
231  ncolors = palette->ncolors;
232  memset(colors, 0, ncolors * sizeof(SDL_Color));
233
234  SDL_SetColors(screen, colors, 0, 256);
235
236  gr_palette_faded_out = 1;
237 }
238
239
240 void gr_palette_step_up( int r, int g, int b )
241 {
242  int i;
243  ubyte *p = gr_palette;
244  int temp;
245
246  SDL_Palette *palette;
247  SDL_Color colors[256];
248
249  if (gr_palette_faded_out) return;
250
251  if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
252
253  last_r = r;
254  last_g = g;
255  last_b = b;
256
257  palette = screen->format->palette;
258
259  if (palette == NULL) {
260     return; // Display is not palettised
261  }
262
263  for (i=0; i<256; i++) {
264    temp = (int)(*p++) + r + gr_palette_gamma;
265    if (temp<0) temp=0;
266    else if (temp>63) temp=63;
267    colors[i].r = temp * 4;
268    temp = (int)(*p++) + g + gr_palette_gamma;
269    if (temp<0) temp=0;
270    else if (temp>63) temp=63;
271    colors[i].g = temp * 4;
272    temp = (int)(*p++) + b + gr_palette_gamma;
273    if (temp<0) temp=0;
274    else if (temp>63) temp=63;
275    colors[i].b = temp * 4;
276  }
277
278  SDL_SetColors(screen, colors, 0, 256);
279 }
280
281 //added on 980913 by adb to fix palette problems
282 // need a min without side effects...
283 #undef min
284 static inline int min(int x, int y) { return x < y ? x : y; }
285 //end changes by adb
286
287 void gr_palette_load( ubyte *pal )      
288 {
289  int i, j;
290  SDL_Palette *palette;
291  SDL_Color colors[256];
292
293  for (i=0; i<768; i++ ) {
294      gr_current_pal[i] = pal[i];
295      if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
296  }
297
298  palette = screen->format->palette;
299
300  if (palette == NULL) {
301     return; // Display is not palettised
302  }
303
304  for (i = 0, j = 0; j < 256; j++) {
305      //changed on 980913 by adb to fix palette problems
306      colors[j].r = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
307      colors[j].g = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
308      colors[j].b = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
309      //end changes by adb
310  }
311  SDL_SetColors(screen, colors, 0, 256);
312
313  gr_palette_faded_out = 0;
314  init_computed_colors();
315 }
316
317
318
319 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
320 {
321  int i, j, k;
322  ubyte c;
323  fix fade_palette[768];
324  fix fade_palette_delta[768];
325
326  SDL_Palette *palette;
327  SDL_Color fade_colors[256];
328
329  if (gr_palette_faded_out) return 0;
330
331  palette = screen->format->palette;
332  if (palette == NULL) {
333     return -1; // Display is not palettised
334  }
335
336  if (pal==NULL) pal=gr_current_pal;
337
338  for (i=0; i<768; i++ ) {
339      gr_current_pal[i] = pal[i];
340      fade_palette[i] = i2f(pal[i]);
341      fade_palette_delta[i] = fade_palette[i] / nsteps;
342  }
343  for (j=0; j<nsteps; j++ )      {
344      for (i=0, k = 0; k<256; k++ )      {
345          fade_palette[i] -= fade_palette_delta[i];
346          if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
347             fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
348          c = f2i(fade_palette[i]);
349          if (c > 63) c = 63;
350          fade_colors[k].r = c * 4;
351          i++;
352
353          fade_palette[i] -= fade_palette_delta[i];
354          if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
355             fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
356          c = f2i(fade_palette[i]);
357          if (c > 63) c = 63;
358          fade_colors[k].g = c * 4;
359          i++;
360
361          fade_palette[i] -= fade_palette_delta[i];
362          if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
363             fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
364          c = f2i(fade_palette[i]);
365          if (c > 63) c = 63;
366          fade_colors[k].b = c * 4;
367          i++;
368      }
369
370   SDL_SetColors(screen, fade_colors, 0, 256);
371  }
372
373  gr_palette_faded_out = 1;
374  return 0;
375 }
376
377
378
379 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
380 {
381  int i, j, k, ncolors;
382  ubyte c;
383  fix fade_palette[768];
384  fix fade_palette_delta[768];
385
386  SDL_Palette *palette;
387  SDL_Color fade_colors[256];
388
389  if (!gr_palette_faded_out) return 0;
390
391  palette = screen->format->palette;
392
393  if (palette == NULL) {
394     return -1; // Display is not palettised
395  }
396
397  ncolors = palette->ncolors;
398
399  for (i=0; i<768; i++ ) {
400      gr_current_pal[i] = pal[i];
401      fade_palette[i] = 0;
402      fade_palette_delta[i] = i2f(pal[i]) / nsteps;
403  }
404
405  for (j=0; j<nsteps; j++ )      {
406      for (i=0, k = 0; k<256; k++ )      {
407          fade_palette[i] += fade_palette_delta[i];
408          if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
409             fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
410          c = f2i(fade_palette[i]);
411          if (c > 63) c = 63;
412          fade_colors[k].r = c * 4;
413          i++;
414
415          fade_palette[i] += fade_palette_delta[i];
416          if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
417             fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
418          c = f2i(fade_palette[i]);
419          if (c > 63) c = 63;
420          fade_colors[k].g = c * 4;
421          i++;
422
423          fade_palette[i] += fade_palette_delta[i];
424          if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
425             fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
426          c = f2i(fade_palette[i]);
427          if (c > 63) c = 63;
428          fade_colors[k].b = c * 4;
429          i++;
430      }
431
432   SDL_SetColors(screen, fade_colors, 0, 256);
433  }
434  //added on 980913 by adb to fix palette problems
435  gr_palette_load(pal);
436  //end changes by adb
437
438  gr_palette_faded_out = 0;
439  return 0;
440 }
441
442
443
444 void gr_palette_read(ubyte * pal)
445 {
446  SDL_Palette *palette;
447  int i, j;
448
449  palette = screen->format->palette;
450
451  if (palette == NULL) {
452     return; // Display is not palettised
453  }
454
455  for (i = 0, j=0; i < 256; i++) {
456      pal[j++] = palette->colors[i].r / 4;
457      pal[j++] = palette->colors[i].g / 4;
458      pal[j++] = palette->colors[i].b / 4;
459  }
460 }