So i'm trying to follow the Handmade Hero series in Odin but i don't exactly understand how callback functions work with odin or how i would create one. Also feel free to point out any other issues you see in the code, im trying to learn.
So the function would be Win32MainWindowCallback and im trying to call it when initializing the WindowClass
#Creating a windows callback function in odin?
1 messages · Page 1 of 1 (latest)
When you try to compile, you should get an error, already giving you a hint: Win32MainWindowCallback needs a different calling convention, Win32MainWindowCallback :: proc "stdcall" (Window: windows.HWND, ... because it is interfacing with windows API.
And you are usually not calling this callback yourself, but windows is calling it for you, e.g. when you call PeekMessageW.
see https://odin-lang.org/docs/overview/#calling-conventions and https://odin-lang.org/docs/overview/#explicit-context-definition
Are there any projects interfacing with the windows api or examples i could look at? I think i fixed the callback function now but still get no window 😅
So windows itself uses the callback, you pass it to windows via the WNDCLASSA struct:
@(export)
win32_main_window_callback :: proc(window: HWND, message: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT { //...}
// inside main:
window_class: win32.WNDCLASSA = {}
window_class.lpfnWndProc = win32.WNDPROC(win32_main_window_callback) // <-- pass callback to windows here
if RegisterClassA(&window_class) != 0 { // <-- Attach the window_class
window := CreateWindowExA(
///...
)
//...}
feel free to ping me anytime on hmh questions, i ran up to day 270. @edgy shell maybe has surpassed me now too, im sure he'd chip in on questions.
I'm working on building fences for sheep right now, and so only sporadically on discord, or even programming sadly, but if you @ me i can get back to you right away usually
here's a simple start
And when you don’t specify a calling convention, it uses Odin’s as the default, which
is the same as cdecl but passes an implicit context pointer on each call. (Note: This is subject to change)
https://odin-lang.org/docs/overview/#procedure-type
At a glance I don’t know why your window would not be showing up, but I know if you compared what your code is doing versus Casey’s, you may find some disparity that is causing it
Then you can read up on the thing you missed, if any
If nobody’s solution has helped, I could show a sanitized version of what my HMH project is doing
Oh yea, and try stepping line-by-line in the debugger to find where it fails. Some windows functions will let you call GetLastError https://learn.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-getlasterror after it fails to get an error code you can look up
Thanks everyone for helping out, something strange im finding out when debugging right now is that the code from @royal meteor
if RegisterClassExW(&WindowClass) != 0
Works as expected however in my application it does not, it only works when i preemptively assign it to a variable for some reason
test := RegisterClassExW(&WindowClass)
if test != 0
Not quite sure why that is the case but i get a window when i change that for now
@shell shoal what do you mean it does not
are you using a debugger
what happens at that line
I took a look at your code and you have ((^bool)(Buffer.Memory)^)
you are dereferencing Buffer.Memory at 0 and it will crash
sry i didnt update that code, im using Buffer.memory != nil instead atm
ah aight