]> icculus.org git repositories - taylor/freespace2.git/blob - src/graphics/gropengl.cpp
Enable string printing. Enable texture mapping
[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.15  2002/05/29 19:06:48  theoddone33
11  * Enable string printing.  Enable texture mapping
12  *
13  * Revision 1.14  2002/05/29 08:54:40  relnev
14  * "fixed" bitmap drawing.
15  *
16  * copied more d3d code over.
17  *
18  * Revision 1.13  2002/05/29 06:25:13  theoddone33
19  * Keyboard input, mouse tracking now work
20  *
21  * Revision 1.12  2002/05/29 04:52:45  relnev
22  * bitmap
23  *
24  * Revision 1.11  2002/05/29 04:29:56  relnev
25  * removed some unncessary stubbing, implemented opengl rect
26  *
27  * Revision 1.10  2002/05/29 04:13:27  theoddone33
28  * enable opengl_line
29  *
30  * Revision 1.9  2002/05/29 03:35:51  relnev
31  * added rest of init
32  *
33  * Revision 1.8  2002/05/29 03:30:05  relnev
34  * update opengl stubs
35  *
36  * Revision 1.7  2002/05/29 02:52:32  theoddone33
37  * Enable OpenGL renderer
38  *
39  * Revision 1.6  2002/05/28 04:56:51  theoddone33
40  * runs a little bit now
41  *
42  * Revision 1.5  2002/05/28 04:07:28  theoddone33
43  * New graphics stubbing arrangement
44  *
45  * Revision 1.4  2002/05/27 23:39:34  relnev
46  * 0
47  *
48  * Revision 1.3  2002/05/27 22:35:01  theoddone33
49  * more symbols
50  *
51  * Revision 1.2  2002/05/27 22:32:02  theoddone33
52  * throw all d3d stuff at opengl
53  *
54  * Revision 1.1.1.1  2002/05/03 03:28:09  root
55  * Initial import.
56  *
57  * 
58  * 10    7/14/99 9:42a Dave
59  * Put in clear_color debug function. Put in base for 3dnow stuff / P3
60  * stuff
61  * 
62  * 9     7/09/99 9:51a Dave
63  * Added thick polyline code.
64  * 
65  * 8     6/29/99 10:35a Dave
66  * Interface polygon bitmaps! Whee!
67  * 
68  * 7     2/03/99 11:44a Dave
69  * Fixed d3d transparent textures.
70  * 
71  * 6     1/24/99 11:37p Dave
72  * First full rev of beam weapons. Very customizable. Removed some bogus
73  * Int3()'s in low level net code.
74  * 
75  * 5     12/18/98 1:13a Dave
76  * Rough 1024x768 support for Direct3D. Proper detection and usage through
77  * the launcher.
78  * 
79  * 4     12/06/98 2:36p Dave
80  * Drastically improved nebula fogging.
81  * 
82  * 3     11/11/98 5:37p Dave
83  * Checkin for multiplayer testing.
84  * 
85  * 2     10/07/98 10:53a Dave
86  * Initial checkin.
87  * 
88  * 1     10/07/98 10:49a Dave
89  * 
90  * 14    5/20/98 9:46p John
91  * added code so the places in code that change half the palette don't
92  * have to clear the screen.
93  * 
94  * 13    5/06/98 5:30p John
95  * Removed unused cfilearchiver.  Removed/replaced some unused/little used
96  * graphics functions, namely gradient_h and _v and pixel_sp.   Put in new
97  * DirectX header files and libs that fixed the Direct3D alpha blending
98  * problems.
99  * 
100  * 12    4/14/98 12:15p John
101  * Made 16-bpp movies work.
102  * 
103  * 11    3/12/98 5:36p John
104  * Took out any unused shaders.  Made shader code take rgbc instead of
105  * matrix and vector since noone used it like a matrix and it would have
106  * been impossible to do in hardware.   Made Glide implement a basic
107  * shader for online help.  
108  * 
109  * 10    3/10/98 4:18p John
110  * Cleaned up graphics lib.  Took out most unused gr functions.   Made D3D
111  * & Glide have popups and print screen.  Took out all >8bpp software
112  * support.  Made Fred zbuffer.  Made zbuffer allocate dynamically to
113  * support Fred.  Made zbuffering key off of functions rather than one
114  * global variable.
115  * 
116  * 9     12/02/97 4:00p John
117  * Added first rev of thruster glow, along with variable levels of
118  * translucency, which retquired some restructing of palman.
119  * 
120  * 8     10/03/97 9:10a John
121  * added better antialiased line drawer
122  * 
123  * 7     9/23/97 10:45a John
124  * made so you can tell bitblt code to rle a bitmap by passing flag to
125  * gr_set_bitmap
126  * 
127  * 6     9/09/97 11:01a Sandeep
128  * fixed warning level 4 bugs
129  * 
130  * 5     7/10/97 2:06p John
131  * added code to specify alphablending type for bitmaps.
132  * 
133  * 4     6/17/97 7:04p John
134  * added d3d support for gradients.
135  * fixed some color bugs by adding screen signatures instead of watching
136  * flags and palette changes.
137  * 
138  * 3     6/12/97 2:50a Lawrance
139  * bm_unlock() now passed bitmap number, not pointer
140  * 
141  * 2     6/11/97 1:12p John
142  * Started fixing all the text colors in the game.
143  * 
144  * 1     5/12/97 12:14p John
145  *
146  * $NoKeywords: $
147  */
148
149 #ifndef PLAT_UNIX
150 #include <windows.h>
151 #include <windowsx.h>
152 #endif
153 #include <GL/gl.h>
154
155 #include "osapi.h"
156 #include "2d.h"
157 #include "bmpman.h"
158 #include "floating.h"
159 #include "palman.h"
160 #include "grinternal.h"
161 #include "gropengl.h"
162 #include "line.h"
163
164 static int Inited = 0;
165
166 static GLuint bitmapTex;
167 static GLubyte *bitmapMem;
168
169 #ifdef PLAT_UNIX
170 // Throw in some dummy functions - DDOI
171
172 int D3D_32bit = 0;              // grd3d.cpp
173 int D3D_fog_mode = -1;          // grd3d.cpp
174 int D3D_inited = 0;             // grd3d.cpp
175 int D3D_zbias = 1;              // grd3d.cpp
176 int D3d_rendition_uvs = 0;      // grd3d.cpp
177
178 void gr_dd_activate(int active)         // grdirectdraw.cpp
179 {
180         STUB_FUNCTION;
181 }
182
183 void gr_directdraw_cleanup()            // grdirectdraw.cpp
184 {
185         STUB_FUNCTION;
186 }
187
188 void gr_directdraw_force_windowed()     // grdirectdraw.cpp
189 {
190         STUB_FUNCTION;
191 }
192
193 void gr_directdraw_init()
194 {
195         STUB_FUNCTION;
196 }
197
198
199 void gr_d3d_preload(int x, int y)
200 {
201         STUB_FUNCTION;
202 }
203
204 void d3d_start_frame()
205 {
206         STUB_FUNCTION;
207 }
208
209 void d3d_stop_frame()
210 {
211         STUB_FUNCTION;
212 }
213
214 void d3d_flush ()
215 {
216         STUB_FUNCTION;
217 }
218
219 void d3d_zbias (int a)
220 {
221         STUB_FUNCTION;
222 }
223 #endif
224
225 void gr_opengl_activate(int b)
226 {
227         STUB_FUNCTION;
228 }
229
230 void gr_opengl_preload_init()
231 {
232         STUB_FUNCTION;
233 }
234
235 void gr_opengl_pixel(int x, int y)
236 {
237         if ( x < gr_screen.clip_left ) return;
238         if ( x > gr_screen.clip_right ) return;
239         if ( y < gr_screen.clip_top ) return;
240         if ( y > gr_screen.clip_bottom ) return;
241 }
242
243 void gr_opengl_clear()
244 {
245         glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
246 }
247
248 void opengl_tcache_frame ();
249 void gr_opengl_flip()
250 {
251         if (!Inited) return;
252
253         SDL_GL_SwapBuffers ();
254
255         opengl_tcache_frame ();
256 }
257
258 void gr_opengl_flip_window(uint _hdc, int x, int y, int w, int h )
259 {
260         STUB_FUNCTION;
261 }
262
263 void gr_opengl_set_clip(int x,int y,int w,int h)
264 {
265         // check for sanity of parameters
266         if (x < 0)
267                 x = 0;
268         if (y < 0)
269                 y = 0;
270
271         if (x >= gr_screen.max_w)
272                 x = gr_screen.max_w - 1;
273         if (y >= gr_screen.max_h)
274                 y = gr_screen.max_h - 1;
275
276         if (x + w > gr_screen.max_w)
277                 w = gr_screen.max_w - x;
278         if (y + h > gr_screen.max_h)
279                 h = gr_screen.max_h - y;
280         
281         if (w > gr_screen.max_w)
282                 w = gr_screen.max_w;
283         if (h > gr_screen.max_h)
284                 h = gr_screen.max_h;
285         
286         gr_screen.offset_x = x;
287         gr_screen.offset_y = y;
288         gr_screen.clip_left = 0;
289         gr_screen.clip_right = w-1;
290         gr_screen.clip_top = 0;
291         gr_screen.clip_bottom = h-1;
292         gr_screen.clip_width = w;
293         gr_screen.clip_height = h;
294         
295         STUB_FUNCTION;
296 }
297
298 void gr_opengl_reset_clip()
299 {
300         gr_screen.offset_x = 0;
301         gr_screen.offset_y = 0;
302         gr_screen.clip_left = 0;
303         gr_screen.clip_top = 0;
304         gr_screen.clip_right = gr_screen.max_w - 1;
305         gr_screen.clip_bottom = gr_screen.max_h - 1;
306         gr_screen.clip_width = gr_screen.max_w;
307         gr_screen.clip_height = gr_screen.max_h;
308 }
309
310 void gr_opengl_set_bitmap( int bitmap_num, int alphablend_mode, int bitblt_mode, float alpha, int sx, int sy )
311 {
312         gr_screen.current_alpha = alpha;
313         gr_screen.current_alphablend_mode = alphablend_mode;
314         gr_screen.current_bitblt_mode = bitblt_mode;
315         gr_screen.current_bitmap = bitmap_num;
316
317         gr_screen.current_bitmap_sx = sx;
318         gr_screen.current_bitmap_sy = sy;
319 }
320
321 void gr_opengl_create_shader(shader * shade, float r, float g, float b, float c )
322 {
323         shade->screen_sig = gr_screen.signature;
324         shade->r = r;
325         shade->g = g;
326         shade->b = b;
327         shade->c = c;   
328 }
329
330 void gr_opengl_set_shader( shader * shade )
331 {       
332         if ( shade )    {
333                 if (shade->screen_sig != gr_screen.signature)   {
334                         gr_create_shader( shade, shade->r, shade->g, shade->b, shade->c );
335                 }
336                 gr_screen.current_shader = *shade;
337         } else {
338                 gr_create_shader( &gr_screen.current_shader, 0.0f, 0.0f, 0.0f, 0.0f );
339         }
340 }
341
342 void gr_opengl_bitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
343 {
344         bitmap * bmp;
345         
346         bmp = bm_lock( gr_screen.current_bitmap, 16, 0 );
347         
348         int ix, iy, iw, ih;
349         int px, py, qx, qy;
350         GLubyte *sptr, *dptr;
351         
352         float s, t;
353         
354         int cw = min(bmp->w, w);
355         int ch = min(bmp->h, h);
356
357         glColor4f(1.0, 1.0, 1.0, 1.0);  
358         glBindTexture(GL_TEXTURE_2D, bitmapTex);
359                 
360         py = y;
361         for (iy = sy; iy < ch; iy += 256) {
362                 px = x;
363                 ih = min(256, (ch-iy));
364                 qy = py+ih;
365                 for (ix = sx; ix < cw; ix += 256) {
366                         dptr = bitmapMem;
367                         sptr = ((unsigned char *)bmp->data) + 2*(iy*bmp->w + ix);
368                         
369                         iw = min(256, (cw-ix));
370                                 
371                         qx = px+iw;
372                         
373                         int ihx = ih;
374                         while (ihx > 0) {
375                                 memcpy(dptr, sptr, iw*2);
376                                 
377                                 sptr += 2*bmp->w;
378                                 dptr += 2*iw;
379                                 
380                                 ihx--;
381                         }                       
382                         
383                         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, iw, ih, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, bitmapMem);
384                         glBegin(GL_QUADS);
385                                 /* upper left */
386                                 s = 0.0;
387                                 t = 0.0;
388                                 glTexCoord2f(s, t);
389                                 glVertex2i(px, py);
390                                 
391                                 /* lower left */
392                                 s = 0.0;
393                                 t = (float)ih / 256.0;
394                                 glTexCoord2f(s, t);
395                                 glVertex2i(px, qy);
396                                 
397                                 /* lower right */
398                                 s = (float)iw / 256.0;
399                                 t = (float)ih / 256.0;
400                                 glTexCoord2f(s, t);
401                                 glVertex2i(qx, qy);
402                                 
403                                 /* upper left */
404                                 s = (float)iw / 256.0;
405                                 t = 0.0;
406                                 glTexCoord2f(s, t);
407                                 glVertex2i(qx, py);
408                         glEnd();
409                         
410                         px = qx;
411                 }
412                 
413                 py = qy;
414         }       
415 }
416
417
418 void gr_d3d_bitmap_ex(int x,int y,int w,int h,int sx,int sy)
419 {
420         int reclip;
421         #ifndef NDEBUG
422         int count = 0;
423         #endif
424
425         int dx1=x, dx2=x+w-1;
426         int dy1=y, dy2=y+h-1;
427
428         int bw, bh;
429         bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL );
430
431         do {
432                 reclip = 0;
433                 #ifndef NDEBUG
434                         if ( count > 1 ) Int3();
435                         count++;
436                 #endif
437         
438                 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
439                 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
440                 if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
441                 if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
442                 if ( dx2 > gr_screen.clip_right )       { dx2 = gr_screen.clip_right; }
443                 if ( dy2 > gr_screen.clip_bottom )      { dy2 = gr_screen.clip_bottom; }
444
445                 if ( sx < 0 ) {
446                         dx1 -= sx;
447                         sx = 0;
448                         reclip = 1;
449                 }
450
451                 if ( sy < 0 ) {
452                         dy1 -= sy;
453                         sy = 0;
454                         reclip = 1;
455                 }
456
457                 w = dx2-dx1+1;
458                 h = dy2-dy1+1;
459
460                 if ( sx + w > bw ) {
461                         w = bw - sx;
462                         dx2 = dx1 + w - 1;
463                 }
464
465                 if ( sy + h > bh ) {
466                         h = bh - sy;
467                         dy2 = dy1 + h - 1;
468                 }
469
470                 if ( w < 1 ) return;            // clipped away!
471                 if ( h < 1 ) return;            // clipped away!
472
473         } while (reclip);
474
475         // Make sure clipping algorithm works
476         #ifndef NDEBUG
477                 Assert( w > 0 );
478                 Assert( h > 0 );
479                 Assert( w == (dx2-dx1+1) );
480                 Assert( h == (dy2-dy1+1) );
481                 Assert( sx >= 0 );
482                 Assert( sy >= 0 );
483                 Assert( sx+w <= bw );
484                 Assert( sy+h <= bh );
485                 Assert( dx2 >= dx1 );
486                 Assert( dy2 >= dy1 );
487                 Assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) );
488                 Assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) );
489                 Assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) );
490                 Assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) );
491         #endif
492
493         // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions.
494         // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
495
496         gr_opengl_bitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
497 }
498
499 void gr_opengl_bitmap(int x, int y)
500 {
501         int w, h;
502
503         bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
504         int dx1=x, dx2=x+w-1;
505         int dy1=y, dy2=y+h-1;
506         int sx=0, sy=0;
507
508         if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
509         if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
510         if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
511         if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
512         if ( dx2 > gr_screen.clip_right )       { dx2 = gr_screen.clip_right; }
513         if ( dy2 > gr_screen.clip_bottom )      { dy2 = gr_screen.clip_bottom; }
514
515         if ( sx < 0 ) return;
516         if ( sy < 0 ) return;
517         if ( sx >= w ) return;
518         if ( sy >= h ) return;
519
520         // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
521
522         gr_opengl_bitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);        
523 }
524
525 static void opengl_scanline(int x1,int x2,int y)
526 {
527         STUB_FUNCTION;
528 }
529
530 static void gr_opengl_rect_internal(int x, int y, int w, int h, int r, int g, int b, int a)
531 {
532         int saved_zbuf;
533
534         saved_zbuf = gr_zbuffer_get();
535         gr_zbuffer_set(GR_ZBUFF_NONE);
536         gr_set_cull(0);
537         
538         glColor4ub(r, g, b, a);
539         glBegin(GL_QUADS);
540                 /* upper left */
541                 glVertex2f(x, y);
542                 
543                 /* lower left */
544                 glVertex2f(x, y+x);
545         
546                 /* lower right */
547                 glVertex2f(x+w, y+h);
548                 
549                 /* upper right */
550                 glVertex2f(x+w, y);
551         glEnd();
552         
553         gr_zbuffer_set(saved_zbuf);
554         gr_set_cull(1);
555 }
556
557 void gr_opengl_rect(int x,int y,int w,int h)
558 {
559         gr_opengl_rect_internal(x, y, w, h, gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
560 }
561
562 void gr_opengl_shade(int x,int y,int w,int h)
563 {
564         int r,g,b,a;
565         
566         float shade1 = 1.0f;
567         float shade2 = 6.0f;
568
569         r = fl2i(gr_screen.current_shader.r*255.0f*shade1);
570         if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
571         g = fl2i(gr_screen.current_shader.g*255.0f*shade1);
572         if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
573         b = fl2i(gr_screen.current_shader.b*255.0f*shade1);
574         if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
575         a = fl2i(gr_screen.current_shader.c*255.0f*shade2);
576         if ( a < 0 ) a = 0; else if ( a > 255 ) a = 255;
577
578         gr_opengl_rect_internal(x, y, w, h, r, g, b, a);        
579 }
580
581 void gr_opengl_aabitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
582 {
583         if ( w < 1 ) return;
584         if ( h < 1 ) return;
585
586         if ( !gr_screen.current_color.is_alphacolor )   return;
587
588         float u_scale, v_scale;
589
590 //      gr_d3d_set_state( TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
591
592         if ( !gr_tcache_set( gr_screen.current_bitmap, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale ) )     {
593                 // Couldn't set texture
594                 //mprintf(( "GLIDE: Error setting aabitmap texture!\n" ));
595                 return;
596         }
597
598 //      LPD3DTLVERTEX src_v;
599 //      D3DTLVERTEX d3d_verts[4];
600
601         float u0, u1, v0, v1;
602         float x1, x2, y1, y2;
603         int bw, bh;
604
605         bm_get_info( gr_screen.current_bitmap, &bw, &bh );
606
607         u0 = u_scale*i2fl(sx)/i2fl(bw);
608         v0 = v_scale*i2fl(sy)/i2fl(bh);
609
610         u1 = u_scale*i2fl(sx+w)/i2fl(bw);
611         v1 = v_scale*i2fl(sy+h)/i2fl(bh);
612
613         x1 = i2fl(x+gr_screen.offset_x);
614         y1 = i2fl(y+gr_screen.offset_y);
615         x2 = i2fl(x+w+gr_screen.offset_x);
616         y2 = i2fl(y+h+gr_screen.offset_y);
617
618         if ( gr_screen.current_color.is_alphacolor )    {
619                         glColor4ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue,gr_screen.current_color.alpha);
620         } else {
621                 glColor3ub(gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue);
622         }
623
624         glBegin (GL_TRIANGLE_FAN);
625           glTexCoord2f (u0, v0);
626           glVertex3f (x1, y1, 0.99);
627
628           glTexCoord2f (u1, v0);
629           glVertex3f (x2, y1, 0.99);
630
631           glTexCoord2f (u1, v1);
632           glVertex3f (x2, y2, 0.99);
633
634           glTexCoord2f (u0, v1);
635           glVertex3f (x1, y2, 0.99);
636         glEnd ();
637 }
638
639 void gr_opengl_aabitmap_ex(int x,int y,int w,int h,int sx,int sy)
640 {
641         int reclip;
642         #ifndef NDEBUG
643         int count = 0;
644         #endif
645
646         int dx1=x, dx2=x+w-1;
647         int dy1=y, dy2=y+h-1;
648
649         int bw, bh;
650         bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL );
651
652         do {
653                 reclip = 0;
654                 #ifndef NDEBUG
655                         if ( count > 1 ) Int3();
656                         count++;
657                 #endif
658         
659                 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
660                 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
661                 if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
662                 if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
663                 if ( dx2 > gr_screen.clip_right )       { dx2 = gr_screen.clip_right; }
664                 if ( dy2 > gr_screen.clip_bottom )      { dy2 = gr_screen.clip_bottom; }
665
666                 if ( sx < 0 ) {
667                         dx1 -= sx;
668                         sx = 0;
669                         reclip = 1;
670                 }
671
672                 if ( sy < 0 ) {
673                         dy1 -= sy;
674                         sy = 0;
675                         reclip = 1;
676                 }
677
678                 w = dx2-dx1+1;
679                 h = dy2-dy1+1;
680
681                 if ( sx + w > bw ) {
682                         w = bw - sx;
683                         dx2 = dx1 + w - 1;
684                 }
685
686                 if ( sy + h > bh ) {
687                         h = bh - sy;
688                         dy2 = dy1 + h - 1;
689                 }
690
691                 if ( w < 1 ) return;            // clipped away!
692                 if ( h < 1 ) return;            // clipped away!
693
694         } while (reclip);
695
696         // Make sure clipping algorithm works
697         #ifndef NDEBUG
698                 Assert( w > 0 );
699                 Assert( h > 0 );
700                 Assert( w == (dx2-dx1+1) );
701                 Assert( h == (dy2-dy1+1) );
702                 Assert( sx >= 0 );
703                 Assert( sy >= 0 );
704                 Assert( sx+w <= bw );
705                 Assert( sy+h <= bh );
706                 Assert( dx2 >= dx1 );
707                 Assert( dy2 >= dy1 );
708                 Assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) );
709                 Assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) );
710                 Assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) );
711                 Assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) );
712         #endif
713
714         // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions.
715         gr_opengl_aabitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
716 }
717
718 void gr_opengl_aabitmap(int x, int y)
719 {
720         int w, h;
721
722         bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
723         int dx1=x, dx2=x+w-1;
724         int dy1=y, dy2=y+h-1;
725         int sx=0, sy=0;
726
727         if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
728         if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
729         if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
730         if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
731         if ( dx2 > gr_screen.clip_right )       { dx2 = gr_screen.clip_right; }
732         if ( dy2 > gr_screen.clip_bottom )      { dy2 = gr_screen.clip_bottom; }
733
734         if ( sx < 0 ) return;
735         if ( sy < 0 ) return;
736         if ( sx >= w ) return;
737         if ( sy >= h ) return;
738
739         // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
740         gr_aabitmap_ex(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
741 }
742
743
744 void gr_opengl_string( int sx, int sy, char *s )
745 {
746         int width, spacing, letter;
747         int x, y;
748
749         if ( !Current_font )    {
750                 return;
751         }
752
753         gr_set_bitmap(Current_font->bitmap_id);
754
755         x = sx;
756         y = sy;
757
758         if (sx==0x8000) {                       //centered
759                 x = get_centered_x(s);
760         } else {
761                 x = sx;
762         }
763         
764         spacing = 0;
765
766         while (*s)      {
767                 x += spacing;
768
769                 while (*s== '\n' )      {
770                         s++;
771                         y += Current_font->h;
772                         if (sx==0x8000) {                       //centered
773                                 x = get_centered_x(s);
774                         } else {
775                                 x = sx;
776                         }
777                 }
778                 if (*s == 0 ) break;
779
780                 letter = get_char_width(s[0],s[1],&width,&spacing);
781                 s++;
782
783                 //not in font, draw as space
784                 if (letter<0)   {
785                         continue;
786                 }
787
788                 int xd, yd, xc, yc;
789                 int wc, hc;
790
791                 // Check if this character is totally clipped
792                 if ( x + width < gr_screen.clip_left ) continue;
793                 if ( y + Current_font->h < gr_screen.clip_top ) continue;
794                 if ( x > gr_screen.clip_right ) continue;
795                 if ( y > gr_screen.clip_bottom ) continue;
796
797                 xd = yd = 0;
798                 if ( x < gr_screen.clip_left ) xd = gr_screen.clip_left - x;
799                 if ( y < gr_screen.clip_top ) yd = gr_screen.clip_top - y;
800                 xc = x+xd;
801                 yc = y+yd;
802
803                 wc = width - xd; hc = Current_font->h - yd;
804                 if ( xc + wc > gr_screen.clip_right ) wc = gr_screen.clip_right - xc;
805                 if ( yc + hc > gr_screen.clip_bottom ) hc = gr_screen.clip_bottom - yc;
806
807                 if ( wc < 1 ) continue;
808                 if ( hc < 1 ) continue;
809
810                 font_char *ch;
811         
812                 ch = &Current_font->char_data[letter];
813
814                 int u = Current_font->bm_u[letter];
815                 int v = Current_font->bm_v[letter];
816
817                 gr_opengl_aabitmap_ex_internal( xc, yc, wc, hc, u+xd, v+yd );
818         }
819 }
820
821 void gr_opengl_circle( int xc, int yc, int d )
822 {
823         int p,x, y, r;
824
825         r = d/2;
826         p=3-d;
827         x=0;
828         y=r;
829
830         // Big clip
831         if ( (xc+r) < gr_screen.clip_left ) return;
832         if ( (xc-r) > gr_screen.clip_right ) return;
833         if ( (yc+r) < gr_screen.clip_top ) return;
834         if ( (yc-r) > gr_screen.clip_bottom ) return;
835
836         while(x<y)      {
837                 // Draw the first octant
838                 opengl_scanline( xc-y, xc+y, yc-x );
839                 opengl_scanline( xc-y, xc+y, yc+x );
840
841                 if (p<0) 
842                         p=p+(x<<2)+6;
843                 else    {
844                         // Draw the second octant
845                         opengl_scanline( xc-x, xc+x, yc-y );
846                         opengl_scanline( xc-x, xc+x, yc+y );
847                         p=p+((x-y)<<2)+10;
848                         y--;
849                 }
850                 x++;
851         }
852         if(x==y)        {
853                 opengl_scanline( xc-x, xc+x, yc-y );
854                 opengl_scanline( xc-x, xc+x, yc+y );
855         }
856         return;
857 }
858
859
860 void gr_opengl_line(int x1,int y1,int x2,int y2)
861 {
862         int clipped = 0, swapped=0;
863
864         glDisable ( GL_DEPTH_TEST );
865         glEnable ( GL_BLEND );
866         glBlendFunc ( GL_SRC_ALPHA, GL_DST_ALPHA );
867
868         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);
869                 
870         glBegin (GL_LINE);
871           glColor4f (gr_screen.current_color.red, gr_screen.current_color.green, gr_screen.current_color.blue, gr_screen.current_color.alpha);
872           glVertex3f (i2fl (x1+gr_screen.offset_x),i2fl (y1+gr_screen.offset_y), 0.99f);
873           glVertex3f (i2fl (x2+gr_screen.offset_x),i2fl (y2+gr_screen.offset_y), 0.99f);
874         glEnd ();
875 }
876
877 #define FIND_SCALED_NUM(x,x0,x1,y0,y1) (((((x)-(x0))*((y1)-(y0)))/((x1)-(x0)))+(y0))
878
879 void gr_opengl_scaler(vertex *va, vertex *vb )
880 {
881         float x0, y0, x1, y1;
882         float u0, v0, u1, v1;
883         float clipped_x0, clipped_y0, clipped_x1, clipped_y1;
884         float clipped_u0, clipped_v0, clipped_u1, clipped_v1;
885         float xmin, xmax, ymin, ymax;
886         int dx0, dy0, dx1, dy1;
887
888         //============= CLIP IT =====================
889
890         x0 = va->sx; y0 = va->sy;
891         x1 = vb->sx; y1 = vb->sy;
892
893         xmin = i2fl(gr_screen.clip_left); ymin = i2fl(gr_screen.clip_top);
894         xmax = i2fl(gr_screen.clip_right); ymax = i2fl(gr_screen.clip_bottom);
895
896         u0 = va->u; v0 = va->v;
897         u1 = vb->u; v1 = vb->v;
898
899         // Check for obviously offscreen bitmaps...
900         if ( (y1<=y0) || (x1<=x0) ) return;
901         if ( (x1<xmin ) || (x0>xmax) ) return;
902         if ( (y1<ymin ) || (y0>ymax) ) return;
903
904         clipped_u0 = u0; clipped_v0 = v0;
905         clipped_u1 = u1; clipped_v1 = v1;
906
907         clipped_x0 = x0; clipped_y0 = y0;
908         clipped_x1 = x1; clipped_y1 = y1;
909
910         // Clip the left, moving u0 right as necessary
911         if ( x0 < xmin )        {
912                 clipped_u0 = FIND_SCALED_NUM(xmin,x0,x1,u0,u1);
913                 clipped_x0 = xmin;
914         }
915
916         // Clip the right, moving u1 left as necessary
917         if ( x1 > xmax )        {
918                 clipped_u1 = FIND_SCALED_NUM(xmax,x0,x1,u0,u1);
919                 clipped_x1 = xmax;
920         }
921
922         // Clip the top, moving v0 down as necessary
923         if ( y0 < ymin )        {
924                 clipped_v0 = FIND_SCALED_NUM(ymin,y0,y1,v0,v1);
925                 clipped_y0 = ymin;
926         }
927
928         // Clip the bottom, moving v1 up as necessary
929         if ( y1 > ymax )        {
930                 clipped_v1 = FIND_SCALED_NUM(ymax,y0,y1,v0,v1);
931                 clipped_y1 = ymax;
932         }
933         
934         dx0 = fl2i(clipped_x0); dx1 = fl2i(clipped_x1);
935         dy0 = fl2i(clipped_y0); dy1 = fl2i(clipped_y1);
936
937         if (dx1<=dx0) return;
938         if (dy1<=dy0) return;
939
940         //============= DRAW IT =====================
941         int u, v, du, dv;
942         int y, w;
943         ubyte * sbits;
944         bitmap * bp;
945         ubyte * spixels;
946         float tmpu, tmpv;
947
948         tmpu = (clipped_u1-clipped_u0) / (dx1-dx0);
949         if ( fl_abs(tmpu) < 0.001f ) {
950                 return;         // scaled up way too far!
951         }
952         tmpv = (clipped_v1-clipped_v0) / (dy1-dy0);
953         if ( fl_abs(tmpv) < 0.001f ) {
954                 return;         // scaled up way too far!
955         }
956
957         bp = bm_lock( gr_screen.current_bitmap, 8, 0 );
958
959         du = fl2f(tmpu*(bp->w-1));
960         dv = fl2f(tmpv*(bp->h-1));
961
962         v = fl2f(clipped_v0*(bp->h-1));
963         u = fl2f(clipped_u0*(bp->w-1)); 
964         w = dx1 - dx0 + 1;
965
966         spixels = (ubyte *)bp->data;
967
968         for (y=dy0; y<=dy1; y++ )                       {
969                 sbits = &spixels[bp->rowsize*(v>>16)];
970
971                 int x, tmp_u;
972                 tmp_u = u;
973                 for (x=0; x<w; x++ )                    {
974                         ubyte c = sbits[ tmp_u >> 16 ];
975                         if ( c != 255 ) {
976                                 gr_set_color( gr_palette[c*3+0], gr_palette[c*3+1], gr_palette[c*3+2] );
977                                 gr_pixel( x+dx0, y );
978                         }
979                         tmp_u += du;
980                 }
981                 v += dv;
982         }
983
984         bm_unlock(gr_screen.current_bitmap);
985
986         STUB_FUNCTION;
987 }
988
989 void gr_opengl_tmapper( int nv, vertex * verts[], uint flags )
990 {
991         int i;
992         float u_scale = 1.0f, v_scale = 1.0f;
993         int bw = 1, bh = 1;
994
995         // Make nebula use the texture mapper... this blends the colors better.
996         if ( flags & TMAP_FLAG_NEBULA ){
997                 Int3 ();
998         }
999
1000         /*
1001         gr_texture_source texture_source = (gr_texture_source)-1;
1002         gr_alpha_blend alpha_blend = (gr_alpha_blend)-1;
1003         gr_zbuffer_type zbuffer_type = (gr_zbuffer_type)-1;
1004         
1005         if ( gr_zbuffering )    {
1006                 if ( is_scaler || (gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER)   )       {
1007                         zbuffer_type = ZBUFFER_TYPE_READ;
1008                 } else {
1009                         zbuffer_type = ZBUFFER_TYPE_FULL;
1010                 }
1011         } else {
1012                 zbuffer_type = ZBUFFER_TYPE_NONE;
1013         }
1014         */
1015
1016         int alpha;
1017
1018         int tmap_type = TCACHE_TYPE_NORMAL;
1019
1020         int r, g, b;
1021
1022         if ( flags & TMAP_FLAG_TEXTURED )       {
1023                 r = g = b = 255;
1024         } else {
1025                 r = gr_screen.current_color.red;
1026                 g = gr_screen.current_color.green;
1027                 b = gr_screen.current_color.blue;
1028         }
1029
1030         if ( gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER )        
1031         {
1032                 // Some blend function stuff here - DDOI
1033                 STUB_FUNCTION;
1034
1035                 float factor = gr_screen.current_alpha;
1036
1037                 alpha = 255;
1038
1039                 if ( factor <= 1.0f )   {
1040                         int tmp_alpha = fl2i(gr_screen.current_alpha*255.0f);
1041                         r = (r*tmp_alpha)/255;
1042                         g = (g*tmp_alpha)/255;
1043                         b = (b*tmp_alpha)/255;
1044                 }
1045         } else {
1046                 STUB_FUNCTION;
1047                 alpha = 255;
1048         }
1049
1050         if ( flags & TMAP_FLAG_TEXTURED )       {
1051                 if ( !gr_tcache_set(gr_screen.current_bitmap, tmap_type, &u_scale, &v_scale, 0, gr_screen.current_bitmap_sx, gr_screen.current_bitmap_sy ))
1052                 {
1053                         mprintf(( "Not rendering a texture because it didn't fit in VRAM!\n" ));
1054                         return;
1055                 }
1056
1057                 // use nonfiltered textures for bitmap sections
1058                 /*
1059                 if(flags & TMAP_FLAG_BITMAP_SECTION){
1060                         texture_source = TEXTURE_SOURCE_NO_FILTERING;
1061                 } else {
1062                         texture_source = TEXTURE_SOURCE_DECAL;
1063                 }
1064                 */
1065         }
1066
1067         int x1, y1, x2, y2;
1068         x1 = gr_screen.clip_left*16;
1069         x2 = gr_screen.clip_right*16+15;
1070         y1 = gr_screen.clip_top*16;
1071         y2 = gr_screen.clip_bottom*16+15;
1072
1073         float uoffset = 0.0f;
1074         float voffset = 0.0f;
1075
1076         float minu=0.0f, minv=0.0f, maxu=1.0f, maxv=1.0f;
1077
1078         if ( flags & TMAP_FLAG_TEXTURED )
1079         {
1080                 STUB_FUNCTION;
1081         }
1082
1083         int a;
1084
1085         STUB_FUNCTION;  // DDOI - this still needs work
1086         glBegin (GL_TRIANGLE_FAN);
1087         for (i=0;i<nv;i++)
1088         {
1089                 if (flags & TMAP_FLAG_ALPHA) a = verts[i]->a;
1090                 else a = alpha;
1091
1092                 if (flags & TMAP_FLAG_NEBULA ) {
1093                         /*
1094                         int pal = (verts[i]->b*(NEBULA_COLORS-1))/255;
1095                         r = gr_palette[pal*3+0];
1096                         g = gr_palette[pal*3+1];
1097                         b = gr_palette[pal*3+2];
1098                         */
1099                 }else if ( (flags & TMAP_FLAG_RAMP) && (flags & TMAP_FLAG_GOURAUD) )   {
1100                         r = Gr_gamma_lookup[verts[i]->b];
1101                         g = Gr_gamma_lookup[verts[i]->b];
1102                         b = Gr_gamma_lookup[verts[i]->b];
1103                 } else if ( (flags & TMAP_FLAG_RGB)  && (flags & TMAP_FLAG_GOURAUD) )   {
1104                         // Make 0.75 be 256.0f
1105                         r = Gr_gamma_lookup[verts[i]->r];
1106                         g = Gr_gamma_lookup[verts[i]->g];
1107                         b = Gr_gamma_lookup[verts[i]->b];
1108                 } else {
1109                         // use constant RGB values...
1110                 }
1111
1112                 glColor4i (r,g,b,a);
1113
1114                 // DDOI - FIXME fog stuff
1115                 // DDOI - FIXME TexCoord stuff
1116                 glVertex3f (verts[i]->sx+gr_screen.offset_x,
1117                                 verts[i]->sy+gr_screen.offset_y,
1118                                 0.99f);
1119         }
1120 }
1121
1122
1123 void gr_opengl_gradient(int x1,int y1,int x2,int y2)
1124 {
1125         STUB_FUNCTION;
1126 }
1127
1128 void gr_opengl_set_palette(ubyte *new_palette, int is_alphacolor)
1129 {
1130         STUB_FUNCTION;
1131 }
1132
1133 void gr_opengl_get_color( int * r, int * g, int * b )
1134 {
1135         if (r) *r = gr_screen.current_color.red;
1136         if (g) *g = gr_screen.current_color.green;
1137         if (b) *b = gr_screen.current_color.blue;
1138 }
1139
1140 void gr_opengl_init_color(color *c, int r, int g, int b)
1141 {
1142         c->screen_sig = gr_screen.signature;
1143         c->red = (unsigned char)r;
1144         c->green = (unsigned char)g;
1145         c->blue = (unsigned char)b;
1146         c->alpha = 255;
1147         c->ac_type = AC_TYPE_NONE;
1148         c->alphacolor = -1;
1149         c->is_alphacolor = 0;
1150         c->magic = 0xAC01;
1151 }
1152
1153 void gr_opengl_init_alphacolor( color *clr, int r, int g, int b, int alpha, int type )
1154 {
1155         if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
1156         if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
1157         if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
1158         if ( alpha < 0 ) alpha = 0; else if ( alpha > 255 ) alpha = 255;
1159
1160         gr_opengl_init_color( clr, r, g, b );
1161
1162         clr->alpha = (unsigned char)alpha;
1163         clr->ac_type = (ubyte)type;
1164         clr->alphacolor = -1;
1165         clr->is_alphacolor = 1;
1166 }
1167
1168 void gr_opengl_set_color( int r, int g, int b )
1169 {
1170         Assert((r >= 0) && (r < 256));
1171         Assert((g >= 0) && (g < 256));
1172         Assert((b >= 0) && (b < 256));
1173
1174         gr_opengl_init_color( &gr_screen.current_color, r, g, b );      
1175 }
1176
1177 void gr_opengl_set_color_fast(color *dst)
1178 {
1179         if ( dst->screen_sig != gr_screen.signature )   {
1180                 if ( dst->is_alphacolor )       {
1181                         gr_opengl_init_alphacolor( dst, dst->red, dst->green, dst->blue, dst->alpha, dst->ac_type );
1182                 } else {
1183                         gr_opengl_init_color( dst, dst->red, dst->green, dst->blue );
1184                 }
1185         }
1186         gr_screen.current_color = *dst;
1187 }
1188
1189 void gr_opengl_print_screen(char *filename)
1190 {
1191         STUB_FUNCTION;
1192 }
1193
1194 int gr_opengl_supports_res_ingame(int res)
1195 {
1196         STUB_FUNCTION;
1197         
1198         return 1;
1199 }
1200
1201 int gr_opengl_supports_res_interface(int res)
1202 {
1203         STUB_FUNCTION;
1204         
1205         return 1;
1206 }
1207
1208 void opengl_tcache_cleanup ();
1209 void gr_opengl_cleanup()
1210 {
1211         if ( !Inited )  return;
1212
1213
1214         gr_reset_clip();
1215         gr_clear();
1216         gr_flip();
1217
1218         opengl_tcache_cleanup ();
1219
1220         Inited = 0;
1221 }
1222
1223 void gr_opengl_fog_set(int fog_mode, int r, int g, int b, float near, float far)
1224 {
1225         STUB_FUNCTION;
1226 }
1227
1228 void gr_opengl_get_pixel(int x, int y, int *r, int *g, int *b)
1229 {
1230         STUB_FUNCTION;
1231 }
1232
1233 void gr_opengl_get_region(int front, int w, int g, ubyte *data)
1234 {
1235         STUB_FUNCTION;
1236 }
1237
1238 void gr_opengl_set_cull(int cull)
1239 {
1240         STUB_FUNCTION;
1241 }
1242
1243 void gr_opengl_filter_set(int filter)
1244 {
1245         STUB_FUNCTION;
1246 }
1247
1248 // cross fade
1249 void gr_opengl_cross_fade(int bmap1, int bmap2, int x1, int y1, int x2, int y2, float pct)
1250 {
1251         STUB_FUNCTION;
1252 }
1253
1254
1255 typedef struct tcache_slot_opengl {
1256         GLuint  texture_handle;
1257         float   u_scale, v_scale;
1258         int     bitmap_id;
1259         int     size;
1260         char    used_this_frame;
1261         int     time_created;
1262         ushort  w,h;
1263
1264         // sections
1265         tcache_slot_opengl      *data_sections[MAX_BMAP_SECTIONS_X][MAX_BMAP_SECTIONS_Y];
1266         tcache_slot_opengl      *parent;
1267 } tcache_slot_opengl;
1268
1269 static void *Texture_sections = NULL;
1270 tcache_slot_opengl *Textures = NULL;
1271
1272 int GL_texture_sections = 0;
1273 int GL_texture_ram = 0;
1274 int GL_frame_count = 0;
1275 int GL_min_texture_width = 0;
1276 int GL_max_texture_width = 0;
1277 int GL_min_texture_height = 0;
1278 int GL_max_texture_height = 0;
1279 int GL_square_textures = 0;
1280 int GL_pow2_textures = 0;
1281 int GL_textures_in = 0;
1282 int GL_textures_in_frame = 0;
1283 int GL_last_bitmap_id = -1;
1284 int GL_last_detail = -1;
1285 int GL_last_bitmap_type = -1;
1286 int GL_last_section_x = -1;
1287 int GL_last_section_y = -1;
1288
1289 int vram_full = 0;
1290
1291 void opengl_tcache_init (int use_sections)
1292 {
1293         int i, idx, s_idx;
1294
1295         // DDOI - FIXME skipped a lot of stuff here
1296         STUB_FUNCTION;
1297
1298         GL_min_texture_width = 16;
1299         GL_min_texture_height = 16;
1300         GL_max_texture_width = 256;
1301         GL_max_texture_height = 256;
1302
1303         GL_square_textures = 1;
1304
1305         Textures = (tcache_slot_opengl *)malloc(MAX_BITMAPS*sizeof(tcache_slot_opengl));
1306         if ( !Textures )        {
1307                 exit(1);
1308         }
1309
1310         if(use_sections){
1311                 Texture_sections = (tcache_slot_opengl*)malloc(MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1312                 if(!Texture_sections){
1313                         exit(1);
1314                 }
1315                 memset(Texture_sections, 0, MAX_BITMAPS * MAX_BMAP_SECTIONS_X * MAX_BMAP_SECTIONS_Y * sizeof(tcache_slot_opengl));
1316         }
1317
1318         // Init the texture structures
1319         int section_count = 0;
1320         for( i=0; i<MAX_BITMAPS; i++ )  {
1321                 /*
1322                 Textures[i].vram_texture = NULL;
1323                 Textures[i].vram_texture_surface = NULL;
1324                 */
1325                 Textures[i].texture_handle = 0;
1326
1327                 Textures[i].bitmap_id = -1;
1328                 Textures[i].size = 0;
1329                 Textures[i].used_this_frame = 0;
1330
1331                 Textures[i].parent = NULL;
1332
1333                 // allocate sections
1334                 if(use_sections){
1335                         for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1336                                 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1337                                         Textures[i].data_sections[idx][s_idx] = &((tcache_slot_opengl*)Texture_sections)[section_count++];
1338                                         Textures[i].data_sections[idx][s_idx]->parent = &Textures[i];
1339                                         /*
1340                                         Textures[i].data_sections[idx][s_idx]->vram_texture = NULL;
1341                                         Textures[i].data_sections[idx][s_idx]->vram_texture_surface = NULL;
1342                                         */
1343                                         Textures[i].data_sections[idx][s_idx]->texture_handle = 0;
1344                                         Textures[i].data_sections[idx][s_idx]->bitmap_id = -1;
1345                                         Textures[i].data_sections[idx][s_idx]->size = 0;
1346                                         Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
1347                                 }
1348                         }
1349                 } else {
1350                         for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1351                                 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1352                                         Textures[i].data_sections[idx][s_idx] = NULL;
1353                                 }
1354                         }
1355                 }
1356         }
1357
1358         GL_texture_sections = use_sections;
1359
1360         //GL_last_detail = Detail.hardware_textures;
1361         GL_last_bitmap_id = -1;
1362         GL_last_bitmap_type = -1;
1363
1364         GL_last_section_x = -1;
1365         GL_last_section_y = -1;
1366
1367         GL_textures_in = 0;
1368         GL_textures_in_frame = 0;
1369 }
1370
1371 int opengl_free_texture (tcache_slot_opengl *t);
1372
1373 void opengl_tcache_flush ()
1374 {
1375         int i;
1376
1377         for( i=0; i<MAX_BITMAPS; i++ )  {
1378                 opengl_free_texture ( &Textures[i] );
1379         }
1380         if (GL_textures_in != 0) {
1381                 mprintf(( "WARNING: VRAM is at %d instead of zero after flushing!\n", GL_textures_in ));
1382                 GL_textures_in = 0;
1383         }
1384
1385         GL_last_bitmap_id = -1;
1386         GL_last_section_x = -1;
1387         GL_last_section_y = -1;
1388 }
1389
1390 void opengl_tcache_cleanup ()
1391 {
1392         opengl_tcache_flush ();
1393
1394         GL_textures_in = 0;
1395         GL_textures_in_frame = 0;
1396
1397         if ( Textures ) {
1398                 free(Textures);
1399                 Textures = NULL;
1400         }
1401
1402         if( Texture_sections != NULL ){
1403                 free(Texture_sections);
1404                 Texture_sections = NULL;
1405         }
1406 }
1407
1408 void opengl_tcache_frame ()
1409 {
1410         int idx, s_idx;
1411
1412         GL_last_bitmap_id = -1;
1413         GL_textures_in_frame = 0;
1414
1415         GL_frame_count++;
1416
1417         int i;
1418         for( i=0; i<MAX_BITMAPS; i++ )  {
1419                 Textures[i].used_this_frame = 0;
1420
1421                 // data sections
1422                 if(Textures[i].data_sections[0][0] != NULL){
1423                         Assert(GL_texture_sections);
1424                         if(GL_texture_sections){
1425                                 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1426                                         for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1427                                                 if(Textures[i].data_sections[idx][s_idx] != NULL){
1428                                                         Textures[i].data_sections[idx][s_idx]->used_this_frame = 0;
1429                                                 }
1430                                         }
1431                                 }
1432                         }
1433                 }
1434         }
1435
1436         if ( vram_full )        {
1437                 opengl_tcache_flush();
1438                 vram_full = 0;
1439         }
1440 }
1441
1442 int opengl_free_texture ( tcache_slot_opengl *t )
1443 {
1444         int idx, s_idx;
1445         
1446
1447         // Bitmap changed!!     
1448         if ( t->bitmap_id > -1 )        {
1449                 // if I, or any of my children have been used this frame, bail  
1450                 if(t->used_this_frame){
1451                         return 0;
1452                 }
1453                 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1454                         for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1455                                 if((t->data_sections[idx][s_idx] != NULL) && (t->data_sections[idx][s_idx]->used_this_frame)){
1456                                         return 0;
1457                                 }
1458                         }
1459                 }
1460
1461                 // ok, now we know its legal to free everything safely
1462                 glDeleteTextures (1, &t->texture_handle);
1463                 t->texture_handle = 0;
1464
1465                 if ( GL_last_bitmap_id == t->bitmap_id )       {
1466                         GL_last_bitmap_id = -1;
1467                 }
1468
1469                 // if this guy has children, free them too, since the children
1470                 // actually make up his size
1471                 for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1472                         for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1473                                 if(t->data_sections[idx][s_idx] != NULL){
1474                                         opengl_free_texture(t->data_sections[idx][s_idx]);
1475                                 }
1476                         }
1477                 }
1478
1479                 t->bitmap_id = -1;
1480                 t->used_this_frame = 0;
1481                 GL_textures_in -= t->size;
1482         }
1483
1484         return 1;
1485 }
1486
1487 void opengl_tcache_get_adjusted_texture_size(int w_in, int h_in, int *w_out, int *h_out)
1488 {
1489         int tex_w, tex_h;
1490
1491         // bogus
1492         if((w_out == NULL) ||  (h_out == NULL)){
1493                 return;
1494         }
1495
1496         // starting size
1497         tex_w = w_in;
1498         tex_h = h_in;
1499
1500         /*
1501            // DDOI - TODO
1502         if ( D3D_pow2_textures )        {
1503                 int i;
1504                 for (i=0; i<16; i++ )   {
1505                         if ( (tex_w > (1<<i)) && (tex_w <= (1<<(i+1))) )        {
1506                                 tex_w = 1 << (i+1);
1507                                 break;
1508                         }
1509                 }
1510
1511                 for (i=0; i<16; i++ )   {
1512                         if ( (tex_h > (1<<i)) && (tex_h <= (1<<(i+1))) )        {
1513                                 tex_h = 1 << (i+1);
1514                                 break;
1515                         }
1516                 }
1517         }
1518         */
1519
1520         if ( tex_w < GL_min_texture_width ) {
1521                 tex_w = GL_min_texture_width;
1522         } else if ( tex_w > GL_max_texture_width )     {
1523                 tex_w = GL_max_texture_width;
1524         }
1525
1526         if ( tex_h < GL_min_texture_height ) {
1527                 tex_h = GL_min_texture_height;
1528         } else if ( tex_h > GL_max_texture_height )    {
1529                 tex_h = GL_max_texture_height;
1530         }
1531
1532         if ( GL_square_textures )      {
1533                 int new_size;
1534                 // Make the both be equal to larger of the two
1535                 new_size = max(tex_w, tex_h);
1536                 tex_w = new_size;
1537                 tex_h = new_size;
1538         }
1539
1540         // store the outgoing size
1541         *w_out = tex_w;
1542         *h_out = tex_h;
1543 }
1544
1545 // data == start of bitmap data
1546 // sx == x offset into bitmap
1547 // sy == y offset into bitmap
1548 // src_w == absolute width of section on source bitmap
1549 // src_h == absolute height of section on source bitmap
1550 // bmap_w == width of source bitmap
1551 // bmap_h == height of source bitmap
1552 // tex_w == width of final texture
1553 // tex_h == height of final texture
1554 int opengl_create_texture_sub(int bitmap_type, int texture_handle, ushort *data, int sx, int sy, int src_w, int src_h, int bmap_w, int bmap_h, int tex_w, int tex_h, tcache_slot_opengl *t, int reload, int fail_on_full)
1555 {
1556         int ret_val = 1;
1557
1558         // bogus
1559         if(t == NULL){
1560                 return 0;
1561         }
1562
1563         if ( t->used_this_frame )       {
1564                 mprintf(( "ARGHH!!! Texture already used this frame!  Cannot free it!\n" ));
1565                 return 0;
1566         }
1567         if ( !reload )  {
1568                 // gah
1569                 if(!opengl_free_texture(t)){
1570                         return 0;
1571                 }
1572         }
1573
1574         // get final texture size
1575         opengl_tcache_get_adjusted_texture_size(tex_w, tex_h, &tex_w, &tex_h);
1576
1577         if ( (tex_w < 1) || (tex_h < 1) )       {
1578                 mprintf(("Bitmap is to small at %dx%d.\n", tex_w, tex_h ));
1579                 return 0;
1580         }
1581
1582         if ( bitmap_type == TCACHE_TYPE_AABITMAP )      {
1583                 t->u_scale = (float)bmap_w / (float)tex_w;
1584                 t->v_scale = (float)bmap_h / (float)tex_h;
1585         } else if(bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
1586                 t->u_scale = (float)src_w / (float)tex_w;
1587                 t->v_scale = (float)src_h / (float)tex_h;
1588         } else {
1589                 t->u_scale = 1.0f;
1590                 t->v_scale = 1.0f;
1591         }
1592
1593         /*
1594         bmp_data = (ushort *)data;
1595         ubyte *bmp_data_byte = (ubyte*)data;
1596         */
1597
1598         /* DDOI - FIXME: Uh, actually loading the texture sound good? */
1599
1600         glGenTextures (1, &t->texture_handle);
1601         glBindTexture (GL_TEXTURE_2D, t->texture_handle);
1602
1603         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
1604         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
1605         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1606         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1607
1608         glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, tex_w, tex_h, 0, GL_RGBA,
1609                         GL_UNSIGNED_SHORT_1_5_5_5_REV, data);
1610         
1611         t->bitmap_id = texture_handle;
1612         t->time_created = GL_frame_count;
1613         t->used_this_frame = 0;
1614         t->size = tex_w * tex_h * 2;
1615         t->w = (ushort)tex_w;
1616         t->h = (ushort)tex_h;
1617         GL_textures_in_frame += t->size;
1618         if (!reload) {
1619                 GL_textures_in += t->size;
1620         }
1621
1622         STUB_FUNCTION;
1623
1624         return ret_val;
1625 }
1626
1627 int opengl_create_texture (int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int fail_on_full)
1628 {
1629         ubyte flags;
1630         bitmap *bmp;
1631         int final_w, final_h;
1632         ubyte bpp = 16;
1633         int reload = 0;
1634
1635         // setup texture/bitmap flags
1636         flags = 0;
1637         switch(bitmap_type){
1638                 case TCACHE_TYPE_AABITMAP:
1639                         flags |= BMP_AABITMAP;
1640                         bpp = 8;
1641                         break;
1642                 case TCACHE_TYPE_NORMAL:
1643                         flags |= BMP_TEX_OTHER;
1644                 case TCACHE_TYPE_XPARENT:
1645                         flags |= BMP_TEX_XPARENT;
1646                         break;
1647                 case TCACHE_TYPE_NONDARKENING:
1648                         Int3();
1649                         flags |= BMP_TEX_NONDARK;
1650                         break;
1651         }
1652
1653         // lock the bitmap into the proper format
1654         bmp = bm_lock(bitmap_handle, bpp, flags);
1655         if ( bmp == NULL ) {
1656                 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
1657                 return 0;
1658         }
1659
1660         int max_w = bmp->w;
1661         int max_h = bmp->h;
1662
1663         /*
1664            // DDOI - TODO
1665         if ( bitmap_type != TCACHE_TYPE_AABITMAP )      {
1666                 max_w /= D3D_texture_divider;
1667                 max_h /= D3D_texture_divider;
1668
1669                 // Detail.debris_culling goes from 0 to 4.
1670                 max_w /= 16 >> Detail.hardware_textures;
1671                 max_h /= 16 >> Detail.hardware_textures;
1672         }
1673         */
1674
1675         // get final texture size as it will be allocated as a DD surface
1676         opengl_tcache_get_adjusted_texture_size(max_w, max_h, &final_w, &final_h); 
1677
1678         // if this tcache slot has no bitmap
1679         if ( tslot->bitmap_id < 0) {
1680                 reload = 0;
1681         }
1682         // different bitmap altogether - determine if the new one can use the old one's slot
1683         else if (tslot->bitmap_id != bitmap_handle)     {
1684                 if((final_w == tslot->w) && (final_h == tslot->h)){
1685                         reload = 1;
1686                         //ml_printf("Reloading texture %d\n", bitmap_handle);
1687                 } else {
1688                         reload = 0;
1689                 }
1690         }
1691
1692         // call the helper
1693         int ret_val = opengl_create_texture_sub(bitmap_type, bitmap_handle, (ushort*)bmp->data, 0, 0, bmp->w, bmp->h, bmp->w, bmp->h, max_w, max_h, tslot, reload, fail_on_full);
1694
1695         // unlock the bitmap
1696         bm_unlock(bitmap_handle);
1697
1698         return ret_val;
1699 }
1700
1701 int opengl_create_texture_sectioned(int bitmap_handle, int bitmap_type, tcache_slot_opengl *tslot, int sx, int sy, int fail_on_full)
1702 {
1703         ubyte flags;
1704         bitmap *bmp;
1705         int final_w, final_h;
1706         int section_x, section_y;
1707         int reload = 0;
1708
1709         // setup texture/bitmap flags
1710         Assert(bitmap_type == TCACHE_TYPE_BITMAP_SECTION);
1711         if(bitmap_type != TCACHE_TYPE_BITMAP_SECTION){
1712                 bitmap_type = TCACHE_TYPE_BITMAP_SECTION;
1713         }
1714         flags = BMP_TEX_XPARENT;
1715
1716         // lock the bitmap in the proper format
1717         bmp = bm_lock(bitmap_handle, 16, flags);
1718         if ( bmp == NULL ) {
1719                 mprintf(("Couldn't lock bitmap %d.\n", bitmap_handle ));
1720                 return 0;
1721         }
1722         // determine the width and height of this section
1723         bm_get_section_size(bitmap_handle, sx, sy, &section_x, &section_y);
1724
1725         // get final texture size as it will be allocated as a DD surface
1726         opengl_tcache_get_adjusted_texture_size(section_x, section_y, &final_w, &final_h);
1727
1728         // if this tcache slot has no bitmap
1729         if ( tslot->bitmap_id < 0) {
1730                 reload = 0;
1731         }
1732         // different bitmap altogether - determine if the new one can use the old one's slot
1733         else if (tslot->bitmap_id != bitmap_handle)     {
1734                 if((final_w == tslot->w) && (final_h == tslot->h)){
1735                         reload = 1;
1736                 } else {
1737                         reload = 0;
1738                 }
1739         }
1740
1741         // call the helper
1742         int ret_val = opengl_create_texture_sub(bitmap_type, bitmap_handle, (ushort*)bmp->data, bmp->sections.sx[sx], bmp->sections.sy[sy], section_x, section_y, bmp->w, bmp->h, section_x, section_y, tslot, reload, fail_on_full);
1743
1744         // unlock the bitmap
1745         bm_unlock(bitmap_handle);
1746
1747         return ret_val;
1748 }
1749
1750                 
1751 extern int bm_get_cache_slot( int bitmap_id, int separate_ani_frames );
1752 int gr_opengl_tcache_set(int bitmap_id, int bitmap_type, float *u_scale, float *v_scale, int fail_on_full = 0, int sx = -1, int sy = -1, int force = 0)
1753 {
1754         bitmap *bmp = NULL;
1755
1756         int idx, s_idx;
1757         int ret_val = 1;
1758
1759         if (bitmap_id < 0)
1760         {
1761                 GL_last_bitmap_id = -1;
1762                 return 0;
1763         }
1764
1765         /*
1766         if ( GL_last_detail != Detail.hardware_textures )      {
1767                 GL_last_detail = Detail.hardware_textures;
1768                 opengl_tcache_flush();
1769         }
1770         */
1771
1772         if (vram_full) {
1773                 return 0;
1774         }
1775
1776         int n = bm_get_cache_slot (bitmap_id, 1);
1777         tcache_slot_opengl *t = &Textures[n];
1778
1779         if ( (GL_last_bitmap_id == bitmap_id) && (GL_last_bitmap_type==bitmap_type) && (t->bitmap_id == bitmap_id) && (GL_last_section_x == sx) && (GL_last_section_y == sy))       {
1780                 t->used_this_frame++;
1781
1782                 // mark all children as used
1783                 if(GL_texture_sections){
1784                         for(idx=0; idx<MAX_BMAP_SECTIONS_X; idx++){
1785                                 for(s_idx=0; s_idx<MAX_BMAP_SECTIONS_Y; s_idx++){
1786                                         if(t->data_sections[idx][s_idx] != NULL){
1787                                                 t->data_sections[idx][s_idx]->used_this_frame++;
1788                                         }
1789                                 }
1790                         }
1791                 }
1792
1793                 *u_scale = t->u_scale;
1794                 *v_scale = t->v_scale;
1795                 return 1;
1796         }
1797
1798         if (bitmap_type == TCACHE_TYPE_BITMAP_SECTION){
1799                 Assert((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y));
1800                 if(!((sx >= 0) && (sy >= 0) && (sx < MAX_BMAP_SECTIONS_X) && (sy < MAX_BMAP_SECTIONS_Y))){
1801                         return 0;
1802                 }
1803
1804                 ret_val = 1;
1805
1806                 // if the texture sections haven't been created yet
1807                 if((t->bitmap_id < 0) || (t->bitmap_id != bitmap_id)){
1808
1809                         // lock the bitmap in the proper format
1810                         bmp = bm_lock(bitmap_id, 16, BMP_TEX_XPARENT);
1811                         bm_unlock(bitmap_id);
1812
1813                         // now lets do something for each texture
1814
1815                         for(idx=0; idx<bmp->sections.num_x; idx++){
1816                                 for(s_idx=0; s_idx<bmp->sections.num_y; s_idx++){
1817                                         // hmm. i'd rather we didn't have to do it this way...
1818                                         if(!opengl_create_texture_sectioned(bitmap_id, bitmap_type, t->data_sections[idx][s_idx], idx, s_idx, fail_on_full)){
1819                                                 ret_val = 0;
1820                                         }
1821
1822                                         // not used this frame
1823                                         t->data_sections[idx][s_idx]->used_this_frame = 0;
1824                                 }
1825                         }
1826
1827                         // zero out pretty much everything in the parent struct since he's just the root
1828                         t->bitmap_id = bitmap_id;
1829                         t->texture_handle = 0;
1830                         t->time_created = t->data_sections[sx][sy]->time_created;
1831                         t->used_this_frame = 0;
1832                         /*
1833                         t->vram_texture = NULL;
1834                         t->vram_texture_surface = NULL
1835                         */
1836                 }
1837
1838                 // argh. we failed to upload. free anything we can
1839                 if(!ret_val){
1840                         opengl_free_texture(t);
1841                 }
1842                 // swap in the texture we want
1843                 else {
1844                         t = t->data_sections[sx][sy];
1845                 }
1846         }
1847         // all other "normal" textures
1848         else if((bitmap_id < 0) || (bitmap_id != t->bitmap_id)){
1849                 ret_val = opengl_create_texture( bitmap_id, bitmap_type, t, fail_on_full );
1850         }
1851
1852         // everything went ok
1853         if(ret_val && (t->texture_handle) && !vram_full){
1854                 *u_scale = t->u_scale;
1855                 *v_scale = t->v_scale;
1856
1857                 glBindTexture (GL_TEXTURE_2D, t->texture_handle );
1858         
1859                 GL_last_bitmap_id = t->bitmap_id;
1860                 GL_last_bitmap_type = bitmap_type;
1861                 GL_last_section_x = sx;
1862                 GL_last_section_y = sy;
1863
1864                 t->used_this_frame++;
1865         }
1866         // gah
1867         else {
1868                 return 0;
1869         }
1870
1871         return 1;
1872 }
1873
1874 void gr_opengl_set_clear_color(int r, int g, int b)
1875 {
1876         STUB_FUNCTION;
1877 }
1878
1879 void gr_opengl_aaline(vertex *v1, vertex *v2)
1880 {
1881         STUB_FUNCTION;
1882 }
1883
1884 void gr_opengl_flash(int r, int g, int b)
1885 {
1886         STUB_FUNCTION;
1887 }
1888
1889 int gr_opengl_zbuffer_get()
1890 {
1891         STUB_FUNCTION;
1892         
1893         return GR_ZBUFF_NONE;
1894 }
1895
1896 int gr_opengl_zbuffer_set(int mode)
1897 {
1898         STUB_FUNCTION;
1899         
1900         return GR_ZBUFF_NONE;
1901 }
1902
1903 void gr_opengl_zbuffer_clear(int mode)
1904 {
1905         STUB_FUNCTION;
1906 }
1907
1908 void gr_opengl_set_gamma(float gamma)
1909 {
1910         STUB_FUNCTION;
1911 }
1912
1913 void gr_opengl_fade_in(int instantaneous)
1914 {
1915         STUB_FUNCTION;
1916 }
1917
1918 void gr_opengl_fade_out(int instantaneous)
1919 {
1920         STUB_FUNCTION;
1921 }
1922
1923 int gr_opengl_save_screen()
1924 {
1925         STUB_FUNCTION;
1926         
1927         return -1;
1928 }
1929
1930 void gr_opengl_restore_screen(int id)
1931 {
1932         STUB_FUNCTION;
1933 }
1934
1935 void gr_opengl_free_screen(int id)
1936 {
1937         STUB_FUNCTION;
1938 }
1939
1940 void gr_opengl_dump_frame_start(int first_frame, int frames_between_dumps)
1941 {
1942         STUB_FUNCTION;
1943 }
1944
1945 void gr_opengl_dump_frame_stop()
1946 {
1947         STUB_FUNCTION;
1948 }
1949
1950 void gr_opengl_dump_frame()
1951 {
1952         STUB_FUNCTION;
1953 }
1954
1955 uint gr_opengl_lock()
1956 {
1957         STUB_FUNCTION;
1958         
1959         return 1;
1960 }
1961         
1962 void gr_opengl_unlock()
1963 {
1964 }
1965         
1966 void gr_opengl_init()
1967 {
1968         if ( Inited )   {
1969                 gr_opengl_cleanup();
1970                 Inited = 0;
1971         }
1972
1973         mprintf(( "Initializing opengl graphics device...\n" ));
1974         Inited = 1;
1975
1976 #ifdef PLAT_UNIX
1977         if (SDL_InitSubSystem (SDL_INIT_VIDEO) < 0)
1978         {
1979                 fprintf (stderr, "Couldn't init SDL: %s", SDL_GetError());
1980                 exit (1);
1981         }
1982
1983         atexit (SDL_Quit);
1984
1985         SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
1986         SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
1987         SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
1988         SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
1989         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
1990                                                 
1991         if (SDL_SetVideoMode (640, 480, 0, SDL_OPENGL) == NULL)
1992         {
1993                 fprintf (stderr, "Couldn't set video mode: %s", SDL_GetError ());
1994                 exit (1);
1995         }               
1996 #endif
1997         glViewport(0, 0, gr_screen.max_w, gr_screen.max_h);
1998
1999         glMatrixMode(GL_PROJECTION);
2000         glLoadIdentity();
2001         glMatrixMode(GL_MODELVIEW);
2002         glLoadIdentity();
2003         
2004         glEnable(GL_TEXTURE_2D);
2005         
2006         glGenTextures(1, &bitmapTex);
2007         glBindTexture(GL_TEXTURE_2D, bitmapTex);
2008         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2009         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2010         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2011
2012         glMatrixMode(GL_PROJECTION);
2013         glPushMatrix();
2014         glLoadIdentity();
2015         
2016         glOrtho(0, gr_screen.max_w, gr_screen.max_h, 0, -1.0, 1.0);             
2017         
2018         bitmapMem = (GLubyte *)malloc(256*256*4);
2019         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5_REV, bitmapMem);
2020         //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_CHAR, bitmapMem);
2021         
2022         glFlush();
2023         
2024         int bpp = 15;
2025         
2026         switch( bpp )   {
2027         case 8:
2028                 Gr_red.bits = 8;
2029                 Gr_red.shift = 16;
2030                 Gr_red.scale = 1;
2031                 Gr_red.mask = 0xff0000;
2032
2033                 Gr_green.bits = 8;
2034                 Gr_green.shift = 8;
2035                 Gr_green.scale = 1;
2036                 Gr_green.mask = 0xff00;
2037
2038                 Gr_blue.bits = 8;
2039                 Gr_blue.shift = 0;
2040                 Gr_blue.scale = 1;
2041                 Gr_blue.mask = 0xff;
2042                 
2043                 break;
2044                 
2045         case 15:
2046                 Gr_red.bits = 5;
2047                 Gr_red.shift = 10;
2048                 Gr_red.scale = 8;
2049                 Gr_red.mask = 0x7C00;
2050
2051                 Gr_green.bits = 5;
2052                 Gr_green.shift = 5;
2053                 Gr_green.scale = 8;
2054                 Gr_green.mask = 0x3E0;
2055
2056                 Gr_blue.bits = 5;
2057                 Gr_blue.shift = 0;
2058                 Gr_blue.scale = 8;
2059                 Gr_blue.mask = 0x1F;
2060
2061                 break;
2062
2063         case 16:
2064                 Gr_red.bits = 5;
2065                 Gr_red.shift = 11;
2066                 Gr_red.scale = 8;
2067                 Gr_red.mask = 0xF800;
2068
2069                 Gr_green.bits = 6;
2070                 Gr_green.shift = 5;
2071                 Gr_green.scale = 4;
2072                 Gr_green.mask = 0x7E0;
2073
2074                 Gr_blue.bits = 5;
2075                 Gr_blue.shift = 0;
2076                 Gr_blue.scale = 8;
2077                 Gr_blue.mask = 0x1F;
2078
2079                 break;
2080
2081         case 24:
2082         case 32:
2083                 Gr_red.bits = 8;
2084                 Gr_red.shift = 16;
2085                 Gr_red.scale = 1;
2086                 Gr_red.mask = 0xff0000;
2087
2088                 Gr_green.bits = 8;
2089                 Gr_green.shift = 8;
2090                 Gr_green.scale = 1;
2091                 Gr_green.mask = 0xff00;
2092
2093                 Gr_blue.bits = 8;
2094                 Gr_blue.shift = 0;
2095                 Gr_blue.scale = 1;
2096                 Gr_blue.mask = 0xff;
2097
2098                 break;
2099
2100         default:
2101                 Int3(); // Illegal bpp
2102         }
2103
2104
2105         opengl_tcache_init (0);
2106         gr_opengl_clear();
2107
2108         Gr_current_red = &Gr_red;
2109         Gr_current_blue = &Gr_blue;
2110         Gr_current_green = &Gr_green;
2111         Gr_current_alpha = &Gr_alpha;
2112                                 
2113         gr_screen.gf_flip = gr_opengl_flip;
2114         gr_screen.gf_flip_window = gr_opengl_flip_window;
2115         gr_screen.gf_set_clip = gr_opengl_set_clip;
2116         gr_screen.gf_reset_clip = gr_opengl_reset_clip;
2117         gr_screen.gf_set_font = grx_set_font;
2118         
2119         gr_screen.gf_set_color = gr_opengl_set_color;
2120         gr_screen.gf_set_bitmap = gr_opengl_set_bitmap;
2121         gr_screen.gf_create_shader = gr_opengl_create_shader;
2122         gr_screen.gf_set_shader = gr_opengl_set_shader;
2123         gr_screen.gf_clear = gr_opengl_clear;
2124         // gr_screen.gf_bitmap = gr_opengl_bitmap;
2125         // gr_screen.gf_bitmap_ex = gr_opengl_bitmap_ex;
2126         gr_screen.gf_aabitmap = gr_opengl_aabitmap;
2127         gr_screen.gf_aabitmap_ex = gr_opengl_aabitmap_ex;
2128         
2129         gr_screen.gf_rect = gr_opengl_rect;
2130         gr_screen.gf_shade = gr_opengl_shade;
2131         gr_screen.gf_string = gr_opengl_string;
2132         gr_screen.gf_circle = gr_opengl_circle;
2133
2134         gr_screen.gf_line = gr_opengl_line;
2135         gr_screen.gf_aaline = gr_opengl_aaline;
2136         gr_screen.gf_pixel = gr_opengl_pixel;
2137         gr_screen.gf_scaler = gr_opengl_scaler;
2138         gr_screen.gf_tmapper = gr_opengl_tmapper;
2139
2140         gr_screen.gf_gradient = gr_opengl_gradient;
2141
2142         gr_screen.gf_set_palette = gr_opengl_set_palette;
2143         gr_screen.gf_get_color = gr_opengl_get_color;
2144         gr_screen.gf_init_color = gr_opengl_init_color;
2145         gr_screen.gf_init_alphacolor = gr_opengl_init_alphacolor;
2146         gr_screen.gf_set_color_fast = gr_opengl_set_color_fast;
2147         gr_screen.gf_print_screen = gr_opengl_print_screen;
2148
2149         gr_screen.gf_fade_in = gr_opengl_fade_in;
2150         gr_screen.gf_fade_out = gr_opengl_fade_out;
2151         gr_screen.gf_flash = gr_opengl_flash;
2152         
2153         gr_screen.gf_zbuffer_get = gr_opengl_zbuffer_get;
2154         gr_screen.gf_zbuffer_set = gr_opengl_zbuffer_set;
2155         gr_screen.gf_zbuffer_clear = gr_opengl_zbuffer_clear;
2156         
2157         gr_screen.gf_save_screen = gr_opengl_save_screen;
2158         gr_screen.gf_restore_screen = gr_opengl_restore_screen;
2159         gr_screen.gf_free_screen = gr_opengl_free_screen;
2160         
2161         gr_screen.gf_dump_frame_start = gr_opengl_dump_frame_start;
2162         gr_screen.gf_dump_frame_stop = gr_opengl_dump_frame_stop;
2163         gr_screen.gf_dump_frame = gr_opengl_dump_frame;
2164         
2165         gr_screen.gf_set_gamma = gr_opengl_set_gamma;
2166         
2167         gr_screen.gf_lock = gr_opengl_lock;
2168         gr_screen.gf_unlock = gr_opengl_unlock;
2169         
2170         gr_screen.gf_fog_set = gr_opengl_fog_set;       
2171
2172         gr_screen.gf_get_region = gr_opengl_get_region;
2173
2174         gr_screen.gf_get_pixel = gr_opengl_get_pixel;
2175
2176         gr_screen.gf_set_cull = gr_opengl_set_cull;
2177
2178         gr_screen.gf_cross_fade = gr_opengl_cross_fade;
2179
2180         gr_screen.gf_filter_set = gr_opengl_filter_set;
2181
2182         gr_screen.gf_tcache_set = gr_opengl_tcache_set;
2183
2184         gr_screen.gf_set_clear_color = gr_opengl_set_clear_color;
2185 }
2186