Debian Codegraph @

prboom2/src/SDL/i_video.c

raw
1 /* Emacs style mode select -*- C -*-
2 *-----------------------------------------------------------------------------
3 *
4 *
5 * PrBoom: a Doom port merged with LxDoom and LSDLDoom
6 * based on BOOM, a modified and improved DOOM engine
7 * Copyright (C) 1999 by
8 * id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
9 * Copyright (C) 1999-2006 by
10 * Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
11 * Copyright 2005, 2006 by
12 * Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 * 02111-1307, USA.
28 *
29 * DESCRIPTION:
30 * DOOM graphics stuff for SDL
31 *
32 *-----------------------------------------------------------------------------
33 */
34
35 #include <SDL_render.h>
36 #include <SDL_video.h>
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40
41 #ifdef _WIN32
42 #ifndef WIN32_LEAN_AND_MEAN
43 #define WIN32_LEAN_AND_MEAN 1
44 #endif
45 #include <windows.h>
46 #endif // _WIN32
47
48 #include <stdlib.h>
49
50 #ifdef HAVE_UNISTD_H
51 #include <unistd.h>
52 #endif
53
54 #include "SDL.h"
55 //e6y
56 #ifdef _WIN32
57 #include <SDL_syswm.h>
58 #endif
59
60 #include "doomstat.h"
61 #include "doomdef.h"
62 #include "doomtype.h"
63 #include "v_video.h"
64 #include "r_draw.h"
65 #include "r_things.h"
66 #include "r_plane.h"
67 #include "r_main.h"
68 #include "f_wipe.h"
69 #include "d_main.h"
70 #include "d_event.h"
71 #include "d_deh.h"
72 #include "i_video.h"
73 #include "i_capture.h"
74 #include "z_zone.h"
75 #include "s_sound.h"
76 #include "sounds.h"
77 #include "w_wad.h"
78 #include "st_stuff.h"
79 #include "am_map.h"
80 #include "g_game.h"
81 #include "lprintf.h"
82 #include "i_system.h"
83 #include "gl_struct.h"
84
85 #include "e6y.h"//e6y
86 #include "i_main.h"
87
88 #include "dsda/args.h"
89 #include "dsda/configuration.h"
90 #include "dsda/game_controller.h"
91 #include "dsda/palette.h"
92 #include "dsda/pause.h"
93 #include "dsda/settings.h"
94 #include "dsda/skip.h"
95 #include "dsda/time.h"
96 #include "dsda/gl/render_scale.h"
97
98 //e6y: new mouse code
99 static SDL_Cursor* cursors[2] = {NULL, NULL};
100
101 dboolean window_focused;
102
103 // Window resize state.
104 static void ApplyWindowResize(SDL_Event *resize_event);
105
106 static void ActivateMouse(void);
107 static void DeactivateMouse(void);
108 //static int AccelerateMouse(int val);
109 static void I_ReadMouse(void);
110 static dboolean MouseShouldBeGrabbed();
111 static void UpdateFocus(void);
112
113 extern const int gl_colorbuffer_bits;
114 extern const int gl_depthbuffer_bits;
115
116 extern void M_QuitDOOM(int choice);
117 int desired_fullscreen;
118 int exclusive_fullscreen;
119 SDL_Surface *screen;
120 static SDL_Surface *buffer;
121 SDL_Window *sdl_window;
122 SDL_Renderer *sdl_renderer;
123 SDL_Texture *sdl_texture;
124 static SDL_GLContext sdl_glcontext;
125 unsigned int windowid = 0;
126 SDL_Rect src_rect = { 0, 0, 0, 0 }; // Drawn pixels, independent of window size
127 SDL_Rect window_rect = { 0, 0, 0, 0 }; // Physical window
128 SDL_Rect renderer_rect = { 0, 0, 0, 0 }; // The window, but with HiDPI accounted
129 SDL_Rect viewport_rect = { 0, 0, 0, 0 }; // The renderer, but without the black bars
130
131 ////////////////////////////////////////////////////////////////////////////
132 // Input code
133 int leds_always_off = 0; // Expected by m_misc, not relevant
134
135 // Mouse handling
136 static dboolean mouse_enabled; // usemouse, but can be overriden by -nomouse
137 int mouse_hide_timer = 0;
138
139 /////////////////////////////////////////////////////////////////////////////////
140 // Keyboard handling
141
142 // Vanilla keymap taken from chocolate-doom and adjusted for prboom-plus
143 #define SCANCODE_TO_KEYS_ARRAY { \
144 0, 0, 0, 0, 'a', /* 0-9 */ \
145 'b', 'c', 'd', 'e', 'f', \
146 'g', 'h', 'i', 'j', 'k', /* 10-19 */ \
147 'l', 'm', 'n', 'o', 'p', \
148 'q', 'r', 's', 't', 'u', /* 20-29 */ \
149 'v', 'w', 'x', 'y', 'z', \
150 '1', '2', '3', '4', '5', /* 30-39 */ \
151 '6', '7', '8', '9', '0', \
152 KEYD_ENTER, KEYD_ESCAPE, KEYD_BACKSPACE, KEYD_TAB, ' ', /* 40-49 */ \
153 KEYD_MINUS, KEYD_EQUALS, '[', ']', '\\', \
154 '\\', ';', '\'', '`', ',', /* 50-59 */ \
155 '.', '/', KEYD_CAPSLOCK, KEYD_F1, KEYD_F2, \
156 KEYD_F3, KEYD_F4, KEYD_F5, KEYD_F6, KEYD_F7, /* 60-69 */ \
157 KEYD_F8, KEYD_F9, KEYD_F10, KEYD_F11, KEYD_F12, KEYD_PRINTSC, \
158 KEYD_SCROLLLOCK, KEYD_PAUSE, KEYD_INSERT, KEYD_HOME, /* 70-79 */ \
159 KEYD_PAGEUP, KEYD_DEL, KEYD_END, KEYD_PAGEDOWN, KEYD_RIGHTARROW, \
160 KEYD_LEFTARROW, KEYD_DOWNARROW, KEYD_UPARROW, /* 80-89 */ \
161 KEYD_NUMLOCK, KEYD_KEYPADDIVIDE, \
162 KEYD_KEYPADMULTIPLY, KEYD_KEYPADMINUS, KEYD_KEYPADPLUS, \
163 KEYD_KEYPADENTER, KEYD_KEYPAD1, KEYD_KEYPAD2, KEYD_KEYPAD3, \
164 KEYD_KEYPAD4, KEYD_KEYPAD5, KEYD_KEYPAD6, /* 90-99 */ \
165 KEYD_KEYPAD7, KEYD_KEYPAD8, KEYD_KEYPAD9, KEYD_KEYPAD0, \
166 KEYD_KEYPADPERIOD, 0, 0, 0, KEYD_EQUALS /* 100-103 */ \
167 }
168
169 // Map keys like vanilla doom
170 static int VanillaTranslateKey(SDL_Keysym* key)
171 {
172 static const int scancode_map[] = SCANCODE_TO_KEYS_ARRAY;
173 int rc = 0, sc = key->scancode;
174
175 if (sc > 3 && sc < sizeof(scancode_map) / sizeof(scancode_map[0]))
176 rc = scancode_map[sc];
177
178 // Key is mapped..
179 if (rc)
180 return rc;
181
182 switch (sc) { // Code (Ctrl/Shift/Alt) from scancode.
183 case SDL_SCANCODE_LSHIFT:
184 case SDL_SCANCODE_RSHIFT:
185 return KEYD_RSHIFT;
186
187 case SDL_SCANCODE_LCTRL:
188 case SDL_SCANCODE_RCTRL:
189 return KEYD_RCTRL;
190
191 case SDL_SCANCODE_LALT:
192 case SDL_SCANCODE_RALT:
193 case SDL_SCANCODE_LGUI:
194 case SDL_SCANCODE_RGUI:
195 return KEYD_RALT;
196
197 // Default to the symbolic key (outside of vanilla keys)
198 default:
199 return key->sym;
200 }
201 }
202
203 //
204 // Translates the key currently in key
205 //
206
207 static int I_TranslateKey(SDL_Keysym* key)
208 {
209 int rc = 0;
210
211 if (dsda_IntConfig(dsda_config_vanilla_keymap))
212 return VanillaTranslateKey(key);
213
214 switch (key->sym) {
215 case SDLK_LEFT: rc = KEYD_LEFTARROW; break;
216 case SDLK_RIGHT: rc = KEYD_RIGHTARROW; break;
217 case SDLK_DOWN: rc = KEYD_DOWNARROW; break;
218 case SDLK_UP: rc = KEYD_UPARROW; break;
219 case SDLK_ESCAPE: rc = KEYD_ESCAPE; break;
220 case SDLK_RETURN: rc = KEYD_ENTER; break;
221 case SDLK_TAB: rc = KEYD_TAB; break;
222 case SDLK_F1: rc = KEYD_F1; break;
223 case SDLK_F2: rc = KEYD_F2; break;
224 case SDLK_F3: rc = KEYD_F3; break;
225 case SDLK_F4: rc = KEYD_F4; break;
226 case SDLK_F5: rc = KEYD_F5; break;
227 case SDLK_F6: rc = KEYD_F6; break;
228 case SDLK_F7: rc = KEYD_F7; break;
229 case SDLK_F8: rc = KEYD_F8; break;
230 case SDLK_F9: rc = KEYD_F9; break;
231 case SDLK_F10: rc = KEYD_F10; break;
232 case SDLK_F11: rc = KEYD_F11; break;
233 case SDLK_F12: rc = KEYD_F12; break;
234 case SDLK_BACKSPACE: rc = KEYD_BACKSPACE; break;
235 case SDLK_DELETE: rc = KEYD_DEL; break;
236 case SDLK_INSERT: rc = KEYD_INSERT; break;
237 case SDLK_PAGEUP: rc = KEYD_PAGEUP; break;
238 case SDLK_PAGEDOWN: rc = KEYD_PAGEDOWN; break;
239 case SDLK_HOME: rc = KEYD_HOME; break;
240 case SDLK_END: rc = KEYD_END; break;
241 case SDLK_PAUSE: rc = KEYD_PAUSE; break;
242 case SDLK_EQUALS: rc = KEYD_EQUALS; break;
243 case SDLK_MINUS: rc = KEYD_MINUS; break;
244 case SDLK_KP_0: rc = KEYD_KEYPAD0; break;
245 case SDLK_KP_1: rc = KEYD_KEYPAD1; break;
246 case SDLK_KP_2: rc = KEYD_KEYPAD2; break;
247 case SDLK_KP_3: rc = KEYD_KEYPAD3; break;
248 case SDLK_KP_4: rc = KEYD_KEYPAD4; break;
249 case SDLK_KP_5: rc = KEYD_KEYPAD5; break;
250 case SDLK_KP_6: rc = KEYD_KEYPAD6; break;
251 case SDLK_KP_7: rc = KEYD_KEYPAD7; break;
252 case SDLK_KP_8: rc = KEYD_KEYPAD8; break;
253 case SDLK_KP_9: rc = KEYD_KEYPAD9; break;
254 case SDLK_KP_PLUS: rc = KEYD_KEYPADPLUS; break;
255 case SDLK_KP_MINUS: rc = KEYD_KEYPADMINUS; break;
256 case SDLK_KP_DIVIDE: rc = KEYD_KEYPADDIVIDE; break;
257 case SDLK_KP_MULTIPLY: rc = KEYD_KEYPADMULTIPLY; break;
258 case SDLK_KP_ENTER: rc = KEYD_KEYPADENTER; break;
259 case SDLK_KP_PERIOD: rc = KEYD_KEYPADPERIOD; break;
260 case SDLK_LSHIFT:
261 case SDLK_RSHIFT: rc = KEYD_RSHIFT; break;
262 case SDLK_LCTRL:
263 case SDLK_RCTRL: rc = KEYD_RCTRL; break;
264 case SDLK_LALT:
265 case SDLK_LGUI:
266 case SDLK_RALT:
267 case SDLK_RGUI: rc = KEYD_RALT; break;
268 case SDLK_CAPSLOCK: rc = KEYD_CAPSLOCK; break;
269 case SDLK_PRINTSCREEN: rc = KEYD_PRINTSC; break;
270 case SDLK_SCROLLLOCK: rc = KEYD_SCROLLLOCK; break;
271 default: rc = key->sym; break;
272 }
273
274 return rc;
275
276 }
277
278 dboolean I_WindowFocused(void)
279 {
280 return window_focused;
281 }
282
283 /////////////////////////////////////////////////////////////////////////////////
284 // Main input code
285
286 /* cph - pulled out common button code logic */
287 //e6y static
288 int I_SDLtoDoomMouseState(Uint32 buttonstate)
289 {
290 return 0
291 | (buttonstate & SDL_BUTTON(1) ? 1 : 0)
292 | (buttonstate & SDL_BUTTON(2) ? 2 : 0)
293 | (buttonstate & SDL_BUTTON(3) ? 4 : 0)
294 | (buttonstate & SDL_BUTTON(6) ? 8 : 0)
295 | (buttonstate & SDL_BUTTON(7) ? 16 : 0)
296 | (buttonstate & SDL_BUTTON(4) ? 32 : 0)
297 | (buttonstate & SDL_BUTTON(5) ? 64 : 0)
298 | (buttonstate & SDL_BUTTON(8) ? 128 : 0)
299 ;
300 }
301
302 static void I_GetEvent(void)
303 {
304 event_t event;
305
306 SDL_Event SDLEvent;
307 SDL_Event *Event = &SDLEvent;
308
309 while (SDL_PollEvent(Event))
310 {
311 switch (Event->type) {
312 case SDL_KEYDOWN:
313 #ifdef __APPLE__
314 if (Event->key.keysym.mod & KMOD_GUI)
315 {
316 // Switch windowed<->fullscreen if pressed <Command-F>
317 if (Event->key.keysym.sym == SDLK_f)
318 {
319 V_ToggleFullscreen();
320 break;
321 }
322 }
323 #else
324 if (Event->key.keysym.mod & KMOD_LALT)
325 {
326 // Prevent executing action on Alt-Tab
327 if (Event->key.keysym.sym == SDLK_TAB)
328 {
329 break;
330 }
331 // Switch windowed<->fullscreen if pressed Alt-Enter
332 else if (Event->key.keysym.sym == SDLK_RETURN)
333 {
334 V_ToggleFullscreen();
335 break;
336 }
337 // Immediately exit on Alt+F4 ("Boss Key")
338 else if (Event->key.keysym.sym == SDLK_F4)
339 {
340 I_SafeExit(0);
341 break;
342 }
343 }
344 #endif
345 event.type = ev_keydown;
346 event.data1.i = I_TranslateKey(&Event->key.keysym);
347 D_PostEvent(&event);
348 break;
349
350 case SDL_KEYUP:
351 {
352 event.type = ev_keyup;
353 event.data1.i = I_TranslateKey(&Event->key.keysym);
354 D_PostEvent(&event);
355 }
356 break;
357
358 case SDL_MOUSEBUTTONDOWN:
359 case SDL_MOUSEBUTTONUP:
360 if (mouse_enabled && window_focused)
361 {
362 event.type = ev_mouse;
363 event.data1.i = I_SDLtoDoomMouseState(SDL_GetMouseState(NULL, NULL));
364 D_PostEvent(&event);
365 }
366 break;
367
368 case SDL_MOUSEWHEEL:
369 if (mouse_enabled && window_focused)
370 {
371 int mouseb;
372
373 if (Event->wheel.y > 0)
374 mouseb = KEYD_MWHEELUP;
375 else if (Event->wheel.y < 0)
376 mouseb = KEYD_MWHEELDOWN;
377 else if (Event->wheel.x < 0)
378 mouseb = KEYD_MWHEELLEFT;
379 else if (Event->wheel.x > 0)
380 mouseb = KEYD_MWHEELRIGHT;
381 else
382 mouseb = 0;
383
384 if(mouseb)
385 {
386 event.data1.i = mouseb;
387
388 event.type = ev_keydown;
389 D_PostEvent(&event);
390
391 event.type = ev_keyup;
392 D_PostEvent(&event);
393 }
394 }
395 break;
396
397 case SDL_CONTROLLERBUTTONDOWN:
398 case SDL_CONTROLLERBUTTONUP:
399 if (dsda_AllowGameController())
400 dsda_PollGameControllerButtons();
401 break;
402
403 case SDL_TEXTINPUT:
404 event.type = ev_text;
405 event.text = Event->text.text;
406 D_PostEvent(&event);
407 break;
408
409 case SDL_WINDOWEVENT:
410 if (Event->window.windowID == windowid)
411 {
412 switch (Event->window.event)
413 {
414 case SDL_WINDOWEVENT_FOCUS_GAINED:
415 case SDL_WINDOWEVENT_FOCUS_LOST:
416 UpdateFocus();
417 break;
418 case SDL_WINDOWEVENT_SIZE_CHANGED:
419 ApplyWindowResize(Event);
420 break;
421 }
422 }
423 break;
424
425 case SDL_QUIT:
426 S_StartVoidSound(sfx_swtchn);
427 M_QuitDOOM(0);
428
429 default:
430 break;
431 }
432 }
433 }
434
435 //
436 // I_StartTic
437 //
438
439 void I_StartTic (void)
440 {
441 I_GetEvent();
442
443 if (dsda_AllowMouse())
444 I_ReadMouse();
445
446 if (dsda_AllowGameController())
447 dsda_PollGameController();
448 }
449
450 //
451 // I_StartFrame
452 //
453 void I_StartFrame (void)
454 {
455 }
456
457 static void I_FlushMousePosition(void)
458 {
459 int x, y;
460
461 SDL_GetRelativeMouseState(&x, &y);
462 }
463
464 void I_InitMouse(void)
465 {
466 static Uint8 empty_cursor_data = 0;
467
468 // check if the user wants to use the mouse
469 mouse_enabled = dsda_IntConfig(dsda_config_use_mouse) && !dsda_Flag(dsda_arg_nomouse);
470
471 SDL_PumpEvents();
472
473 // Save the default cursor so it can be recalled later
474 cursors[0] = SDL_GetCursor();
475 // Create an empty cursor
476 cursors[1] = SDL_CreateCursor(&empty_cursor_data, &empty_cursor_data, 8, 1, 0, 0);
477
478 I_FlushMousePosition();
479 }
480
481 //
482 // I_InitInputs
483 //
484
485 static void I_InitInputs(void)
486 {
487 AccelChanging();
488 I_InitMouse();
489 dsda_InitGameController();
490 }
491
492 ///////////////////////////////////////////////////////////
493 // Palette stuff.
494 //
495 static void I_UploadNewPalette(int pal, int force)
496 {
497 // This is used to replace the current 256 colour cmap with a new one
498 // Used by 256 colour PseudoColor modes
499
500 static int cachedgamma;
501 static size_t num_pals;
502 dsda_playpal_t* playpal_data;
503
504 if (V_IsOpenGLMode())
505 return;
506
507 playpal_data = dsda_PlayPalData();
508
509 if ((playpal_data->colours == NULL) || (cachedgamma != usegamma) || force) {
510 int pplump;
511 int gtlump;
512 register const byte * palette;
513 register const byte * gtable;
514 register int i;
515
516 pplump = W_GetNumForName(playpal_data->lump_name);
517 gtlump = W_CheckNumForName2("GAMMATBL", ns_prboom);
518 palette = (const byte*) W_LumpByNum(pplump);
519 gtable = (const byte*) W_LumpByNum(gtlump) + 256 * (cachedgamma = usegamma);
520
521 num_pals = W_LumpLength(pplump) / (3 * 256);
522 num_pals *= 256;
523
524 if (!playpal_data->colours) {
525 // First call - allocate and prepare colour array
526 playpal_data->colours =
527 (SDL_Color*) Z_Malloc(sizeof(*playpal_data->colours) * num_pals);
528 }
529
530 // set the colormap entries
531 for (i = 0; (size_t) i < num_pals; i++) {
532 playpal_data->colours[i].r = gtable[palette[0]];
533 playpal_data->colours[i].g = gtable[palette[1]];
534 playpal_data->colours[i].b = gtable[palette[2]];
535 palette += 3;
536 }
537
538 num_pals /= 256;
539 }
540
541 #ifdef RANGECHECK
542 if ((size_t)pal >= num_pals)
543 I_Error("I_UploadNewPalette: Palette number out of range (%d>=%d)",
544 pal, num_pals);
545 #endif
546
547 SDL_SetPaletteColors(screen->format->palette, playpal_data->colours + 256 * pal, 0, 256);
548 }
549
550 //////////////////////////////////////////////////////////////////////////////
551 // Graphics API
552
553 void I_ShutdownGraphics(void)
554 {
555 SDL_FreeCursor(cursors[1]);
556 DeactivateMouse();
557 }
558
559 static dboolean queue_frame_capture;
560 static dboolean queue_screenshot;
561
562 void I_QueueFrameCapture(void)
563 {
564 queue_frame_capture = true;
565 }
566
567 void I_QueueScreenshot(void)
568 {
569 queue_screenshot = true;
570 }
571
572 void I_HandleCapture(void)
573 {
574 if (queue_frame_capture)
575 {
576 I_CaptureFrame();
577 queue_frame_capture = false;
578 }
579
580 if (queue_screenshot)
581 {
582 M_ScreenShot();
583 queue_screenshot = false;
584 }
585 }
586
587 //
588 // I_FinishUpdate
589 //
590 static int newpal = 0;
591 #define NO_PALETTE_CHANGE 1000
592
593 void I_FinishUpdate (void)
594 {
595 if (V_IsOpenGLMode()) {
596 // proff 04/05/2000: swap OpenGL buffers
597 gld_Finish();
598 return;
599 }
600
601 if (SDL_MUSTLOCK(screen)) {
602 int h;
603 byte *src;
604 byte *dest;
605
606 if (SDL_LockSurface(screen) < 0) {
607 lprintf(LO_INFO,"I_FinishUpdate: %s\n", SDL_GetError());
608 return;
609 }
610
611 dest=(byte*)screen->pixels;
612 src=screens[0].data;
613 h=screen->h;
614 for (; h>0; h--)
615 {
616 memcpy(dest,src,SCREENWIDTH); //e6y
617 dest+=screen->pitch;
618 src+=screens[0].pitch;
619 }
620
621 SDL_UnlockSurface(screen);
622 }
623
624 /* Update the display buffer (flipping video pages if supported)
625 * If we need to change palette, that implicitely does a flip */
626 if (newpal != NO_PALETTE_CHANGE) {
627 I_UploadNewPalette(newpal, false);
628 newpal = NO_PALETTE_CHANGE;
629 }
630
631 // Blit from the paletted 8-bit screen buffer to the intermediate
632 // 32-bit RGBA buffer that we can load into the texture.
633 SDL_LowerBlit(screen, &src_rect, buffer, &src_rect);
634
635 // Update the intermediate texture with the contents of the RGBA buffer.
636 SDL_UpdateTexture(sdl_texture, &src_rect, buffer->pixels, buffer->pitch);
637
638 // Make sure the pillarboxes are kept clear each frame.
639 SDL_RenderClear(sdl_renderer);
640
641 SDL_RenderCopy(sdl_renderer, sdl_texture, &src_rect, NULL);
642
643 I_HandleCapture();
644
645 // Draw!
646 SDL_RenderPresent(sdl_renderer);
647 }
648
649 //
650 // I_ScreenShot - moved to i_sshot.c
651 //
652
653 //
654 // I_SetPalette
655 //
656 void I_SetPalette (int pal)
657 {
658 newpal = pal;
659 }
660
661 // I_PreInitGraphics
662
663 static void I_ShutdownSDL(void)
664 {
665 if (sdl_glcontext) SDL_GL_DeleteContext(sdl_glcontext);
666 if (screen) SDL_FreeSurface(screen);
667 if (buffer) SDL_FreeSurface(buffer);
668 if (sdl_texture) SDL_DestroyTexture(sdl_texture);
669 if (sdl_renderer) SDL_DestroyRenderer(sdl_renderer);
670 if (sdl_window) SDL_DestroyWindow(sdl_window);
671
672 SDL_Quit();
673 return;
674 }
675
676 void I_PreInitGraphics(void)
677 {
678 int p;
679
680 // Initialize SDL
681 unsigned int flags = 0;
682 if (!(dsda_Flag(dsda_arg_nodraw) && dsda_Flag(dsda_arg_nosound)))
683 flags = SDL_INIT_VIDEO;
684 #ifdef PRBOOM_DEBUG
685 flags |= SDL_INIT_NOPARACHUTE;
686 #endif
687
688 p = SDL_Init(flags);
689 if (p < 0)
690 {
691 I_Error("Could not initialize SDL [%s]", SDL_GetError());
692 }
693
694 I_AtExit(I_ShutdownSDL, true, "I_ShutdownSDL", exit_priority_normal);
695 }
696
697 // e6y: resolution limitation is removed
698 void I_InitBuffersRes(void)
699 {
700 R_InitMeltRes();
701 R_InitSpritesRes();
702 R_InitBuffersRes();
703 R_InitPlanesRes();
704 R_InitVisplanesRes();
705 }
706
707 #define MAX_RESOLUTIONS_COUNT 128
708 const char *screen_resolutions_list[MAX_RESOLUTIONS_COUNT] = {NULL};
709
710 //
711 // I_GetScreenResolution
712 // Get current resolution from the config variable (WIDTHxHEIGHT format)
713 // 640x480 if screen_resolution variable has wrong data
714 //
715 void I_GetScreenResolution(void)
716 {
717 int width, height;
718 const char *screen_resolution;
719
720 desired_screenwidth = 640;
721 desired_screenheight = 480;
722
723 screen_resolution = dsda_StringConfig(dsda_config_screen_resolution);
724
725 if (screen_resolution)
726 {
727 if (sscanf(screen_resolution, "%dx%d", &width, &height) == 2)
728 {
729 desired_screenwidth = width;
730 desired_screenheight = height;
731 }
732 }
733 }
734
735 // make sure the canonical resolutions are always available
736 static const struct {
737 const int w, h;
738 } canonicals[] = {
739 { 640, 480}, // Doom 95
740 { 320, 240}, // Doom 95
741 {1120, 400}, // 21:9
742 { 854, 400}, // 16:9
743 { 768, 400}, // 16:10
744 { 640, 400}, // MBF
745 { 560, 200}, // 21:9
746 { 426, 200}, // 16:9
747 { 384, 200}, // 16:10
748 { 320, 200}, // Vanilla Doom
749 };
750 static const int num_canonicals = sizeof(canonicals)/sizeof(*canonicals);
751
752 // [FG] sort resolutions by width first and height second
753 static int cmp_resolutions (const void *a, const void *b)
754 {
755 const char *const *sa = (const char *const *) a;
756 const char *const *sb = (const char *const *) b;
757
758 int wa, wb, ha, hb;
759
760 if (sscanf(*sa, "%dx%d", &wa, &ha) != 2) wa = ha = 0;
761 if (sscanf(*sb, "%dx%d", &wb, &hb) != 2) wb = hb = 0;
762
763 return (wa == wb) ? ha - hb : wa - wb;
764 }
765
766 static void I_AppendResolution(SDL_DisplayMode *mode, int *current_resolution_index, int *list_size)
767 {
768 int i;
769 char mode_name[256];
770
771
772 snprintf(mode_name, sizeof(mode_name), "%dx%d", mode->w, mode->h);
773
774 for(i = 0; i < *list_size; i++)
775 if (!strcmp(mode_name, screen_resolutions_list[i]))
776 return;
777
778 screen_resolutions_list[*list_size] = Z_Strdup(mode_name);
779
780 if (mode->w == desired_screenwidth && mode->h == desired_screenheight)
781 *current_resolution_index = *list_size;
782
783 (*list_size)++;
784 }
785
786 static void I_AppendCustomResolution(int *current_resolution_index, int *list_size)
787 {
788 const char *custom_resolution;
789
790 custom_resolution = dsda_StringConfig(dsda_config_custom_resolution);
791
792 if (strlen(custom_resolution))
793 {
794 SDL_DisplayMode mode;
795
796 if (sscanf(custom_resolution, "%4dx%4d", &mode.w, &mode.h) == 2)
797 {
798 I_AppendResolution(&mode, current_resolution_index, list_size);
799 }
800 }
801 }
802
803 //
804 // I_FillScreenResolutionsList
805 // Get all the supported screen resolutions
806 // and fill the list with them
807 //
808 static void I_FillScreenResolutionsList(void)
809 {
810 int display_index = 0;
811 SDL_DisplayMode mode;
812 int i, list_size, current_resolution_index, count;
813 char desired_resolution[256];
814
815 // do it only once
816 if (screen_resolutions_list[0])
817 {
818 return;
819 }
820
821 if (desired_screenwidth == 0 || desired_screenheight == 0)
822 {
823 I_GetScreenResolution();
824 }
825
826 // Don't call SDL_ListModes if SDL has not been initialized
827 count = 0;
828 if (!nodrawers)
829 count = SDL_GetNumDisplayModes(display_index);
830
831 list_size = 0;
832 current_resolution_index = -1;
833
834 // on success, SDL_GetNumDisplayModes() always returns at least 1
835 if (count > 0)
836 {
837 // -2 for the desired resolution and for NULL
838 count = MIN(count, MAX_RESOLUTIONS_COUNT - 2 - num_canonicals);
839
840 for(i = count - 1 + num_canonicals; i >= 0; i--)
841 {
842 // make sure the canonical resolutions are always available
843 if (i > count - 1)
844 {
845 // no hard-coded resolutions for mode-changing fullscreen
846 if (exclusive_fullscreen)
847 continue;
848
849 mode.w = canonicals[i - count].w;
850 mode.h = canonicals[i - count].h;
851 }
852 else
853 {
854 SDL_GetDisplayMode(display_index, i, &mode);
855 }
856
857 I_AppendResolution(&mode, &current_resolution_index, &list_size);
858 }
859
860 I_AppendCustomResolution(&current_resolution_index, &list_size);
861
862 screen_resolutions_list[list_size] = NULL;
863 }
864
865 snprintf(desired_resolution, sizeof(desired_resolution), "%dx%d", desired_screenwidth, desired_screenheight);
866
867 // [FG] if the desired resolution not in the list, append it
868 if (current_resolution_index == -1)
869 {
870 screen_resolutions_list[list_size] = Z_Strdup(desired_resolution);
871 list_size++;
872 }
873
874 // [FG] sort the list
875 SDL_qsort(screen_resolutions_list, list_size, sizeof(*screen_resolutions_list), cmp_resolutions);
876
877 // [FG] find the desired resolution again
878 for (i = 0; i < list_size; i++)
879 {
880 if (!strcmp(desired_resolution, screen_resolutions_list[i]))
881 {
882 current_resolution_index = i;
883 break;
884 }
885 }
886
887 screen_resolutions_list[list_size] = NULL;
888 // This code is inside of the onUpdate for screen resolution, so it must avoid recursion
889 {
890 const char* dsda_HackStringConfig(dsda_config_identifier_t id, const char* value, dboolean persist);
891
892 dsda_HackStringConfig(dsda_config_screen_resolution,
893 screen_resolutions_list[current_resolution_index], false);
894 }
895 }
896
897 // e6y
898 // It is a simple test of CPU cache misses.
899 unsigned int I_TestCPUCacheMisses(int width, int height, unsigned int mintime)
900 {
901 int i, k;
902 char *s, *d, *ps, *pd;
903 unsigned int tickStart;
904
905 s = (char*)Z_Malloc(width * height);
906 d = (char*)Z_Malloc(width * height);
907
908 tickStart = SDL_GetTicks();
909 k = 0;
910 do
911 {
912 ps = s;
913 pd = d;
914 for(i = 0; i < height; i++)
915 {
916 pd[0] = ps[0];
917 pd += width;
918 ps += width;
919 }
920 k++;
921 }
922 while (SDL_GetTicks() - tickStart < mintime);
923
924 Z_Free(d);
925 Z_Free(s);
926
927 return k;
928 }
929
930 // CPhipps -
931 // I_CalculateRes
932 // Calculates the screen resolution, possibly using the supplied guide
933 void I_CalculateRes(int width, int height)
934 {
935 SCREENWIDTH = width;
936 SCREENHEIGHT = height;
937
938 if (V_IsOpenGLMode())
939 {
940 SCREENPITCH = SCREENWIDTH;
941 }
942 else
943 {
944 unsigned int count1, count2;
945 int pitch1, pitch2;
946
947 // e6y
948 // Trying to optimise screen pitch for reducing of CPU cache misses.
949 // It is extremally important for wiping in software.
950 // I have ~20x improvement in speed with using 1056 instead of 1024 on Pentium4
951 // and only ~10% for Core2Duo
952 if (nodrawers)
953 {
954 SCREENPITCH = ((width + 15) & ~15) + 32;
955 }
956 else
957 {
958 unsigned int mintime = 100;
959 int w = (width+15) & ~15;
960 pitch1 = w;
961 pitch2 = w + 32;
962
963 count1 = I_TestCPUCacheMisses(pitch1, SCREENHEIGHT, mintime);
964 count2 = I_TestCPUCacheMisses(pitch2, SCREENHEIGHT, mintime);
965
966 lprintf(LO_DEBUG, "I_CalculateRes: trying to optimize screen pitch\n");
967 lprintf(LO_DEBUG, " test case for pitch=%d is processed %d times for %d msec\n", pitch1, count1, mintime);
968 lprintf(LO_DEBUG, " test case for pitch=%d is processed %d times for %d msec\n", pitch2, count2, mintime);
969
970 SCREENPITCH = (count2 > count1 ? pitch2 : pitch1);
971
972 lprintf(LO_DEBUG, " optimized screen pitch is %d\n", SCREENPITCH);
973 }
974 }
975 }
976
977 static video_mode_t I_GetModeFromString(const char *modestr)
978 {
979 video_mode_t mode;
980
981 if (!stricmp(modestr,"gl")) {
982 mode = VID_MODEGL;
983 } else if (!stricmp(modestr,"OpenGL")) {
984 mode = VID_MODEGL;
985 } else {
986 mode = VID_MODESW;
987 }
988
989 return mode;
990 }
991
992 static video_mode_t I_DesiredVideoMode(void) {
993 dsda_arg_t *arg;
994 video_mode_t mode;
995
996 arg = dsda_Arg(dsda_arg_vidmode);
997 if (arg->found)
998 mode = I_GetModeFromString(arg->value.v_string);
999 else
1000 mode = I_GetModeFromString(dsda_StringConfig(dsda_config_videomode));
1001
1002 return mode;
1003 }
1004
1005 // CPhipps -
1006 // I_InitScreenResolution
1007 // Sets the screen resolution
1008 void I_InitScreenResolution(void)
1009 {
1010 int i, w, h;
1011 char c, x;
1012 dsda_arg_t *arg;
1013 video_mode_t mode;
1014 int init = (sdl_window == NULL);
1015
1016 I_GetScreenResolution();
1017
1018 desired_fullscreen = dsda_IntConfig(dsda_config_use_fullscreen);
1019
1020 if (init)
1021 {
1022 //e6y: ability to change screen resolution from GUI
1023 I_FillScreenResolutionsList();
1024
1025 if (dsda_Flag(dsda_arg_fullscreen))
1026 desired_fullscreen = 1;
1027
1028 if (dsda_Flag(dsda_arg_window))
1029 desired_fullscreen = 0;
1030
1031 // Video stuff
1032 arg = dsda_Arg(dsda_arg_width);
1033 if (arg->found)
1034 desired_screenwidth = arg->value.v_int;
1035
1036 arg = dsda_Arg(dsda_arg_height);
1037 if (arg->found)
1038 desired_screenheight = arg->value.v_int;
1039
1040 // e6y
1041 // change the screen size for the current session only
1042 // syntax: -geom WidthxHeight[w|f]
1043 // examples: -geom 320x200f, -geom 640x480w, -geom 1024x768
1044 w = desired_screenwidth;
1045 h = desired_screenheight;
1046
1047 arg = dsda_Arg(dsda_arg_geometry);
1048 if (arg->found)
1049 {
1050 int count = sscanf(arg->value.v_string, "%d%c%d%c", &w, &x, &h, &c);
1051
1052 // at least width and height must be specified
1053 // restoring original values if not
1054 if (count < 3 || tolower(x) != 'x')
1055 {
1056 w = desired_screenwidth;
1057 h = desired_screenheight;
1058 }
1059 else
1060 {
1061 if (count >= 4)
1062 {
1063 if (tolower(c) == 'w')
1064 desired_fullscreen = 0;
1065 if (tolower(c) == 'f')
1066 desired_fullscreen = 1;
1067 }
1068 }
1069 }
1070 }
1071 else
1072 {
1073 w = desired_screenwidth;
1074 h = desired_screenheight;
1075 }
1076
1077 mode = I_DesiredVideoMode();
1078
1079 V_InitMode(mode);
1080
1081 I_CalculateRes(w, h);
1082 V_FreeScreens();
1083
1084 // set first three to standard values
1085 for (i=0; i<3; i++) {
1086 screens[i].width = SCREENWIDTH;
1087 screens[i].height = SCREENHEIGHT;
1088 screens[i].pitch = SCREENPITCH;
1089 }
1090
1091 // statusbar
1092 screens[4].width = SCREENWIDTH;
1093 screens[4].height = SCREENHEIGHT;
1094 screens[4].pitch = SCREENPITCH;
1095
1096 I_InitBuffersRes();
1097
1098 lprintf(LO_DEBUG, "I_InitScreenResolution: Using resolution %dx%d\n", SCREENWIDTH, SCREENHEIGHT);
1099 }
1100
1101 //
1102 // Set the window caption
1103 //
1104
1105 void I_SetWindowCaption(void)
1106 {
1107 SDL_SetWindowTitle(NULL, PROJECT_NAME " " PROJECT_VERSION);
1108 }
1109
1110 //
1111 // Set the application icon
1112 //
1113
1114 #include "icon.c"
1115
1116 void I_SetWindowIcon(void)
1117 {
1118 static SDL_Surface *surface = NULL;
1119
1120 // do it only once, because of crash in SDL_InitVideoMode in SDL 1.3
1121 if (!surface)
1122 {
1123 surface = SDL_CreateRGBSurfaceFrom(icon_data,
1124 icon_w, icon_h, 32, icon_w * 4,
1125 0xff << 0, 0xff << 8, 0xff << 16, 0xff << 24);
1126 }
1127
1128 if (surface)
1129 {
1130 SDL_SetWindowIcon(NULL, surface);
1131 }
1132 }
1133
1134 void I_InitGraphics(void)
1135 {
1136 static int firsttime=1;
1137
1138 if (firsttime)
1139 {
1140 firsttime = 0;
1141
1142 I_AtExit(I_ShutdownGraphics, true, "I_ShutdownGraphics", exit_priority_normal);
1143 lprintf(LO_DEBUG, "I_InitGraphics: %dx%d\n", SCREENWIDTH, SCREENHEIGHT);
1144
1145 /* Set the video mode */
1146 I_UpdateVideoMode();
1147
1148 //e6y: setup the window title
1149 I_SetWindowCaption();
1150
1151 //e6y: set the application icon
1152 I_SetWindowIcon();
1153
1154 /* Initialize the input system */
1155 I_InitInputs();
1156
1157 //e6y: new mouse code
1158 UpdateFocus();
1159 UpdateGrab();
1160 }
1161 }
1162
1163 void I_UpdateVideoMode(void)
1164 {
1165 int init_flags = SDL_WINDOW_ALLOW_HIGHDPI;
1166 int screen_multiply;
1167 int render_vsync;
1168 int integer_scaling;
1169 const char *sdl_video_window_pos;
1170 int sdl_video_display_index;
1171 int x, y;
1172 const dboolean novsync = dsda_Flag(dsda_arg_timedemo) ||
1173 dsda_Flag(dsda_arg_fastdemo);
1174
1175 exclusive_fullscreen = dsda_IntConfig(dsda_config_exclusive_fullscreen) &&
1176 I_DesiredVideoMode() == VID_MODESW;
1177 render_vsync = dsda_IntConfig(dsda_config_render_vsync) && !novsync;
1178 sdl_video_window_pos = dsda_StringConfig(dsda_config_sdl_video_window_pos);
1179 sdl_video_display_index = dsda_IntConfig(dsda_config_sdl_video_display_index);
1180 screen_multiply = dsda_IntConfig(dsda_config_render_screen_multiply);
1181 integer_scaling = dsda_IntConfig(dsda_config_integer_scaling);
1182
1183 if(sdl_window)
1184 {
1185 // video capturing cannot be continued with new screen settings
1186 I_CaptureFinish();
1187
1188 if (V_IsOpenGLMode())
1189 {
1190 gld_CleanMemory();
1191 gld_CleanStaticMemory();
1192 }
1193
1194 I_InitScreenResolution();
1195
1196 if (sdl_glcontext) SDL_GL_DeleteContext(sdl_glcontext);
1197 if (screen) SDL_FreeSurface(screen);
1198 if (buffer) SDL_FreeSurface(buffer);
1199 if (sdl_texture) SDL_DestroyTexture(sdl_texture);
1200 if (sdl_renderer) SDL_DestroyRenderer(sdl_renderer);
1201 SDL_DestroyWindow(sdl_window);
1202
1203 sdl_renderer = NULL;
1204 sdl_window = NULL;
1205 sdl_glcontext = NULL;
1206 screen = NULL;
1207 buffer = NULL;
1208 sdl_texture = NULL;
1209 }
1210
1211 // Initialize SDL with this graphics mode
1212 if (V_IsOpenGLMode()) {
1213 init_flags |= SDL_WINDOW_OPENGL;
1214 }
1215
1216 // [FG] aspect ratio correction for the canonical video modes
1217 if ((SCREENHEIGHT == 200 || SCREENHEIGHT == 400) && dsda_IntConfig(dsda_config_aspect_ratio_correction))
1218 {
1219 ACTUALHEIGHT = 6*SCREENHEIGHT/5;
1220 }
1221 else
1222 {
1223 ACTUALHEIGHT = SCREENHEIGHT;
1224 }
1225
1226 x = SDL_WINDOWPOS_CENTERED_DISPLAY(sdl_video_display_index);
1227 y = SDL_WINDOWPOS_CENTERED_DISPLAY(sdl_video_display_index);
1228 if (sdl_video_window_pos)
1229 {
1230 int nx, ny;
1231 if (sscanf(sdl_video_window_pos, "%d,%d", &nx, &ny) == 2)
1232 {
1233 x = nx;
1234 y = ny;
1235 }
1236 }
1237
1238 if (desired_fullscreen)
1239 {
1240 if (exclusive_fullscreen)
1241 init_flags |= SDL_WINDOW_FULLSCREEN;
1242 else
1243 init_flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
1244 }
1245
1246 if (V_IsOpenGLMode())
1247 {
1248 SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 0 );
1249 SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 0 );
1250 SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 0 );
1251 SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 0 );
1252 SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 0 );
1253 SDL_GL_SetAttribute( SDL_GL_ACCUM_RED_SIZE, 0 );
1254 SDL_GL_SetAttribute( SDL_GL_ACCUM_GREEN_SIZE, 0 );
1255 SDL_GL_SetAttribute( SDL_GL_ACCUM_BLUE_SIZE, 0 );
1256 SDL_GL_SetAttribute( SDL_GL_ACCUM_ALPHA_SIZE, 0 );
1257 SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
1258 SDL_GL_SetAttribute( SDL_GL_BUFFER_SIZE, gl_colorbuffer_bits );
1259 SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, gl_depthbuffer_bits );
1260 SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 8 );
1261
1262 //e6y: anti-aliasing
1263 gld_MultisamplingInit();
1264
1265 sdl_window = SDL_CreateWindow(
1266 PROJECT_NAME " " PROJECT_VERSION,
1267 x, y,
1268 SCREENWIDTH * screen_multiply, ACTUALHEIGHT * screen_multiply,
1269 init_flags);
1270 sdl_glcontext = SDL_GL_CreateContext(sdl_window);
1271 SDL_SetWindowMinimumSize(sdl_window, SCREENWIDTH, ACTUALHEIGHT);
1272 }
1273 else
1274 {
1275 int flags = SDL_RENDERER_TARGETTEXTURE;
1276
1277 if (render_vsync)
1278 flags |= SDL_RENDERER_PRESENTVSYNC;
1279
1280 sdl_window = SDL_CreateWindow(
1281 PROJECT_NAME " " PROJECT_VERSION,
1282 x, y,
1283 SCREENWIDTH * screen_multiply, ACTUALHEIGHT * screen_multiply,
1284 init_flags);
1285 sdl_renderer = SDL_CreateRenderer(sdl_window, -1, flags);
1286
1287 SDL_SetWindowMinimumSize(sdl_window, SCREENWIDTH, ACTUALHEIGHT);
1288 SDL_RenderSetLogicalSize(sdl_renderer, SCREENWIDTH, ACTUALHEIGHT);
1289
1290 // [FG] force integer scales
1291 SDL_RenderSetIntegerScale(sdl_renderer, integer_scaling);
1292
1293 screen = SDL_CreateRGBSurface(0, SCREENWIDTH, SCREENHEIGHT, 8, 0, 0, 0, 0);
1294 buffer = SDL_CreateRGBSurface(0, SCREENWIDTH, SCREENHEIGHT, 32, 0, 0, 0, 0);
1295 SDL_FillRect(buffer, NULL, 0);
1296
1297 sdl_texture = SDL_CreateTextureFromSurface(sdl_renderer, buffer);
1298
1299 if(screen == NULL) {
1300 I_Error("Couldn't set %dx%d video mode [%s]", SCREENWIDTH, SCREENHEIGHT, SDL_GetError());
1301 }
1302 }
1303
1304 // When creating the window, its not allowed to set a position in a different display
1305 // This allows that
1306 SDL_SetWindowPosition(sdl_window, x, y);
1307
1308 if (desired_fullscreen)
1309 {
1310 if (exclusive_fullscreen)
1311 SDL_SetWindowFullscreen(sdl_window, SDL_WINDOW_FULLSCREEN);
1312 else
1313 SDL_SetWindowFullscreen(sdl_window, SDL_WINDOW_FULLSCREEN_DESKTOP);
1314 }
1315 else
1316 {
1317 SDL_SetWindowResizable(sdl_window, SDL_TRUE);
1318 }
1319
1320 // Workaround for SDL 2.0.14 alt-tab bug (taken from Doom Retro)
1321 #if defined(_WIN32)
1322 {
1323 SDL_version ver;
1324 SDL_GetVersion(&ver);
1325 if (ver.major == 2 && ver.minor == 0 && (ver.patch == 14 || ver.patch == 16))
1326 {
1327 SDL_SetHintWithPriority(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "1", SDL_HINT_OVERRIDE);
1328 }
1329 }
1330 #endif
1331
1332 windowid = SDL_GetWindowID(sdl_window);
1333
1334 if (V_IsOpenGLMode())
1335 {
1336 SDL_GL_SetSwapInterval((render_vsync ? 1 : 0));
1337 }
1338
1339 if (V_IsSoftwareMode())
1340 {
1341 lprintf(LO_DEBUG, "I_UpdateVideoMode: 0x%x, %s, %s\n", init_flags, screen && screen->pixels ? "SDL buffer" : "own buffer", screen && SDL_MUSTLOCK(screen) ? "lock-and-copy": "direct access");
1342
1343 // Get the info needed to render to the display
1344 if (!SDL_MUSTLOCK(screen))
1345 {
1346 screens[0].not_on_heap = true;
1347 screens[0].data = (unsigned char *) (screen->pixels);
1348 screens[0].pitch = screen->pitch;
1349 }
1350 else
1351 {
1352 screens[0].not_on_heap = false;
1353 }
1354
1355 V_AllocScreens();
1356
1357 R_InitBuffer(SCREENWIDTH, SCREENHEIGHT);
1358 }
1359
1360 // e6y: wide-res
1361 // Need some initialisations before level precache
1362 R_ExecuteSetViewSize();
1363
1364 V_SetPalette(0);
1365 I_UploadNewPalette(0, true);
1366
1367 if (V_IsOpenGLMode())
1368 {
1369 int temp;
1370 lprintf(LO_DEBUG, "SDL OpenGL PixelFormat:\n");
1371 SDL_GL_GetAttribute( SDL_GL_RED_SIZE, &temp );
1372 lprintf(LO_DEBUG, " SDL_GL_RED_SIZE: %i\n",temp);
1373 SDL_GL_GetAttribute( SDL_GL_GREEN_SIZE, &temp );
1374 lprintf(LO_DEBUG, " SDL_GL_GREEN_SIZE: %i\n",temp);
1375 SDL_GL_GetAttribute( SDL_GL_BLUE_SIZE, &temp );
1376 lprintf(LO_DEBUG, " SDL_GL_BLUE_SIZE: %i\n",temp);
1377 SDL_GL_GetAttribute( SDL_GL_STENCIL_SIZE, &temp );
1378 lprintf(LO_DEBUG, " SDL_GL_STENCIL_SIZE: %i\n",temp);
1379 SDL_GL_GetAttribute( SDL_GL_ACCUM_RED_SIZE, &temp );
1380 lprintf(LO_DEBUG, " SDL_GL_ACCUM_RED_SIZE: %i\n",temp);
1381 SDL_GL_GetAttribute( SDL_GL_ACCUM_GREEN_SIZE, &temp );
1382 lprintf(LO_DEBUG, " SDL_GL_ACCUM_GREEN_SIZE: %i\n",temp);
1383 SDL_GL_GetAttribute( SDL_GL_ACCUM_BLUE_SIZE, &temp );
1384 lprintf(LO_DEBUG, " SDL_GL_ACCUM_BLUE_SIZE: %i\n",temp);
1385 SDL_GL_GetAttribute( SDL_GL_ACCUM_ALPHA_SIZE, &temp );
1386 lprintf(LO_DEBUG, " SDL_GL_ACCUM_ALPHA_SIZE: %i\n",temp);
1387 SDL_GL_GetAttribute( SDL_GL_DOUBLEBUFFER, &temp );
1388 lprintf(LO_DEBUG, " SDL_GL_DOUBLEBUFFER: %i\n",temp);
1389 SDL_GL_GetAttribute( SDL_GL_BUFFER_SIZE, &temp );
1390 lprintf(LO_DEBUG, " SDL_GL_BUFFER_SIZE: %i\n",temp);
1391 SDL_GL_GetAttribute( SDL_GL_DEPTH_SIZE, &temp );
1392 lprintf(LO_DEBUG, " SDL_GL_DEPTH_SIZE: %i\n",temp);
1393 SDL_GL_GetAttribute( SDL_GL_MULTISAMPLESAMPLES, &temp );
1394 lprintf(LO_DEBUG, " SDL_GL_MULTISAMPLESAMPLES: %i\n",temp);
1395 SDL_GL_GetAttribute( SDL_GL_MULTISAMPLEBUFFERS, &temp );
1396 lprintf(LO_DEBUG, " SDL_GL_MULTISAMPLEBUFFERS: %i\n",temp);
1397 SDL_GL_GetAttribute( SDL_GL_STENCIL_SIZE, &temp );
1398 lprintf(LO_DEBUG, " SDL_GL_STENCIL_SIZE: %i\n",temp);
1399
1400 gld_Init(SCREENWIDTH, SCREENHEIGHT);
1401 }
1402
1403 ST_SetResolution();
1404 AM_SetResolution();
1405
1406 if (V_IsOpenGLMode())
1407 {
1408 M_ChangeFOV();
1409 deh_changeCompTranslucency();
1410
1411 // elim - Sets up viewport sizing for render-to-texture scaling
1412 dsda_GLSetRenderViewportParams();
1413 dsda_GLSetRenderViewport();
1414 }
1415
1416 src_rect.w = SCREENWIDTH;
1417 src_rect.h = SCREENHEIGHT;
1418 }
1419
1420 static void ActivateMouse(void)
1421 {
1422 SDL_SetRelativeMouseMode(SDL_TRUE);
1423 SDL_GetRelativeMouseState(NULL, NULL);
1424 }
1425
1426 static void DeactivateMouse(void)
1427 {
1428 SDL_ShowCursor(SDL_ENABLE);
1429 SDL_SetRelativeMouseMode(SDL_FALSE);
1430 }
1431
1432 // Interpolates mouse input to mitigate stuttering
1433 static void CorrectMouseStutter(int *x, int *y)
1434 {
1435 static int x_remainder_old, y_remainder_old;
1436 int x_remainder, y_remainder;
1437 fixed_t fractic, correction_factor;
1438
1439 if (!dsda_IntConfig(dsda_config_mouse_stutter_correction))
1440 {
1441 return;
1442 }
1443
1444 fractic = dsda_TickElapsedTime();
1445
1446 *x += x_remainder_old;
1447 *y += y_remainder_old;
1448
1449 correction_factor = FixedDiv(fractic, fractic + 1000000 / TICRATE);
1450
1451 x_remainder = FixedMul(*x, correction_factor);
1452 *x -= x_remainder;
1453 x_remainder_old = x_remainder;
1454
1455 y_remainder = FixedMul(*y, correction_factor);
1456 *y -= y_remainder;
1457 y_remainder_old = y_remainder;
1458 }
1459
1460 //
1461 // Read the change in mouse state to generate mouse motion events
1462 //
1463 // This is to combine all mouse movement for a tic into one mouse
1464 // motion event.
1465 static void I_ReadMouse(void)
1466 {
1467 if (!mouse_enabled)
1468 return;
1469
1470 //e6y: new mouse code
1471 UpdateGrab();
1472
1473 if (window_focused)
1474 {
1475 int x, y;
1476
1477 SDL_GetRelativeMouseState(&x, &y);
1478 CorrectMouseStutter(&x, &y);
1479
1480 if (x != 0 || y != 0)
1481 {
1482 event_t event;
1483 event.type = ev_mousemotion;
1484 event.data1.i = x;
1485 event.data2.i = -y;
1486
1487 D_PostEvent(&event);
1488
1489 if (!menuactive)
1490 mouse_hide_timer = 2 * TICRATE;
1491 }
1492 }
1493 }
1494
1495 static dboolean MouseShouldBeGrabbed()
1496 {
1497 // never grab the mouse when in screensaver mode
1498
1499 //if (screensaver_mode)
1500 // return false;
1501
1502 // if the window doesnt have focus, never grab it
1503 if (!window_focused)
1504 return false;
1505
1506 // if we specify not to grab the mouse, never grab
1507 if (!mouse_enabled)
1508 return false;
1509
1510 // always grab the mouse in camera mode when playing levels
1511 // and menu is not active
1512 if (walkcamera.type)
1513 return (demoplayback && gamestate == GS_LEVEL && !menuactive);
1514
1515 // during playback the mouse should be hidden when not moving
1516 if (demoplayback && !menuactive && mouse_hide_timer > 0)
1517 {
1518 if (!dsda_SkipMode())
1519 mouse_hide_timer--;
1520
1521 return false;
1522 }
1523
1524 // always grab the mouse when full screen (dont want to
1525 // see the mouse pointer)
1526 if (desired_fullscreen)
1527 return true;
1528
1529 // when menu is active or game is paused, release the mouse
1530 if (menuactive || dsda_Paused())
1531 return false;
1532
1533 // grab mouse when playing levels
1534 return true;
1535 }
1536
1537 // Update the value of window_focused when we get a focus event
1538 //
1539 // We try to make ourselves be well-behaved: the grab on the mouse
1540 // is removed if we lose focus (such as a popup window appearing),
1541 // and we dont move the mouse around if we aren't focused either.
1542 static void UpdateFocus(void)
1543 {
1544 Uint32 flags = 0;
1545
1546 window_focused = false;
1547 if(sdl_window)
1548 {
1549 flags = SDL_GetWindowFlags(sdl_window);
1550 if ((flags & SDL_WINDOW_SHOWN) && !(flags & SDL_WINDOW_MINIMIZED) && (flags & SDL_WINDOW_INPUT_FOCUS))
1551 {
1552 window_focused = true;
1553 }
1554 }
1555
1556 // e6y
1557 // Reuse of a current palette to avoid black screen at software fullscreen modes
1558 // after switching to OS and back
1559 if (desired_fullscreen && window_focused)
1560 {
1561 V_TouchPalette();
1562 }
1563
1564 S_ResetVolume();
1565 }
1566
1567 void UpdateGrab(void)
1568 {
1569 static dboolean currently_grabbed = false;
1570 dboolean grab;
1571
1572 grab = MouseShouldBeGrabbed();
1573
1574 if (grab)
1575 {
1576 if (!currently_grabbed)
1577 ActivateMouse();
1578
1579 if (!demoplayback || walkcamera.type)
1580 SDL_WarpMouseInWindow(sdl_window, window_rect.w / 2, window_rect.h / 2);
1581 }
1582 else if (currently_grabbed)
1583 DeactivateMouse();
1584
1585 currently_grabbed = grab;
1586 }
1587
1588 static void ApplyWindowResize(SDL_Event *resize_event)
1589 {
1590 I_SetWindowRect();
1591 I_SetViewportRect();
1592
1593 if (!V_IsOpenGLMode() || !sdl_window)
1594 return;
1595
1596 dsda_GLSetRenderViewportParams();
1597 }
1598
1599 void I_SetWindowRect()
1600 {
1601 SDL_GetWindowSize(sdl_window, &window_rect.w, &window_rect.h);
1602
1603 if (V_IsOpenGLMode())
1604 SDL_GL_GetDrawableSize(sdl_window, &renderer_rect.w, &renderer_rect.h);
1605 else
1606 SDL_GetRendererOutputSize(sdl_renderer, &renderer_rect.w, &renderer_rect.h);
1607 }
1608
1609 void I_SetViewportRect()
1610 {
1611 float viewport_aspect = (float)SCREENWIDTH / (float)ACTUALHEIGHT;
1612
1613 // Black bars on left and right of viewport
1614 if (((float)renderer_rect.w / (float)renderer_rect.h) > viewport_aspect)
1615 {
1616 viewport_rect.w = (int)((float)renderer_rect.h * viewport_aspect);
1617 viewport_rect.h = renderer_rect.h;
1618 viewport_rect.x = (renderer_rect.w - viewport_rect.w) >> 1;
1619 viewport_rect.y = 0;
1620 }
1621 // Either matching window's aspect ratio, or black bars on top and bottom (ie 21:9 on a 16:9 display)
1622 else
1623 {
1624 viewport_rect.w = renderer_rect.w;
1625 viewport_rect.h = (int)((float)renderer_rect.w / viewport_aspect);
1626 viewport_rect.x = 0;
1627 viewport_rect.y = (renderer_rect.h - viewport_rect.h) >> 1;
1628 }
1629 }