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