Skip to content

Commit

Permalink
Refactor GetKeyboardState function
Browse files Browse the repository at this point in the history
  • Loading branch information
flibitijibibo committed Jan 1, 2024
1 parent c9c8a88 commit 1c0f0ad
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 30 deletions.
12 changes: 3 additions & 9 deletions src/FNAPlatform/FNAPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ static FNAPlatform()
PollEvents = SDL2_FNAPlatform.PollEvents;
GetGraphicsAdapters = SDL2_FNAPlatform.GetGraphicsAdapters;
GetCurrentDisplayMode = SDL2_FNAPlatform.GetCurrentDisplayMode;
GetKeyboardScancodeArray = SDL2_FNAPlatform.GetKeyboardScancodeArray;
ScancodeToKey = SDL2_FNAPlatform.ScancodeToKey;
GetKeyboardState = SDL2_FNAPlatform.GetKeyboardState;
GetKeyFromScancode = SDL2_FNAPlatform.GetKeyFromScancode;
IsTextInputActive = SDL2_FNAPlatform.IsTextInputActive;
StartTextInput = SDL2.SDL.SDL_StartTextInput;
Expand Down Expand Up @@ -263,13 +262,8 @@ ref bool textInputSuppress
public delegate DisplayMode GetCurrentDisplayModeFunc(int adapterIndex);
public static readonly GetCurrentDisplayModeFunc GetCurrentDisplayMode;

// Should return a pointer to an array that does not need to be freed by the client.
public delegate IntPtr GetKeyboardScancodeArrayFunc(out int numkeys);
public static readonly GetKeyboardScancodeArrayFunc GetKeyboardScancodeArray;

// Maps scancode array position to Key. To be used in conjunction with GetKeyboardScancodeArray.
public delegate Keys ScancodeToKeyFunc(int scancode);
public static readonly ScancodeToKeyFunc ScancodeToKey;
public delegate void GetKeyboardStateFunc(List<Keys> activeKeys);
public static readonly GetKeyboardStateFunc GetKeyboardState;

public delegate Keys GetKeyFromScancodeFunc(Keys scancode);
public static readonly GetKeyFromScancodeFunc GetKeyFromScancode;
Expand Down
22 changes: 12 additions & 10 deletions src/FNAPlatform/SDL2_FNAPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2858,19 +2858,21 @@ public static Keys GetKeyFromScancode(Keys scancode)
return Keys.None;
}

public static IntPtr GetKeyboardScancodeArray(out int numkeys)
public static unsafe void GetKeyboardState(List<Keys> activeKeys)
{
return SDL.SDL_GetKeyboardState(out numkeys);
}

public static Keys ScancodeToKey(int scancode)
{
if (INTERNAL_scanMap.TryGetValue(scancode, out Keys key))
int numkeys;
byte* state = (byte*) SDL.SDL_GetKeyboardState(out numkeys);
for (int i = 0; i < numkeys; i += 1)
{
return key;
if (state[i] != 0)
{
Keys key;
if (INTERNAL_scanMap.TryGetValue(i, out key))
{
activeKeys.Add(key);
}
}
}

return Keys.None;
}

#endregion
Expand Down
12 changes: 1 addition & 11 deletions src/Input/Keyboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,8 @@ public static class Keyboard
/// <returns>Current keyboard state.</returns>
public unsafe static KeyboardState GetState()
{
// Returns a pointer that does not need to be freed.
byte* state = (byte*) FNAPlatform.GetKeyboardScancodeArray(out int numkeys);

activeKeys.Clear();
for (int i = 0; i < numkeys; i += 1)
{
if (state[i] != 0)
{
activeKeys.Add(FNAPlatform.ScancodeToKey(i));
}
}

FNAPlatform.GetKeyboardState(activeKeys);
return new KeyboardState(activeKeys);
}

Expand Down

0 comments on commit 1c0f0ad

Please sign in to comment.