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