]> icculus.org git repositories - taylor/freespace2.git/blob - src/graphics/grgl1.cpp
fix some mouse handling issues
[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 "SDL_opengl.h"
10
11 #include "gropengl.h"
12 #include "gropenglinternal.h"
13 #include "grgl1.h"
14 #include "2d.h"
15 #include "mouse.h"
16 #include "pstypes.h"
17 #include "cfile.h"
18 #include "bmpman.h"
19 #include "grinternal.h"
20 #include "osapi.h"
21 #include "osregistry.h"
22
23
24 int GL_one_inited = 0;
25
26
27 static GLuint GL_stream_tex = 0;
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_opengl_reset_clip();
105         gr_opengl_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         if (GL_context) {
118                 SDL_GL_DeleteContext(GL_context);
119                 GL_context = NULL;
120         }
121
122         GL_one_inited = 0;
123 }
124
125 static void opengl1_init_func_pointers()
126 {
127         gr_screen.gf_flip = gr_opengl1_flip;
128         gr_screen.gf_set_clip = gr_opengl1_set_clip;
129         gr_screen.gf_reset_clip = gr_opengl_reset_clip;
130
131         gr_screen.gf_clear = gr_opengl_clear;
132
133         gr_screen.gf_aabitmap = gr_opengl1_aabitmap;
134         gr_screen.gf_aabitmap_ex = gr_opengl1_aabitmap_ex;
135
136         gr_screen.gf_rect = gr_opengl1_rect;
137         gr_screen.gf_shade = gr_opengl1_shade;
138         gr_screen.gf_string = gr_opengl1_string;
139         gr_screen.gf_circle = gr_opengl1_circle;
140
141         gr_screen.gf_line = gr_opengl1_line;
142         gr_screen.gf_aaline = gr_opengl1_aaline;
143         gr_screen.gf_pixel = gr_opengl1_pixel;
144         gr_screen.gf_scaler = gr_opengl1_scaler;
145         gr_screen.gf_tmapper = gr_opengl1_tmapper;
146
147         gr_screen.gf_gradient = gr_opengl1_gradient;
148
149         gr_screen.gf_print_screen = gr_opengl_print_screen;
150
151         gr_screen.gf_fade_in = gr_opengl1_fade_in;
152         gr_screen.gf_fade_out = gr_opengl1_fade_out;
153         gr_screen.gf_flash = gr_opengl1_flash;
154
155         gr_screen.gf_zbuffer_clear = gr_opengl1_zbuffer_clear;
156
157         gr_screen.gf_save_screen = gr_opengl1_save_screen;
158         gr_screen.gf_restore_screen = gr_opengl1_restore_screen;
159         gr_screen.gf_free_screen = gr_opengl1_free_screen;
160
161         gr_screen.gf_dump_frame_start = gr_opengl1_dump_frame_start;
162         gr_screen.gf_dump_frame_stop = gr_opengl1_dump_frame_stop;
163         gr_screen.gf_dump_frame = gr_opengl1_dump_frame;
164
165         gr_screen.gf_stream_start = gr_opengl1_stream_start;
166         gr_screen.gf_stream_frame = gr_opengl1_stream_frame;
167         gr_screen.gf_stream_stop = gr_opengl1_stream_stop;
168
169         gr_screen.gf_set_gamma = gr_opengl1_set_gamma;
170
171         gr_screen.gf_lock = gr_opengl_lock;
172         gr_screen.gf_unlock = gr_opengl_unlock;
173
174         gr_screen.gf_fog_set = gr_opengl1_fog_set;
175
176         gr_screen.gf_get_region = gr_opengl1_get_region;
177
178         gr_screen.gf_set_cull = gr_opengl_set_cull;
179
180         gr_screen.gf_cross_fade = gr_opengl1_cross_fade;
181
182         gr_screen.gf_preload_init = gr_opengl1_preload_init;
183         gr_screen.gf_preload = gr_opengl1_preload;
184
185         gr_screen.gf_zbias = gr_opengl_zbias;
186
187         gr_screen.gf_set_viewport = gr_opengl1_set_viewport;
188
189         gr_screen.gf_activate = gr_opengl_activate;
190
191         gr_screen.gf_release_texture = gr_opengl1_release_texture;
192 }
193
194 int opengl1_init()
195 {
196         if (GL_one_inited) {
197                 return 1;
198         }
199
200         GL_one_inited = 1;
201
202         SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, 0);
203         SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
204         SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
205
206         GL_context = SDL_GL_CreateContext(GL_window);
207
208         if ( !GL_context ) {
209                 opengl1_cleanup();
210                 return 0;
211         }
212
213         mprintf(("  Vendor   : %s\n", glGetString(GL_VENDOR)));
214         mprintf(("  Renderer : %s\n", glGetString(GL_RENDERER)));
215         mprintf(("  Version  : %s\n", glGetString(GL_VERSION)));
216
217         // set up generic variables
218         opengl_set_variables();
219
220         opengl1_init_func_pointers();
221         opengl1_tcache_init();
222
223         // initial viewport setup
224         gr_opengl1_set_viewport(gr_screen.max_w, gr_screen.max_h);
225
226         glShadeModel(GL_SMOOTH);
227         glEnable(GL_DITHER);
228         glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
229         glHint(GL_FOG_HINT, GL_NICEST);
230
231         glEnable(GL_DEPTH_TEST);
232         glEnable(GL_BLEND);
233
234         glEnable(GL_TEXTURE_2D);
235
236         glDepthRange(0.0, 1.0);
237
238         glPixelStorei(GL_PACK_ALIGNMENT, 1);
239         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
240
241         glFlush();
242
243         gr_opengl_clear();
244         gr_opengl_set_cull(1);
245
246         return 1;
247 }
248
249 void gr_opengl1_flip()
250 {
251         if ( !GL_one_inited ) {
252                 return;
253         }
254
255         gr_reset_clip();
256
257         mouse_eval_deltas();
258
259         Gr_opengl_mouse_saved = 0;
260
261         if ( mouse_is_visible() )       {
262                 int mx, my;
263
264                 mouse_get_pos( &mx, &my );
265
266                 gr_opengl1_save_mouse_area(mx,my,32,32);
267
268                 if (Gr_cursor == -1) {
269 #ifndef NDEBUG
270                         gr_set_color(255,255,255);
271                         gr_line(mx, my, mx+7, my + 7);
272                         gr_line(mx, my, mx+5, my );
273                         gr_line(mx, my, mx, my+5);
274 #endif
275                 } else {
276                         gr_set_bitmap(Gr_cursor);
277                         gr_bitmap(mx, my);
278                 }
279          }
280
281 #ifndef NDEBUG
282         GLenum error = GL_NO_ERROR;
283
284         do {
285                 error = glGetError();
286
287                 if (error != GL_NO_ERROR) {
288                         nprintf(("Warning", "!!DEBUG!! OpenGL Error: %d\n", error));
289                 }
290         } while (error != GL_NO_ERROR);
291 #endif
292
293         SDL_GL_SwapWindow(GL_window);
294
295         opengl1_tcache_frame();
296
297         int cnt = GL_activate;
298         if ( cnt )      {
299                 GL_activate-=cnt;
300                 opengl1_tcache_flush();
301                 // gr_opengl_clip_cursor(1); /* mouse grab, see opengl_activate */
302         }
303
304         cnt = GL_deactivate;
305         if ( cnt )      {
306                 GL_deactivate-=cnt;
307                 // gr_opengl_clip_cursor(0);  /* mouse grab, see opengl_activate */
308         }
309 }
310
311 void gr_opengl1_set_clip(int x, int y, int w, int h)
312 {
313         // check for sanity of parameters
314         CAP(x, 0, gr_screen.max_w - 1);
315         CAP(y, 0, gr_screen.max_h - 1);
316         CAP(w, 0, gr_screen.max_w - x);
317         CAP(h, 0, gr_screen.max_h - y);
318
319         gr_screen.offset_x = x;
320         gr_screen.offset_y = y;
321         gr_screen.clip_left = 0;
322         gr_screen.clip_right = w-1;
323         gr_screen.clip_top = 0;
324         gr_screen.clip_bottom = h-1;
325         gr_screen.clip_width = w;
326         gr_screen.clip_height = h;
327
328         x = fl2i((x * GL_viewport_scale_w) + 0.5f) + GL_viewport_x;
329         y = fl2i((y * GL_viewport_scale_h) + 0.5f) + GL_viewport_y;
330         w = fl2i((w * GL_viewport_scale_w) + 0.5f);
331         h = fl2i((h * GL_viewport_scale_h) + 0.5f);
332
333         glEnable(GL_SCISSOR_TEST);
334         glScissor(x, GL_viewport_h-y-h, w, h);
335 }
336
337 void gr_opengl1_fog_set(int fog_mode, int r, int g, int b, float fog_near, float fog_far)
338 {
339         SDL_assert((r >= 0) && (r < 256));
340         SDL_assert((g >= 0) && (g < 256));
341         SDL_assert((b >= 0) && (b < 256));
342
343         if (fog_mode == GR_FOGMODE_NONE) {
344                 if (gr_screen.current_fog_mode != fog_mode) {
345                         glDisable(GL_FOG);
346                 }
347
348                 gr_screen.current_fog_mode = fog_mode;
349
350                 return;
351         }
352
353         if (gr_screen.current_fog_mode != fog_mode) {
354                 glEnable(GL_FOG);
355                 glFogi(GL_FOG_MODE, GL_LINEAR);
356
357                 gr_screen.current_fog_mode = fog_mode;
358         }
359
360         if ( (gr_screen.current_fog_color.red != r) ||
361                         (gr_screen.current_fog_color.green != g) ||
362                         (gr_screen.current_fog_color.blue != b) ) {
363                 gr_init_color( &gr_screen.current_fog_color, r, g, b );
364
365                 GLfloat fc[4];
366
367                 fc[0] = r / 255.0f;
368                 fc[1] = g / 255.0f;
369                 fc[2] = b / 255.0f;
370                 fc[3] = 1.0f;
371
372                 glFogfv(GL_FOG_COLOR, fc);
373         }
374
375         if( (fog_near >= 0.0f) && (fog_far >= 0.0f) &&
376                         ((fog_near != gr_screen.fog_near) ||
377                         (fog_far != gr_screen.fog_far)) ) {
378                 gr_screen.fog_near = fog_near;
379                 gr_screen.fog_far = fog_far;
380
381                 glFogf(GL_FOG_START, fog_near);
382                 glFogf(GL_FOG_END, fog_far);
383         }
384 }
385
386 void gr_opengl1_zbuffer_clear(int mode)
387 {
388         if (mode) {
389                 Gr_zbuffering = 1;
390                 Gr_zbuffering_mode = GR_ZBUFF_FULL;
391                 Gr_global_zbuffering = 1;
392
393                 opengl1_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_NONE, ZBUFFER_TYPE_FULL );
394                 glClear ( GL_DEPTH_BUFFER_BIT );
395         } else {
396                 Gr_zbuffering = 0;
397                 Gr_zbuffering_mode = GR_ZBUFF_NONE;
398                 Gr_global_zbuffering = 0;
399         }
400 }
401
402 void gr_opengl1_fade_in(int instantaneous)
403 {
404         // Empty - DDOI
405 }
406
407 void gr_opengl1_fade_out(int instantaneous)
408 {
409         // Empty - DDOI
410 }
411
412 void gr_opengl1_get_region(int front, int w, int h, ubyte *data)
413 {
414         if (front) {
415                 glReadBuffer(GL_FRONT);
416         } else {
417                 glReadBuffer(GL_BACK);
418         }
419
420         opengl1_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
421
422         glPixelStorei(GL_UNPACK_ROW_LENGTH, GL_viewport_w);
423
424         int x = GL_viewport_x;
425         int y = (GL_viewport_y+GL_viewport_h)-h-1;
426
427         GLenum pxtype = GL_UNSIGNED_SHORT_5_5_5_1;
428
429         if (gr_screen.bytes_per_pixel == 4) {
430                 pxtype = GL_UNSIGNED_BYTE;
431         }
432
433         glReadPixels(x, y, w, h, GL_RGBA, pxtype, data);
434
435         glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
436 }
437
438 void gr_opengl1_save_mouse_area(int x, int y, int w, int h)
439 {
440         int x1, y1, x2, y2;
441
442         if (Gr_saved_screen_tex) {
443                 // already saved, don't need it again
444                 return;
445         }
446
447         w = fl2i((w * GL_viewport_scale_w) + 0.5f);
448         h = fl2i((h * GL_viewport_scale_h) + 0.5f);
449
450         x1 = x;
451         y1 = y;
452         x2 = x+w-1;
453         y2 = y+h-1;
454
455         CAP(x1, 0, GL_viewport_w);
456         CAP(x2, 0, GL_viewport_w);
457         CAP(y1, 0, GL_viewport_h);
458         CAP(y2, 0, GL_viewport_h);
459
460         Gr_opengl_mouse_saved_x = x1;
461         Gr_opengl_mouse_saved_y = y1;
462         Gr_opengl_mouse_saved_w = x2 - x1 + 1;
463         Gr_opengl_mouse_saved_h = y2 - y1 + 1;
464
465         if ( (Gr_opengl_mouse_saved_w < 1) || (Gr_opengl_mouse_saved_h < 1) ) {
466                 return;
467         }
468
469         if (Gr_opengl_mouse_saved_data == NULL) {
470                 Gr_opengl_mouse_saved_data = (ubyte*)malloc(w * h * 3);
471
472                 if ( !Gr_opengl_mouse_saved_data ) {
473                         return;
474                 }
475         }
476
477         x1 = GL_viewport_x+Gr_opengl_mouse_saved_x;
478         y1 = (GL_viewport_y+GL_viewport_h)-Gr_opengl_mouse_saved_y-Gr_opengl_mouse_saved_h;
479
480         glReadBuffer(GL_BACK);
481
482         glReadPixels(x1, y1, Gr_opengl_mouse_saved_w, Gr_opengl_mouse_saved_h,
483                         GL_RGB, GL_UNSIGNED_BYTE, Gr_opengl_mouse_saved_data);
484
485         Gr_opengl_mouse_saved = 1;
486 }
487
488 int gr_opengl1_save_screen()
489 {
490         gr_reset_clip();
491
492         if (Gr_saved_screen_tex) {
493                 mprintf(( "Screen already saved!\n" ));
494                 return -1;
495         }
496
497         glGenTextures(1, &Gr_saved_screen_tex);
498
499         if ( !Gr_saved_screen_tex ) {
500                 mprintf(( "Couldn't create texture for saved screen!\n" ));
501                 return -1;
502         }
503
504         glBindTexture(GL_TEXTURE_2D, Gr_saved_screen_tex);
505
506         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
507         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
508         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
509         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
510         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
511
512         glReadBuffer(GL_FRONT);
513
514         glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, GL_viewport_x, GL_viewport_y,
515                         GL_viewport_w, GL_viewport_h, 0);
516
517         if (Gr_opengl_mouse_saved) {
518                 int x = Gr_opengl_mouse_saved_x;
519                 int y = GL_viewport_h-Gr_opengl_mouse_saved_y-Gr_opengl_mouse_saved_h;
520
521                 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, Gr_opengl_mouse_saved_w,
522                                 Gr_opengl_mouse_saved_h, GL_RGB, GL_UNSIGNED_BYTE,
523                                 Gr_opengl_mouse_saved_data);
524         }
525
526         glBindTexture(GL_TEXTURE_2D, 0);
527
528         return 0;
529 }
530
531 void gr_opengl1_restore_screen(int)
532 {
533         gr_reset_clip();
534
535         if ( !Gr_saved_screen_tex ) {
536                 gr_clear();
537                 return;
538         }
539
540         int x = 0;
541         int y = 0;
542         int w = fl2i(GL_viewport_w / GL_viewport_scale_w + 0.5f);
543         int h = fl2i(GL_viewport_h / GL_viewport_scale_h + 0.5f);
544
545         const int tex_coord[] = { 0, 1, 0, 0, 1, 1, 1, 0 };     // y-flipped
546         const int ver_coord[] = { x, y, x, h, w, y, w, h };
547
548         glColor4ub(255, 255, 255, 255);
549
550         glBindTexture(GL_TEXTURE_2D, Gr_saved_screen_tex);
551
552         opengl1_set_state(TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_NONE, ZBUFFER_TYPE_NONE);
553
554         glEnableClientState(GL_TEXTURE_COORD_ARRAY);
555         glEnableClientState(GL_VERTEX_ARRAY);
556
557         glTexCoordPointer(2, GL_INT, 0, &tex_coord);
558         glVertexPointer(2, GL_INT, 0, &ver_coord);
559
560         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
561
562         glDisableClientState(GL_TEXTURE_COORD_ARRAY);
563         glDisableClientState(GL_VERTEX_ARRAY);
564
565         glBindTexture(GL_TEXTURE_2D, 0);
566 }
567
568 void gr_opengl1_free_screen(int)
569 {
570         if (Gr_saved_screen_tex) {
571                 glDeleteTextures(1, &Gr_saved_screen_tex);
572                 Gr_saved_screen_tex = 0;
573         }
574 }
575
576 void gr_opengl1_dump_frame_start(int first_frame, int frames_between_dumps)
577 {
578         STUB_FUNCTION;
579 }
580
581 void gr_opengl1_dump_frame_stop()
582 {
583         STUB_FUNCTION;
584 }
585
586 void gr_opengl1_dump_frame()
587 {
588         STUB_FUNCTION;
589 }
590
591 static int GL_stream_w = 0;
592 static int GL_stream_h = 0;
593 static bool GL_stream_scale = false;
594 static float GL_stream_scale_by = 1.0f;
595
596 static rb_t GL_stream[4];
597
598 void gr_opengl1_stream_start(int x, int y, int w, int h)
599 {
600         if (GL_stream_tex) {
601                 return;
602         }
603
604         if (gr_screen.use_sections) {
605                 mprintf(("GR_STREAM: Bitmap sections not supported\n"));
606                 return;
607         }
608
609         int tex_w = next_pow2(w);
610         int tex_h = next_pow2(h);
611
612         glGenTextures(1, &GL_stream_tex);
613
614         glBindTexture(GL_TEXTURE_2D, GL_stream_tex);
615
616         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
617         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
618         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
619         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
620
621         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_w, tex_h, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, NULL);
622
623         glBindTexture(GL_TEXTURE_2D, 0);
624
625         uint scale = os_config_read_uint("Video", "ScaleMovies", 1);
626
627         if (scale) {
628                 GL_stream_scale = true;
629
630                 GL_stream_scale_by = GL_viewport_w / i2fl(w);
631         } else {
632                 GL_stream_scale = false;
633         }
634
635         int sx, sy;
636
637         if (x < 0) {
638                 sx = scale ? 0 : ((gr_screen.max_w - w) / 2);
639         } else {
640                 sx = x;
641         }
642
643         if (y < 0) {
644                 sy = scale ? ((480 - h) / 2) : ((gr_screen.max_h - h) / 2);
645         } else {
646                 sy = y;
647         }
648
649         GL_stream_w = w;
650         GL_stream_h = h;
651
652         GL_stream[0].x = i2fl(sx);
653         GL_stream[0].y = i2fl(sy);
654         GL_stream[0].u = 0.0f;
655         GL_stream[0].v = 0.0f;
656
657         GL_stream[1].x = i2fl(sx);
658         GL_stream[1].y = i2fl(sy + h);
659         GL_stream[1].u = 0.0f;
660         GL_stream[1].v = i2fl(h) / i2fl(tex_h);
661
662         GL_stream[2].x = i2fl(sx + w);
663         GL_stream[2].y = i2fl(sy);
664         GL_stream[2].u = i2fl(w) / i2fl(tex_w);
665         GL_stream[2].v = 0.0f;
666
667         GL_stream[3].x = i2fl(sx + w);
668         GL_stream[3].y = i2fl(sy + h);
669         GL_stream[3].u = i2fl(w) / i2fl(tex_w);
670         GL_stream[3].v = i2fl(h) / i2fl(tex_h);
671
672         glDisable(GL_DEPTH_TEST);
673 }
674
675 void gr_opengl1_stream_frame(ubyte *frame)
676 {
677         if ( !GL_stream_tex ) {
678                 return;
679         }
680
681         glEnableClientState(GL_VERTEX_ARRAY);
682         glVertexPointer(2, GL_FLOAT, sizeof(rb_t), &GL_stream[0].x);
683
684         glEnableClientState(GL_TEXTURE_COORD_ARRAY);
685         glTexCoordPointer(2, GL_FLOAT, sizeof(rb_t), &GL_stream[0].u);
686
687         glBindTexture(GL_TEXTURE_2D, GL_stream_tex);
688
689         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, GL_stream_w, GL_stream_h, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, frame);
690
691         if (GL_stream_scale) {
692                 glPushMatrix();
693                 glLoadIdentity();
694                 glScalef(GL_stream_scale_by, GL_stream_scale_by, 1.0f);
695         }
696
697         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
698
699         if (GL_stream_scale) {
700                 glPopMatrix();
701         }
702
703         glBindTexture(GL_TEXTURE_2D, 0);
704
705         glDisableClientState(GL_TEXTURE_COORD_ARRAY);
706         glDisableClientState(GL_VERTEX_ARRAY);
707 }
708
709 void gr_opengl1_stream_stop()
710 {
711         if (GL_stream_tex) {
712                 glBindTexture(GL_TEXTURE_2D, 0);
713                 glDeleteTextures(1, &GL_stream_tex);
714                 GL_stream_tex = 0;
715
716                 glEnable(GL_DEPTH_TEST);
717         }
718 }
719
720 void gr_opengl1_set_viewport(int width, int height)
721 {
722         int w, h, x, y;
723
724         float ratio = gr_screen.max_w / i2fl(gr_screen.max_h);
725
726         w = width;
727         h = fl2i((width / ratio) + 0.5f);
728
729         if (h > height) {
730                 h = height;
731                 w = fl2i((height * ratio) + 0.5f);
732         }
733
734         x = (width - w) / 2;
735         y = (height - h) / 2;
736
737         GL_viewport_x = x;
738         GL_viewport_y = y;
739         GL_viewport_w = w;
740         GL_viewport_h = h;
741         GL_viewport_scale_w = w / i2fl(gr_screen.max_w);
742         GL_viewport_scale_h = h / i2fl(gr_screen.max_h);
743
744         glViewport(GL_viewport_x, GL_viewport_y, GL_viewport_w, GL_viewport_h);
745
746         glMatrixMode(GL_PROJECTION);
747         glLoadIdentity();
748         glOrtho(0, GL_viewport_w, GL_viewport_h, 0, 0.0, 1.0);
749         glMatrixMode(GL_MODELVIEW);
750         glLoadIdentity();
751         glScalef(GL_viewport_scale_w, GL_viewport_scale_h, 1.0f);
752
753         // free mouse cursor storage, since the size might have changed
754         if (Gr_opengl_mouse_saved_data) {
755                 free(Gr_opengl_mouse_saved_data);
756                 Gr_opengl_mouse_saved_data = NULL;
757         }
758
759         // clear screen once to fix issues with edges on non-4:3
760         gr_opengl_clear();
761 }