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