diff --git a/window.odin b/window.odin index e15009e..a2bad34 100644 --- a/window.odin +++ b/window.odin @@ -10,15 +10,15 @@ Window :: struct { Event :: union { Event_Quit, - Event_Key, + Event_Input, Event_Resize, } Event_Quit :: struct {} -Event_Key_State :: enum { - Down, +Event_Input_State :: enum { Up, + Down, } Virtual_Key :: enum { Unknown, @@ -33,10 +33,26 @@ Virtual_Key :: enum { Shift, Control, Alt, Arrow_Up, Arrow_Down, Arrow_Left, Arrow_Right, } -Event_Key :: struct { +Event_Keyboard :: struct { virtual_key: Virtual_Key, - state: Event_Key_State, + state: Event_Input_State, } +Event_Mouse :: union { + Event_Mouse_Move, + Event_Mouse_Button, +} +Event_Mouse_Move :: struct { + x, y: u16, +} +Mouse_Button :: enum { Left, Middle, Right } +Event_Mouse_Button :: struct { button: Mouse_Button, state: Event_Input_State } + +Event_Input :: union { + Event_Keyboard, + Event_Mouse, + // TODO: SS - Add 'Event_Gamepad'? +} + Event_Resize :: struct { old_width, old_height: u16, new_width, new_height: u16, diff --git a/window_windows.odin b/window_windows.odin index 9e9ebac..a922769 100644 --- a/window_windows.odin +++ b/window_windows.odin @@ -107,6 +107,8 @@ update_windows :: proc(window: ^Window) -> Event { } } + win.CoInitializeEx(nil) + ok := win.PeekMessageW( lpMsg = &msg, hWnd = nil, // NOTE: SS - Don't like that this needs to be nil but apparently it does for this window to receive the WM_CLOSE and WM_QUIT messages. @@ -126,12 +128,63 @@ update_windows :: proc(window: ^Window) -> Event { // fmt.printfln("Message type: %x", msg.message) switch msg.message { + // Keyboard. case win.WM_KEYDOWN, win.WM_SYSKEYDOWN: { - return Event_Key { virtual_key = get_virtual_key_windows(i32(msg.wParam)), state = .Down } + input_event: Event_Input = Event_Keyboard { + virtual_key = get_virtual_key_windows(i32(msg.wParam)), + state = .Down + } + + return input_event } case win.WM_KEYUP, win.WM_SYSKEYUP: { - return Event_Key { virtual_key = get_virtual_key_windows(i32(msg.wParam)), state = .Up } + input_event: Event_Input = Event_Keyboard { + virtual_key = get_virtual_key_windows(i32(msg.wParam)), + state = .Up + } + + return input_event } + // Mouse. + case win.WM_MOUSEMOVE: { + x := win.GET_X_LPARAM(msg.lParam) + y := win.GET_Y_LPARAM(msg.lParam) + + assert(x >= 0) + assert(y >= 0) + + mouse_event: Event_Mouse = Event_Mouse_Move{ + x = u16(x), + y = u16(y), + } + + return Event_Input(mouse_event) + } + case win.WM_LBUTTONDOWN, win.WM_LBUTTONUP: { + mouse_event: Event_Mouse = Event_Mouse_Button { + button = .Left, + state = msg.message == win.WM_LBUTTONDOWN ? .Down : .Up + } + + return Event_Input(mouse_event) + } + case win.WM_MBUTTONDOWN, win.WM_MBUTTONUP: { + mouse_event: Event_Mouse = Event_Mouse_Button { + button = .Middle, + state = msg.message == win.WM_MBUTTONDOWN ? .Down : .Up + } + + return Event_Input(mouse_event) + } + case win.WM_RBUTTONDOWN, win.WM_RBUTTONUP: { + mouse_event: Event_Mouse = Event_Mouse_Button { + button = .Right, + state = msg.message == win.WM_RBUTTONDOWN ? .Down : .Up + } + + return Event_Input(mouse_event) + } + case win.WM_QUIT: { return Event_Quit {} }