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