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