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