]> icculus.org git repositories - taylor/freespace2.git/blob - src/graphics/gropengl.cpp
added rest of init
[taylor/freespace2.git] / src / graphics / gropengl.cpp
1 /*
2  * $Logfile: /Freespace2/code/Graphics/GrOpenGL.cpp $
3  * $Revision$
4  * $Date$
5  * $Author$
6  *
7  * Code that uses the OpenGL graphics library
8  *
9  * $Log$
10  * Revision 1.9  2002/05/29 03:35:51  relnev
11  * added rest of init
12  *
13  * Revision 1.8  2002/05/29 03:30:05  relnev
14  * update opengl stubs
15  *
16  * Revision 1.7  2002/05/29 02:52:32  theoddone33
17  * Enable OpenGL renderer
18  *
19  * Revision 1.6  2002/05/28 04:56:51  theoddone33
20  * runs a little bit now
21  *
22  * Revision 1.5  2002/05/28 04:07:28  theoddone33
23  * New graphics stubbing arrangement
24  *
25  * Revision 1.4  2002/05/27 23:39:34  relnev
26  * 0
27  *
28  * Revision 1.3  2002/05/27 22:35:01  theoddone33
29  * more symbols
30  *
31  * Revision 1.2  2002/05/27 22:32:02  theoddone33
32  * throw all d3d stuff at opengl
33  *
34  * Revision 1.1.1.1  2002/05/03 03:28:09  root
35  * Initial import.
36  *
37  * 
38  * 10    7/14/99 9:42a Dave
39  * Put in clear_color debug function. Put in base for 3dnow stuff / P3
40  * stuff
41  * 
42  * 9     7/09/99 9:51a Dave
43  * Added thick polyline code.
44  * 
45  * 8     6/29/99 10:35a Dave
46  * Interface polygon bitmaps! Whee!
47  * 
48  * 7     2/03/99 11:44a Dave
49  * Fixed d3d transparent textures.
50  * 
51  * 6     1/24/99 11:37p Dave
52  * First full rev of beam weapons. Very customizable. Removed some bogus
53  * Int3()'s in low level net code.
54  * 
55  * 5     12/18/98 1:13a Dave
56  * Rough 1024x768 support for Direct3D. Proper detection and usage through
57  * the launcher.
58  * 
59  * 4     12/06/98 2:36p Dave
60  * Drastically improved nebula fogging.
61  * 
62  * 3     11/11/98 5:37p Dave
63  * Checkin for multiplayer testing.
64  * 
65  * 2     10/07/98 10:53a Dave
66  * Initial checkin.
67  * 
68  * 1     10/07/98 10:49a Dave
69  * 
70  * 14    5/20/98 9:46p John
71  * added code so the places in code that change half the palette don't
72  * have to clear the screen.
73  * 
74  * 13    5/06/98 5:30p John
75  * Removed unused cfilearchiver.  Removed/replaced some unused/little used
76  * graphics functions, namely gradient_h and _v and pixel_sp.   Put in new
77  * DirectX header files and libs that fixed the Direct3D alpha blending
78  * problems.
79  * 
80  * 12    4/14/98 12:15p John
81  * Made 16-bpp movies work.
82  * 
83  * 11    3/12/98 5:36p John
84  * Took out any unused shaders.  Made shader code take rgbc instead of
85  * matrix and vector since noone used it like a matrix and it would have
86  * been impossible to do in hardware.   Made Glide implement a basic
87  * shader for online help.  
88  * 
89  * 10    3/10/98 4:18p John
90  * Cleaned up graphics lib.  Took out most unused gr functions.   Made D3D
91  * & Glide have popups and print screen.  Took out all >8bpp software
92  * support.  Made Fred zbuffer.  Made zbuffer allocate dynamically to
93  * support Fred.  Made zbuffering key off of functions rather than one
94  * global variable.
95  * 
96  * 9     12/02/97 4:00p John
97  * Added first rev of thruster glow, along with variable levels of
98  * translucency, which retquired some restructing of palman.
99  * 
100  * 8     10/03/97 9:10a John
101  * added better antialiased line drawer
102  * 
103  * 7     9/23/97 10:45a John
104  * made so you can tell bitblt code to rle a bitmap by passing flag to
105  * gr_set_bitmap
106  * 
107  * 6     9/09/97 11:01a Sandeep
108  * fixed warning level 4 bugs
109  * 
110  * 5     7/10/97 2:06p John
111  * added code to specify alphablending type for bitmaps.
112  * 
113  * 4     6/17/97 7:04p John
114  * added d3d support for gradients.
115  * fixed some color bugs by adding screen signatures instead of watching
116  * flags and palette changes.
117  * 
118  * 3     6/12/97 2:50a Lawrance
119  * bm_unlock() now passed bitmap number, not pointer
120  * 
121  * 2     6/11/97 1:12p John
122  * Started fixing all the text colors in the game.
123  * 
124  * 1     5/12/97 12:14p John
125  *
126  * $NoKeywords: $
127  */
128
129 #ifndef PLAT_UNIX
130 #include <windows.h>
131 #include <windowsx.h>
132 #endif
133
134 #include "osapi.h"
135 #include "2d.h"
136 #include "bmpman.h"
137 #include "floating.h"
138 #include "palman.h"
139 #include "grinternal.h"
140 #include "gropengl.h"
141 #include "line.h"
142
143 static int Inited = 0;
144 #ifdef PLAT_UNIX
145 // Throw in some dummy functions - DDOI
146
147 int D3D_32bit = 0;              // grd3d.cpp
148 int D3D_fog_mode = -1;          // grd3d.cpp
149 int D3D_inited = 0;             // grd3d.cpp
150 int D3D_zbias = 1;              // grd3d.cpp
151 int D3d_rendition_uvs = 0;      // grd3d.cpp
152
153 void gr_dd_activate(int active)         // grdirectdraw.cpp
154 {
155         STUB_FUNCTION;
156 }
157
158 void gr_directdraw_cleanup()            // grdirectdraw.cpp
159 {
160         STUB_FUNCTION;
161 }
162
163 void gr_directdraw_force_windowed()     // grdirectdraw.cpp
164 {
165         STUB_FUNCTION;
166 }
167
168 void gr_directdraw_init()
169 {
170         STUB_FUNCTION;
171 }
172
173
174 void gr_d3d_preload(int x, int y)
175 {
176         STUB_FUNCTION;
177 }
178
179 void d3d_start_frame()
180 {
181         STUB_FUNCTION;
182 }
183
184 void d3d_stop_frame()
185 {
186         STUB_FUNCTION;
187 }
188
189 void d3d_flush ()
190 {
191         STUB_FUNCTION;
192 }
193
194 void d3d_zbias (int a)
195 {
196         STUB_FUNCTION;
197 }
198 #endif
199
200 void gr_opengl_activate(int b)
201 {
202         STUB_FUNCTION;
203 }
204
205 void gr_opengl_preload_init()
206 {
207         STUB_FUNCTION;
208 }
209
210 void gr_opengl_pixel(int x, int y)
211 {
212         if ( x < gr_screen.clip_left ) return;
213         if ( x > gr_screen.clip_right ) return;
214         if ( y < gr_screen.clip_top ) return;
215         if ( y > gr_screen.clip_bottom ) return;
216 }
217
218 void gr_opengl_clear()
219 {
220         STUB_FUNCTION;
221 }
222
223 void gr_opengl_flip()
224 {
225 #ifdef PLAT_UNIX
226         if (Inited) SDL_GL_SwapBuffers ();
227 #endif
228 }
229
230 void gr_opengl_flip_window(uint _hdc, int x, int y, int w, int h )
231 {
232         STUB_FUNCTION;
233 }
234
235 void gr_opengl_set_clip(int x,int y,int w,int h)
236 {
237         // check for sanity of parameters
238         if (x < 0)
239                 x = 0;
240         if (y < 0)
241                 y = 0;
242
243         if (x >= gr_screen.max_w)
244                 x = gr_screen.max_w - 1;
245         if (y >= gr_screen.max_h)
246                 y = gr_screen.max_h - 1;
247
248         if (x + w > gr_screen.max_w)
249                 w = gr_screen.max_w - x;
250         if (y + h > gr_screen.max_h)
251                 h = gr_screen.max_h - y;
252         
253         if (w > gr_screen.max_w)
254                 w = gr_screen.max_w;
255         if (h > gr_screen.max_h)
256                 h = gr_screen.max_h;
257         
258         gr_screen.offset_x = x;
259         gr_screen.offset_y = y;
260         gr_screen.clip_left = 0;
261         gr_screen.clip_right = w-1;
262         gr_screen.clip_top = 0;
263         gr_screen.clip_bottom = h-1;
264         gr_screen.clip_width = w;
265         gr_screen.clip_height = h;
266         
267         STUB_FUNCTION;
268 }
269
270 void gr_opengl_reset_clip()
271 {
272         gr_screen.offset_x = 0;
273         gr_screen.offset_y = 0;
274         gr_screen.clip_left = 0;
275         gr_screen.clip_top = 0;
276         gr_screen.clip_right = gr_screen.max_w - 1;
277         gr_screen.clip_bottom = gr_screen.max_h - 1;
278         gr_screen.clip_width = gr_screen.max_w;
279         gr_screen.clip_height = gr_screen.max_h;
280         
281         STUB_FUNCTION;
282 }
283
284 void gr_opengl_set_font(int fontnum)
285 {
286         STUB_FUNCTION;
287 }
288
289 void gr_opengl_set_color( int r, int g, int b )
290 {
291         Assert((r >= 0) && (r < 256));
292         Assert((g >= 0) && (g < 256));
293         Assert((b >= 0) && (b < 256));
294
295         gr_screen.current_color.red = (unsigned char)r;
296         gr_screen.current_color.green = (unsigned char)g;
297         gr_screen.current_color.blue = (unsigned char)b;
298         
299 //      STUB_FUNCTION;
300 #warning STUB_FUNCTION: gr_opengl_set_color
301 }
302
303 void gr_opengl_set_bitmap( int bitmap_num, int alphablend_mode, int bitblt_mode, float alpha, int sx, int sy )
304 {
305         gr_screen.current_alpha = alpha;
306         gr_screen.current_alphablend_mode = alphablend_mode;
307         gr_screen.current_bitblt_mode = bitblt_mode;
308         gr_screen.current_bitmap = bitmap_num;
309
310         gr_screen.current_bitmap_sx = sx;
311         gr_screen.current_bitmap_sy = sy;
312         
313         STUB_FUNCTION;
314 }
315
316 void gr_opengl_create_shader(shader * shade, float r, float g, float b, float c )
317 {
318         shade->screen_sig = gr_screen.signature;
319         shade->r = r;
320         shade->g = g;
321         shade->b = b;
322         shade->c = c;
323         
324         STUB_FUNCTION;
325 }
326
327 void gr_opengl_set_shader( shader * shade )
328 {       
329         if ( shade )    {
330                 if (shade->screen_sig != gr_screen.signature)   {
331                         gr_create_shader( shade, shade->r, shade->g, shade->b, shade->c );
332                 }
333                 gr_screen.current_shader = *shade;
334         } else {
335                 gr_create_shader( &gr_screen.current_shader, 0.0f, 0.0f, 0.0f, 0.0f );
336         }
337         
338         STUB_FUNCTION;
339 }
340
341
342 void gr_opengl_bitmap_ex(int x,int y,int w,int h,int sx,int sy)
343 {
344         int i,j;
345         bitmap * bmp;
346         ubyte * sptr;
347
348         bmp = bm_lock( gr_screen.current_bitmap, 8, 0 );
349         sptr = (ubyte *)( bmp->data + (sy*bmp->w+sx) );
350
351 //      mprintf(( "x=%d, y=%d, w=%d, h=%d\n", x, y, w, h ));
352 //      mprintf(( "sx=%d, sy=%d, bw=%d, bh=%d\n", sx, sy, bmp->w, bmp->h ));
353
354         for (i=0; i<h; i++ )    {
355                 for ( j=0; j<w; j++ )   {
356                         gr_set_color( gr_palette[sptr[j]*3+0], gr_palette[sptr[j]*3+1], gr_palette[sptr[j]*3+2] );
357                         gr_pixel( x+j, i+y );
358                 }
359                 sptr += bmp->w;
360         }
361         bm_unlock(gr_screen.current_bitmap);
362         
363         STUB_FUNCTION;
364 }
365
366 void gr_opengl_bitmap(int x, int y)
367 {
368         int w, h;
369
370         bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
371         int dx1=x, dx2=x+w-1;
372         int dy1=y, dy2=y+h-1;
373         int sx=0, sy=0;
374
375         if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
376         if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
377         if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
378         if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
379         if ( dx2 > gr_screen.clip_right )       { dx2 = gr_screen.clip_right; }
380         if ( dy2 > gr_screen.clip_bottom )      { dy2 = gr_screen.clip_bottom; }
381
382         if ( sx < 0 ) return;
383         if ( sy < 0 ) return;
384         if ( sx >= w ) return;
385         if ( sy >= h ) return;
386
387         // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
388
389         gr_bitmap_ex(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);        
390 }
391
392 static void opengl_scanline(int x1,int x2,int y)
393 {
394         STUB_FUNCTION;
395 }
396
397 void gr_opengl_rect(int x,int y,int w,int h)
398 {
399         int i, swapped=0;
400         int x1 = x, x2;
401         int y1 = y, y2;
402
403         if ( w > 0 )
404                  x2 = x + w - 1;
405         else
406                  x2 = x + w + 1;
407
408         if ( h > 0 )
409                 y2 = y + h - 1;
410         else
411                 y2 = y + h + 1;
412                 
413         if ( x2 < x1 )  {
414                 int tmp;        
415                 tmp = x1;
416                 x1 = x2;
417                 x2 = tmp;
418                 w = -w;
419                 swapped = 1;
420         }
421
422         if ( y2 < y1 )  {
423                 int tmp;        
424                 tmp = y1;
425                 y1 = y2;
426                 y2 = tmp;
427                 h = -h;
428                 swapped = 1;
429         }
430
431         for (i=0; i<h; i++ )
432                 opengl_scanline( x1, x2, y1+i );
433                 
434         STUB_FUNCTION;
435 }
436
437
438 void gr_opengl_shade(int x,int y,int w,int h)
439 {
440         STUB_FUNCTION;
441 }
442
443 void opengl_mtext(int x, int y, char *s, int len )
444 {
445         STUB_FUNCTION;
446 }
447
448 void gr_opengl_string(int x,int y,char * text)
449 {
450         char *p, *p1;
451         int w, h;
452
453         p1 = text;
454         do {
455                 p = strchr( p1, '\n' );
456                 if ( p ) { 
457                         *p = 0;
458                         p++;
459                 }
460                 gr_get_string_size( &w, &h, p1 );
461
462                 if ( x == 0x8000 )
463                         opengl_mtext(gr_screen.offset_x+(gr_screen.clip_width-w)/2,y+gr_screen.offset_y,p1,strlen(p1));
464                 else
465                         opengl_mtext(gr_screen.offset_x+x,y+gr_screen.offset_y,p1,strlen(p1));
466
467                 p1 = p;
468                 if ( p1 && (strlen(p1) < 1) ) p1 = NULL;
469                 y += h;
470         } while(p1!=NULL);
471 }
472
473
474
475
476 void gr_opengl_circle( int xc, int yc, int d )
477 {
478         int p,x, y, r;
479
480         r = d/2;
481         p=3-d;
482         x=0;
483         y=r;
484
485         // Big clip
486         if ( (xc+r) < gr_screen.clip_left ) return;
487         if ( (xc-r) > gr_screen.clip_right ) return;
488         if ( (yc+r) < gr_screen.clip_top ) return;
489         if ( (yc-r) > gr_screen.clip_bottom ) return;
490
491         while(x<y)      {
492                 // Draw the first octant
493                 opengl_scanline( xc-y, xc+y, yc-x );
494                 opengl_scanline( xc-y, xc+y, yc+x );
495
496                 if (p<0) 
497                         p=p+(x<<2)+6;
498                 else    {
499                         // Draw the second octant
500                         opengl_scanline( xc-x, xc+x, yc-y );
501                         opengl_scanline( xc-x, xc+x, yc+y );
502                         p=p+((x-y)<<2)+10;
503                         y--;
504                 }
505                 x++;
506         }
507         if(x==y)        {
508                 opengl_scanline( xc-x, xc+x, yc-y );
509                 opengl_scanline( xc-x, xc+x, yc+y );
510         }
511         return;
512 }
513
514
515 void gr_opengl_line(int x1,int y1,int x2,int y2)
516 {
517         int i;
518    int xstep,ystep;
519    int dy=y2-y1;
520    int dx=x2-x1;
521    int error_term=0;
522         int clipped = 0, swapped=0;
523
524         INT_CLIPLINE(x1,y1,x2,y2,gr_screen.clip_left,gr_screen.clip_top,gr_screen.clip_right,gr_screen.clip_bottom,return,clipped=1,swapped=1);
525                 
526         if(dy<0)        {
527                 dy=-dy;
528       ystep=-1;
529         }       else    {
530       ystep=1;
531         }
532
533    if(dx<0)     {
534       dx=-dx;
535       xstep=-1;
536    } else {
537       xstep=1;
538         }
539
540         if(dx>dy)       {
541
542                 for(i=dx+1;i>0;i--) {
543                         gr_pixel( x1, y1 ); 
544                         x1 += xstep;
545          error_term+=dy;
546
547          if(error_term>dx)      {
548                                 error_term-=dx;
549             y1+=ystep;
550          }
551       }
552    } else {
553
554       for(i=dy+1;i>0;i--)       {
555                         gr_pixel( x1, y1 ); 
556                         y1 += ystep;
557          error_term+=dx;
558          if(error_term>0)       {
559             error_term-=dy;
560             x1+=xstep;
561          }
562
563       }
564
565    }
566    
567    STUB_FUNCTION;
568 }
569
570 #define FIND_SCALED_NUM(x,x0,x1,y0,y1) (((((x)-(x0))*((y1)-(y0)))/((x1)-(x0)))+(y0))
571
572 void gr_opengl_scaler(vertex *va, vertex *vb )
573 {
574         float x0, y0, x1, y1;
575         float u0, v0, u1, v1;
576         float clipped_x0, clipped_y0, clipped_x1, clipped_y1;
577         float clipped_u0, clipped_v0, clipped_u1, clipped_v1;
578         float xmin, xmax, ymin, ymax;
579         int dx0, dy0, dx1, dy1;
580
581         //============= CLIP IT =====================
582
583         x0 = va->sx; y0 = va->sy;
584         x1 = vb->sx; y1 = vb->sy;
585
586         xmin = i2fl(gr_screen.clip_left); ymin = i2fl(gr_screen.clip_top);
587         xmax = i2fl(gr_screen.clip_right); ymax = i2fl(gr_screen.clip_bottom);
588
589         u0 = va->u; v0 = va->v;
590         u1 = vb->u; v1 = vb->v;
591
592         // Check for obviously offscreen bitmaps...
593         if ( (y1<=y0) || (x1<=x0) ) return;
594         if ( (x1<xmin ) || (x0>xmax) ) return;
595         if ( (y1<ymin ) || (y0>ymax) ) return;
596
597         clipped_u0 = u0; clipped_v0 = v0;
598         clipped_u1 = u1; clipped_v1 = v1;
599
600         clipped_x0 = x0; clipped_y0 = y0;
601         clipped_x1 = x1; clipped_y1 = y1;
602
603         // Clip the left, moving u0 right as necessary
604         if ( x0 < xmin )        {
605                 clipped_u0 = FIND_SCALED_NUM(xmin,x0,x1,u0,u1);
606                 clipped_x0 = xmin;
607         }
608
609         // Clip the right, moving u1 left as necessary
610         if ( x1 > xmax )        {
611                 clipped_u1 = FIND_SCALED_NUM(xmax,x0,x1,u0,u1);
612                 clipped_x1 = xmax;
613         }
614
615         // Clip the top, moving v0 down as necessary
616         if ( y0 < ymin )        {
617                 clipped_v0 = FIND_SCALED_NUM(ymin,y0,y1,v0,v1);
618                 clipped_y0 = ymin;
619         }
620
621         // Clip the bottom, moving v1 up as necessary
622         if ( y1 > ymax )        {
623                 clipped_v1 = FIND_SCALED_NUM(ymax,y0,y1,v0,v1);
624                 clipped_y1 = ymax;
625         }
626         
627         dx0 = fl2i(clipped_x0); dx1 = fl2i(clipped_x1);
628         dy0 = fl2i(clipped_y0); dy1 = fl2i(clipped_y1);
629
630         if (dx1<=dx0) return;
631         if (dy1<=dy0) return;
632
633         //============= DRAW IT =====================
634         int u, v, du, dv;
635         int y, w;
636         ubyte * sbits;
637         bitmap * bp;
638         ubyte * spixels;
639         float tmpu, tmpv;
640
641         tmpu = (clipped_u1-clipped_u0) / (dx1-dx0);
642         if ( fl_abs(tmpu) < 0.001f ) {
643                 return;         // scaled up way too far!
644         }
645         tmpv = (clipped_v1-clipped_v0) / (dy1-dy0);
646         if ( fl_abs(tmpv) < 0.001f ) {
647                 return;         // scaled up way too far!
648         }
649
650         bp = bm_lock( gr_screen.current_bitmap, 8, 0 );
651
652         du = fl2f(tmpu*(bp->w-1));
653         dv = fl2f(tmpv*(bp->h-1));
654
655         v = fl2f(clipped_v0*(bp->h-1));
656         u = fl2f(clipped_u0*(bp->w-1)); 
657         w = dx1 - dx0 + 1;
658
659         spixels = (ubyte *)bp->data;
660
661         for (y=dy0; y<=dy1; y++ )                       {
662                 sbits = &spixels[bp->rowsize*(v>>16)];
663
664                 int x, tmp_u;
665                 tmp_u = u;
666                 for (x=0; x<w; x++ )                    {
667                         ubyte c = sbits[ tmp_u >> 16 ];
668                         if ( c != 255 ) {
669                                 gr_set_color( gr_palette[c*3+0], gr_palette[c*3+1], gr_palette[c*3+2] );
670                                 gr_pixel( x+dx0, y );
671                         }
672                         tmp_u += du;
673                 }
674                 v += dv;
675         }
676
677         bm_unlock(gr_screen.current_bitmap);
678
679         STUB_FUNCTION;
680 }
681
682
683
684 void gr_opengl_tmapper( int nv, vertex * verts[], uint flags )
685 {
686         STUB_FUNCTION;
687 }
688
689
690 void gr_opengl_gradient(int x1,int y1,int x2,int y2)
691 {
692         STUB_FUNCTION;
693 }
694
695 void gr_opengl_set_palette(ubyte *new_palette, int is_alphacolor)
696 {
697         STUB_FUNCTION;
698 }
699
700 void gr_opengl_get_color( int * r, int * g, int * b )
701 {
702         if (r) *r = gr_screen.current_color.red;
703         if (g) *g = gr_screen.current_color.green;
704         if (b) *b = gr_screen.current_color.blue;
705         
706         STUB_FUNCTION;
707 }
708
709 void gr_opengl_init_color(color *c, int r, int g, int b)
710 {
711         gr_screen.current_color.screen_sig = gr_screen.signature;
712         gr_screen.current_color.red = (unsigned char)r;
713         gr_screen.current_color.green = (unsigned char)g;
714         gr_screen.current_color.blue = (unsigned char)b;
715         
716         STUB_FUNCTION;
717 }
718
719 void gr_opengl_set_color_fast(color *dst)
720 {
721         if ( dst->screen_sig != gr_screen.signature )   {
722                 gr_init_color( dst, dst->red, dst->green, dst->blue );
723                 return;
724         }
725         gr_screen.current_color = *dst;
726         
727         STUB_FUNCTION;
728 }
729
730 void gr_opengl_print_screen(char *filename)
731 {
732         STUB_FUNCTION;
733 }
734
735 int gr_opengl_supports_res_ingame(int res)
736 {
737         STUB_FUNCTION;
738         
739         return 1;
740 }
741
742 int gr_opengl_supports_res_interface(int res)
743 {
744         STUB_FUNCTION;
745         
746         return 1;
747 }
748
749 void gr_opengl_cleanup()
750 {
751         if ( !Inited )  return;
752
753         gr_reset_clip();
754         gr_clear();
755         gr_flip();
756
757         Inited = 0;
758 }
759
760 void gr_opengl_fog_set(int fog_mode, int r, int g, int b, float near, float far)
761 {
762         STUB_FUNCTION;
763 }
764
765 void gr_opengl_get_pixel(int x, int y, int *r, int *g, int *b)
766 {
767         STUB_FUNCTION;
768 }
769
770 void gr_opengl_get_region(int front, int w, int g, ubyte *data)
771 {
772         STUB_FUNCTION;
773 }
774
775 void gr_opengl_set_cull(int cull)
776 {
777         STUB_FUNCTION;
778 }
779
780 void gr_opengl_filter_set(int filter)
781 {
782         STUB_FUNCTION;
783 }
784
785 // cross fade
786 void gr_opengl_cross_fade(int bmap1, int bmap2, int x1, int y1, int x2, int y2, float pct)
787 {
788         STUB_FUNCTION;
789 }
790
791 int gr_opengl_tcache_set(int bitmap_id, int bitmap_type, float *u_ratio, float *v_ratio, int fail_on_full = 0, int sx = -1, int sy = -1, int force = 0)
792 {
793         STUB_FUNCTION;
794         
795         return 1;
796 }
797
798 void gr_opengl_set_clear_color(int r, int g, int b)
799 {
800         STUB_FUNCTION;
801 }
802
803 void gr_opengl_aabitmap(int x, int y)
804 {
805         STUB_FUNCTION;
806 }
807
808 void gr_opengl_aabitmap_ex(int x,int y,int w,int h,int sx,int sy)
809 {
810         STUB_FUNCTION;
811 }
812
813 void gr_opengl_aaline(vertex *v1, vertex *v2)
814 {
815         STUB_FUNCTION;
816 }
817
818 void gr_opengl_init_alphacolor( color *clr, int r, int g, int b, int alpha, int type )
819 {
820         STUB_FUNCTION;
821 }
822
823 void gr_opengl_flash(int r, int g, int b)
824 {
825         STUB_FUNCTION;
826 }
827
828 int gr_opengl_zbuffer_get()
829 {
830         STUB_FUNCTION;
831         
832         return GR_ZBUFF_NONE;
833 }
834
835 int gr_opengl_zbuffer_set(int mode)
836 {
837         STUB_FUNCTION;
838         
839         return GR_ZBUFF_NONE;
840 }
841
842 void gr_opengl_zbuffer_clear(int mode)
843 {
844         STUB_FUNCTION;
845 }
846
847 void gr_opengl_set_gamma(float gamma)
848 {
849         STUB_FUNCTION;
850 }
851
852 void gr_opengl_fade_in(int instantaneous)
853 {
854         STUB_FUNCTION;
855 }
856
857 void gr_opengl_fade_out(int instantaneous)
858 {
859         STUB_FUNCTION;
860 }
861
862 int gr_opengl_save_screen()
863 {
864         STUB_FUNCTION;
865         
866         return -1;
867 }
868
869 void gr_opengl_restore_screen(int id)
870 {
871         STUB_FUNCTION;
872 }
873
874 void gr_opengl_free_screen(int id)
875 {
876         STUB_FUNCTION;
877 }
878
879 void gr_opengl_dump_frame_start(int first_frame, int frames_between_dumps)
880 {
881         STUB_FUNCTION;
882 }
883
884 void gr_opengl_dump_frame_stop()
885 {
886         STUB_FUNCTION;
887 }
888
889 void gr_opengl_dump_frame()
890 {
891         STUB_FUNCTION;
892 }
893
894 uint gr_opengl_lock()
895 {
896         STUB_FUNCTION;
897         
898         return 1;
899 }
900         
901 void gr_opengl_unlock()
902 {
903 }
904         
905 void gr_opengl_init()
906 {
907         if ( Inited )   {
908                 gr_opengl_cleanup();
909                 Inited = 0;
910         }
911
912         mprintf(( "Initializing opengl graphics device...\n" ));
913         Inited = 1;
914
915 #ifdef PLAT_UNIX
916         if (SDL_InitSubSystem (SDL_INIT_VIDEO) < 0)
917         {
918                 fprintf (stderr, "Couldn't init SDL: %s", SDL_GetError());
919                 exit (1);
920         }
921
922         atexit (SDL_Quit);
923
924         SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
925         SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
926         SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
927         SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
928         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
929                                                 
930         if (SDL_SetVideoMode (640, 480, 0, SDL_OPENGL) == NULL)
931         {
932                 fprintf (stderr, "Couldn't set video mode: %s", SDL_GetError ());
933                 exit (1);
934         }
935 #endif
936         int bpp = 16;
937         
938         switch( bpp )   {
939         case 8:
940                 Gr_red.bits = 8;
941                 Gr_red.shift = 16;
942                 Gr_red.scale = 1;
943                 Gr_red.mask = 0xff0000;
944
945                 Gr_green.bits = 8;
946                 Gr_green.shift = 8;
947                 Gr_green.scale = 1;
948                 Gr_green.mask = 0xff00;
949
950                 Gr_blue.bits = 8;
951                 Gr_blue.shift = 0;
952                 Gr_blue.scale = 1;
953                 Gr_blue.mask = 0xff;
954
955         case 15:
956                 Gr_red.bits = 5;
957                 Gr_red.shift = 10;
958                 Gr_red.scale = 8;
959                 Gr_red.mask = 0x7C00;
960
961                 Gr_green.bits = 5;
962                 Gr_green.shift = 5;
963                 Gr_green.scale = 8;
964                 Gr_green.mask = 0x3E0;
965
966                 Gr_blue.bits = 5;
967                 Gr_blue.shift = 0;
968                 Gr_blue.scale = 8;
969                 Gr_blue.mask = 0x1F;
970
971                 break;
972
973         case 16:
974                 Gr_red.bits = 5;
975                 Gr_red.shift = 11;
976                 Gr_red.scale = 8;
977                 Gr_red.mask = 0xF800;
978
979                 Gr_green.bits = 6;
980                 Gr_green.shift = 5;
981                 Gr_green.scale = 4;
982                 Gr_green.mask = 0x7E0;
983
984                 Gr_blue.bits = 5;
985                 Gr_blue.shift = 0;
986                 Gr_blue.scale = 8;
987                 Gr_blue.mask = 0x1F;
988
989                 break;
990
991         case 24:
992         case 32:
993                 Gr_red.bits = 8;
994                 Gr_red.shift = 16;
995                 Gr_red.scale = 1;
996                 Gr_red.mask = 0xff0000;
997
998                 Gr_green.bits = 8;
999                 Gr_green.shift = 8;
1000                 Gr_green.scale = 1;
1001                 Gr_green.mask = 0xff00;
1002
1003                 Gr_blue.bits = 8;
1004                 Gr_blue.shift = 0;
1005                 Gr_blue.scale = 1;
1006                 Gr_blue.mask = 0xff;
1007
1008                 break;
1009
1010         default:
1011                 Int3(); // Illegal bpp
1012         }
1013
1014
1015         gr_opengl_clear();
1016
1017         Gr_current_red = &Gr_red;
1018         Gr_current_blue = &Gr_blue;
1019         Gr_current_green = &Gr_green;
1020         Gr_current_alpha = &Gr_alpha;
1021                                 
1022         gr_screen.gf_flip = gr_opengl_flip;
1023         gr_screen.gf_flip_window = gr_opengl_flip_window;
1024         gr_screen.gf_set_clip = gr_opengl_set_clip;
1025         gr_screen.gf_reset_clip = gr_opengl_reset_clip;
1026         gr_screen.gf_set_font = gr_opengl_set_font;
1027         
1028         gr_screen.gf_set_color = gr_opengl_set_color;
1029         gr_screen.gf_set_bitmap = gr_opengl_set_bitmap;
1030         gr_screen.gf_create_shader = gr_opengl_create_shader;
1031         gr_screen.gf_set_shader = gr_opengl_set_shader;
1032         gr_screen.gf_clear = gr_opengl_clear;
1033         // gr_screen.gf_bitmap = gr_opengl_bitmap;
1034         // gr_screen.gf_bitmap_ex = gr_opengl_bitmap_ex;
1035         gr_screen.gf_aabitmap = gr_opengl_aabitmap;
1036         gr_screen.gf_aabitmap_ex = gr_opengl_aabitmap_ex;
1037         
1038         gr_screen.gf_rect = gr_opengl_rect;
1039         gr_screen.gf_shade = gr_opengl_shade;
1040         gr_screen.gf_string = gr_opengl_string;
1041         gr_screen.gf_circle = gr_opengl_circle;
1042
1043         gr_screen.gf_line = gr_opengl_line;
1044         gr_screen.gf_aaline = gr_opengl_aaline;
1045         gr_screen.gf_pixel = gr_opengl_pixel;
1046         gr_screen.gf_scaler = gr_opengl_scaler;
1047         gr_screen.gf_tmapper = gr_opengl_tmapper;
1048
1049         gr_screen.gf_gradient = gr_opengl_gradient;
1050
1051         gr_screen.gf_set_palette = gr_opengl_set_palette;
1052         gr_screen.gf_get_color = gr_opengl_get_color;
1053         gr_screen.gf_init_color = gr_opengl_init_color;
1054         gr_screen.gf_init_alphacolor = gr_opengl_init_alphacolor;
1055         gr_screen.gf_set_color_fast = gr_opengl_set_color_fast;
1056         gr_screen.gf_print_screen = gr_opengl_print_screen;
1057
1058         gr_screen.gf_fade_in = gr_opengl_fade_in;
1059         gr_screen.gf_fade_out = gr_opengl_fade_out;
1060         gr_screen.gf_flash = gr_opengl_flash;
1061         
1062         gr_screen.gf_zbuffer_get = gr_opengl_zbuffer_get;
1063         gr_screen.gf_zbuffer_set = gr_opengl_zbuffer_set;
1064         gr_screen.gf_zbuffer_clear = gr_opengl_zbuffer_clear;
1065         
1066         gr_screen.gf_save_screen = gr_opengl_save_screen;
1067         gr_screen.gf_restore_screen = gr_opengl_restore_screen;
1068         gr_screen.gf_free_screen = gr_opengl_free_screen;
1069         
1070         gr_screen.gf_dump_frame_start = gr_opengl_dump_frame_start;
1071         gr_screen.gf_dump_frame_stop = gr_opengl_dump_frame_stop;
1072         gr_screen.gf_dump_frame = gr_opengl_dump_frame;
1073         
1074         gr_screen.gf_set_gamma = gr_opengl_set_gamma;
1075         
1076         gr_screen.gf_lock = gr_opengl_lock;
1077         gr_screen.gf_unlock = gr_opengl_unlock;
1078         
1079         gr_screen.gf_fog_set = gr_opengl_fog_set;       
1080
1081         gr_screen.gf_get_region = gr_opengl_get_region;
1082
1083         gr_screen.gf_get_pixel = gr_opengl_get_pixel;
1084
1085         gr_screen.gf_set_cull = gr_opengl_set_cull;
1086
1087         gr_screen.gf_cross_fade = gr_opengl_cross_fade;
1088
1089         gr_screen.gf_filter_set = gr_opengl_filter_set;
1090
1091         gr_screen.gf_tcache_set = gr_opengl_tcache_set;
1092
1093         gr_screen.gf_set_clear_color = gr_opengl_set_clear_color;
1094 }
1095