DEADSOFTWARE

render: add option -dDISABLE_RENDER
[d2df-sdl.git] / src / game / sdl2 / g_system.pas
index 6750d68e6364570a0485647bc8c61d7e1bf832d4..74e8d0354079582af6be99980952b11d6e8f5769 100644 (file)
@@ -19,13 +19,9 @@ interface
 
   uses Utils;
 
-  (* --- Utils --- *)
-  function sys_GetTicks (): Int64;
-  procedure sys_Delay (ms: Integer);
-
   (* --- Graphics --- *)
   function sys_GetDisplayModes (bpp: Integer): SSArray;
-  function sys_SetDisplayMode (w, h, bpp: Integer; fullscreen: Boolean): Boolean;
+  function sys_SetDisplayMode (w, h, bpp: Integer; fullscreen, maximized: Boolean): Boolean;
   procedure sys_EnableVSync (yes: Boolean);
   procedure sys_Repaint;
 
@@ -33,20 +29,35 @@ interface
   function sys_HandleInput (): Boolean;
   procedure sys_RequestQuit;
 
+{$IFDEF ENABLE_TOUCH}
+  function sys_IsTextInputActive (): Boolean;
+  procedure sys_ShowKeyboard (yes: Boolean);
+{$ENDIF}
+
   (* --- Init --- *)
   procedure sys_Init;
   procedure sys_Final;
 
+  var (* hooks *)
+    sys_CharPress: procedure (ch: AnsiChar) = nil;
+    sys_ScreenResize: procedure (w, h: Integer) = nil;
+
 implementation
 
   uses
-    SysUtils, SDL2, Math,
-    e_log, e_graphics, e_input, e_sound,
-    {$INCLUDE ../nogl/noGLuses.inc}
     {$IFDEF ENABLE_HOLMES}
-      g_holmes, sdlcarcass, fui_ctls,
+      sdlcarcass,
+    {$ENDIF}
+    {$IFDEF ENABLE_RENDER}
+      r_render,
     {$ENDIF}
-    g_touch, g_options, g_window, g_console, g_game, g_menu, g_gui, g_main, g_basic;
+    {$IFDEF ENABLE_MENU}
+      g_gui,
+    {$ENDIF}
+    SysUtils, SDL2, Math, ctypes,
+    e_log, e_input, e_sound,
+    g_options, g_console, g_game, g_basic
+  ;
 
   const
     GameTitle = 'Doom 2D: Forever (SDL 2, %s)';
@@ -54,50 +65,20 @@ implementation
   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;
 
-  (* --------- Utils --------- *)
-
-  function sys_GetTicks (): Int64;
-  begin
-    result := SDL_GetTicks()
-  end;
-
-  procedure sys_Delay (ms: Integer);
-  begin
-    SDL_Delay(ms)
-  end;
+{$IFDEF ENABLE_TOUCH}
+  var (* touch *)
+    angleFire: Boolean;
+    keyFinger: array [VK_FIRSTKEY..VK_LASTKEY] of Integer;
+{$ENDIF}
 
   (* --------- Graphics --------- *)
 
-  procedure UpdateSize (w, h: Integer);
-  begin
-    gWinSizeX := w;
-    gWinSizeY := h;
-    gWinRealPosX := 0;
-    gWinRealPosY := 0;
-    gScreenWidth := w;
-    gScreenHeight := h;
-    {$IFDEF ENABLE_HOLMES}
-      fuiScrWdt := w;
-      fuiScrHgt := h;
-    {$ENDIF}
-    e_ResizeWindow(w, h);
-    e_InitGL;
-    g_Game_SetupScreenSize;
-    {$IFNDEF ANDOIRD}
-      (* This will fix menu reset on keyboard showing *)
-      g_Menu_Reset;
-    {$ENDIF}
-    g_Game_ClearLoading;
-    {$IFDEF ENABLE_HOLMES}
-      if assigned(oglInitCB) then oglInitCB;
-    {$ENDIF}
-  end;
-
   function GetTitle (): PChar;
     var info: AnsiString;
   begin
@@ -107,8 +88,8 @@ implementation
     result := PChar(Format(GameTitle, [info]))
   end;
 
-  function InitWindow (w, h, bpp: Integer; fullScreen: Boolean): Boolean;
-    var flags: UInt32;
+  function InitWindow (w, h, bpp: Integer; fullScreen, maximized: Boolean): Boolean;
+    var flags: UInt32; x, y: cint;
   begin
     // note: on window close make: if assigned(oglDeinitCB) then oglDeinitCB;
     e_LogWritefln('InitWindow %s %s %s %s', [w, h, bpp, fullScreen]);
@@ -131,20 +112,39 @@ implementation
       {$ENDIF}
       flags := SDL_WINDOW_OPENGL or SDL_WINDOW_RESIZABLE;
       if fullScreen then flags := flags or SDL_WINDOW_FULLSCREEN;
-      window := SDL_CreateWindow(GetTitle(), 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;
+      window := SDL_CreateWindow(GetTitle(), x, y, w, h, flags);
       if window <> nil then
       begin
         context := SDL_GL_CreateContext(window);
         if context <> nil then
         begin
-          {$IFDEF NOGL_INIT}
-            nogl_Init;
-          {$ENDIF}
-          UpdateSize(w, h);
+          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;
+          if @sys_ScreenResize <> nil then
+            sys_ScreenResize(w, h);
           result := true
         end
         else
         begin
+          // SDL_DestroyWindow(window);
           e_LogWritefln('SDL: unable to create OpenGL context: %s', [SDL_GetError])
         end
       end
@@ -156,9 +156,32 @@ implementation
     else
     begin
       if fullScreen then flags := SDL_WINDOW_FULLSCREEN else flags := 0;
-      SDL_SetWindowSize(window, w, h);
       SDL_SetWindowFullscreen(window, flags);
-      UpdateSize(w, h);
+      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;
+      if @sys_ScreenResize <> nil then
+        sys_ScreenResize(w, h);
       result := true
     end
   end;
@@ -208,9 +231,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 --------- *)
@@ -358,15 +381,160 @@ implementation
     end
   end;
 
+  (* --------- Touch --------- *)
+
+{$IFDEF ENABLE_TOUCH}
+  function sys_IsTextInputActive (): Boolean;
+  begin
+    Result := SDL_IsTextInputActive() = SDL_True
+  end;
+
+  procedure sys_ShowKeyboard (yes: Boolean);
+  begin
+    {$IFNDEF HEADLESS}
+      if g_dbg_input then
+        e_LogWritefln('g_Touch_ShowKeyboard(%s)', [yes]);
+      (* on desktop we always receive text (needed for cheats) *)
+      if yes or (SDL_HasScreenKeyboardSupport() = SDL_FALSE) then
+        SDL_StartTextInput
+      else
+        SDL_StopTextInput
+    {$ENDIF}
+  end;
+
+  procedure HandleTouch (const ev: TSDL_TouchFingerEvent);
+    var
+      x, y, i, finger: Integer;
+
+    function IntersectControl(ctl, xx, yy: Integer): Boolean;
+      {$IFDEF ENABLE_RENDER}
+        var x, y, w, h: Integer; founded: Boolean;
+      {$ENDIF}
+    begin
+      {$IFDEF ENABLE_RENDER}
+        r_Render_GetKeyRect(ctl, x, y, w, h, founded);
+        Result := founded and (xx >= x) and (yy >= y) and (xx <= x + w) and (yy <= y + h);
+      {$ELSE}
+        Result := False
+      {$ENDIF}
+    end;
+
+    procedure KeyUp (finger, i: Integer);
+    begin
+      if g_dbg_input then
+        e_LogWritefln('Input Debug: g_touch.KeyUp, finger=%s, key=%s', [finger, i]);
+
+      keyFinger[i] := 0;
+      e_KeyUpDown(i, False);
+      g_Console_ProcessBind(i, False);
+
+      (* up/down + fire hack *)
+{$IFDEF ENABLE_MENU}
+      if g_touch_fire and (gGameSettings.GameType <> GT_NONE) and (g_ActiveWindow = nil) and angleFire then
+{$ELSE}
+      if g_touch_fire and (gGameSettings.GameType <> GT_NONE) and angleFire then
+{$ENDIF}
+      begin
+        if (i = VK_UP) or (i = VK_DOWN) then
+        begin
+          angleFire := False;
+          keyFinger[VK_FIRE] := 0;
+          e_KeyUpDown(VK_FIRE, False);
+          g_Console_ProcessBind(VK_FIRE, False)
+        end
+      end
+    end;
+
+    procedure KeyDown (finger, i: Integer);
+    begin
+      if g_dbg_input then
+        e_LogWritefln('Input Debug: g_touch.KeyDown, finger=%s, key=%s', [finger, i]);
+
+      keyFinger[i] := finger;
+      e_KeyUpDown(i, True);
+      g_Console_ProcessBind(i, True);
+
+      (* up/down + fire hack *)
+{$IFDEF ENABLE_MENU}
+      if g_touch_fire and (gGameSettings.GameType <> GT_NONE) and (g_ActiveWindow = nil) then
+{$ELSE}
+      if g_touch_fire and (gGameSettings.GameType <> GT_NONE) then
+{$ENDIF}
+      begin
+        if i = VK_UP then
+        begin
+          angleFire := True;
+          keyFinger[VK_FIRE] := -1;
+          e_KeyUpDown(VK_FIRE, True);
+          g_Console_ProcessBind(VK_FIRE, True)
+        end
+        else if i = VK_DOWN then
+        begin
+          angleFire := True;
+          keyFinger[VK_FIRE] := -1;
+          e_KeyUpDown(VK_FIRE, True);
+          g_Console_ProcessBind(VK_FIRE, True)
+        end
+      end
+    end;
+
+    procedure KeyMotion (finger, i: Integer);
+    begin
+      if keyFinger[i] <> finger then
+      begin
+        KeyUp(finger, i);
+        KeyDown(finger, i)
+      end
+    end;
+
+  begin
+    if not g_touch_enabled then
+      Exit;
+
+    finger := ev.fingerId + 2;
+    x := Trunc(ev.x * gWinSizeX);
+    y := Trunc(ev.y * gWinSizeY);
+
+    for i := VK_FIRSTKEY to VK_LASTKEY do
+    begin
+      if IntersectControl(i, x, y) then
+      begin
+        if ev.type_ = SDL_FINGERUP then
+          KeyUp(finger, i)
+        else if ev.type_ = SDL_FINGERMOTION then
+          KeyMotion(finger, i)
+        else if ev.type_ = SDL_FINGERDOWN then
+          keyDown(finger, i)
+      end
+      else if keyFinger[i] = finger then
+      begin
+        if ev.type_ = SDL_FINGERUP then
+          KeyUp(finger, i)
+        else if ev.type_ = SDL_FINGERMOTION then
+          KeyUp(finger, i)
+      end
+    end
+  end;
+{$ENDIF} // TOUCH
+
   (* --------- Input --------- *)
 
   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_RESIZED:
+        if @sys_ScreenResize <> nil then
+          sys_ScreenResize(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;
@@ -378,6 +546,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;
@@ -408,9 +586,11 @@ 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;
 
@@ -421,8 +601,9 @@ implementation
     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);
+    if @sys_CharPress <> nil then
+      if IsValid1251(Word(ch)) and IsPrintable1251(ch) then
+        sys_CharPress(sch)
   end;
 
   function sys_HandleInput (): Boolean;
@@ -442,7 +623,9 @@ implementation
         SDL_JOYDEVICEADDED: HandleJoyAdd(ev.jdevice);
         SDL_JOYDEVICEREMOVED: HandleJoyRemove(ev.jdevice);
         SDL_TEXTINPUT: HandleTextInput(ev.text);
-        SDL_FINGERMOTION, SDL_FINGERDOWN, SDL_FINGERUP: g_Touch_HandleEvent(ev.tfinger);
+        {$IFDEF ENABLE_TOUCH}
+          SDL_FINGERMOTION, SDL_FINGERDOWN, SDL_FINGERUP: HandleTouch(ev.tfinger);
+        {$ENDIF}
         {$IFDEF ENABLE_HOLMES}
           SDL_MOUSEBUTTONDOWN, SDL_MOUSEBUTTONUP, SDL_MOUSEWHEEL, SDL_MOUSEMOTION: fuiOnSDLEvent(ev);
         {$ENDIF}
@@ -470,12 +653,22 @@ 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);
+    {$IFDEF ENABLE_TOUCH}
+      sys_ShowKeyboard(FALSE);
+      g_touch_enabled := SDL_GetNumTouchDevices() > 0;
+    {$ELSE}
+      g_touch_enabled := False;
+    {$ENDIF}
   end;
 
   procedure sys_Final;
@@ -483,9 +676,6 @@ implementation
     e_WriteLog('Releasing SDL2', TMsgType.Notify);
     if context <> nil then
     begin
-      {$IFDEF NOGL_INIT}
-        nogl_Quit;
-      {$ENDIF}
       SDL_GL_DeleteContext(context);
       context := nil;
     end;
@@ -499,4 +689,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.