Lumenarium/src/gs_input.h

111 lines
2.4 KiB
C
Raw Normal View History

2019-10-30 14:28:02 +00:00
enum key_state_flags
{
KeyState_WasDown = 1 << 0,
KeyState_IsDown = 1 << 1,
};
2019-11-11 20:02:24 +00:00
#define KeyWasDown(event) ((event & KeyState_WasDown) > 0)
#define KeyIsDown(event) ((event & KeyState_IsDown) > 0)
2019-10-30 14:28:02 +00:00
struct input_entry
{
key_code Key;
b32 State;
b32 Modifiers;
};
struct input_queue
{
s32 QueueSize;
s32 QueueUsed;
input_entry* Entries;
};
struct mouse_state
{
s32 Scroll;
v2 Pos;
v2 OldPos;
v2 DeltaPos;
v2 DownPos;
2019-11-01 14:38:44 +00:00
2019-11-11 20:02:24 +00:00
b32 LeftButtonState;
b32 MiddleButtonState;
b32 RightButtonState;
2019-10-30 14:28:02 +00:00
};
internal input_queue
InitializeInputQueue (u8* Memory, s32 MemorySize)
{
input_queue Result = {};
s32 EntriesCount = MemorySize / sizeof(input_entry);
Result.QueueSize = EntriesCount;
Result.QueueUsed = 0;
Result.Entries = (input_entry*)Memory;
return Result;
}
internal void
ResetInputQueue (input_queue* Queue)
{
Queue->QueueUsed = 0;
}
internal void
AddInputEventEntry (input_queue* Queue, key_code Key,
b32 WasDown, b32 IsDown, b32 ShiftDown, b32 AltDown, b32 CtrlDown, b32 SysDown)
{
Assert(Queue->QueueUsed < Queue->QueueSize);
input_entry* Entry = Queue->Entries + Queue->QueueUsed++;
Entry->Key = Key;
Entry->State = (key_state_flags)0;
if (WasDown) { Entry->State |= KeyState_WasDown; }
if (IsDown) { Entry->State |= KeyState_IsDown; }
Entry->Modifiers = 0;
if (ShiftDown) { Entry->Modifiers |= Modifier_Shift; }
if (CtrlDown) { Entry->Modifiers |= Modifier_Ctrl; }
if (AltDown) { Entry->Modifiers |= Modifier_Alt; }
if (SysDown) { Entry->Modifiers |= Modifier_Sys; }
}
internal b32
KeyTransitionedDown (input_entry Entry)
{
2019-11-11 20:02:24 +00:00
return (!KeyWasDown(Entry.State) && KeyIsDown(Entry.State));
2019-10-30 14:28:02 +00:00
}
internal b32
KeyTransitionedUp (input_entry Entry)
{
2019-11-11 20:02:24 +00:00
return (KeyWasDown(Entry.State) && !KeyIsDown(Entry.State));
2019-10-30 14:28:02 +00:00
}
internal b32
KeyHeldDown (input_entry Entry)
{
2019-11-11 20:02:24 +00:00
return (KeyWasDown(Entry.State) && KeyIsDown(Entry.State));
}
internal b32
MouseButtonTransitionedDown (b32 MouseButtonState)
{
return (!KeyWasDown(MouseButtonState) && KeyIsDown(MouseButtonState));
}
internal b32
MouseButtonTransitionedUp (b32 MouseButtonState)
{
return (KeyWasDown(MouseButtonState) && !KeyIsDown(MouseButtonState));
}
internal b32
MouseButtonHeldDown (b32 MouseButtonState)
{
b32 WasDown = KeyWasDown(MouseButtonState);
b32 IsDown = KeyIsDown(MouseButtonState);
return (WasDown && IsDown);
2019-10-30 14:28:02 +00:00
}