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