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