Fixed windows race-condition when peeking messages. Added Mouse events.
This commit is contained in:
26
window.odin
26
window.odin
@@ -10,15 +10,15 @@ Window :: struct {
|
|||||||
|
|
||||||
Event :: union {
|
Event :: union {
|
||||||
Event_Quit,
|
Event_Quit,
|
||||||
Event_Key,
|
Event_Input,
|
||||||
Event_Resize,
|
Event_Resize,
|
||||||
}
|
}
|
||||||
|
|
||||||
Event_Quit :: struct {}
|
Event_Quit :: struct {}
|
||||||
|
|
||||||
Event_Key_State :: enum {
|
Event_Input_State :: enum {
|
||||||
Down,
|
|
||||||
Up,
|
Up,
|
||||||
|
Down,
|
||||||
}
|
}
|
||||||
Virtual_Key :: enum {
|
Virtual_Key :: enum {
|
||||||
Unknown,
|
Unknown,
|
||||||
@@ -33,10 +33,26 @@ Virtual_Key :: enum {
|
|||||||
Shift, Control, Alt,
|
Shift, Control, Alt,
|
||||||
Arrow_Up, Arrow_Down, Arrow_Left, Arrow_Right,
|
Arrow_Up, Arrow_Down, Arrow_Left, Arrow_Right,
|
||||||
}
|
}
|
||||||
Event_Key :: struct {
|
Event_Keyboard :: struct {
|
||||||
virtual_key: Virtual_Key,
|
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 {
|
Event_Resize :: struct {
|
||||||
old_width, old_height: u16,
|
old_width, old_height: u16,
|
||||||
new_width, new_height: u16,
|
new_width, new_height: u16,
|
||||||
|
|||||||
@@ -107,6 +107,8 @@ update_windows :: proc(window: ^Window) -> Event {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
win.CoInitializeEx(nil)
|
||||||
|
|
||||||
ok := win.PeekMessageW(
|
ok := win.PeekMessageW(
|
||||||
lpMsg = &msg,
|
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.
|
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)
|
// fmt.printfln("Message type: %x", msg.message)
|
||||||
|
|
||||||
switch msg.message {
|
switch msg.message {
|
||||||
|
// Keyboard.
|
||||||
case win.WM_KEYDOWN, win.WM_SYSKEYDOWN: {
|
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: {
|
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: {
|
case win.WM_QUIT: {
|
||||||
return Event_Quit {}
|
return Event_Quit {}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user