]> icculus.org git repositories - btb/d2x.git/blob - arch/sdl/gr.c
added pcx_get_dimensions(), other stuff from d2src
[btb/d2x.git] / arch / sdl / gr.c
1 /* $Id: gr.c,v 1.7 2002-08-04 23:18:11 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/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 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 //      gr_testunlock();
75         //end addition -MM
76  SDL_UpdateRect(screen,0,0,0,0);
77 }
78
79 extern int VGA_current_mode; // DPH: kludge - remove at all costs
80
81 int gr_set_mode(u_int32_t mode)
82 {
83         int w,h;
84
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(DESCENT_VERSION, "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 int gr_check_fullscreen(void){
150         return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
151 }
152
153 int gr_toggle_fullscreen(void){
154         sdl_video_flags^=SDL_FULLSCREEN;
155         SDL_WM_ToggleFullScreen(screen);
156         return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
157 }
158
159 int gr_init(void)
160 {
161         // Only do this function once!
162         if (gr_installed==1)
163                 return -1;
164
165         if (SDL_Init(SDL_INIT_VIDEO) < 0)
166         {
167                 Error("SDL library video initialisation failed: %s.",SDL_GetError());
168         }
169         MALLOC( grd_curscreen,grs_screen,1 );
170         memset( grd_curscreen, 0, sizeof(grs_screen));
171
172 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
173         if (FindArg("-fullscreen"))
174              sdl_video_flags|=SDL_FULLSCREEN;
175 //end addition -MM
176         //added 05/19/99 Matt Mueller - make HW surface optional
177         if (FindArg("-hwsurface"))
178              sdl_video_flags|=SDL_HWSURFACE;
179         //end addition -MM
180         if (FindArg("-nosdlvidmodecheck"))
181                 checkvidmodeok=0;
182         
183         grd_curscreen->sc_canvas.cv_color = 0;
184         grd_curscreen->sc_canvas.cv_drawmode = 0;
185         grd_curscreen->sc_canvas.cv_font = NULL;
186         grd_curscreen->sc_canvas.cv_font_fg_color = 0;
187         grd_curscreen->sc_canvas.cv_font_bg_color = 0;
188         gr_set_current_canvas( &grd_curscreen->sc_canvas );
189
190         gr_installed = 1;
191         // added on 980913 by adb to add cleanup
192         atexit(gr_close);
193         // end changes by adb
194
195         return 0;
196 }
197
198 void gr_close()
199 {
200         if (gr_installed==1)
201         {
202                 gr_installed = 0;
203                 d_free(grd_curscreen);
204         }
205 }
206
207 // Palette functions follow.
208
209 static int last_r=0, last_g=0, last_b=0;
210
211 void gr_palette_clear()
212 {
213  SDL_Palette *palette;
214  SDL_Color colors[256];
215  int ncolors;
216
217  palette = screen->format->palette;
218
219  if (palette == NULL) {
220     return; // Display is not palettised
221  }
222
223  ncolors = palette->ncolors;
224  memset(colors, 0, ncolors * sizeof(SDL_Color));
225
226  SDL_SetColors(screen, colors, 0, 256);
227
228  gr_palette_faded_out = 1;
229 }
230
231
232 void gr_palette_step_up( int r, int g, int b )
233 {
234  int i;
235  ubyte *p = gr_palette;
236  int temp;
237
238  SDL_Palette *palette;
239  SDL_Color colors[256];
240
241  if (gr_palette_faded_out) return;
242
243  if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
244
245  last_r = r;
246  last_g = g;
247  last_b = b;
248
249  palette = screen->format->palette;
250
251  if (palette == NULL) {
252     return; // Display is not palettised
253  }
254
255  for (i=0; i<256; i++) {
256    temp = (int)(*p++) + r + gr_palette_gamma;
257    if (temp<0) temp=0;
258    else if (temp>63) temp=63;
259    colors[i].r = temp * 4;
260    temp = (int)(*p++) + g + gr_palette_gamma;
261    if (temp<0) temp=0;
262    else if (temp>63) temp=63;
263    colors[i].g = temp * 4;
264    temp = (int)(*p++) + b + gr_palette_gamma;
265    if (temp<0) temp=0;
266    else if (temp>63) temp=63;
267    colors[i].b = temp * 4;
268  }
269
270  SDL_SetColors(screen, colors, 0, 256);
271 }
272
273 //added on 980913 by adb to fix palette problems
274 // need a min without side effects...
275 #undef min
276 static inline int min(int x, int y) { return x < y ? x : y; }
277 //end changes by adb
278
279 void gr_palette_load( ubyte *pal )      
280 {
281  int i, j;
282  SDL_Palette *palette;
283  SDL_Color colors[256];
284
285  for (i=0; i<768; i++ ) {
286      gr_current_pal[i] = pal[i];
287      if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
288  }
289
290  palette = screen->format->palette;
291
292  if (palette == NULL) {
293     return; // Display is not palettised
294  }
295
296  for (i = 0, j = 0; j < 256; j++) {
297      //changed on 980913 by adb to fix palette problems
298      colors[j].r = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
299      colors[j].g = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
300      colors[j].b = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
301      //end changes by adb
302  }
303  SDL_SetColors(screen, colors, 0, 256);
304
305  gr_palette_faded_out = 0;
306  init_computed_colors();
307 }
308
309
310
311 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
312 {
313  int i, j, k;
314  ubyte c;
315  fix fade_palette[768];
316  fix fade_palette_delta[768];
317
318  SDL_Palette *palette;
319  SDL_Color fade_colors[256];
320
321  if (gr_palette_faded_out) return 0;
322
323  palette = screen->format->palette;
324  if (palette == NULL) {
325     return -1; // Display is not palettised
326  }
327
328  if (pal==NULL) pal=gr_current_pal;
329
330  for (i=0; i<768; i++ ) {
331      gr_current_pal[i] = pal[i];
332      fade_palette[i] = i2f(pal[i]);
333      fade_palette_delta[i] = fade_palette[i] / nsteps;
334  }
335  for (j=0; j<nsteps; j++ )      {
336      for (i=0, k = 0; k<256; k++ )      {
337          fade_palette[i] -= fade_palette_delta[i];
338          if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
339             fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
340          c = f2i(fade_palette[i]);
341          if (c > 63) c = 63;
342          fade_colors[k].r = c * 4;
343          i++;
344
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].g = 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].b = c * 4;
359          i++;
360      }
361
362   SDL_SetColors(screen, fade_colors, 0, 256);
363  }
364
365  gr_palette_faded_out = 1;
366  return 0;
367 }
368
369
370
371 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
372 {
373  int i, j, k, ncolors;
374  ubyte c;
375  fix fade_palette[768];
376  fix fade_palette_delta[768];
377
378  SDL_Palette *palette;
379  SDL_Color fade_colors[256];
380
381  if (!gr_palette_faded_out) return 0;
382
383  palette = screen->format->palette;
384
385  if (palette == NULL) {
386     return -1; // Display is not palettised
387  }
388
389  ncolors = palette->ncolors;
390
391  for (i=0; i<768; i++ ) {
392      gr_current_pal[i] = pal[i];
393      fade_palette[i] = 0;
394      fade_palette_delta[i] = i2f(pal[i]) / nsteps;
395  }
396
397  for (j=0; j<nsteps; j++ )      {
398      for (i=0, k = 0; k<256; k++ )      {
399          fade_palette[i] += fade_palette_delta[i];
400          if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
401             fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
402          c = f2i(fade_palette[i]);
403          if (c > 63) c = 63;
404          fade_colors[k].r = c * 4;
405          i++;
406
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].g = 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].b = c * 4;
421          i++;
422      }
423
424   SDL_SetColors(screen, fade_colors, 0, 256);
425  }
426  //added on 980913 by adb to fix palette problems
427  gr_palette_load(pal);
428  //end changes by adb
429
430  gr_palette_faded_out = 0;
431  return 0;
432 }
433
434
435
436 void gr_palette_read(ubyte * pal)
437 {
438  SDL_Palette *palette;
439  int i, j;
440
441  palette = screen->format->palette;
442
443  if (palette == NULL) {
444     return; // Display is not palettised
445  }
446
447  for (i = 0, j=0; i < 256; i++) {
448      pal[j++] = palette->colors[i].r / 4;
449      pal[j++] = palette->colors[i].g / 4;
450      pal[j++] = palette->colors[i].b / 4;
451  }
452 }