DEADSOFTWARE

implemented rotation and text input
[d2df-sdl.git] / src / wrappers / sdl2 / sdl2allegro.inc
index 78c6f41f158f10301c163e2c56a7e8dbb46181f8..3392c68ef45f081cd50a796f15d189122fc31401 100644 (file)
@@ -83,10 +83,10 @@ interface
       1: (mem: TMem);
       2: (unknown: TUnknown);
       {$IFDEF ANDROID}
-      3: (androidio: TAndroidIO);
+      //3: (androidio: TAndroidIO);
       {$ENDIF}
       {$IFDEF WINDOWS}
-      3: (windowsio: TWindowsIO);
+      //3: (windowsio: TWindowsIO);
       {$ENDIF}
     end;
 
@@ -205,7 +205,7 @@ interface
       type_: UInt32;                                          // SDL_TEXTINPUT
       timestamp: UInt32;
       windowID: UInt32;                                       // The window with keyboard focus, if any
-      text: array[0..SDL_TEXTINPUTEVENT_TEXT_SIZE] of Char;   // The input text
+      text: array[0..SDL_TEXTINPUTEVENT_TEXT_SIZE - 1] of Char;   // The input text
     end;
 
     TSDL_QuitEvent = record
@@ -375,12 +375,33 @@ implementation
     maxKeyBuffer = 64;
 
   var
-    __crt0_argv: PPchar; cvar; external;
-    myargv: array [0..255] of Pchar;
     keyring: array [0..maxKeyBuffer - 1] of Integer;
     keybeg, keyend: Integer;
-
-    ticks: UInt32; (* !!! VOLATILE !!! *)
+    inputChar: Char;
+    inputText: Boolean;
+    ticks: UInt32;
+    quit: Boolean;
+
+    s2lc: array [0..KEY_MAX] of char = (
+      #00, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
+      'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4',
+      '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', #00,
+      #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, '`', '-', '=', #00,
+      #09, '[', ']', #10, ';', #39, '\', '\', ',', '.', '/', ' ', #00, #00, #00, #00,
+      #00, #00, #00, #00, #00, '/', '*', '-', '+', #00, #00, #00, #00, #00, #00, #00,
+      #00, #00, #00, #00, '@', #00, ':', #00, '=', #00, ';', #00, #00, #00, #00, #00,
+      #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, #00
+    );
+    s2uc: array [0..KEY_MAX] of char = (
+      #00, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
+      'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ')', '!', '@', '#', '$',
+      '%', '^', '&', '*', '(', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', #00,
+      #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, '~', '_', '+', #00,
+      #09, '{', '}', #10, ':', '"', '|', '|', '<', '>', '?', ' ', #00, #00, #00, #00,
+      #00, #00, #00, #00, #00, '/', '*', '-', '+', #00, #00, #00, #00, #00, #00, #00,
+      #00, #00, #00, #00, '@', #00, ':', #00, '=', #00, ';', #00, #00, #00, #00, #00,
+      #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, #00
+    );
 
   function IsEmptyKeyboard: Boolean;
   begin
@@ -410,14 +431,10 @@ implementation
       keyend := (keyend + 1) mod maxKeyBuffer
     end
   end;
-  procedure KeyboardWatcherEND;
-  begin
-  end;
+  procedure KeyboardWatcherEND; begin end;
 
   /// MACRO ///
 
-//from "sdl_pixels.h"
-
   function SDL_PIXELFLAG(X: Cardinal): Cardinal;
   begin
     Result := (X shr 28) and $0F;
@@ -471,15 +488,20 @@ implementation
 
   function SDL_IsTextInputActive: TSDL_Bool;
   begin
-    result := SDL_FALSE
+    if inputText then
+      result := SDL_TRUE
+    else
+      result := SDL_FALSE
   end;
 
   procedure SDL_StartTextInput;
   begin
+    inputText := True
   end;
 
   procedure SDL_StopTextInput;
   begin
+    inputText := False
   end;
 
 (********** JOYSTICK **********)
@@ -550,33 +572,27 @@ implementation
   begin
     inc(ticks)
   end;
-  procedure AllegroTimerCallbackEND;
-  begin
-  end;
-
-
+  procedure AllegroTimerCallbackEND; begin end;
 
   function SDL_GetPerformanceCounter: UInt64;
   begin
-    //e_LogWriteln('SDL_GetPerformanceCounter');
+    (* TODO *)
     result := ticks;
   end;
 
   function SDL_GetPerformanceFrequency: UInt64;
   begin
-    //e_LogWriteln('SDL_GetPerformanceFrequency');
+    (* TODO *)
     result := 1
   end;
 
   procedure SDL_Delay(ms: UInt32);
   begin
-    //e_LogWriteln('SDL_Delay');
-    //rest(ms)
+    rest(ms)
   end;
 
   function SDL_GetTicks: UInt32;
   begin
-    //e_LogWriteln('SDL_GetTicks');
     result := ticks;
   end;
 
@@ -643,10 +659,7 @@ implementation
     e_LogWritefln('SDL_CreateWindow %s %s %s %s %s %u', [title, x, y, w, h, flags]);
     result := nil;
 
-{$IF DEFINED(AL_TEXT)}
-    mode := GFX_TEXT;
-    w := 0; h := 0;
-{$ELSEIF DEFINED(GO32V2)}
+{$IF DEFINED(GO32V2)}
     mode := GFX_AUTODETECT;
 {$ELSE}
     if (flags and (SDL_WINDOW_FULLSCREEN or SDL_WINDOW_FULLSCREEN_DESKTOP)) <> 0 then
@@ -659,17 +672,13 @@ implementation
     if set_gfx_mode(mode, w, h, 0, 0) = 0 then
     begin
       new(window);
-{$IF NOT DEFINED(AL_TEXT)}
       set_window_title(title);
       if sdl2allegro_screen <> nil then
         destroy_bitmap(sdl2allegro_screen);
-      //sdl2allegro_screen := create_video_bitmap(w, h);
-      //if sdl2allegro_screen = nil then
-        sdl2allegro_screen := create_system_bitmap(w, h);
+      sdl2allegro_screen := create_system_bitmap(w, h);
       if sdl2allegro_screen = nil then
         sdl2allegro_screen := create_bitmap(w, h);
       ASSERT(sdl2allegro_screen <> nil);
-{$ENDIF}
       window.w := w;
       window.h := h;
       window.mode := mode;
@@ -684,10 +693,7 @@ implementation
     result := -1;
     if window = nil then exit;
 
-{$IF DEFINED(AL_TEXT)}
-    mode := GFX_TEXT;
-    window.w := 0; window.h := 0;
-{$ELSEIF DEFINED(GO32V2)}
+{$IF DEFINED(GO32V2)}
     mode := GFX_AUTODETECT;
 {$ELSE}
     if (flags and (SDL_WINDOW_FULLSCREEN or SDL_WINDOW_FULLSCREEN_DESKTOP)) <> 0 then
@@ -699,19 +705,13 @@ implementation
     set_color_depth(DEFAULT_DEPTH);
     if set_gfx_mode(mode, window.w, window.h, 0, 0) = 0 then
     begin
-{$IF NOT DEFINED(AL_TEXT)}
-(* 
      if sdl2allegro_screen <> nil then
         destroy_bitmap(sdl2allegro_screen);
-*)
-      //sdl2allegro_screen := create_video_bitmap(window.w, window.h);
-      //if sdl2allegro_screen = nil then
-        sdl2allegro_screen := create_system_bitmap(window.w, window.h);
+      sdl2allegro_screen := create_system_bitmap(window.w, window.h);
       if sdl2allegro_screen = nil then
         sdl2allegro_screen := create_bitmap(window.w, window.h);
       ASSERT(sdl2allegro_screen <> nil);
       set_palette(desktop_palette);
-{$ENDIF}
       window.mode := mode;
       result := 0
     end
@@ -721,24 +721,16 @@ implementation
   begin
     e_LogWritefln('SDL_SetWindowSize %s %s', [w, h]);
     if window = nil then exit;
-{$IF DEFINED(AL_TEXT)}
-  window.mode := GFX_TEXT;
-  w := 0; h := 0;
-{$ENDIF}
     set_color_depth(DEFAULT_DEPTH);
     if set_gfx_mode(window.mode, w, h, 0, 0) = 0 then
     begin
-{$IF NOT DEFINED(AL_TEXT)}
       if sdl2allegro_screen <> nil then
         destroy_bitmap(sdl2allegro_screen);
-      //sdl2allegro_screen := create_video_bitmap(w, h);
-      //if sdl2allegro_screen = nil then
-        sdl2allegro_screen := create_system_bitmap(w, h);
+      sdl2allegro_screen := create_system_bitmap(w, h);
       if sdl2allegro_screen = nil then
         sdl2allegro_screen := create_bitmap(w, h);
       ASSERT(sdl2allegro_screen <> nil);
       set_palette(desktop_palette);
-{$ENDIF}
       window.w := w;
       window.h := h;
     end
@@ -789,27 +781,17 @@ implementation
 
 (********** OPENGL MANAGEMENT **********)
 
-  function SDL_GL_MakeCurrent(window: PSDL_Window; context: TSDL_GLContext): SInt32;
-  begin
-    e_LogWriteln('SDL_GL_MakeCurrent');
-    result := 0
-  end;
-
-  procedure SDL_GL_DeleteContext(context: TSDL_GLContext);
+  function SDL_GL_SetAttribute(attr: TSDL_GLattr; value: SInt32): SInt32;
   begin
-    e_LogWriteln('SDL_GL_DeleteContext');
+    e_LogWritefln('SDL_GL_SetAttribute %s %s', [attr, value]);
+    allegro_error := 'Attribute ' + IntToStr(attr) + ' := ' + IntToStr(value) + 'not supported';
+    result := -1
   end;
 
-  procedure SDL_GL_SwapWindow(window: PSDL_Window);
-    var res: cint;
+  function SDL_GL_GetAttribute(attr: TSDL_GLattr; value: PInt): SInt32;
   begin
-    //e_LogWriteln('SDL_GL_SwapWindow');
-{$IF NOT DEFINED(AL_TEXT)}
-    ASSERT(sdl2allegro_screen <> nil);
-    blit(sdl2allegro_screen, screen, 0, 0, 0, 0, sdl2allegro_screen.w, sdl2allegro_screen.h);
-    res := show_video_bitmap(screen);
-    //ASSERT(res = 0);
-{$ENDIF}
+    e_LogWritefln('SDL_GL_GetAttribute %s', [attr]);
+    value^ := 0; result := -1
   end;
 
   function SDL_GL_CreateContext(window: PSDL_Window): TSDL_GLContext;
@@ -818,30 +800,41 @@ implementation
     result := window
   end;
 
-  function SDL_GL_SetSwapInterval(interval: SInt32): SInt32;
+  function SDL_GL_MakeCurrent(window: PSDL_Window; context: TSDL_GLContext): SInt32;
   begin
-    e_LogWritefln('SDL_GL_SetSwapInterval %s', [interval]);
+    e_LogWriteln('SDL_GL_MakeCurrent');
     result := 0
   end;
 
-  function SDL_GL_SetAttribute(attr: TSDL_GLattr; value: SInt32): SInt32;
+  procedure SDL_GL_DeleteContext(context: TSDL_GLContext);
   begin
-    e_LogWritefln('SDL_GL_SetAttribute %s %s', [attr, value]);
-    result := -1;
+    e_LogWriteln('SDL_GL_DeleteContext');
   end;
 
-  function SDL_GL_GetAttribute(attr: TSDL_GLattr; value: PInt): SInt32;
+  function SDL_GL_SetSwapInterval(interval: SInt32): SInt32;
   begin
-    e_LogWritefln('SDL_GL_GetAttribute %s', [attr]);
-    value^ := 0; result := -1
+    e_LogWritefln('SDL_GL_SetSwapInterval %s', [interval]);
+    result := 0
+  end;
+
+  procedure SDL_GL_SwapWindow(window: PSDL_Window);
+  begin
+    ASSERT(sdl2allegro_screen <> nil);
+    acquire_screen;
+    blit(sdl2allegro_screen, screen, 0, 0, 0, 0, sdl2allegro_screen.w, sdl2allegro_screen.h);
+    show_video_bitmap(screen);
+    release_screen;
   end;
 
 (********** EVENTS **********)
 
   function SDL_PushEvent(event: PSDL_Event): SInt32;
   begin
-    e_LogWriteln('SDL_PushEvent');
-    result := 0
+    result := 1;
+    case event.type_ of
+      SDL_QUITEV: quit := True;
+      else ASSERT(FALSE); result := 0
+    end
   end;
 
   function allegro_to_sdl_scancode (x: Integer): Integer;
@@ -926,30 +919,65 @@ implementation
   end;
 
   function SDL_PollEvent(event: PSDL_Event): SInt32;
-    var alscan: Integer;
+    var alscan: Integer; pressed, shift, caps: Boolean;
   begin
-    //e_LogWriteln('SDL_PollEvent');
-    poll_keyboard;
     result := 0;
+
+    if quit then
+    begin
+      quit := False;
+      event.type_ := SDL_QUITEV;
+      event.quit.timestamp := ticks;
+      result := 1;
+      Exit
+    end;
+
+    if inputText and (inputChar <> #0) then
+    begin
+      event.type_ := SDL_TEXTINPUT;
+      event.text.timestamp := ticks;
+      event.text.windowID := 0;
+      event.text.text[0] := inputChar;
+      event.text.text[1] := #0;
+      inputChar := #0;
+      result := 1;
+      Exit
+    end;
+
+    poll_keyboard;
     if not IsEmptyKeyboard then
     begin
       alscan := NextScancode;
-      if alscan and $80 = 0 then
+      pressed := alscan and $80 = 0;
+      if pressed then
+      begin
+        shift := key_shifts and KB_SHIFT_FLAG <> 0;
+        caps := key_shifts and KB_CAPSLOCK_FLAG <> 0;
+        if shift xor caps then
+          inputChar := s2uc[alscan and $7F]
+        else
+          inputChar := s2lc[alscan and $7F];
         event.type_ := SDL_KEYDOWN
+      end
       else
-        event.type_ := SDL_KEYUP;
-      event.key.timestamp := 0;
+      begin
+        inputChar := #0;
+        event.type_ := SDL_KEYUP
+      end;
+      event.key.timestamp := ticks;
       event.key.windowID := 0;
-      (* df not use it?
-      if alscan and $80 = 0 then
+      (**** df not use it?
+      if pressed then
         event.key.state := SDL_PRESSED
       else
         event.key.state := SDL_RELEASED;
-      *)
+      ****)
+      event.key._repeat := 0;
       event.key.keysym.scancode := allegro_to_sdl_scancode(alscan);
       event.key.keysym.sym := 0; (* df not use it? *)
       event.key.keysym._mod := 0; (* df not use it? *)
-      result := 1
+      result := 1;
+      Exit
     end
   end;
 
@@ -958,33 +986,53 @@ implementation
   function SDL_ShowCursor(toggle: SInt32): SInt32;
   begin
     e_LogWritefln('SDL_ShowCursor %s', [toggle]);
+    (* TODO *)
     result := 0
   end;
 
 (********** SDL **********)
 
+  procedure AllegroQuitCallback; cdecl;
+  begin
+    quit := True
+  end;
+  procedure AllegroQuitCallbackEND; begin end;
+
   function SDL_SetHint( const name: PChar; const value: PChar) : boolean;
   begin
     e_LogWritefln('SDL_SetHint %s %s', [name, value]);
+    (* TODO *)
     result := false
   end;
 
   function SDL_GetError: PAnsiChar;
   begin
-    e_LogWritefln('SDL_GetError => %s', [allegro_error]);
     result := allegro_error;
   end;
 
-  function SDL_Init(flags: UInt32): SInt32;
+{$IFDEF GO32V2}
+  (* HACK: allegro crashes while init without this *)
+  var
+    __crt0_argv: PPchar; cvar; external;
+    myargv: array [0..255] of Pchar;
+
+  procedure FIX_ARGV;
   begin
-    e_LogWritefln('SDL_Init %u', [flags]);
-    result := -1;
     __crt0_argv := @myargv[0];
     myargv[0] := PChar(ParamStr(0));
     e_LogWritefln('argv[0] = %s', [myargv[0]]);
+  end;
+{$ENDIF}
+
+  function SDL_Init(flags: UInt32): SInt32;
+  begin
+    result := -1;
+    {$IFDEF GO32V2}
+      FIX_ARGV;
+    {$ENDIF}
     if allegro_init = 0 then
     begin
-      e_LogWriteln('SDL_Init inited! ' + ParamStr(0) + ' tickssize=' + IntToStr(sizeof(keyring)) );
+      e_LogWriteln('Allegro Init: ok');
       {$IFDEF GO32V2}
         (* without this df dies with fire when swapped *)
         lock_data(ticks, sizeof(ticks));
@@ -993,19 +1041,22 @@ implementation
         lock_data(keyend, sizeof(keyend));
         lock_data(keyring, sizeof(keyring));
         lock_code(@KeyboardWatcher, PtrUInt(@KeyboardWatcherEND) - PtrUInt(@KeyboardWatcher));
+        lock_data(quit, sizeof(quit));
+        lock_code(@AllegroQuitCallback, PtrUInt(@AllegroQuitCallbackEND) - PtrUInt(@AllegroQuitCallback));
       {$ENDIF}
       install_timer;
       install_keyboard;
       keyboard_lowlevel_callback := KeyboardWatcher;
       set_keyboard_rate(0, 0);
       install_int_ex(AllegroTimerCallback, MSEC_TO_TIMER(1));
+      set_close_button_callback(AllegroQuitCallback);
       result := 0
     end
   end;
 
   procedure SDL_Quit;
   begin
-    e_LogWriteln('SDL_Quit');
+    set_close_button_callback(nil);
     remove_keyboard;
     remove_timer;
     {$IFDEF GO32V2}
@@ -1015,7 +1066,8 @@ implementation
       unlock_data(keyend, sizeof(keyend));
       unlock_data(keyring, sizeof(keyring));
       unlock_code(@KeyboardWatcher, PtrUInt(@KeyboardWatcherEND) - PtrUInt(@KeyboardWatcher));
+      unlock_data(quit, sizeof(quit));
+      unlock_code(@AllegroQuitCallback, PtrUInt(@AllegroQuitCallbackEND) - PtrUInt(@AllegroQuitCallback));
     {$ENDIF}
     allegro_exit
   end;
-