X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=src%2Fgame%2Fg_window.pas;h=8336ea9b82cb39b11cd0b5b95c8e992e8cc3b0dd;hb=a9ab7bf0c72e15b932b20aad24b10db60138bb21;hp=12a038f68950f620a4c804e6d7d7078b65b60946;hpb=8aa3b888af2fecd61de66106bc2b027fda93914d;p=d2df-sdl.git diff --git a/src/game/g_window.pas b/src/game/g_window.pas index 12a038f..8336ea9 100644 --- a/src/game/g_window.pas +++ b/src/game/g_window.pas @@ -1,4 +1,4 @@ -(* Copyright (C) DooM 2D:Forever Developers +(* Copyright (C) Doom 2D: Forever Developers * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,8 +24,6 @@ uses function SDLMain (): Integer; function GetTimer (): Int64; procedure ResetTimer (); -function CreateGLWindow (Title: PChar): Boolean; -procedure KillGLWindow (); procedure PushExitEvent (); function ProcessMessage (): Boolean; procedure ReDrawWindow (); @@ -34,6 +32,7 @@ procedure Sleep (ms: LongWord); function GetDisplayModes (dbpp: LongWord; var selres: LongWord): SSArray; function g_Window_SetDisplay (preserveGL: Boolean=false): Boolean; function g_Window_SetSize (w, h: Word; fullscreen: Boolean): Boolean; +procedure g_SetVSync (vsync: Boolean); procedure ProcessLoading (forceUpdate: Boolean=false); @@ -52,16 +51,20 @@ implementation uses {$IFDEF WINDOWS}Windows,{$ENDIF} +{$INCLUDE ../nogl/noGLuses.inc} +{$IFDEF ENABLE_HOLMES} + g_holmes, sdlcarcass, fui_ctls, +{$ENDIF} SysUtils, Classes, MAPDEF, - SDL2, GL, GLExt, e_graphics, e_log, e_texture, g_main, + SDL2, e_graphics, e_log, e_texture, g_main, g_console, e_input, g_options, g_game, g_basic, g_textures, e_sound, g_sound, g_menu, ENet, g_net, - g_map, g_gfx, g_monsters, g_holmes, xprofiler, - sdlcarcass, gh_ui; + g_map, g_gfx, g_monsters, xprofiler, + g_touch; const - ProgressUpdateMSecs = 100; + ProgressUpdateMSecs = 1;//100; var h_Wnd: PSDL_Window = nil; @@ -70,9 +73,11 @@ var flag: Boolean; {$IF not DEFINED(HEADLESS)} wTitle: PChar = nil; + wasFullscreen: Boolean = true; // so we need to recreate the window {$ENDIF} wNeedTimeReset: Boolean = false; wMinimized: Boolean = false; + wMaximized: Boolean = false; wLoadingProgress: Boolean = false; wLoadingQuit: Boolean = false; {$IFNDEF WINDOWS} @@ -81,16 +86,27 @@ var {$ENDIF} -procedure KillGLWindow (); +procedure KillGLWindow (preserveGL: Boolean); begin - if (h_Wnd <> nil) then +{$IFDEF ENABLE_HOLMES} + if (h_GL <> nil) and (not preserveGL) then begin if (assigned(oglDeinitCB)) then oglDeinitCB(); end; +{$ENDIF} + if (h_Wnd <> nil) then SDL_DestroyWindow(h_Wnd); + if (h_GL <> nil) and (not preserveGL) then begin - if assigned(oglDeinitCB) then oglDeinitCB(); + +{$IFDEF USE_NANOGL} + nanoGL_Destroy; +{$ENDIF} + +{$IFDEF USE_NOGL} + nogl_Quit; +{$ENDIF} + + SDL_GL_DeleteContext(h_GL); end; - if (h_Wnd <> nil) then SDL_DestroyWindow(h_Wnd); - if (h_GL <> nil) then SDL_GL_DeleteContext(h_GL); h_Wnd := nil; - h_GL := nil; + if (not preserveGL) then h_GL := nil; end; @@ -99,6 +115,7 @@ function g_Window_SetDisplay (preserveGL: Boolean = false): Boolean; var mode, cmode: TSDL_DisplayMode; wFlags: LongWord = 0; + nw, nh: Integer; {$ENDIF} begin {$IF not DEFINED(HEADLESS)} @@ -106,11 +123,9 @@ begin e_WriteLog('Setting display mode...', TMsgType.Notify); - wFlags := SDL_WINDOW_OPENGL or SDL_WINDOW_RESIZABLE; - if gFullscreen then wFlags := wFlags or SDL_WINDOW_FULLSCREEN; - if gWinMaximized then wFlags := wFlags or SDL_WINDOW_MAXIMIZED; - - KillGLWindow(); + wFlags := SDL_WINDOW_OPENGL {or SDL_WINDOW_RESIZABLE}; + if gFullscreen then wFlags := wFlags {or SDL_WINDOW_FULLSCREEN} else wFlags := wFlags or SDL_WINDOW_RESIZABLE; + if (not gFullscreen) and (not preserveGL) and gWinMaximized then wFlags := wFlags or SDL_WINDOW_MAXIMIZED else gWinMaximized := false; if gFullscreen then begin @@ -121,25 +136,64 @@ begin mode.driverdata := nil; if (SDL_GetClosestDisplayMode(0, @mode, @cmode) = nil) then begin + e_WriteLog('SDL: cannot find display mode for '+IntToStr(gScreenWidth), TMsgType.Notify); gScreenWidth := 800; gScreenHeight := 600; end else begin + e_WriteLog('SDL: found display mode for '+IntToStr(gScreenWidth)+'x'+IntToStr(gScreenHeight)+': '+IntToStr(cmode.w)+'x'+IntToStr(cmode.h), TMsgType.Notify); gScreenWidth := cmode.w; gScreenHeight := cmode.h; end; end; - h_Wnd := SDL_CreateWindow(PChar(wTitle), gWinRealPosX, gWinRealPosY, gScreenWidth, gScreenHeight, wFlags); - if (h_Wnd = nil) then exit; + if (preserveGL) and (h_Wnd <> nil) and (not gFullscreen) and (not wasFullscreen) then + begin + //SDL_SetWindowMaximumSize(h_Wnd, gScreenWidth, gScreenHeight); + //SDL_SetWindowDisplayMode(h_Wnd, @cmode); + if (wMaximized) then SDL_RestoreWindow(h_Wnd); + wMaximized := false; + gWinMaximized := false; + SDL_SetWindowSize(h_Wnd, gScreenWidth, gScreenHeight); + //SDL_SetWindowFullscreen(h_Wnd, SDL_WINDOW_FULLSCREEN); + //SDL_SetWindowFullscreen(h_Wnd, 0); + end + else + begin + KillGLWindow(preserveGL); + h_Wnd := SDL_CreateWindow(PChar(wTitle), gWinRealPosX, gWinRealPosY, gScreenWidth, gScreenHeight, wFlags); + if gFullscreen then + SDL_SetWindowFullscreen(h_Wnd, SDL_WINDOW_FULLSCREEN); + if (h_Wnd = nil) then exit; + end; + wasFullscreen := gFullscreen; SDL_GL_MakeCurrent(h_Wnd, h_GL); SDL_ShowCursor(SDL_DISABLE); - if (h_GL <> nil) then + if (h_GL <> nil) then g_SetVSync(gVSync); + if (gFullscreen) then begin - if assigned(oglInitCB) then oglInitCB(); + nw := 0; + nh := 0; + SDL_GetWindowSize(h_Wnd, @nw, @nh); + if (nw > 128) and (nh > 128) then + begin + e_WriteLog('SDL: fullscreen window got size '+IntToStr(nw)+'x'+IntToStr(nh)+': '+IntToStr(gScreenWidth)+'x'+IntToStr(gScreenHeight), TMsgType.Notify); + gScreenWidth := nw; + gScreenHeight := nh; + end + else + begin + e_WriteLog('SDL: fullscreen window got invalid size: '+IntToStr(nw)+'x'+IntToStr(nh), TMsgType.Notify); + end; end; + + {$IFDEF ENABLE_HOLMES} + fuiScrWdt := gScreenWidth; + fuiScrHgt := gScreenHeight; + if (h_GL <> nil) and (not preserveGL) then begin if (assigned(oglInitCB)) then oglInitCB(); end; + {$ENDIF} {$ENDIF} result := true; @@ -180,14 +234,25 @@ begin end; -procedure ChangeWindowSize (); +procedure ChangeWindowSize (requested: Boolean); begin + e_LogWritefln(' ChangeWindowSize: (ws=%dx%d) (ss=%dx%d)', [gWinSizeX, gWinSizeY, gScreenWidth, gScreenHeight]); gWinSizeX := gScreenWidth; gWinSizeY := gScreenHeight; {$IF not DEFINED(HEADLESS)} + {$IFDEF ENABLE_HOLMES} + fuiScrWdt := gScreenWidth; + fuiScrHgt := gScreenHeight; + {$ENDIF} e_ResizeWindow(gScreenWidth, gScreenHeight); g_Game_SetupScreenSize(); - g_Menu_Reset(); + {$IF DEFINED(ANDROID)} + (* This will fix menu reset on keyboard showing *) + if requested then + g_Menu_Reset; + {$ELSE} + g_Menu_Reset; + {$ENDIF} g_Game_ClearLoading(); {$ENDIF} end; @@ -206,6 +271,7 @@ begin if (gScreenWidth <> w) or (gScreenHeight <> h) then begin result := true; + preserve := true; gScreenWidth := w; gScreenHeight := h; end; @@ -213,6 +279,7 @@ begin if (gFullscreen <> fullscreen) then begin result := true; + preserve := true; gFullscreen := fullscreen; preserve := true; end; @@ -220,7 +287,7 @@ begin if result then begin g_Window_SetDisplay(preserve); - ChangeWindowSize(); + ChangeWindowSize(true); end; {$ENDIF} end; @@ -247,6 +314,8 @@ begin SDL_WINDOWEVENT_MINIMIZED: begin e_UnpressAllKeys(); + if (gPlayer1 <> nil) then gPlayer1.releaseAllWeaponSwitchKeys(); + if (gPlayer2 <> nil) then gPlayer2.releaseAllWeaponSwitchKeys(); if not wMinimized then begin e_ResizeWindow(0, 0); @@ -262,9 +331,21 @@ begin SDL_WINDOWEVENT_RESIZED: begin - gScreenWidth := ev.data1; - gScreenHeight := ev.data2; - ChangeWindowSize(); + e_LogWritefln('Resize: (os=%dx%d) (ns=%dx%d)', [gScreenWidth, gScreenHeight, Integer(ev.data1), Integer(ev.data2)]); + {if (gFullscreen) then + begin + e_LogWriteln(' fullscreen fix applied.'); + if (gScreenWidth <> ev.data1) or (gScreenHeight <> ev.data2) then + begin + SDL_SetWindowSize(h_Wnd, gScreenWidth, gScreenHeight); + end; + end + else} + begin + gScreenWidth := ev.data1; + gScreenHeight := ev.data2; + end; + ChangeWindowSize(false); SwapBuffers(); if g_debug_WinMsgs then begin @@ -278,13 +359,14 @@ begin SDL_WINDOWEVENT_MAXIMIZED: begin + wMaximized := true; if wMinimized then begin e_ResizeWindow(gScreenWidth, gScreenHeight); wMinimized := false; wActivate := true; end; - if not gWinMaximized then + if (not gWinMaximized) and (not gFullscreen) then begin gWinMaximized := true; if g_debug_WinMsgs then @@ -297,13 +379,14 @@ begin SDL_WINDOWEVENT_RESTORED: begin + wMaximized := false; if wMinimized then begin e_ResizeWindow(gScreenWidth, gScreenHeight); wMinimized := false; wActivate := true; end; - if gWinMaximized then gWinMaximized := false; + gWinMaximized := false; if g_debug_WinMsgs then begin g_Console_Add('Now restored'); @@ -321,6 +404,8 @@ begin begin wDeactivate := true; e_UnpressAllKeys(); + if (gPlayer1 <> nil) then gPlayer1.releaseAllWeaponSwitchKeys(); + if (gPlayer2 <> nil) then gPlayer2.releaseAllWeaponSwitchKeys(); //e_WriteLog('window lost focus!', MSG_NOTIFY); end; end; @@ -347,7 +432,9 @@ begin gWinActive := false; - if assigned(winBlurCB) then winBlurCB(); + {$IFDEF ENABLE_HOLMES} + if assigned(winBlurCB) then winBlurCB(); + {$ENDIF} end; end else if wActivate then @@ -370,7 +457,10 @@ begin end; gWinActive := true; - if assigned(winFocusCB) then winFocusCB(); + + {$IFDEF ENABLE_HOLMES} + if assigned(winFocusCB) then winFocusCB(); + {$ENDIF} end; end; end; @@ -408,22 +498,24 @@ begin SDL_KEYDOWN, SDL_KEYUP: begin key := ev.key.keysym.scancode; + if key = SDL_SCANCODE_AC_BACK then + key := SDL_SCANCODE_ESCAPE; down := (ev.type_ = SDL_KEYDOWN); - {$IF not DEFINED(HEADLESS)} - if evSDLCB(ev) then + {$IF not DEFINED(HEADLESS) and DEFINED(ENABLE_HOLMES)} + if fuiOnSDLEvent(ev) then begin // event eaten, but... if not down then e_KeyUpDown(key, false); exit; end; {$ENDIF} - if down then KeyPress(key); e_KeyUpDown(key, down); + if down then KeyPress(key); end; - {$IF not DEFINED(HEADLESS)} + {$IF not DEFINED(HEADLESS) and DEFINED(ENABLE_HOLMES)} SDL_MOUSEBUTTONDOWN, SDL_MOUSEBUTTONUP, SDL_MOUSEWHEEL, SDL_MOUSEMOTION: - evSDLCB(ev); + fuiOnSDLEvent(ev); {$ENDIF} SDL_TEXTINPUT: @@ -431,9 +523,12 @@ begin Utf8ToUnicode(@uc, PChar(ev.text.text), 1); keychr := Word(uc); if (keychr > 127) then keychr := Word(wchar2win(WideChar(keychr))); - CharPress(AnsiChar(keychr)); + if (keychr > 0) and (keychr <= 255) then CharPress(AnsiChar(keychr)); end; + SDL_FINGERMOTION, SDL_FINGERDOWN, SDL_FINGERUP: + g_Touch_HandleEvent(ev.tfinger); + // other key presses and joysticks are handled in e_input end; end; @@ -461,15 +556,34 @@ begin if not g_Window_SetDisplay() then begin - KillGLWindow(); + KillGLWindow(false); e_WriteLog('Window creation error (resolution not supported?)', TMsgType.Fatal); exit; end; {$IF not DEFINED(HEADLESS)} - h_Gl := SDL_GL_CreateContext(h_Wnd); - if (h_Gl = nil) then exit; - if assigned(oglInitCB) then oglInitCB(); + h_GL := SDL_GL_CreateContext(h_Wnd); + if (h_GL = nil) then exit; + {$IFDEF ENABLE_HOLMES} + fuiScrWdt := gScreenWidth; + fuiScrHgt := gScreenHeight; + {$ENDIF} + SDL_GL_MakeCurrent(h_Wnd, h_GL); +{$IFDEF USE_NANOGL} + if nanoGL_Init() = 0 then + begin + KillGLWindow(false); + e_WriteLog('nanoGL initialization error', TMsgType.Fatal); + exit; + end; +{$ENDIF} +{$IFDEF USE_NOGL} + nogl_Init; +{$ENDIF} + {$IFDEF ENABLE_HOLMES} + if (assigned(oglInitCB)) then oglInitCB(); + {$ENDIF} + if (h_GL <> nil) then g_SetVSync(gVSync); {$ENDIF} e_ResizeWindow(gScreenWidth, gScreenHeight); @@ -577,7 +691,8 @@ begin begin if g_Texture_Get('INTER', ID) then begin - e_DrawSize(ID, 0, 0, 0, false, false, gScreenWidth, gScreenHeight) + e_DrawSize(ID, 0, 0, 0, false, false, gScreenWidth, gScreenHeight); + e_DarkenQuadWH(0, 0, gScreenWidth, gScreenHeight, 150); end else begin @@ -696,7 +811,7 @@ begin end; -procedure InitOpenGL (vsync: Boolean); +procedure g_SetVSync (vsync: Boolean); {$IF not DEFINED(HEADLESS)} var v: Byte; @@ -704,15 +819,35 @@ var begin {$IF not DEFINED(HEADLESS)} if vsync then v := 1 else v := 0; - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); - SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); - SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); - SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); - SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); - SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); // lights; it is enough to have 1-bit stencil buffer for lighting, but... - SDL_GL_SetSwapInterval(v); + if (SDL_GL_SetSwapInterval(v) <> 0) then + begin + e_WriteLog('oops; can''t change vsync option, restart required', TMsgType.Warning); + end + else + begin + if vsync then e_WriteLog('VSync: ON', TMsgType.Notify) else e_WriteLog('VSync: OFF', TMsgType.Notify); + end; +{$ENDIF} +end; + + +procedure InitOpenGL (); +begin +{$IF not DEFINED(HEADLESS)} + {$IFDEF USE_GLES1} + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); + {$ELSE} + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); + SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); // lights; it is enough to have 1-bit stencil buffer for lighting, but... + {$ENDIF} {$ENDIF} end; @@ -767,9 +902,21 @@ var {$ENDIF} arg: AnsiString; mdfo: TStream; + {$IFDEF ENABLE_HOLMES} + itmp: Integer; + valres: Word; + {$ENDIF} begin {$IFDEF HEADLESS} e_NoGraphics := true; +{$ELSE} + {$IFDEF ENABLE_HOLMES} + if (not g_holmes_imfunctional) then + begin + uiInitialize(); + uiContext.font := 'win14'; + end; + {$ENDIF} {$ENDIF} idx := 1; @@ -798,16 +945,43 @@ begin if arg = '--aimline' then g_dbg_aimline_on := true; {.$ENDIF} +{$IFDEF ENABLE_HOLMES} if arg = '--holmes' then begin g_holmes_enabled := true; g_Game_SetDebugMode(); end; + if (arg = '--holmes-ui-scale') or (arg = '-holmes-ui-scale') then begin if (idx <= ParamCount) then begin - if not conParseFloat(gh_ui_scale, ParamStr(idx)) then gh_ui_scale := 1.0; + if not conParseFloat(fuiRenderScale, ParamStr(idx)) then fuiRenderScale := 1.0; Inc(idx); end; end; + if (arg = '--holmes-font') or (arg = '-holmes-font') then + begin + if (idx <= ParamCount) then + begin + itmp := 0; + val(ParamStr(idx), itmp, valres); + {$IFNDEF HEADLESS} + if (valres = 0) and (not g_holmes_imfunctional) then + begin + case itmp of + 8: uiContext.font := 'win8'; + 14: uiContext.font := 'win14'; + 16: uiContext.font := 'win16'; + end; + end; + {$ELSE} + // fuck off, fpc! + itmp := itmp; + valres := valres; + {$ENDIF} + Inc(idx); + end; + end; +{$ENDIF} + if (arg = '--game-scale') or (arg = '-game-scale') then begin if (idx <= ParamCount) then @@ -827,12 +1001,13 @@ begin end; e_WriteLog('Initializing OpenGL', TMsgType.Notify); - InitOpenGL(gVSync); + InitOpenGL(); e_WriteLog('Creating GL window', TMsgType.Notify); if not CreateGLWindow(PChar(Format('Doom 2D: Forever %s', [GAME_VERSION]))) then begin result := 0; + e_WriteLog('Unable to create GL window: ' + SDL_GetError(), TMsgType.Fatal); exit; end; @@ -848,15 +1023,16 @@ begin e_LogWritefln('stencil buffer size: %s', [ltmp]); gwin_has_stencil := (ltmp > 0); - if not glHasExtension('GL_ARB_texture_non_power_of_two') then + if glHasExtension('GL_ARB_texture_non_power_of_two') or + glHasExtension('GL_OES_texture_npot') then begin - e_WriteLog('NPOT textures: NO', TMsgType.Warning); - glLegacyNPOT := true; + e_WriteLog('NPOT textures: YES', TMsgType.Notify); + glLegacyNPOT := false; end else begin - e_WriteLog('NPOT textures: YES', TMsgType.Notify); - glLegacyNPOT := false; + e_WriteLog('NPOT textures: NO', TMsgType.Warning); + glLegacyNPOT := true; end; gwin_dump_extensions := false; {$ENDIF} @@ -876,7 +1052,7 @@ begin while not ProcessMessage() do begin end; Release(); - KillGLWindow(); + KillGLWindow(false); result := 0; end;