DEADSOFTWARE

revive sdl2 system driver
[d2df-sdl.git] / src / game / sdl / g_system.pas
index a72f6584446c64c1065ac8ec236fb0934b0c9a58..5fe730283d1c6384f36ba8d46abcabca7a1a1abe 100644 (file)
@@ -18,9 +18,7 @@ unit g_system;
 interface
 
   (* To fix:
-   *   - Text input
    *   - Joystick support
-   *   - Window resizing using SDL_VIDEORESIZE
    *)
 
   uses Utils;
@@ -48,16 +46,21 @@ implementation
   uses
     SysUtils, SDL, GL,
     e_log, e_graphics, e_input,
-    g_options, g_window, g_console, g_game, g_menu;
+    g_options, g_window, g_console, g_game, g_menu, g_gui, g_main;
+
+  const
+    GameTitle = 'Doom 2D: Forever (SDL 1.2)';
 
   var
+    userResize: Boolean;
+    modeResize: Integer;
     screen: PSDL_Surface;
 
   (* --------- Utils --------- *)
 
   function sys_GetTicks (): Int64;
   begin
-    Result := SDL_GetTicks()
+    result := SDL_GetTicks()
   end;
 
   procedure sys_Delay (ms: Integer);
@@ -89,26 +92,26 @@ implementation
   function InitWindow (w, h, bpp: Integer; fullScreen: Boolean): Boolean;
     var flags: Uint32;
   begin
-    e_LogWritefln('InitWindow %s %s %s %s', [w, h, bpp, fullscreen]);
-    Result := False;
+    e_LogWritefln('InitWindow %s %s %s %s', [w, h, bpp, fullScreen]);
+    result := False;
     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...
-    flags := SDL_OPENGL or SDL_VIDEORESIZE;
-    if fullScreen then
-      flags := flags or SDL_FULLSCREEN;
+    flags := SDL_OPENGL;
+    if fullScreen then flags := flags or SDL_FULLSCREEN;
+    if userResize then flags := flags or SDL_VIDEORESIZE;
     if (screen = nil) or (SDL_VideoModeOk(w, h, bpp, flags) <> 0) then
     begin
       SDL_FreeSurface(screen);
       screen := SDL_SetVideoMode(w, h, bpp, flags);
       if screen <> nil then
       begin
-        SDL_WM_SetCaption('Doom 2D: Forever (SDL 1.2)', nil);
+        SDL_WM_SetCaption(GameTitle, nil);
         UpdateSize(w, h);
-        Result := True
+        result := True
       end
     end
     else
@@ -236,6 +239,8 @@ implementation
       SDLK_PAUSE: x := IK_PAUSE;
       SDLK_A..SDLK_Z: x := IK_A + (key - SDLK_A);
       SDLK_MINUS: x := IK_MINUS;
+      SDLK_RMETA: x := IK_RMETA;
+      SDLK_LMETA: x := IK_LMETA;
     else
       x := IK_INVALID
     end;
@@ -243,16 +248,25 @@ implementation
   end;
 
   procedure HandleKeyboard (var ev: TSDL_KeyboardEvent);
-    var down: Boolean; key, stb: Integer;
+    var down, repeated: Boolean; key: Integer; ch: Char;
   begin
+    key := Key2Stub(ev.keysym.sym);
     down := (ev.type_ = SDL_KEYDOWN);
-    key := ev.keysym.sym;
-    stb := Key2Stub(key);
+    repeated := down and e_KeyPressed(key);
+    ch := wchar2win(WideChar(ev.keysym.unicode));
     if g_dbg_input then
-      e_LogWritefln('Input Debug: keysym, press=%s, scancode=%s SDL.ev.key.state=%s', [down, key, ev.state]);
-    e_KeyUpDown(stb, down);
-    g_Console_ProcessBind(stb, down);
-    (* !!! need text input -- console, cheaats, fields *)
+      e_LogWritefln('Input Debug: keysym, down=%s, sym=%s, state=%s, unicode=%s, stubsym=%s, cp1251=%s', [down, ev.keysym.sym, ev.state, ev.keysym.unicode, key, Ord(ch)]);
+    if not repeated then
+    begin
+      e_KeyUpDown(key, down);
+      g_Console_ProcessBind(key, down);
+    end
+    else if gConsoleShow or gChatShow or (g_ActiveWindow <> nil) then
+    begin
+      KeyPress(key) // key repeat in menus and shit
+    end;
+    if down and IsValid1251(ev.keysym.unicode) and IsPrintable1251(ch) then
+      CharPress(ch)
   end;
 
   function sys_HandleInput (): Boolean;
@@ -263,8 +277,17 @@ implementation
     begin
       case ev.type_ of
         SDL_QUITEV: result := true;
-        SDL_VIDEORESIZE: InitWindow(ev.resize.w, ev.resize.h, gBPP, gFullscreen);
+        SDL_VIDEORESIZE:
+          begin
+            if g_dbg_input then
+              e_LogWritefln('Input Debug: SDL_VIDEORESIZE %s %s', [ev.resize.w, ev.resize.h]);
+            if modeResize = 1 then
+              UpdateSize(ev.resize.w, ev.resize.h)
+            else if modeResize > 1 then
+              InitWindow(ev.resize.w, ev.resize.h, gBPP, gFullscreen)
+          end;
         SDL_KEYUP, SDL_KEYDOWN: HandleKeyboard(ev.key);
+        SDL_VIDEOEXPOSE: sys_Repaint;
       end
     end
   end;
@@ -281,14 +304,17 @@ implementation
   procedure sys_Init;
     var flags: Uint32; ok: Boolean;
   begin
+    e_WriteLog('Init SDL', TMsgType.Notify);
     flags := SDL_INIT_VIDEO or SDL_INIT_AUDIO or
              SDL_INIT_TIMER or SDL_INIT_JOYSTICK
              (*or SDL_INIT_NOPARACHUTE*);
     if SDL_Init(flags) <> 0 then
-      raise Exception.Create('SDL: Init failed: ' + SDL_GetError());
+      raise Exception.Create('SDL: Init failed: ' + SDL_GetError);
     ok := InitWindow(gScreenWidth, gScreenHeight, gBPP, gFullScreen);
     if not ok then
-      raise Exception.Create('SDL: failed to set videomode: ' + SDL_GetError)
+      raise Exception.Create('SDL: Failed to set videomode: ' + SDL_GetError);
+    SDL_EnableUNICODE(1);
+    SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
   end;
 
   procedure sys_Final;
@@ -298,4 +324,10 @@ implementation
     SDL_Quit
   end;
 
+initialization
+  (* window resize are broken both on linux and osx, so disabled by default *)
+  conRegVar('sdl_allow_resize', @userResize, 'allow to resize window by user', 'allow to resize window by user');
+  conRegVar('sdl_resize_action', @modeResize, 'set window resize mode (0: ignore, 1: change, 2: reset)', '');
+  userResize := false;
+  modeResize := 0;
 end.