Using memory arena in the game that supports hot reload
This commit is contained in:
+50
-39
@@ -3,6 +3,8 @@
|
||||
#undef min
|
||||
#undef max
|
||||
|
||||
#include <game.h>
|
||||
|
||||
#include <Core/HAL/Filesystem/Filesystem.h>
|
||||
#include <Core/JulietInit.h>
|
||||
#include <Core/Logging/LogManager.h>
|
||||
@@ -10,6 +12,12 @@
|
||||
#include <Entity/Entity.h>
|
||||
#include <Entity/EntityManager.h>
|
||||
|
||||
GameState* gGameState = nullptr;
|
||||
GameState* GetGameState()
|
||||
{
|
||||
return gGameState;
|
||||
}
|
||||
|
||||
// Test code
|
||||
namespace Game
|
||||
{
|
||||
@@ -26,56 +34,59 @@ namespace Game
|
||||
int Health;
|
||||
};
|
||||
DEFINE_ENTITY(Rock);
|
||||
|
||||
} // namespace Game
|
||||
|
||||
using namespace Juliet;
|
||||
|
||||
extern "C" JULIET_API void GameInit(GameInitParams* params)
|
||||
{
|
||||
// Example allocation in GameArena
|
||||
struct GameState
|
||||
{
|
||||
float TotalTime;
|
||||
int Score;
|
||||
};
|
||||
|
||||
auto* gameState = ArenaPushStruct<GameState>(params->GameArena);
|
||||
gameState->TotalTime = 0.0f;
|
||||
gameState->Score = 0;
|
||||
|
||||
printf("Game Arena Allocated: %p\n", static_cast<void*>(gameState));
|
||||
|
||||
using namespace Game;
|
||||
|
||||
// Entity Use case
|
||||
InitEntityManager(params->GameArena);
|
||||
auto& manager = GetEntityManager();
|
||||
Door* door = MakeEntity<Door>(manager, 10.0f, 2.0f);
|
||||
door->IsOpened = true;
|
||||
|
||||
Entity* ent = door->Base;
|
||||
[[maybe_unused]] Door* stillDoor = DownCast<Door>(ent);
|
||||
Assert(door == stillDoor);
|
||||
|
||||
Rock* rock = MakeEntity<Rock>(manager, 1.f, 2.f);
|
||||
rock->Health = 100;
|
||||
Assert(door->Base != rock->Base);
|
||||
|
||||
printf("Door is %s\n", door->IsOpened ? "Opened" : "Closed");
|
||||
printf("Rock has %d health points\n", rock->Health);
|
||||
}
|
||||
|
||||
extern "C" JULIET_API void __cdecl GameShutdown()
|
||||
{
|
||||
printf("Shutting down game...\n");
|
||||
|
||||
using namespace Juliet;
|
||||
using namespace Game;
|
||||
|
||||
ShutdownEntityManager();
|
||||
}
|
||||
|
||||
extern "C" JULIET_API void __cdecl GameUpdate([[maybe_unused]] float deltaTime)
|
||||
extern "C" JULIET_API void __cdecl GameUpdate(Juliet::GameData* params, [[maybe_unused]] float deltaTime)
|
||||
{
|
||||
using namespace Juliet;
|
||||
using namespace Game;
|
||||
|
||||
gGameState = params->GameState;
|
||||
if (!gGameState)
|
||||
{
|
||||
Arena* gameStateArena = ArenaAllocate({ .ReserveSize = Megabytes(1) } JULIET_DEBUG_PARAM("Game Total Arena"));
|
||||
auto* gameState = ArenaPushStruct<GameState>(gameStateArena);
|
||||
gGameState = params->GameState = gameState;
|
||||
gameState->TotalArena = gameStateArena;
|
||||
|
||||
gameState->TotalTime = 0.0f;
|
||||
gameState->Score = 0;
|
||||
|
||||
printf("Game Arena Allocated: %p\n", static_cast<void*>(gameState));
|
||||
|
||||
// Bootstrap world
|
||||
auto* worldArena = ArenaAllocate({ .ReserveSize = Megabytes(1) } JULIET_DEBUG_PARAM("World Arena"));
|
||||
World* world = ArenaPushStruct<World>(worldArena JULIET_DEBUG_PARAM("World"));
|
||||
gameState->World = world;
|
||||
gameState->World->WorldArena = worldArena;
|
||||
|
||||
// Entity Use case
|
||||
InitEntityManager(gameState->World);
|
||||
auto& manager = GetEntityManager();
|
||||
Door* door = MakeEntity<Door>(manager, 10.0f, 2.0f);
|
||||
door->IsOpened = true;
|
||||
|
||||
Entity* ent = door->Base;
|
||||
[[maybe_unused]] Door* stillDoor = DownCast<Door>(ent);
|
||||
Assert(door == stillDoor);
|
||||
|
||||
Rock* rock = MakeEntity<Rock>(manager, 1.f, 2.f);
|
||||
rock->Health = 100;
|
||||
Assert(door->Base != rock->Base);
|
||||
|
||||
printf("Door is %s\n", door->IsOpened ? "Opened" : "Closed");
|
||||
printf("Rock has %d health points\n", rock->Health);
|
||||
}
|
||||
|
||||
// printf("Updating game...\n");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user