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