]> icculus.org git repositories - taylor/freespace2.git/blob - src/graphics/grgl1.cpp
merge in updated platform code
[taylor/freespace2.git] / src / graphics / grgl1.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 #include "gropengl.h"
10 #include "grgl1.h"
11 #include "gropenglinternal.h"
12 #include "2d.h"
13 #include "mouse.h"
14 #include "pstypes.h"
15 #include "cfile.h"
16 #include "bmpman.h"
17 #include "grinternal.h"
18
19
20 int OGL_fog_mode = 0;
21
22 int GL_one_inited = 0;
23
24
25 volatile int GL_activate = 0;
26 volatile int GL_deactivate = 0;
27
28 static GLuint Gr_saved_screen_tex = 0;
29
30 static int Gr_opengl_mouse_saved = 0;
31 static int Gr_opengl_mouse_saved_x = 0;
32 static int Gr_opengl_mouse_saved_y = 0;
33 static int Gr_opengl_mouse_saved_w = 0;
34 static int Gr_opengl_mouse_saved_h = 0;
35 static ubyte *Gr_opengl_mouse_saved_data = NULL;
36
37
38 PFNGLSECONDARYCOLORPOINTERPROC vglSecondaryColorPointer = NULL;
39
40
41 static gr_alpha_blend GL_current_alpha_blend = (gr_alpha_blend) -1;
42 static gr_zbuffer_type GL_current_zbuffer_type = (gr_zbuffer_type) -1;
43
44 void opengl1_set_state(gr_texture_source ts, gr_alpha_blend ab, gr_zbuffer_type zt)
45 {
46         opengl1_set_texture_state(ts);
47
48         if (ab != GL_current_alpha_blend) {
49                 switch (ab) {
50                         case ALPHA_BLEND_NONE:                  // 1*SrcPixel + 0*DestPixel
51                                 glBlendFunc(GL_ONE, GL_ZERO);
52                                 break;
53                         case ALPHA_BLEND_ADDITIVE:              // 1*SrcPixel + 1*DestPixel
54                                 glBlendFunc(GL_ONE, GL_ONE);
55                                 break;
56                         case ALPHA_BLEND_ALPHA_ADDITIVE:        // Alpha*SrcPixel + 1*DestPixel
57                                 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
58                                 break;
59                         case ALPHA_BLEND_ALPHA_BLEND_ALPHA:     // Alpha*SrcPixel + (1-Alpha)*DestPixel
60                                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
61                                 break;
62                         case ALPHA_BLEND_ALPHA_BLEND_SRC_COLOR: // Alpha*SrcPixel + (1-SrcPixel)*DestPixel
63                                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_COLOR);
64                                 break;
65                         default:
66                                 break;
67                 }
68
69                 GL_current_alpha_blend = ab;
70         }
71
72         if (zt != GL_current_zbuffer_type) {
73                 switch (zt) {
74                         case ZBUFFER_TYPE_NONE:
75                                 glDepthFunc(GL_ALWAYS);
76                                 glDepthMask(GL_FALSE);
77                                 break;
78                         case ZBUFFER_TYPE_READ:
79                                 glDepthFunc(GL_LESS);
80                                 glDepthMask(GL_FALSE);
81                                 break;
82                         case ZBUFFER_TYPE_WRITE:
83                                 glDepthFunc(GL_ALWAYS);
84                                 glDepthMask(GL_TRUE);
85                                 break;
86                         case ZBUFFER_TYPE_FULL:
87                                 glDepthFunc(GL_LESS);
88                                 glDepthMask(GL_TRUE);
89                                 break;
90                         default:
91                                 break;
92                 }
93
94                 GL_current_zbuffer_type = zt;
95         }
96 }
97
98 void opengl1_cleanup()
99 {
100         if ( !GL_one_inited ) {
101                 return;
102         }
103
104         gr_opengl1_reset_clip();
105         gr_opengl1_clear();
106         gr_opengl1_flip();
107
108         opengl1_tcache_cleanup();
109
110         GL_one_inited = 0;
111 }
112
113 static void opengl1_init_func_pointers()
114 {
115         gr_screen.gf_flip = gr_opengl1_flip;
116         gr_screen.gf_set_clip = gr_opengl1_set_clip;
117         gr_screen.gf_reset_clip = gr_opengl1_reset_clip;
118
119         gr_screen.gf_clear = gr_opengl1_clear;
120
121         gr_screen.gf_aabitmap = gr_opengl1_aabitmap;
122         gr_screen.gf_aabitmap_ex = gr_opengl1_aabitmap_ex;
123
124         gr_screen.gf_rect = gr_opengl1_rect;
125         gr_screen.gf_shade = gr_opengl1_shade;
126         gr_screen.gf_string = gr_opengl1_string;
127         gr_screen.gf_circle = gr_opengl1_circle;
128
129         gr_screen.gf_line = gr_opengl1_line;
130         gr_screen.gf_aaline = gr_opengl1_aaline;
131         gr_screen.gf_pixel = gr_opengl1_pixel;
132         gr_screen.gf_scaler = gr_opengl1_scaler;
133         gr_screen.gf_tmapper = gr_opengl1_tmapper;
134
135         gr_screen.gf_gradient = gr_opengl1_gradient;
136
137         gr_screen.gf_print_screen = gr_opengl1_print_screen;
138
139         gr_screen.gf_fade_in = gr_opengl1_fade_in;
140         gr_screen.gf_fade_out = gr_opengl1_fade_out;
141         gr_screen.gf_flash = gr_opengl1_flash;
142
143         gr_screen.gf_zbuffer_clear = gr_opengl1_zbuffer_clear;
144
145         gr_screen.gf_save_screen = gr_opengl1_save_screen;
146         gr_screen.gf_restore_screen = gr_opengl1_restore_screen;
147         gr_screen.gf_free_screen = gr_opengl1_free_screen;
148
149         gr_screen.gf_dump_frame_start = gr_opengl1_dump_frame_start;
150         gr_screen.gf_dump_frame_stop = gr_opengl1_dump_frame_stop;
151         gr_screen.gf_dump_frame = gr_opengl1_dump_frame;
152
153         gr_screen.gf_set_gamma = gr_opengl1_set_gamma;
154
155         gr_screen.gf_lock = gr_opengl1_lock;
156         gr_screen.gf_unlock = gr_opengl1_unlock;
157
158         gr_screen.gf_fog_set = gr_opengl1_fog_set;
159
160         gr_screen.gf_get_region = gr_opengl1_get_region;
161
162         gr_screen.gf_set_cull = gr_opengl1_set_cull;
163
164         gr_screen.gf_cross_fade = gr_opengl1_cross_fade;
165
166         gr_screen.gf_preload_init = gr_opengl1_preload_init;
167         gr_screen.gf_preload = gr_opengl1_preload;
168
169         gr_screen.gf_zbias = gr_opengl1_zbias;
170
171         gr_screen.gf_force_windowed = gr_opengl_force_windowed;
172         gr_screen.gf_force_fullscreen = gr_opengl_force_fullscreen;
173         gr_screen.gf_toggle_fullscreen = gr_opengl_toggle_fullscreen;
174
175         gr_screen.gf_set_viewport = gr_opengl1_set_viewport;
176
177         gr_screen.gf_activate = gr_opengl1_activate;
178
179         gr_screen.gf_release_texture = gr_opengl1_release_texture;
180 }
181
182 void opengl1_init()
183 {
184         if (GL_one_inited) {
185                 return;
186         }
187
188         /*
189           1 = use secondary color ext
190           2 = use opengl linear fog
191          */
192         OGL_fog_mode = 2;
193
194         // only available with OpenGL 1.2+, must get ptr for Windows
195         vglSecondaryColorPointer = (PFNGLSECONDARYCOLORPOINTERPROC)SDL_GL_GetProcAddress("glSecondaryColorPointer");
196
197         if (vglSecondaryColorPointer) {
198                 OGL_fog_mode = 1;
199         }
200
201         mprintf(("  Fog mode: %s\n", (OGL_fog_mode == 1) ? "secondary color" : "linear"));
202
203         glShadeModel(GL_SMOOTH);
204         glEnable(GL_DITHER);
205         glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
206         glHint(GL_FOG_HINT, GL_NICEST);
207
208         glEnable(GL_DEPTH_TEST);
209         glEnable(GL_BLEND);
210
211         glEnable(GL_TEXTURE_2D);
212
213         glDepthRange(0.0, 1.0);
214
215         glPixelStorei(GL_PACK_ALIGNMENT, 1);
216         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
217
218         glFlush();
219
220         opengl1_init_func_pointers();
221         opengl1_tcache_init();
222
223         gr_opengl1_clear();
224
225         GL_one_inited = 1;
226 }
227
228 void gr_opengl1_activate(int active)
229 {
230         if (active) {
231                 GL_activate++;
232
233                 // don't grab key/mouse if cmdline says so or if we're fullscreen
234         //      if(!Cmdline_no_grab && !(SDL_GetVideoSurface()->flags & SDL_FULLSCREEN)) {
235         //              SDL_WM_GrabInput(SDL_GRAB_ON);
236         //      }
237         } else {
238                 GL_deactivate++;
239
240                 // let go of mouse/keyboard
241         //      SDL_WM_GrabInput(SDL_GRAB_OFF);
242         }
243 }
244
245 void gr_opengl1_clear()
246 {
247         glClearColor(gr_screen.current_clear_color.red / 255.0f,
248                 gr_screen.current_clear_color.green / 255.0f,
249                 gr_screen.current_clear_color.blue / 255.0f, 1.0f);
250
251         glClear( GL_COLOR_BUFFER_BIT );
252 }
253
254 void gr_opengl1_flip()
255 {
256         if ( !GL_one_inited ) {
257                 return;
258         }
259
260         gr_reset_clip();
261
262         mouse_eval_deltas();
263
264         Gr_opengl_mouse_saved = 0;
265
266         if ( mouse_is_visible() )       {
267                 int mx, my;
268
269                 gr_reset_clip();
270                 mouse_get_pos( &mx, &my );
271
272                 gr_opengl1_save_mouse_area(mx,my,32,32);
273
274                 if (Gr_cursor == -1) {
275 #ifndef NDEBUG
276                         gr_set_color(255,255,255);
277                         gr_line(mx, my, mx+7, my + 7);
278                         gr_line(mx, my, mx+5, my );
279                         gr_line(mx, my, mx, my+5);
280 #endif
281                 } else {
282                         gr_set_bitmap(Gr_cursor);
283                         gr_bitmap(mx, my);
284                 }
285          }
286
287 #ifndef NDEBUG
288         GLenum error = GL_NO_ERROR;
289
290         do {
291                 error = glGetError();
292
293                 if (error != GL_NO_ERROR) {
294                         nprintf(("Warning", "!!DEBUG!! OpenGL Error: %d\n", error));
295                 }
296         } while (error != GL_NO_ERROR);
297 #endif
298
299         SDL_GL_SwapWindow(GL_window);
300
301         opengl1_tcache_frame();
302
303         int cnt = GL_activate;
304         if ( cnt )      {
305                 GL_activate-=cnt;
306                 opengl1_tcache_flush();
307                 // gr_opengl_clip_cursor(1); /* mouse grab, see opengl_activate */
308         }
309
310         cnt = GL_deactivate;
311         if ( cnt )      {
312                 GL_deactivate-=cnt;
313                 // gr_opengl_clip_cursor(0);  /* mouse grab, see opengl_activate */
314         }
315 }
316
317 void gr_opengl1_set_clip(int x,int y,int w,int h)
318 {
319         // check for sanity of parameters
320         if (x < 0)
321                 x = 0;
322         if (y < 0)
323                 y = 0;
324
325         if (x >= gr_screen.max_w)
326                 x = gr_screen.max_w - 1;
327         if (y >= gr_screen.max_h)
328                 y = gr_screen.max_h - 1;
329
330         if (x + w > gr_screen.max_w)
331                 w = gr_screen.max_w - x;
332         if (y + h > gr_screen.max_h)
333                 h = gr_screen.max_h - y;
334
335         if (w > gr_screen.max_w)
336                 w = gr_screen.max_w;
337         if (h > gr_screen.max_h)
338                 h = gr_screen.max_h;
339
340         gr_screen.offset_x = x;
341         gr_screen.offset_y = y;
342         gr_screen.clip_left = 0;
343         gr_screen.clip_right = w-1;
344         gr_screen.clip_top = 0;
345         gr_screen.clip_bottom = h-1;
346         gr_screen.clip_width = w;
347         gr_screen.clip_height = h;
348
349         x = fl2i((x * GL_viewport_scale_w) + 0.5f) + GL_viewport_x;
350         y = fl2i((y * GL_viewport_scale_h) + 0.5f) + GL_viewport_y;
351         w = fl2i((w * GL_viewport_scale_w) + 0.5f);
352         h = fl2i((h * GL_viewport_scale_h) + 0.5f);
353
354         glEnable(GL_SCISSOR_TEST);
355         glScissor(x, GL_viewport_h-y-h, w, h);
356 }
357
358 void gr_opengl1_reset_clip()
359 {
360         gr_screen.offset_x = 0;
361         gr_screen.offset_y = 0;
362         gr_screen.clip_left = 0;
363         gr_screen.clip_top = 0;
364         gr_screen.clip_right = gr_screen.max_w - 1;
365         gr_screen.clip_bottom = gr_screen.max_h - 1;
366         gr_screen.clip_width = gr_screen.max_w;
367         gr_screen.clip_height = gr_screen.max_h;
368
369         glDisable(GL_SCISSOR_TEST);
370 }
371
372 void gr_opengl1_print_screen(const char *filename)
373 {
374         char tmp[MAX_FILENAME_LEN];
375         ubyte *buf = NULL;
376
377         SDL_strlcpy( tmp, filename, sizeof(tmp) );
378         SDL_strlcat( tmp, NOX(".tga"), sizeof(tmp) );
379
380         buf = (ubyte*)malloc(GL_viewport_w * GL_viewport_h * 3);
381
382         if (buf == NULL) {
383                 return;
384         }
385
386         CFILE *f = cfopen(tmp, "wb", CFILE_NORMAL, CF_TYPE_ROOT);
387
388         if (f == NULL) {
389                 free(buf);
390                 return;
391         }
392
393         // Write the TGA header
394         cfwrite_ubyte( 0, f );  //      IDLength;
395         cfwrite_ubyte( 0, f );  //      ColorMapType;
396         cfwrite_ubyte( 2, f );  //      ImageType;              // 2 = 24bpp, uncompressed, 10=24bpp rle compressed
397         cfwrite_ushort( 0, f ); // CMapStart;
398         cfwrite_ushort( 0, f ); //      CMapLength;
399         cfwrite_ubyte( 0, f );  // CMapDepth;
400         cfwrite_ushort( 0, f ); //      XOffset;
401         cfwrite_ushort( 0, f ); //      YOffset;
402         cfwrite_ushort( (ushort)GL_viewport_w, f );     //      Width;
403         cfwrite_ushort( (ushort)GL_viewport_h, f );     //      Height;
404         cfwrite_ubyte( 24, f ); //PixelDepth;
405         cfwrite_ubyte( 0, f );  //ImageDesc;
406
407         memset(buf, 0, GL_viewport_w * GL_viewport_h * 3);
408
409         glReadPixels(GL_viewport_x, GL_viewport_y, GL_viewport_w, GL_viewport_h, GL_BGR, GL_UNSIGNED_BYTE, buf);
410
411         cfwrite(buf, GL_viewport_w * GL_viewport_h * 3, 1, f);
412
413         cfclose(f);
414
415         free(buf);
416 }
417
418 void gr_opengl1_fog_set(int fog_mode, int r, int g, int b, float fog_near, float fog_far)
419 {
420         SDL_assert((r >= 0) && (r < 256));
421         SDL_assert((g >= 0) && (g < 256));
422         SDL_assert((b >= 0) && (b < 256));
423
424         if (fog_mode == GR_FOGMODE_NONE) {
425                 if (gr_screen.current_fog_mode != fog_mode) {
426                         glDisable(GL_FOG);
427
428                         if (OGL_fog_mode == 1) {
429                                 glDisable(GL_COLOR_SUM);
430                         }
431                 }
432
433                 gr_screen.current_fog_mode = fog_mode;
434
435                 return;
436         }
437
438         if (gr_screen.current_fog_mode != fog_mode) {
439                 glEnable(GL_FOG);
440
441                 if (OGL_fog_mode == 1) {
442                         glEnable(GL_COLOR_SUM);
443                 } else if (OGL_fog_mode == 2) {
444                         glFogi(GL_FOG_MODE, GL_LINEAR);
445                 }
446
447                 gr_screen.current_fog_mode = fog_mode;
448         }
449
450         if ( (gr_screen.current_fog_color.red != r) ||
451                         (gr_screen.current_fog_color.green != g) ||
452                         (gr_screen.current_fog_color.blue != b) ) {
453                 GLfloat fc[4];
454
455                 gr_init_color( &gr_screen.current_fog_color, r, g, b );
456
457                 fc[0] = r / 255.0f;
458                 fc[1] = g / 255.0f;
459                 fc[2] = b / 255.0f;
460                 fc[3] = 1.0f;
461
462                 glFogfv(GL_FOG_COLOR, fc);
463         }
464
465         if( (fog_near >= 0.0f) && (fog_far >= 0.0f) &&
466                         ((fog_near != gr_screen.fog_near) ||
467                         (fog_far != gr_screen.fog_far)) ) {
468                 gr_screen.fog_near = fog_near;
469                 gr_screen.fog_far = fog_far;
470
471                 if (OGL_fog_mode == 2) {
472                         glFogf(GL_FOG_START, fog_near);
473                         glFogf(GL_FOG_END, fog_far);
474                 }
475         }
476 }
477
478 void gr_opengl1_set_cull(int cull)
479 {
480         if (cull) {
481                 glEnable (GL_CULL_FACE);
482                 glFrontFace (GL_CCW);
483         } else {
484                 glDisable (GL_CULL_FACE);
485         }
486 }
487
488 void gr_opengl1_zbuffer_clear(int mode)
489 {
490         if (mode) {
491                 Gr_zbuffering = 1;
492                 Gr_zbuffering_mode = GR_ZBUFF_FULL;
493                 Gr_global_zbuffering = 1;
494
495                 opengl1_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_NONE, ZBUFFER_TYPE_FULL );
496                 glClear ( GL_DEPTH_BUFFER_BIT );
497         } else {
498                 Gr_zbuffering = 0;
499                 Gr_zbuffering_mode = GR_ZBUFF_NONE;
500                 Gr_global_zbuffering = 0;
501         }
502 }
503
504 void gr_opengl1_fade_in(int instantaneous)
505 {
506         // Empty - DDOI
507 }
508
509 void gr_opengl1_fade_out(int instantaneous)
510 {
511         // Empty - DDOI
512 }
513
514 void gr_opengl1_get_region(int front, int w, int h, ubyte *data)
515 {
516         if (front) {
517                 glReadBuffer(GL_FRONT);
518         } else {
519                 glReadBuffer(GL_BACK);
520         }
521
522         opengl1_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
523
524         glPixelStorei(GL_UNPACK_ROW_LENGTH, GL_viewport_w);
525
526         int x = GL_viewport_x;
527         int y = (GL_viewport_y+GL_viewport_h)-h-1;
528
529         GLenum pxtype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
530
531         if (gr_screen.bytes_per_pixel == 4) {
532                 pxtype = GL_UNSIGNED_BYTE;
533         }
534
535         glReadPixels(x, y, w, h, GL_BGRA, pxtype, data);
536
537         glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
538 }
539
540 void gr_opengl1_save_mouse_area(int x, int y, int w, int h)
541 {
542         int x1, y1, x2, y2;
543
544         w = fl2i((w * GL_viewport_scale_w) + 0.5f);
545         h = fl2i((h * GL_viewport_scale_h) + 0.5f);
546
547         x1 = x;
548         y1 = y;
549         x2 = x+w-1;
550         y2 = y+h-1;
551
552         CAP(x1, 0, GL_viewport_w);
553         CAP(x2, 0, GL_viewport_w);
554         CAP(y1, 0, GL_viewport_h);
555         CAP(y2, 0, GL_viewport_h);
556
557         Gr_opengl_mouse_saved_x = x1;
558         Gr_opengl_mouse_saved_y = y1;
559         Gr_opengl_mouse_saved_w = x2 - x1 + 1;
560         Gr_opengl_mouse_saved_h = y2 - y1 + 1;
561
562         if ( (Gr_opengl_mouse_saved_w < 1) || (Gr_opengl_mouse_saved_h < 1) ) {
563                 return;
564         }
565
566         if (Gr_opengl_mouse_saved_data == NULL) {
567                 Gr_opengl_mouse_saved_data = (ubyte*)malloc(w * h * gr_screen.bytes_per_pixel);
568
569                 if ( !Gr_opengl_mouse_saved_data ) {
570                         return;
571                 }
572         }
573
574         opengl1_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
575
576         x1 = GL_viewport_x+Gr_opengl_mouse_saved_x;
577         y1 = (GL_viewport_y+GL_viewport_h)-Gr_opengl_mouse_saved_y-Gr_opengl_mouse_saved_h;
578
579         GLenum pxtype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
580
581         if (gr_screen.bytes_per_pixel == 4) {
582                 pxtype = GL_UNSIGNED_BYTE;
583         }
584
585         glReadBuffer(GL_BACK);
586         glReadPixels(x1, y1, Gr_opengl_mouse_saved_w, Gr_opengl_mouse_saved_h,
587                         GL_BGRA, pxtype, Gr_opengl_mouse_saved_data);
588
589         Gr_opengl_mouse_saved = 1;
590 }
591
592 int gr_opengl1_save_screen()
593 {
594         gr_reset_clip();
595
596         if (Gr_saved_screen_tex) {
597                 mprintf(( "Screen already saved!\n" ));
598                 return -1;
599         }
600
601         glGenTextures(1, &Gr_saved_screen_tex);
602
603         if ( !Gr_saved_screen_tex ) {
604                 mprintf(( "Couldn't create texture for saved screen!\n" ));
605                 return -1;
606         }
607
608         opengl1_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
609
610         glBindTexture(GL_TEXTURE_2D, Gr_saved_screen_tex);
611
612         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
613         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
614         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
615         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
616         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
617
618         glReadBuffer(GL_FRONT);
619         glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, GL_viewport_x, GL_viewport_y,
620                         GL_viewport_w, GL_viewport_h, 0);
621
622         if (Gr_opengl_mouse_saved) {
623                 int x = Gr_opengl_mouse_saved_x;
624                 int y = GL_viewport_h-Gr_opengl_mouse_saved_y-Gr_opengl_mouse_saved_h;
625
626                 GLenum pxtype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
627
628                 if (gr_screen.bytes_per_pixel == 4) {
629                         pxtype = GL_UNSIGNED_BYTE;
630                 }
631
632                 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, Gr_opengl_mouse_saved_w,
633                                 Gr_opengl_mouse_saved_h, GL_BGRA, pxtype,
634                                 Gr_opengl_mouse_saved_data);
635         }
636
637         glBindTexture(GL_TEXTURE_2D, 0);
638
639         return 0;
640 }
641
642 void gr_opengl1_restore_screen(int id)
643 {
644         gr_reset_clip();
645
646         if ( !Gr_saved_screen_tex ) {
647                 gr_clear();
648                 return;
649         }
650
651         glBindTexture(GL_TEXTURE_2D, Gr_saved_screen_tex);
652
653         if (Gr_opengl_mouse_saved) {
654                 int x = Gr_opengl_mouse_saved_x;
655                 int y = GL_viewport_h-Gr_opengl_mouse_saved_y-Gr_opengl_mouse_saved_h;
656
657                 GLenum pxtype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
658
659                 if (gr_screen.bytes_per_pixel == 4) {
660                         pxtype = GL_UNSIGNED_BYTE;
661                 }
662
663                 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, Gr_opengl_mouse_saved_w,
664                                 Gr_opengl_mouse_saved_h, GL_BGRA, pxtype,
665                                 Gr_opengl_mouse_saved_data);
666         }
667
668         glMatrixMode(GL_MODELVIEW);
669         glPushMatrix();
670         glLoadIdentity();
671         glScalef(1.0f, -1.0f, 1.0f);
672
673         int tex_coord[] = { 0, 0, 0, 1, 1, 0, 1, 1 };
674         int ver_coord[] = { GL_viewport_x, GL_viewport_y, GL_viewport_x,
675                         GL_viewport_h, GL_viewport_w, GL_viewport_y, GL_viewport_w,
676                         GL_viewport_h
677         };
678
679         glEnableClientState(GL_TEXTURE_COORD_ARRAY);
680         glEnableClientState(GL_VERTEX_ARRAY);
681
682         glTexCoordPointer(2, GL_INT, 0, &tex_coord);
683         glVertexPointer(2, GL_INT, 0, &ver_coord);
684
685         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
686
687         glDisableClientState(GL_TEXTURE_COORD_ARRAY);
688         glDisableClientState(GL_VERTEX_ARRAY);
689
690         glPopMatrix();
691
692         glBindTexture(GL_TEXTURE_2D, 0);
693 }
694
695 void gr_opengl1_free_screen(int id)
696 {
697         if (Gr_saved_screen_tex) {
698                 glDeleteTextures(1, &Gr_saved_screen_tex);
699                 Gr_saved_screen_tex = 0;
700         }
701 }
702
703 void gr_opengl1_dump_frame_start(int first_frame, int frames_between_dumps)
704 {
705         STUB_FUNCTION;
706 }
707
708 void gr_opengl1_dump_frame_stop()
709 {
710         STUB_FUNCTION;
711 }
712
713 void gr_opengl1_dump_frame()
714 {
715         STUB_FUNCTION;
716 }
717
718 uint gr_opengl1_lock()
719 {
720         return 1;
721 }
722
723 void gr_opengl1_unlock()
724 {
725 }
726
727 void gr_opengl1_zbias(int bias)
728 {
729         if (bias) {
730                 glEnable(GL_POLYGON_OFFSET_FILL);
731                 glPolygonOffset(0.0f, GLfloat(-bias));
732         } else {
733                 glDisable(GL_POLYGON_OFFSET_FILL);
734         }
735 }
736
737 void gr_opengl1_set_viewport(int width, int height)
738 {
739         int w, h, x, y;
740
741         float ratio = gr_screen.max_w / i2fl(gr_screen.max_h);
742
743         w = width;
744         h = fl2i((width / ratio) + 0.5f);
745
746         if (h > height) {
747                 h = height;
748                 w = fl2i((height * ratio) + 0.5f);
749         }
750
751         x = (width - w) / 2;
752         y = (height - h) / 2;
753
754         GL_viewport_x = x;
755         GL_viewport_y = y;
756         GL_viewport_w = w;
757         GL_viewport_h = h;
758         GL_viewport_scale_w = w / i2fl(gr_screen.max_w);
759         GL_viewport_scale_h = h / i2fl(gr_screen.max_h);
760
761         glViewport(GL_viewport_x, GL_viewport_y, GL_viewport_w, GL_viewport_h);
762
763         glMatrixMode(GL_PROJECTION);
764         glLoadIdentity();
765         glOrtho(0, GL_viewport_w, GL_viewport_h, 0, 0.0, 1.0);
766         glMatrixMode(GL_MODELVIEW);
767         glLoadIdentity();
768         glScalef(GL_viewport_scale_w, GL_viewport_scale_h, 1.0f);
769 }