]> icculus.org git repositories - taylor/freespace2.git/blob - src/graphics/2d.cpp
ryan's struct patch for gcc 2.95
[taylor/freespace2.git] / src / graphics / 2d.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell 
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/Graphics/2d.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Main file for 2d primitives.
16  *
17  * $Log$
18  * Revision 1.10  2002/06/17 06:33:09  relnev
19  * ryan's struct patch for gcc 2.95
20  *
21  * Revision 1.9  2002/06/09 04:41:17  relnev
22  * added copyright header
23  *
24  * Revision 1.8  2002/06/05 04:03:32  relnev
25  * finished cfilesystem.
26  *
27  * removed some old code.
28  *
29  * fixed mouse save off-by-one.
30  *
31  * sound cleanups.
32  *
33  * Revision 1.7  2002/05/30 21:44:48  relnev
34  * implemented some missing texture stuff.
35  *
36  * enable bitmap polys for opengl.
37  *
38  * work around greenness in bitmaps.
39  *
40  * Revision 1.6  2002/05/28 21:36:10  relnev
41  * some more timer junk.
42  *
43  * tried to fix software mode.
44  *
45  * Revision 1.5  2002/05/28 04:18:08  theoddone33
46  * Fix some stuff and add -DFS2_DEMO
47  *
48  * Revision 1.4  2002/05/27 22:43:02  theoddone33
49  * Fix more glide symbols
50  *
51  * Revision 1.3  2002/05/27 22:39:21  theoddone33
52  * Remove glide symbols
53  *
54  * Revision 1.2  2002/05/07 03:16:45  theoddone33
55  * The Great Newline Fix
56  *
57  * Revision 1.1.1.1  2002/05/03 03:28:09  root
58  * Initial import.
59  *
60  * 
61  * 25    8/20/99 2:09p Dave
62  * PXO banner cycling.
63  * 
64  * 24    8/16/99 9:45a Jefff
65  * changes to cursor management to allow a 2nd temporary cursor
66  * 
67  * 23    7/27/99 3:52p Dave
68  * Make star drawing a bit more robust to help lame D3D cards.
69  * 
70  * 22    7/18/99 12:32p Dave
71  * Randomly oriented shockwaves.
72  * 
73  * 21    7/15/99 3:07p Dave
74  * 32 bit detection support. Mouse coord commandline.
75  * 
76  * 20    7/14/99 9:42a Dave
77  * Put in clear_color debug function. Put in base for 3dnow stuff / P3
78  * stuff
79  * 
80  * 19    7/13/99 1:15p Dave
81  * 32 bit support. Whee!
82  * 
83  * 18    7/12/99 11:42a Jefff
84  * Made rectangle drawing smarter in D3D. Made plines draw properly on Ati
85  * Rage Pro.
86  * 
87  * 17    7/09/99 9:51a Dave
88  * Added thick polyline code.
89  * 
90  * 16    7/02/99 3:05p Anoop
91  * Oops. Fixed g3_draw_2d_poly() so that it properly handles poly bitmap
92  * and LFB bitmap calls.
93  * 
94  * 15    6/29/99 10:35a Dave
95  * Interface polygon bitmaps! Whee!
96  * 
97  * 14    4/09/99 2:21p Dave
98  * Multiplayer beta stuff. CD checking.
99  * 
100  * 13    2/03/99 11:44a Dave
101  * Fixed d3d transparent textures.
102  * 
103  * 12    1/29/99 12:47a Dave
104  * Put in sounds for beam weapon. A bunch of interface screens (tech
105  * database stuff).
106  * 
107  * 11    1/15/99 11:29a Neilk
108  * Fixed D3D screen/texture pixel formatting problem. 
109  * 
110  * 10    1/08/99 2:08p Dave
111  * Fixed software rendering for pofview. Super early support for AWACS and
112  * beam weapons.
113  * 
114  * 9     1/06/99 2:24p Dave
115  * Stubs and release build fixes.
116  * 
117  * 8     12/18/98 1:49a Dave
118  * Fixed Fred initialization problem resulting from hi-res mode changes.
119  * 
120  * 7     12/18/98 1:13a Dave
121  * Rough 1024x768 support for Direct3D. Proper detection and usage through
122  * the launcher.
123  * 
124  * 6     12/01/98 10:32a Johnson
125  * Fixed direct3d font problems. Fixed sun bitmap problem. Fixed direct3d
126  * starfield problem.
127  * 
128  * 5     11/30/98 5:31p Dave
129  * Fixed up Fred support for software mode.
130  * 
131  * 4     11/30/98 1:07p Dave
132  * 16 bit conversion, first run.
133  * 
134  * 3     11/11/98 5:37p Dave
135  * Checkin for multiplayer testing.
136  * 
137  * 2     10/07/98 10:52a Dave
138  * Initial checkin.
139  * 
140  * 1     10/07/98 10:48a Dave
141  * 
142  * 122   6/13/98 3:18p Hoffoss
143  * NOX()ed out a bunch of strings that shouldn't be translated.
144  * 
145  * 121   5/23/98 11:26a Hoffoss
146  * Fixed bug where optimized code used EDX and my code trashed it.
147  * 
148  * 120   5/22/98 11:09p John
149  * put in code to hopefull not crash cyrix cpus
150  * 
151  * 119   5/20/98 9:45p John
152  * added code so the places in code that change half the palette don't
153  * have to clear the screen.
154  * 
155  * 118   5/20/98 12:59p Hoffoss
156  * Changed temporary crossfade code to just pop the image again.
157  * 
158  * 117   5/16/98 1:18p John
159  * Made softtware DirectDraw reset palette after Alt+TAB.
160  * 
161  * 116   5/14/98 5:42p John
162  * Revamped the whole window position/mouse code for the graphics windows.
163  * 
164  * 115   5/08/98 10:49a John
165  * Made 'gr d' go into direct3d mode.
166  * 
167  * 114   5/06/98 11:21p John
168  * Fixed a bitmap bug with Direct3D.  Started adding new caching code into
169  * D3D.
170  * 
171  * 113   4/21/98 9:28a John
172  * Added stub for cross-fade.
173  * 
174  * 112   4/10/98 5:20p John
175  * Changed RGB in lighting structure to be ubytes.  Removed old
176  * not-necessary 24 bpp software stuff.
177  * 
178  * 111   4/06/98 12:55p John
179  * Upped the gamma for Fred.
180  * 
181  * 110   3/25/98 8:07p John
182  * Restructured software rendering into two modules; One for windowed
183  * debug mode and one for DirectX fullscreen.   
184  * 
185  * 109   3/24/98 3:58p John
186  * Put in (hopefully) final gamma setting code.
187  * 
188  * 108   3/10/98 4:18p John
189  * Cleaned up graphics lib.  Took out most unused gr functions.   Made D3D
190  * & Glide have popups and print screen.  Took out all >8bpp software
191  * support.  Made Fred zbuffer.  Made zbuffer allocate dynamically to
192  * support Fred.  Made zbuffering key off of functions rather than one
193  * global variable.
194  * 
195  * 107   2/25/98 2:37p John
196  * Made afterburner shake 2x less noticable.  Made 'gr a' leave Glide mode
197  * properly.  Made model_caching never work in hardware mode.
198  * 
199  * 106   2/23/98 2:27p John
200  * Made int3,asserts, etc pass video through.
201  * 
202  * 105   2/20/98 3:13p John
203  * Made popup save code print an error an exit out if 3d accelerated.
204  * 
205  * 104   2/19/98 6:13p John
206  * Made Glide do texturing & zbuffering.
207  * 
208  * 103   1/26/98 5:12p John
209  * Added in code for Pentium Pro specific optimizations. Speed up
210  * zbuffered correct tmapper about 35%.   Speed up non-zbuffered scalers
211  * by about 25%.
212  * 
213  * 102   1/14/98 11:39a Dave
214  * Polished up a bunch of popup support items.
215  * 
216  * 101   1/10/98 1:14p John
217  * Added explanation to debug console commands
218  * 
219  * 100   12/21/97 4:33p John
220  * Made debug console functions a class that registers itself
221  * automatically, so you don't need to add the function to
222  * debugfunctions.cpp.  
223  * 
224  * 99    12/04/97 12:09p John
225  * Made glows use scaler instead of tmapper so they don't rotate.  Had to
226  * add a zbuffered scaler.
227  * 
228  * 98    12/03/97 10:47a John
229  * added functions to save/restore entire screens.
230  * 
231  * 97    12/02/97 3:59p John
232  * Added first rev of thruster glow, along with variable levels of
233  * translucency, which retquired some restructing of palman.
234  * 
235  * 96    11/30/97 12:18p John
236  * added more 24 & 32-bpp primitives
237  * 
238  * 95    11/24/97 11:20a John
239  * added new mode
240  * 
241  * 94    11/14/97 3:54p John
242  * Added triple buffering.
243  * 
244  * 93    11/14/97 12:30p John
245  * Fixed some DirectX bugs.  Moved the 8-16 xlat tables into Graphics
246  * libs.  Made 16-bpp DirectX modes know what bitmap format they're in.
247  * 
248  * 92    10/09/97 5:23p John
249  * Added support for more 16-bpp functions
250  * 
251  * 91    9/20/97 8:16a John
252  * Made .clr files go into the Cache directory. Replaced cfopen(name,NULL)
253  * to delete a file with cf_delete.
254  * 
255  * 90    9/09/97 11:01a Sandeep
256  * fixed warning level 4 bugs
257  * 
258  * 89    9/07/97 10:01p Lawrance
259  * add in support for animating mouse pointer
260  * 
261  * 88    9/03/97 4:32p John
262  * changed bmpman to only accept ani and pcx's.  made passing .pcx or .ani
263  * to bm_load functions not needed.   Made bmpman keep track of palettes
264  * for bitmaps not mapped into game palettes.
265  * 
266  * 87    8/20/97 4:19p John
267  * added delay to hopefully fix mike's problems when getting int3's in
268  * fullscreen.
269  * 
270  * 86    7/16/97 3:07p John
271  * 
272  * 85    6/18/97 12:07p John
273  * fixed some color bugs
274  * 
275  * 84    6/17/97 7:04p John
276  * added d3d support for gradients.
277  * fixed some color bugs by adding screen signatures instead of watching
278  * flags and palette changes.
279  * 
280  * 83    6/17/97 12:03p John
281  * Moved color/alphacolor functions into their own module.  Made all color
282  * functions be part of the low-level graphics drivers, not just the
283  * grsoft.
284  * 
285  * 82    6/12/97 5:04p John
286  * Initial rev of Glide support
287  * 
288  * 81    6/11/97 5:49p John
289  * Changed palette code to only recalculate alphacolors when needed, not
290  * when palette changes.
291  * 
292  * 80    6/11/97 1:12p John
293  * Started fixing all the text colors in the game.
294  * 
295  * 79    6/06/97 5:03p John
296  * fixed bug withalpha colors failing after gr_init
297  * 
298  * 78    6/06/97 4:53p John
299  * made gr_init not bash current color
300  * 
301  * 77    6/05/97 4:53p John
302  * First rev of new antialiased font stuff.
303  * 
304  * 76    5/20/97 10:36a John
305  * Fixed problem with user bitmaps and direct3d caching.
306  * 
307  * 75    5/16/97 9:11a John
308  * fixed bug that made Ctrl+Break in fullscreen hang
309  * 
310  * 74    5/14/97 4:38p John
311  * Fixed print_screen bug.
312  * 
313  * 73    5/14/97 2:10p John
314  * added rudimentary support for windowed direct3d.
315  * 
316  * 72    5/14/97 10:53a John
317  * fixed some discrepencies between d3d and software palette setting.
318  * 
319  * 71    5/13/97 4:39p John
320  * Added console function to set graphics modes.
321  * 
322  * 70    5/13/97 12:39p John
323  * Got fullscreen mode working.
324  * 
325  * 69    5/12/97 12:27p John
326  * Restructured Graphics Library to add support for multiple renderers.
327  * 
328  * 68    5/07/97 2:59p John
329  * Initial rev of D3D texturing.
330  * 
331  * 67    5/01/97 3:32p John
332  * oops... forced 2d to always be in 8bpp,
333  * 
334  * 66    5/01/97 3:23p John
335  * first hooks for some direct 3d setup stuff.
336  * 
337  * 65    4/28/97 5:33p John
338  * fixed a newly introduced bug with fullscreen toggle.
339  * 
340  * 64    4/28/97 4:46p John
341  * 
342  * 63    4/22/97 12:20p John
343  * fixed more resource leaks
344  * 
345  * 62    4/22/97 10:33a John
346  * fixed the 2d resource leaks that Alan found.
347  * 
348  * 61    3/31/97 9:45a Allender
349  * start of new font stuff
350  * 
351  * 60    3/26/97 10:52a Lawrance
352  * mouse always on in menus, disappears in gameplay after 1 second
353  * 
354  * 59    3/14/97 3:55p John
355  * Made tiled tmapper not always be zbuffered.
356  * 
357  * 58    3/13/97 9:09a Allender
358  * variable to allow trashing of the palette.  kind of temporary code
359  * right now...checking needed for undefined references
360  * 
361  * 57    3/12/97 2:51p John
362  * Added some test code for tmapper.  
363  * 
364  * 56    3/12/97 9:25a John
365  * fixed a bug with zbuffering.  Reenabled it by default.
366  * 
367  * 55    3/11/97 5:13p John
368  * made top up bitmaps work.
369  * 
370  * 54    2/26/97 11:59a Allender
371  * comment out warning about display settings not being optimal
372  * 
373  * 53    1/09/97 2:16p John
374  * took out the "Bing!" message
375  * 
376  * 52    1/09/97 11:35a John
377  * Added some 2d functions to get/put screen images.
378  * 
379  * 51    1/07/97 2:01p John
380  * Fairly fast zbuffering for object sorting.
381  * 
382  * 50    1/06/97 2:44p John
383  * Added in slow (but correct) zbuffering
384  * 
385  * 49    12/11/96 12:41p John
386  * Added new code to draw 3d laser using 2d ellipses.
387  * 
388  * 48    12/03/96 5:42p John
389  * took out debug code.
390  * 
391  * 47    12/03/96 5:42p John
392  * made gr_bitmap clip properly.
393  * 
394  * 46    12/03/96 5:06p John
395  * Added code to draw our own cursor.
396  * 
397  * 45    11/26/96 6:50p John
398  * Added some more hicolor primitives.  Made windowed mode run as current
399  * bpp, if bpp is 8,16,or 32.
400  * 
401  * 44    11/21/96 11:23a John
402  * fixed bug with previous.
403  * 
404  * 43    11/21/96 11:21a John
405  * Made gr_get_string_size handle multi line text.
406  * Took out gr_get_multiline_string_size
407  * 
408  * 42    11/21/96 11:06a John
409  * fixed warnings.
410  * 
411  * 41    11/20/96 10:01a Hoffoss
412  * A few minor improvements.
413  * 
414  * 40    11/19/96 2:47p Allender
415  * add support for xparent bitmaps in 32bpp
416  * 
417  * 39    11/18/96 4:46p Allender
418  * changed warning message about bit depth to appear when requested
419  * doesn't equal actual
420  * 
421  * 38    11/18/96 4:35p Allender
422  * new 16bpp gradient functions
423  * 
424  * 37    11/18/96 3:09p John
425  * made gr_clear always clear to black.
426  * 
427  * 36    11/18/96 1:48p Allender
428  * added 16 bit version of shader
429  * 
430  * 35    11/18/96 12:36p John
431  * Added code to dump screen to a PCX file.
432  * 
433  * 34    11/18/96 11:40a John
434  * Added faster gr_set_color method.
435  * 
436  * 33    11/15/96 11:26a John
437  * Added code to clip text to clipping region.
438  * 
439  * 32    11/15/96 11:26a Allender
440  * fixed up 16 bbp version of gr_bitmap
441  * 
442  * 31    11/14/96 9:11a John
443  * Fixed bug that didn't offset the gr_string text by the current clip
444  * region's offset.
445  * 
446  * 30    11/13/96 6:47p John
447  * Added gr_flip function.
448  * 
449  * 29    11/13/96 10:10a John
450  * Increases MAX_WIDTH & HEIGHT for Jasen's massive 1600x1200 display.
451  * 
452  * 28    11/07/96 6:19p John
453  * Added a bunch of 16bpp primitives so the game sort of runs in 16bpp
454  * mode.
455  * 
456  * 27    10/30/96 10:36a Lawrance
457  * added gr_diamond function
458  * 
459  * 26    10/26/96 1:40p John
460  * Added some now primitives to the 2d library and
461  * cleaned up some old ones.
462  *
463  * $NoKeywords: $
464  */
465
466 #ifndef PLAT_UNIX
467 #include <windows.h>
468 #include <windowsx.h>
469 #endif
470
471 #include "osapi.h"
472 #include "2d.h"
473 #include "3d.h"
474 #include "bmpman.h"
475 #include "palman.h"
476 #include "font.h"
477 #include "grinternal.h"
478 #include "systemvars.h"
479 #include "cmdline.h"
480
481 // 3dnow stuff
482 // #include "amd3d.h"
483
484 // Includes for different rendering systems
485 #include "grsoft.h"
486 #include "grd3d.h"
487 #ifndef PLAT_UNIX
488 #include "grglide.h"
489 #endif
490 #include "gropengl.h"
491 #include "grdirectdraw.h"
492
493 screen gr_screen;
494
495 color_gun Gr_red, Gr_green, Gr_blue, Gr_alpha;
496 color_gun Gr_t_red, Gr_t_green, Gr_t_blue, Gr_t_alpha;
497 color_gun Gr_ta_red, Gr_ta_green, Gr_ta_blue, Gr_ta_alpha;
498 color_gun *Gr_current_red, *Gr_current_green, *Gr_current_blue, *Gr_current_alpha;
499
500
501 ubyte Gr_original_palette[768];         // The palette 
502 ubyte Gr_current_palette[768];
503 char Gr_current_palette_name[128] = NOX("none");
504
505 // cursor stuff
506 int Gr_cursor = -1;
507 int Web_cursor_bitmap = -1;
508
509 int Gr_inited = 0;
510
511 // cpu types
512 int Gr_cpu = 0; 
513 int Gr_amd3d = 0;
514 int Gr_katmai = 0;
515 int Gr_mmx = 0;
516
517 uint Gr_signature = 0;
518
519 float Gr_gamma = 1.8f;
520 int Gr_gamma_int = 180;
521 int Gr_gamma_lookup[256];
522
523 void gr_close()
524 {
525         if ( !Gr_inited )       return;
526
527         palette_flush();
528
529         switch( gr_screen.mode )        {
530         case GR_SOFTWARE:               
531                 gr_soft_cleanup();
532                 break;
533 #ifndef PLAT_UNIX               
534         case GR_DIRECTDRAW:
535                 Int3();
536                 gr_directdraw_cleanup();
537                 break;
538         case GR_DIRECT3D:               
539                 gr_d3d_cleanup();
540                 break;
541         case GR_GLIDE:
542                 gr_glide_cleanup();
543                 break;
544 #endif          
545         case GR_OPENGL:
546                 gr_opengl_cleanup();
547                 break;
548         default:
549                 Int3();         // Invalid graphics mode
550         }
551
552         gr_font_close();
553
554         Gr_inited = 0;
555 }
556
557 //XSTR:OFF
558 DCF(gr,"Changes graphics mode")
559 {
560 #ifndef HARDWARE_ONLY
561         int mode = gr_screen.mode;
562
563         if ( Dc_command )       {
564                 dc_get_arg(ARG_STRING);
565                 
566                 if ( !strcmp( Dc_arg, "a"))     {
567                         Int3();
568                         mode = GR_SOFTWARE;
569                 } else if ( !strcmp( Dc_arg, "b"))      {
570                         Int3();
571                         mode = GR_DIRECTDRAW;
572                 } else if ( !strcmp( Dc_arg, "d"))      {
573                         mode = GR_DIRECT3D;
574                 } else if ( !strcmp( Dc_arg, "g"))      {
575 #ifndef PLAT_UNIX
576                         mode = GR_GLIDE;
577 #endif
578                 } else if ( !strcmp( Dc_arg, "o"))      {
579                         mode = GR_OPENGL;
580                 } else {
581                         // print usage, not stats
582                         Dc_help = 1;
583                 }
584
585                 /*
586                 if ( mode != gr_screen.mode )   {
587                         dc_printf( "Setting new video mode...\n" );
588                         int errcode = gr_init( gr_screen.max_w, gr_screen.max_h, mode );
589                         if (errcode)    {
590                                 dc_printf( "Error %d.  Graphics unchanged.\n", errcode );
591                         }
592                 }
593                 */
594         }
595
596         if ( Dc_help )  {
597                 dc_printf( "Usage: gr mode\n" );
598                 dc_printf( "The options can be:\n" );
599                 dc_printf( "Macros:  A=software win32 window (obsolete)\n" );
600                 dc_printf( "         B=software directdraw fullscreen (obsolete)\n" );
601                 dc_printf( "         D=Direct3d\n" );
602                 dc_printf( "         G=Glide\n" );
603                 dc_printf( "         O=OpenGl (obsolete)\n" );
604                 Dc_status = 0;  // don't print status if help is printed.  Too messy.
605         }
606
607         if ( Dc_status )        {
608                 switch( gr_screen.mode )        {
609                 case GR_SOFTWARE:
610                         Int3();
611                         dc_printf( "Win32 software windowed\n" );
612                         break;
613                 case GR_DIRECTDRAW:
614                         Int3();
615                         dc_printf( "DirectDraw software windowed\n" );
616                         break;
617                 case GR_DIRECT3D:
618                         dc_printf( "Direct3D\n" );
619                         break;
620                 case GR_GLIDE:
621 #ifndef PLAT_UNIX
622                         dc_printf( "3Dfx Glide\n" );
623 #endif
624                         break;
625                 case GR_OPENGL:
626                         dc_printf( "OpenGl\n" );
627                         break;
628                 default:
629                         Int3();         // Invalid graphics mode
630                 }
631         }
632 #endif
633 }
634 //XSTR:ON
635
636 // set screen clear color
637 DCF(clear_color, "set clear color r, g, b")
638 {
639         int r, g, b;
640
641         dc_get_arg(ARG_INT);
642         r = Dc_arg_int;
643         dc_get_arg(ARG_INT);
644         g = Dc_arg_int;
645         dc_get_arg(ARG_INT);
646         b = Dc_arg_int;
647
648         // set the color
649         gr_set_clear_color(r, g, b);
650 }
651
652 void gr_set_palette_internal( char *name, ubyte * palette, int restrict_font_to_128 )
653 {
654         if ( palette == NULL )  {
655                 // Create a default palette
656                 int r,g,b,i;
657                 i = 0;
658                                 
659                 for (r=0; r<6; r++ )    
660                         for (g=0; g<6; g++ )    
661                                 for (b=0; b<6; b++ )            {
662                                         Gr_current_palette[i*3+0] = (unsigned char)(r*51);
663                                         Gr_current_palette[i*3+1] = (unsigned char)(g*51);
664                                         Gr_current_palette[i*3+2] = (unsigned char)(b*51);
665                                         i++;
666                                 }
667                 for ( i=216;i<256; i++ )        {
668                         Gr_current_palette[i*3+0] = (unsigned char)((i-216)*6);
669                         Gr_current_palette[i*3+1] = (unsigned char)((i-216)*6);
670                         Gr_current_palette[i*3+2] = (unsigned char)((i-216)*6);
671                 }
672                 memmove( Gr_original_palette, Gr_current_palette, 768 );
673         } else {
674                 memmove( Gr_original_palette, palette, 768 );
675                 memmove( Gr_current_palette, palette, 768 );
676         }
677
678 //      mprintf(("Setting new palette\n" ));
679
680         if ( Gr_inited )        {
681                 if (gr_screen.gf_set_palette)   {
682                         (*gr_screen.gf_set_palette)(Gr_current_palette, restrict_font_to_128 );
683
684                         // Since the palette set code might shuffle the palette,
685                         // reload it into the source palette
686                         if ( palette )
687                                 memmove( palette, Gr_current_palette, 768 );
688                 }
689
690                 // Update Palette Manager tables
691                 memmove( gr_palette, Gr_current_palette, 768 );
692                 palette_update(name, restrict_font_to_128);
693         }
694 }
695
696
697 void gr_set_palette( char *name, ubyte * palette, int restrict_font_to_128 )
698 {
699         char *p;
700         palette_flush();
701         strcpy( Gr_current_palette_name, name );
702         p = strchr( Gr_current_palette_name, '.' );
703         if ( p ) *p = 0;
704         gr_screen.signature = Gr_signature++;
705         gr_set_palette_internal( name, palette, restrict_font_to_128 );
706 }
707
708
709 //void gr_test();
710
711 #define CPUID _asm _emit 0fh _asm _emit 0a2h
712
713 // -----------------------------------------------------------------------
714 // Returns cpu type.
715 void gr_detect_cpu(int *cpu, int *mmx, int *amd3d, int *katmai )
716 {
717 #ifdef PLAT_UNIX
718         STUB_FUNCTION;
719 #else
720         DWORD RegEDX;
721         DWORD RegEAX;
722
723         // Set defaults
724         *cpu = 0;
725         *mmx = 0;
726         *amd3d = 0;
727         *katmai = 0;
728
729         char cpu_vender[16];
730         memset( cpu_vender, 0, sizeof(cpu_vender) );
731                 
732   _asm {
733
734                 // Check for prescence of 
735                 push    eax
736                 push    ebx
737                 push    ecx
738                 push    edx
739
740                 pushfd                  // get extended flags
741                 pop     eax
742                 mov     ebx, eax                // save current flags
743                 xor     eax, 200000h    // toggle bit 21
744                 push    eax                     // push new flags on stack
745                 popfd                                   // flags updated now in flags
746                 pushfd                  // get extended flags
747                 pop     eax             // store extended flags in eax
748                 xor     eax, ebx        // if bit 21 r/w then eax <> 0
749                 je              no_cpuid                
750
751                 mov     eax, 0          // setup CPUID to return vender id
752       CPUID           // code bytes = 0fh,  0a2h
753                 mov     DWORD PTR cpu_vender[0], ebx
754                 mov     DWORD PTR cpu_vender[4], edx
755                 mov     DWORD PTR cpu_vender[8], ecx
756                 
757       mov eax, 1      // setup CPUID to return features
758
759       CPUID           // code bytes = 0fh,  0a2h
760
761                 mov RegEAX, eax // family, etc returned in eax
762       mov RegEDX, edx   // features returned in edx
763                 jmp     done_checking_cpuid
764
765
766 no_cpuid:
767                 mov RegEAX, 4<<8        // family, etc returned in eax
768       mov RegEDX, 0             // features returned in edx
769
770 done_checking_cpuid:                                                            
771                 pop     edx
772                 pop     ecx
773                 pop     ebx
774                 pop     eax
775
776         }
777         
778
779
780         //RegEAX        .  Bits 11:8 is family
781         *cpu = (RegEAX >>8) & 0xF;
782
783         if ( *cpu < 5 ) {
784                 *cpu = 4;                                                               // processor does not support CPUID
785                 *mmx = 0;
786         }
787
788         //RegEAX        .  Bits 11:8 is family
789         *cpu = (RegEAX >>8) & 0xF;
790
791         // Check for MMX
792         BOOL retval = TRUE;
793    if (RegEDX & 0x800000)               // bit 23 is set for MMX technology
794    {
795
796            __try { _asm emms }          // try executing an MMX instruction "emms"
797
798            __except(EXCEPTION_EXECUTE_HANDLER) { retval = FALSE; }
799
800    } else {
801                 retval = FALSE;
802         }
803         if ( retval )   {
804                 *mmx = 1;                       // processor supports CPUID but does not support MMX technology
805         }
806
807         // Check for Katmai
808    if (RegEDX & (1<<25) )               // bit 25 is set for Katmai technology
809    {
810                 *katmai = 1;
811    }
812
813         // Check for Amd 3dnow
814         /*
815         if ( !stricmp( cpu_vender, NOX("AuthenticAMD")) )       {
816
817                 _asm {
818                         mov eax, 0x80000000      // setup CPUID to return extended number of functions
819
820                         CPUID           // code bytes = 0fh,  0a2h
821
822                         mov RegEAX, eax // highest extended function value
823                 }
824
825                 if ( RegEAX > 0x80000000 )      {
826
827                         _asm {
828                                 mov eax, 0x80000001      // setup CPUID to return extended flags
829
830                                 CPUID           // code bytes = 0fh,  0a2h
831
832                                 mov RegEAX, eax // family, etc returned in eax
833                                 mov RegEDX, edx // flags in edx
834                         }
835
836                         if (RegEDX & 0x80000000)               // bit 31 is set for AMD-3D technology
837                         {
838                                 // try executing some 3Dnow instructions
839                                 __try { 
840
841                                         float x = (float)1.25;            
842                                         float y = (float)1.25;            
843                                         float z;                      
844
845                                         _asm {
846                                                 movd            mm1, x
847                                                 movd            mm2, y                  
848                                                 PFMUL(AMD_M1, AMD_M2);               
849                                                 movd            z, mm1
850                                                 femms
851                                                 emms
852                                         }
853
854                                         int should_be_156 = int(z*100);
855
856                                         if ( should_be_156 == 156 )     {
857                                                 *amd3d = 1;
858                                         }
859
860                                 }          
861
862                                 __except(EXCEPTION_EXECUTE_HANDLER) { }
863                         }
864
865                 }               
866         }
867         */
868 #endif
869 }
870
871 // --------------------------------------------------------------------------
872
873 int gr_init(int res, int mode, int depth, int fred_x, int fred_y)
874 {
875         int first_time = 0;
876         int max_w, max_h;
877
878         gr_detect_cpu(&Gr_cpu, &Gr_mmx, &Gr_amd3d, &Gr_katmai );
879
880         mprintf(( "GR_CPU: Family %d, MMX=%s\n", Gr_cpu, (Gr_mmx?"Yes":"No") ));
881         
882 //      gr_test();
883
884         if ( !Gr_inited )       
885                 atexit(gr_close);
886
887         // If already inited, shutdown the previous graphics
888         if ( Gr_inited )        {
889                 switch( gr_screen.mode )        {
890                 case GR_SOFTWARE:                       
891                         gr_soft_cleanup();
892                         break;
893 #ifndef PLAT_UNIX                       
894                 case GR_DIRECTDRAW:
895                         Int3();
896                         gr_directdraw_cleanup();
897                         break;
898                 case GR_DIRECT3D:                       
899                         gr_d3d_cleanup();
900                         break;
901                 case GR_GLIDE:
902                         gr_glide_cleanup();
903                         break;
904 #endif                  
905                 case GR_OPENGL:
906                         gr_opengl_cleanup();
907                         break;
908                 default:
909                         Int3();         // Invalid graphics mode
910                 }
911         } else {
912                 first_time = 1;
913         }
914
915 #if defined(HARDWARE_ONLY)
916 #ifndef PLAT_UNIX
917         if(!Fred_running && !Pofview_running && !Nebedit_running && !Is_standalone){
918                 if((mode != GR_GLIDE) && (mode != GR_DIRECT3D) && (mode != GR_OPENGL)){
919                         mprintf(("Forcing glide startup!\n"));
920                         mode = GR_GLIDE;
921                 }       
922         }
923 #endif
924 #endif
925
926         D3D_enabled = 0;
927         Gr_inited = 1;
928
929         max_w = -1;
930         max_h = -1;
931         if(!Fred_running && !Pofview_running){
932                 // set resolution based on the res type
933                 switch(res){
934                 case GR_640:
935                         max_w = 640;
936                         max_h = 480;
937                         break;
938
939                 case GR_1024:
940                         max_w = 1024;
941                         max_h = 768;
942                         break;
943
944                 default :
945                         Int3();
946                 }
947         } else {                
948                 max_w = fred_x;
949                 max_h = fred_y;
950         }
951
952         // Make w a multiple of 8
953         max_w = ( max_w / 8 )*8;
954         if ( max_w < 8 ) max_w = 8;
955         if ( max_h < 8 ) max_h = 8;
956
957         memset( &gr_screen, 0, sizeof(screen) );
958
959         gr_screen.signature = Gr_signature++;
960         gr_screen.mode = mode;
961         gr_screen.res = res;    
962         gr_screen.max_w = max_w;
963         gr_screen.max_h = max_h;
964         gr_screen.aspect = 1.0f;                        // Normal PC screen
965         gr_screen.offset_x = 0;
966         gr_screen.offset_y = 0;
967         gr_screen.clip_left = 0;
968         gr_screen.clip_top = 0;
969         gr_screen.clip_right = gr_screen.max_w - 1;
970         gr_screen.clip_bottom = gr_screen.max_h - 1;
971         gr_screen.clip_width = gr_screen.max_w;
972         gr_screen.clip_height = gr_screen.max_h;
973
974         switch( gr_screen.mode )        {
975                 case GR_SOFTWARE:
976                         Assert(Fred_running || Pofview_running || Is_standalone || Nebedit_running);
977                         gr_soft_init();
978                         break;
979 #ifndef PLAT_UNIX                       
980                 case GR_DIRECTDRAW:
981                         Int3();
982                         gr_directdraw_init();
983                         break;
984                 case GR_DIRECT3D:
985                         // we only care about possible 32 bit stuff here
986                         Cmdline_force_32bit = 0;
987                         if(depth == 32){
988                                 Cmdline_force_32bit = 1;
989                         } 
990
991                         gr_d3d_init();
992
993                         // bad startup - stupid D3D
994                         extern int D3D_inited;
995                         if(!D3D_inited){
996                                 Gr_inited = 0;
997                                 return 1;
998                         }
999
1000                         break;
1001                 case GR_GLIDE:
1002                         // if we're in high-res. force polygon interface
1003                         if(gr_screen.res == GR_1024){
1004                                 Gr_bitmap_poly = 1;
1005                         }
1006                         gr_glide_init();
1007                         break;
1008 #endif                  
1009                 case GR_OPENGL:
1010                         gr_opengl_init();
1011                         break;
1012                 default:
1013                         Int3();         // Invalid graphics mode
1014         }
1015
1016         memmove( Gr_current_palette, Gr_original_palette, 768 );
1017         gr_set_palette_internal(Gr_current_palette_name, Gr_current_palette,0); 
1018
1019         gr_set_gamma(Gr_gamma);
1020
1021         if ( Gr_cursor == -1 ){
1022                 Gr_cursor = bm_load( "cursor" );
1023         }
1024
1025         // load the web pointer cursor bitmap
1026         if (Web_cursor_bitmap < 0)      {
1027                 int nframes;                                            // used to pass, not really needed (should be 1)
1028                 Web_cursor_bitmap = bm_load_animation("cursorweb", &nframes);
1029                 Assert(Web_cursor_bitmap >= 0);         // if bitmap didnt load, thats not good (this is protected for in release tho)
1030         }
1031
1032         gr_set_color(0,0,0);
1033
1034         gr_set_clear_color(0, 0, 0);
1035
1036         // Call some initialization functions
1037         gr_set_shader(NULL);
1038
1039         return 0;
1040 }
1041
1042 void gr_force_windowed()
1043 {
1044         if ( !Gr_inited )       return;
1045
1046         switch( gr_screen.mode )        {
1047                 case GR_SOFTWARE:
1048                         {                               
1049                                 extern void gr_soft_force_windowed();
1050                                 gr_soft_force_windowed();
1051                         }
1052                         break;
1053 #ifndef PLAT_UNIX                       
1054                 case GR_DIRECTDRAW:
1055                         {
1056                                 Int3();
1057                                 extern void gr_directdraw_force_windowed();
1058                                 gr_directdraw_force_windowed();
1059                         }
1060                         break;
1061                 case GR_DIRECT3D:
1062                         break;
1063                 case GR_GLIDE:
1064                         {
1065                                 extern void gr_glide_force_windowed();
1066                                 gr_glide_force_windowed();
1067                         }
1068                         break;
1069 #endif                  
1070                 case GR_OPENGL:
1071                         break;
1072                 default:
1073                         Int3();         // Invalid graphics mode
1074         }
1075
1076         if ( Os_debugger_running )
1077                 Sleep(1000);            
1078 }
1079
1080 void gr_activate(int active)
1081 {
1082         if ( !Gr_inited ) return;
1083
1084         switch( gr_screen.mode )        {
1085                 case GR_SOFTWARE:
1086                         {                               
1087                                 extern void gr_soft_activate(int active);
1088                                 gr_soft_activate(active);
1089                                 return;
1090                         }
1091                         break;
1092 #ifndef PLAT_UNIX                       
1093                 case GR_DIRECTDRAW:
1094                         {
1095                                 Int3();
1096                                 extern void gr_dd_activate(int active);
1097                                 gr_dd_activate(active);
1098                                 return;
1099                         }
1100                         break;
1101                 case GR_DIRECT3D:
1102                         {       
1103                                 extern void gr_d3d_activate(int active);
1104                                 gr_d3d_activate(active);
1105                                 return;
1106                         }
1107                         break;
1108                 case GR_GLIDE:
1109                         {
1110                                 extern void gr_glide_activate(int active);
1111                                 gr_glide_activate(active);
1112                                 return;
1113                         }
1114                         break;
1115 #endif                  
1116                 case GR_OPENGL:
1117                         {       
1118                                 extern void gr_opengl_activate(int active);
1119                                 gr_opengl_activate(active);
1120                                 return;
1121                         }       
1122                         break;
1123                 default:
1124                         Int3();         // Invalid graphics mode
1125         }
1126
1127 }
1128
1129 // -----------------------------------------------------------------------
1130 // gr_set_cursor_bitmap()
1131 //
1132 // Set the bitmap for the mouse pointer.  This is called by the animating mouse
1133 // pointer code.
1134 //
1135 // The lock parameter just locks basically disables the next call of this function that doesnt
1136 // have an unlock feature.  If adding in more cursor-changing situations, be aware of
1137 // unexpected results. You have been warned.
1138 //
1139 // TODO: investigate memory leak of original Gr_cursor bitmap when this is called
1140 void gr_set_cursor_bitmap(int n, int lock)
1141 {
1142         static int locked = 0;                  
1143         Assert(n >= 0);
1144
1145         if (!locked || (lock == GR_CURSOR_UNLOCK)) {
1146                 Gr_cursor = n;
1147         } else {
1148                 locked = 0;
1149         }
1150
1151         if (lock == GR_CURSOR_LOCK) {
1152                 locked = 1;
1153         }
1154 }
1155
1156 // retrieves the current bitmap
1157 // used in UI_GADGET to save/restore current cursor state
1158 int gr_get_cursor_bitmap()
1159 {
1160         return Gr_cursor;
1161 }
1162
1163
1164 int Gr_bitmap_poly = 0;
1165 DCF(bmap, "")
1166 {
1167         Gr_bitmap_poly = !Gr_bitmap_poly;
1168
1169         if(Gr_bitmap_poly){
1170                 dc_printf("Using poly bitmaps\n");
1171         } else {
1172                 dc_printf("Using LFB bitmaps\n");
1173         }
1174 }
1175
1176 // new bitmap functions
1177 void gr_bitmap(int x, int y)
1178 {
1179         int section_x, section_y;       
1180         int x_line, y_line;
1181         int w, h;
1182
1183         // d3d and glide support texture poly shiz
1184         if(((gr_screen.mode == GR_DIRECT3D) || (gr_screen.mode == GR_GLIDE) || (gr_screen.mode == GR_OPENGL)) && Gr_bitmap_poly){
1185                 int idx, s_idx;
1186                 // float u_scale, v_scale;
1187                 bitmap_section_info *sections;                  
1188
1189                 // render all sections
1190                 bm_get_info(gr_screen.current_bitmap, &w, &h, NULL, NULL, NULL, &sections);
1191                 y_line = 0;
1192                 section_y = 0;
1193                 for(idx=0; idx<sections->num_y; idx++){
1194                         x_line = 0;
1195                         for(s_idx=0; s_idx<sections->num_x; s_idx++){
1196                                 // get the section as a texture in vram                                 
1197                                 gr_set_bitmap(gr_screen.current_bitmap, gr_screen.current_alphablend_mode, gr_screen.current_bitblt_mode, gr_screen.current_alpha, s_idx, idx);
1198
1199                                 // determine the width and height of this section
1200                                 bm_get_section_size(gr_screen.current_bitmap, s_idx, idx, &section_x, &section_y);
1201
1202                                 // draw as a poly
1203                                 g3_draw_2d_poly_bitmap(x + x_line, y + y_line, section_x, section_y, TMAP_FLAG_BITMAP_SECTION);
1204                                 x_line += section_x;
1205                         }
1206                         y_line += section_y;
1207                 }
1208
1209                 // done. whee!
1210                 return;
1211         }                       
1212
1213         // old school bitmaps
1214         switch(gr_screen.mode){
1215         case GR_SOFTWARE:
1216         case GR_DIRECTDRAW:
1217                 grx_bitmap(x, y);
1218                 break;
1219
1220 #ifndef PLAT_UNIX
1221         case GR_DIRECT3D:
1222                 gr_d3d_bitmap(x, y);
1223                 break;
1224         
1225         case GR_GLIDE:          
1226                 gr_glide_bitmap(x, y);          
1227                 break;
1228 #endif
1229         /* don't want opengl bitmap to be called -- slow! */
1230         //case GR_OPENGL:
1231         //      gr_opengl_bitmap(x, y);
1232         //      break;
1233         default:
1234                 Int3();
1235         }
1236 }
1237
1238 void gr_bitmap_ex(int x, int y, int w, int h, int sx, int sy)
1239 {
1240         switch(gr_screen.mode){
1241         case GR_SOFTWARE:
1242         case GR_DIRECTDRAW:
1243                 grx_bitmap_ex(x, y, w, h, sx, sy);
1244                 break;
1245 #ifndef PLAT_UNIX
1246         case GR_DIRECT3D:
1247                 gr_d3d_bitmap_ex(x, y, w, h, sx, sy);
1248                 break;
1249
1250         case GR_GLIDE:
1251                 gr_glide_bitmap_ex(x, y, w, h, sx, sy);
1252                 break;
1253 #endif
1254         /* slow! */
1255         //case GR_OPENGL:
1256         //      gr_opengl_bitmap_ex(x, y, w, h, sx, sy);
1257         //      break;
1258         default:
1259                 Int3();
1260                 break;
1261         }
1262 }
1263
1264 // given endpoints, and thickness, calculate coords of the endpoint
1265 void gr_pline_helper(vector *out, vector *in1, vector *in2, int thickness)
1266 {
1267         vector slope;   
1268
1269         // slope of the line    
1270         if(vm_vec_same(in1, in2)){
1271                 slope = vmd_zero_vector;
1272         } else {
1273                 vm_vec_sub(&slope, in2, in1);
1274                 float temp = -slope.xyz.x;
1275                 slope.xyz.x = slope.xyz.y;
1276                 slope.xyz.y = temp;
1277                 vm_vec_normalize(&slope);
1278         }
1279
1280         // get the points               
1281         vm_vec_scale_add(out, in1, &slope, (float)thickness);
1282 }
1283
1284 // special function for drawing polylines. this function is specifically intended for
1285 // polylines where each section is no more than 90 degrees away from a previous section.
1286 // Moreover, it is _really_ intended for use with 45 degree angles. 
1287 void gr_pline_special(vector **pts, int num_pts, int thickness)
1288 {                               
1289         vector s1, s2, e1, e2, dir;
1290         vector last_e1, last_e2;
1291         vertex v[4];
1292         vertex *verts[4] = {&v[0], &v[1], &v[2], &v[3]};
1293         int saved_zbuffer_mode, idx;            
1294         int started_frame = 0;
1295
1296         // Assert(0);
1297
1298         // if we have less than 2 pts, bail
1299         if(num_pts < 2){
1300                 return;
1301         }       
1302
1303         extern int G3_count;
1304         if(G3_count == 0){
1305                 g3_start_frame(1);              
1306                 started_frame = 1;
1307         }
1308
1309         // turn off zbuffering  
1310         saved_zbuffer_mode = gr_zbuffer_get();
1311         gr_zbuffer_set(GR_ZBUFF_NONE);  
1312
1313         // turn off culling
1314         gr_set_cull(0);
1315
1316         // draw each section
1317         last_e1 = vmd_zero_vector;
1318         last_e2 = vmd_zero_vector;
1319         for(idx=0; idx<num_pts-1; idx++){               
1320                 // get the start and endpoints          
1321                 s1 = *pts[idx];                                                                                                         // start 1 (on the line)
1322                 gr_pline_helper(&s2, pts[idx], pts[idx+1], thickness);  // start 2
1323                 e1 = *pts[idx+1];                                                                                                               // end 1 (on the line)
1324                 vm_vec_sub(&dir, pts[idx+1], pts[idx]);         
1325                 vm_vec_add(&e2, &s2, &dir);                                                                             // end 2
1326                 
1327                 // stuff coords         
1328                 v[0].sx = (float)ceil(s1.xyz.x);
1329                 v[0].sy = (float)ceil(s1.xyz.y);        
1330                 v[0].sw = 0.0f;
1331                 v[0].u = 0.5f;
1332                 v[0].v = 0.5f;
1333                 v[0].flags = PF_PROJECTED;
1334                 v[0].codes = 0;
1335                 v[0].r = gr_screen.current_color.red;
1336                 v[0].g = gr_screen.current_color.green;
1337                 v[0].b = gr_screen.current_color.blue;
1338
1339                 v[1].sx = (float)ceil(s2.xyz.x);
1340                 v[1].sy = (float)ceil(s2.xyz.y);        
1341                 v[1].sw = 0.0f;
1342                 v[1].u = 0.5f;
1343                 v[1].v = 0.5f;
1344                 v[1].flags = PF_PROJECTED;
1345                 v[1].codes = 0;
1346                 v[1].r = gr_screen.current_color.red;
1347                 v[1].g = gr_screen.current_color.green;
1348                 v[1].b = gr_screen.current_color.blue;
1349
1350                 v[2].sx = (float)ceil(e2.xyz.x);
1351                 v[2].sy = (float)ceil(e2.xyz.y);
1352                 v[2].sw = 0.0f;
1353                 v[2].u = 0.5f;
1354                 v[2].v = 0.5f;
1355                 v[2].flags = PF_PROJECTED;
1356                 v[2].codes = 0;
1357                 v[2].r = gr_screen.current_color.red;
1358                 v[2].g = gr_screen.current_color.green;
1359                 v[2].b = gr_screen.current_color.blue;
1360
1361                 v[3].sx = (float)ceil(e1.xyz.x);
1362                 v[3].sy = (float)ceil(e1.xyz.y);
1363                 v[3].sw = 0.0f;
1364                 v[3].u = 0.5f;
1365                 v[3].v = 0.5f;
1366                 v[3].flags = PF_PROJECTED;
1367                 v[3].codes = 0;                         
1368                 v[3].r = gr_screen.current_color.red;
1369                 v[3].g = gr_screen.current_color.green;
1370                 v[3].b = gr_screen.current_color.blue;          
1371
1372                 // draw the polys
1373                 g3_draw_poly_constant_sw(4, verts, TMAP_FLAG_GOURAUD | TMAP_FLAG_RGB, 0.1f);            
1374
1375                 // if we're past the first section, draw a "patch" triangle to fill any gaps
1376                 if(idx > 0){
1377                         // stuff coords         
1378                         v[0].sx = (float)ceil(s1.xyz.x);
1379                         v[0].sy = (float)ceil(s1.xyz.y);
1380                         v[0].sw = 0.0f;
1381                         v[0].u = 0.5f;
1382                         v[0].v = 0.5f;
1383                         v[0].flags = PF_PROJECTED;
1384                         v[0].codes = 0;
1385                         v[0].r = gr_screen.current_color.red;
1386                         v[0].g = gr_screen.current_color.green;
1387                         v[0].b = gr_screen.current_color.blue;
1388
1389                         v[1].sx = (float)ceil(s2.xyz.x);
1390                         v[1].sy = (float)ceil(s2.xyz.y);        
1391                         v[1].sw = 0.0f;
1392                         v[1].u = 0.5f;
1393                         v[1].v = 0.5f;
1394                         v[1].flags = PF_PROJECTED;
1395                         v[1].codes = 0;
1396                         v[1].r = gr_screen.current_color.red;
1397                         v[1].g = gr_screen.current_color.green;
1398                         v[1].b = gr_screen.current_color.blue;
1399
1400
1401                         v[2].sx = (float)ceil(last_e2.xyz.x);
1402                         v[2].sy = (float)ceil(last_e2.xyz.y);
1403                         v[2].sw = 0.0f;
1404                         v[2].u = 0.5f;
1405                         v[2].v = 0.5f;
1406                         v[2].flags = PF_PROJECTED;
1407                         v[2].codes = 0;
1408                         v[2].r = gr_screen.current_color.red;
1409                         v[2].g = gr_screen.current_color.green;
1410                         v[2].b = gr_screen.current_color.blue;
1411
1412                         g3_draw_poly_constant_sw(3, verts, TMAP_FLAG_GOURAUD | TMAP_FLAG_RGB, 0.1f);            
1413                 }
1414
1415                 // store our endpoints
1416                 last_e1 = e1;
1417                 last_e2 = e2;
1418         }
1419
1420         if(started_frame){
1421                 g3_end_frame();
1422         }
1423
1424         // restore zbuffer mode
1425         gr_zbuffer_set(saved_zbuffer_mode);
1426
1427         // restore culling
1428         gr_set_cull(1);         
1429 }
1430