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