DEADSOFTWARE

sdl1/2: fix invalid window title
[d2df-sdl.git] / src / game / sdl2 / g_system.pas
index 9485fd9b6e9a0897a5eb328b7232ae8f7ac8a3d9..9cd1ee194b0ae4b89bb587e593c689067fd477cb 100644 (file)
@@ -24,8 +24,8 @@ interface
   procedure sys_Delay (ms: Integer);
 
   (* --- Graphics --- *)
-  function sys_GetDispalyModes (bpp: Integer): SSArray;
-  function sys_SetDisplayMode (w, h, bpp: Integer; fullscreen: Boolean): Boolean;
+  function sys_GetDisplayModes (bpp: Integer): SSArray;
+  function sys_SetDisplayMode (w, h, bpp: Integer; fullscreen, maximized: Boolean): Boolean;
   procedure sys_EnableVSync (yes: Boolean);
   procedure sys_Repaint;
 
@@ -40,21 +40,22 @@ interface
 implementation
 
   uses
-    SysUtils, SDL2, Math,
+    SysUtils, SDL2, Math, ctypes,
     e_log, e_graphics, e_input, e_sound,
     {$INCLUDE ../nogl/noGLuses.inc}
     {$IFDEF ENABLE_HOLMES}
       g_holmes, sdlcarcass, fui_ctls,
     {$ENDIF}
-    g_touch, g_options, g_window, g_console, g_game, g_menu, g_gui, g_main;
+    g_touch, g_options, g_window, g_console, g_game, g_menu, g_gui, g_main, g_basic;
 
   const
-    GameTitle = 'Doom 2D: Forever (SDL 2)';
+    GameTitle = 'Doom 2D: Forever (SDL 2, %s)';
 
   var
     window: PSDL_Window;
     context: TSDL_GLContext;
-    display: Integer;
+    display, wx, wy: Integer;
+    wc: Boolean;
     JoystickHandle: array [0..e_MaxJoys - 1] of PSDL_Joystick;
     JoystickHatState: array [0..e_MaxJoys - 1, 0..e_MaxJoyHats - 1, HAT_LEFT..HAT_DOWN] of Boolean;
     JoystickZeroAxes: array [0..e_MaxJoys - 1, 0..e_MaxJoyAxes - 1] of Integer;
@@ -73,12 +74,47 @@ implementation
 
   (* --------- Graphics --------- *)
 
+  function LoadGL: Boolean;
+  begin
+    result := true;
+    {$IFDEF NOGL_INIT}
+    nogl_Init;
+    if glRenderToFBO and (not nogl_ExtensionSupported('GL_OES_framebuffer_object')) then
+    {$ELSE}
+    if glRenderToFBO and (not Load_GL_ARB_framebuffer_object) then
+    {$ENDIF}
+    begin
+      e_LogWriteln('GL: framebuffer objects not supported; disabling FBO rendering');
+      glRenderToFBO := false;
+    end;
+  end;
+
+  procedure FreeGL;
+  begin
+    {$IFDEF NOGL_INIT}
+    nogl_Quit();
+    {$ENDIF}
+  end;
+
   procedure UpdateSize (w, h: Integer);
   begin
     gWinSizeX := w;
     gWinSizeY := h;
-    gWinRealPosX := 0;
-    gWinRealPosY := 0;
+    gRC_Width := w;
+    gRC_Height := h;
+    if glRenderToFBO then
+    begin
+      // store real window size in gWinSize, downscale resolution now
+      w := round(w / r_pixel_scale);
+      h := round(h / r_pixel_scale);
+      if not e_ResizeFramebuffer(w, h) then
+      begin
+        e_LogWriteln('GL: could not create framebuffer, falling back to --no-fbo');
+        glRenderToFBO := False;
+        w := gWinSizeX;
+        h := gWinSizeY;
+      end;
+    end;
     gScreenWidth := w;
     gScreenHeight := h;
     {$IFDEF ENABLE_HOLMES}
@@ -88,7 +124,7 @@ implementation
     e_ResizeWindow(w, h);
     e_InitGL;
     g_Game_SetupScreenSize;
-    {$IFNDEF ANDOIRD}
+    {$IFNDEF ANDROID}
       (* This will fix menu reset on keyboard showing *)
       g_Menu_Reset;
     {$ENDIF}
@@ -98,8 +134,17 @@ implementation
     {$ENDIF}
   end;
 
-  function InitWindow (w, h, bpp: Integer; fullScreen: Boolean): Boolean;
-    var flags: UInt32;
+  function GetTitle (): AnsiString;
+    var info: AnsiString;
+  begin
+    info := g_GetBuildHash(false);
+    if info = 'custom build' then
+      info := info + ' by ' + g_GetBuilderName() + ' ' + GAME_BUILDDATE + ' ' + GAME_BUILDTIME;
+    result := Format(GameTitle, [info]);
+  end;
+
+  function InitWindow (w, h, bpp: Integer; fullScreen, maximized: Boolean): Boolean;
+    var flags: UInt32; x, y: cint; title: AnsiString;
   begin
     // note: on window close make: if assigned(oglDeinitCB) then oglDeinitCB;
     e_LogWritefln('InitWindow %s %s %s %s', [w, h, bpp, fullScreen]);
@@ -122,20 +167,45 @@ implementation
       {$ENDIF}
       flags := SDL_WINDOW_OPENGL or SDL_WINDOW_RESIZABLE;
       if fullScreen then flags := flags or SDL_WINDOW_FULLSCREEN;
-      window := SDL_CreateWindow(GameTitle, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, flags);
+      if maximized then flags := flags or SDL_WINDOW_MAXIMIZED;
+      if wc then
+      begin
+        x := SDL_WINDOWPOS_CENTERED;
+        y := SDL_WINDOWPOS_CENTERED
+      end
+      else
+      begin
+        x := wx;
+        y := wy
+      end;
+      title := GetTitle();
+      window := SDL_CreateWindow(PChar(title), x, y, w, h, flags);
       if window <> nil then
       begin
         context := SDL_GL_CreateContext(window);
         if context <> nil then
         begin
-          {$IFDEF USE_NOGL}
-            nogl_Init;
-          {$ENDIF}
+          if not LoadGL then
+          begin
+            e_LogWriteln('GL: unable to load OpenGL functions', TMsgType.Fatal);
+            SDL_GL_DeleteContext(context); context := nil;
+            exit;
+          end;
+          if (fullscreen = false) and (maximized = false) and (wc = false) then
+          begin
+            SDL_GetWindowPosition(window, @x, @y);
+            wx := x; wy := y
+          end;
+          gFullScreen := fullscreen;
+          gWinMaximized := maximized;
+          gRC_FullScreen := fullscreen;
+          gRC_Maximized := maximized;
           UpdateSize(w, h);
           result := true
         end
         else
         begin
+          // SDL_DestroyWindow(window);
           e_LogWritefln('SDL: unable to create OpenGL context: %s', [SDL_GetError])
         end
       end
@@ -147,8 +217,30 @@ implementation
     else
     begin
       if fullScreen then flags := SDL_WINDOW_FULLSCREEN else flags := 0;
-      SDL_SetWindowSize(window, w, h);
       SDL_SetWindowFullscreen(window, flags);
+      SDL_SetWindowSize(window, w, h);
+      if maximized then SDL_MaximizeWindow(window);
+      // always reset to center when changing fullscreen->windowed for safety purposes
+      if wc or (gFullscreen and not fullscreen) or (gWinMaximized and not maximized) then
+      begin
+        x := SDL_WINDOWPOS_CENTERED;
+        y := SDL_WINDOWPOS_CENTERED
+      end
+      else
+      begin
+        x := wx;
+        y := wy
+      end;
+      SDL_SetWindowPosition(window, x, y);
+      if (fullscreen = false) and (maximized = false) and (wc = false) then
+      begin
+        SDL_GetWindowPosition(window, @x, @y);
+        wx := x; wy := y
+      end;
+      gFullScreen := fullscreen;
+      gWinMaximized := maximized;
+      gRC_FullScreen := fullscreen;
+      gRC_Maximized := maximized;
       UpdateSize(w, h);
       result := true
     end
@@ -167,7 +259,7 @@ implementation
       SDL_GL_SetSwapInterval(0)
   end;
 
-  function sys_GetDispalyModes (bpp: Integer): SSArray;
+  function sys_GetDisplayModes (bpp: Integer): SSArray;
     var i, count, num, pw, ph: Integer; m: TSDL_DisplayMode;
   begin
     result := nil;
@@ -199,9 +291,9 @@ implementation
     end
   end;
 
-  function sys_SetDisplayMode (w, h, bpp: Integer; fullScreen: Boolean): Boolean;
+  function sys_SetDisplayMode (w, h, bpp: Integer; fullScreen, maximized: Boolean): Boolean;
   begin
-    result := InitWindow(w, h, bpp, fullScreen)
+    result := InitWindow(w, h, bpp, fullScreen, maximized)
   end;
 
   (* --------- Joystick --------- *)
@@ -354,10 +446,17 @@ implementation
   function HandleWindow (var ev: TSDL_WindowEvent): Boolean;
   begin
     result := false;
+    if g_dbg_input then
+      e_LogWritefln('Window Event: event = %s, data1 = %s, data2 = %s', [ev.event, ev.data1, ev.data2]);
     case ev.event of
       SDL_WINDOWEVENT_RESIZED: UpdateSize(ev.data1, ev.data2);
       SDL_WINDOWEVENT_EXPOSED: sys_Repaint;
       SDL_WINDOWEVENT_CLOSE: result := true;
+      SDL_WINDOWEVENT_MOVED:
+        begin
+          wx := ev.data1;
+          wy := ev.data2
+        end;
       SDL_WINDOWEVENT_FOCUS_LOST, SDL_WINDOWEVENT_MINIMIZED:
         begin
           e_UnpressAllKeys;
@@ -369,6 +468,16 @@ implementation
         end;
       SDL_WINDOWEVENT_FOCUS_GAINED, SDL_WINDOWEVENT_MAXIMIZED, SDL_WINDOWEVENT_RESTORED:
         begin
+          if ev.event = SDL_WINDOWEVENT_MAXIMIZED then
+          begin
+            gWinMaximized := true;
+            gRC_Maximized := true
+          end
+          else if ev.event = SDL_WINDOWEVENT_RESTORED then
+          begin
+            gWinMaximized := false;
+            gRC_Maximized := false
+          end;
           e_MuteChannels(false);
           {$IFDEF ENABLE_HOLMES}
             if assigned(winFocusCB) then winFocusCB;
@@ -399,29 +508,30 @@ implementation
       e_KeyUpDown(key, down);
       g_Console_ProcessBind(key, down);
     end
-    else if gConsoleShow or gChatShow or (g_ActiveWindow <> nil) then
+    else
     begin
-      KeyPress(key) // key repeat in menus and shit
+      if g_dbg_input then
+        e_LogWritefln('Input Debug: keyrep, scancode=%s', [key]);
+      g_Console_ProcessBindRepeat(key);
     end
   end;
 
   procedure HandleTextInput (var ev: TSDL_TextInputEvent);
     var ch: UnicodeChar; sch: AnsiChar;
   begin
-    if g_dbg_input then
-      e_LogWritefln('Input Debug: text, text=%s', [ev.text]);
     Utf8ToUnicode(@ch, PChar(ev.text), 1);
-    if IsValid1251(Word(ch)) then
-    begin
-      sch := AnsiChar(wchar2win(ch));
+    sch := AnsiChar(wchar2win(ch));
+    if g_dbg_input then
+      e_LogWritefln('Input Debug: text, text="%s", ch = %s, sch = %s', [ev.text, Ord(ch), Ord(sch)]);
+    if IsValid1251(Word(ch)) and IsPrintable1251(ch) then
       CharPress(sch);
-    end;
   end;
 
   function sys_HandleInput (): Boolean;
     var ev: TSDL_Event;
   begin
     result := false;
+    ZeroMemory(@ev, sizeof(ev));
     while SDL_PollEvent(@ev) <> 0 do
     begin
       case ev.type_ of
@@ -462,11 +572,15 @@ implementation
         flags := SDL_INIT_TIMER or $00004000;
       {$ENDIF}
     {$ELSE}
-      flags := SDL_INIT_JOYSTICK or SDL_INIT_TIMER or SDL_INIT_VIDEO;
+      flags := SDL_INIT_TIMER or SDL_INIT_VIDEO;
     {$ENDIF}
     SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, '0');
     if SDL_Init(flags) <> 0 then
       raise Exception.Create('SDL: Init failed: ' + SDL_GetError);
+    {$IFNDEF HEADLESS}
+      if SDL_InitSubSystem(SDL_INIT_JOYSTICK) <> 0 then
+        e_LogWritefln('SDL: Init subsystem failed: %s', [SDL_GetError()]);
+    {$ENDIF}
     SDL_ShowCursor(SDL_DISABLE);
   end;
 
@@ -475,9 +589,7 @@ implementation
     e_WriteLog('Releasing SDL2', TMsgType.Notify);
     if context <> nil then
     begin
-      {$IFDEF USE_NOGL}
-        nogl_Quit;
-      {$ENDIF}
+      FreeGL;
       SDL_GL_DeleteContext(context);
       context := nil;
     end;
@@ -491,4 +603,11 @@ implementation
 
 initialization
   conRegVar('sdl2_display_index', @display, 'use display index as base', '');
+  conRegVar('sdl2_window_x', @wx, 'window position x', '');
+  conRegVar('sdl2_window_y', @wy, 'window position y', '');
+  conRegVar('sdl2_window_center', @wc, 'force window creation at center', '');
+  display := 0;
+  wx := SDL_WINDOWPOS_CENTERED;
+  wy := SDL_WINDOWPOS_CENTERED;
+  wc := false
 end.