Hiya I don't have the source (and am a complete beginner at c) so it's a little hard to figure out, but I am trying to understand how the game_memory is used. I've followed episode 14 and built the GameMemory, but in that same episode there is no real usage of that thing ;)
I am wondering how and when the game_state is initialized, also wondering how structs are put in there, I have seen a function in *some* stream called push_struct I think that's involved somehow. I think I understand how the data is retrieved from the game_memory it's by casting the whole game_memory to a game_state and then using the variables in there... right?
so it's a few broad questions, I would also be very happy if one can point me to another episode that goes in these details.
I have this code that's not yest using a game_state but yeah.
typedef struct foo
{
uint8 age;
char* name;
} foo;
void push_foo_struct(foo* s, game_memory* mem){
*(foo*) (mem->PermanentStorage + mem->PermanentOffset) = *s;
mem->PermanentOffset += sizeof(*s);
}
int main (){
game_memory GameMemory = {};
void *BaseAddress = (void *) Gigabytes(1);
GameMemory.PermanentOffset = 0;
GameMemory.PermanentStorageSize = Megabytes(64);
GameMemory.TransientStorageSize = Gigabytes(1);
uint64 TotalStorageSize = GameMemory.PermanentStorageSize + GameMemory.TransientStorageSize;
GameMemory.PermanentStorage = mmap(BaseAddress, TotalStorageSize,
PROT_READ | PROT_WRITE,
MAP_ANON | MAP_PRIVATE,
-1, 0);
GameMemory.TransientStorage = (uint8*)(GameMemory.PermanentStorage) + GameMemory.PermanentStorageSize;
foo thing1;
thing1.age = 23;
thing1.name = "John";
foo thing2;
thing2.age = 30;
thing2.name = "Alfred";
foo thing3;
thing3.age = 232;
thing3.name = "JobbieWock the Ent";
push_foo_struct(&thing1, &GameMemory);
push_foo_struct(&thing2, &GameMemory);
push_foo_struct(&thing3, &GameMemory);
foo* stored_first = ((foo*)(GameMemory.PermanentStorage+sizeof(foo)*0));
foo* stored_second = ((foo*)(GameMemory.PermanentStorage+sizeof(foo)*1));
foo* stored_third = ((foo*)(GameMemory.PermanentStorage+sizeof(foo)*2));
printf("age of %s: %d\n", stored_first->name, stored_first->age);
printf("age of %s: %d\n", stored_second->name, stored_second->age);
printf("age of %s: %d\n", stored_third->name, stored_third->age);
}
edit: before I forget, what I am also wondering about is assuming there are many structs in my game_state, how the initting these structs look like, do I init them inside the game_state and somehow move it in the game_memory ?
or are thee only pointers in the game_state?
edit2: I found the episode where PushStruct is introduced (ep 34 around the 45 min mark), so I think I can manage from here.