#define and typdef header question

Sorry if this question has been asked a bunch of times. I tried skimming through the forum and googling, but I couldn't find where it may have been asked. It's a question that has to do with the #define and typedef Casey uses for the platform function headers. More specifically:

#define GAME_UPDATE_AND_RENDER(name) void name(thread_context *Thread, game_memory *Memory, game_input *Input, game_offscreen_buffer *Buffer)
typedef GAME_UPDATE_AND_RENDER(game_update_and_render);

I believe I understand the #define section of it.
It's basically so "GAME_UPDATE_AND_RENDER(game_update_and_render)" is changed by the compiler to "void game_update_and_render(thread_context *Thread, game_memory *Memory, game_input *Input, game_offscreen_buffer *Buffer)"

However, I don't entirely understand the purpose of the typedef. I am familiar with something like "typedef int8_t int8" but I'm not sure how "typedef GAME_UPDATE_AND_RENDER(game_update_and_render)" works. Would someone mind explaining what it changes? Thank you!
It is exactly same thing. It just get's replaced by preprocessor to:
1
typedef void game_update_and_render(thread_context *Thread, game_memory *Memory, game_input *Input, game_offscreen_buffer *Buffer);

And that is regular typedef you are familiar with.

#define's don't care about semantics. Is it variable definition? Typedef? Function declaration? Doesn't matter. Just reaplace one text with another text and then let real compiler to deal with semantics.

Edited by Mārtiņš Možeiko on
I might've oversold how much I understand typedef. I believe I get how it works with allowing one to use different names for types and structures but I'm not entirely sure how it works with a function prototype. In my current understanding, it looks like typedef is trying to make it so "game_update_and_render(thread_context *Thread, game_memory *Memory, game_input *Input, game_offscreen_buffer *Buffer)" will be a another way to use "void" and I can't imagine that's case. Thanks for the reply. I just am still not understanding what the typedef is doing.
Oh, so question is about typedef.
This typedef defines new type which is function type.
"typedef int X;" defines new type X which is int.
"typedef void A(int, float)" defines function type A which accepts two arguments - int and float.

You can use such type A to create function pointers and call function indirectly.
1
2
3
4
5
6
7
8
9
void RealFunction1(int x, float y) { ... }
void RealFunction2(int x, float y) { ... }

...
A* FunctionPointer = RealFunction1;
FunctionPointer(1, 2.0f); // call RealFunction1

FunctionPointer = RealFunction2;
FunctionPointer(1111, 2222.0f); // call RealFunction2


Function pointer is similar to regular pointer. But instead of pointing to data, it points to code - to a function. And you can change dynamically at runtime to which function it points. So you can make decision at runtime which function to call.

That is exactly what is happening in HandmadeHero. At runtime DLL is loaded and address of function is retrieved by GetProcAddress. Then it is assigned to function pointer which get called when needed. And once dll is reloaded, different address is assigned to function pointer.

Edited by Mārtiņš Možeiko on
Thanks, mmozeiko! That helps so much. I honestly appreciate it a lot :)
Another way to define the function pointer type is:

[Code]
typedef void (*A)(int, int);
[/Code]

Then you can:

[Code]
A fptr = RealFunction;
[/Code]

Edited by vexe on