Skip to content

Instantly share code, notes, and snippets.

@karl-zylinski
Last active September 3, 2024 22:25
Show Gist options
  • Save karl-zylinski/f8761856593776014c9de3368437e790 to your computer and use it in GitHub Desktop.
Save karl-zylinski/f8761856593776014c9de3368437e790 to your computer and use it in GitHub Desktop.
Open a window using Windows API in Odin
package open_win32_window
import win "core:sys/windows"
main :: proc() {
instance := win.HINSTANCE(win.GetModuleHandleW(nil))
assert(instance != nil, "Failed to fetch current instance")
class_name := win.L("Windows Window")
cls := win.WNDCLASSW {
lpfnWndProc = win_proc,
lpszClassName = class_name,
hInstance = instance,
}
class := win.RegisterClassW(&cls)
assert(class != 0, "Class creation failed")
hwnd := win.CreateWindowW(class_name,
win.L("Windows Window"),
win.WS_OVERLAPPEDWINDOW | win.WS_VISIBLE,
100, 100, 1280, 720,
nil, nil, instance, nil)
assert(hwnd != nil, "Window creation Failed")
msg: win.MSG
for win.GetMessageW(&msg, nil, 0, 0) > 0 {
win.TranslateMessage(&msg)
win.DispatchMessageW(&msg)
}
}
win_proc :: proc "stdcall" (hwnd: win.HWND, msg: win.UINT, wparam: win.WPARAM, lparam: win.LPARAM) -> win.LRESULT {
switch(msg) {
case win.WM_DESTROY:
win.PostQuitMessage(0)
}
return win.DefWindowProcW(hwnd, msg, wparam, lparam)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment