172 lines
4.5 KiB
C++
172 lines
4.5 KiB
C++
#include <game.h>
|
|
|
|
#include <Controller/DebugCameraController.h>
|
|
#include <Core/Common/serialization.h>
|
|
#include <Core/HAL/Filesystem/Filesystem.h>
|
|
#include <Core/HAL/Keyboard/Keyboard.h>
|
|
#include <Core/JulietInit.h>
|
|
#include <Core/Logging/LogManager.h>
|
|
#include <Core/Memory/MemoryArena.h>
|
|
#include <Data/World.h>
|
|
#include <Entity/Entity.h>
|
|
#include <Entity/EntityManager.h>
|
|
#include <Graphics/Camera.h>
|
|
#include <Graphics/MeshRenderer.h>
|
|
#include <imgui.h>
|
|
|
|
#if JULIET_DEBUG
|
|
#include <Debug/DebugTopBar.h>
|
|
#include <UnitTest/WorldUnitTest.h>
|
|
#endif
|
|
|
|
// namespace
|
|
// {
|
|
// void serialize_test(Archive* ar, void* payload)
|
|
// {
|
|
// SerializedEntityTest* test = reinterpret_cast<SerializedEntityTest*>(payload);
|
|
// serialize_elem(ar, test->A);
|
|
// }
|
|
// } // namespace
|
|
//
|
|
// DEFINE_ENTITY_SERIALIZED(SerializedEntityTest, serialize_test)
|
|
|
|
namespace
|
|
{
|
|
GameState* gGameState = nullptr;
|
|
}
|
|
|
|
GameState* GetGameState()
|
|
{
|
|
return gGameState;
|
|
}
|
|
|
|
extern "C" JULIET_API void __cdecl GameShutdown()
|
|
{
|
|
printf("Shutting down game...\n");
|
|
|
|
if (gGameState && gGameState->World)
|
|
{
|
|
ShutdownWorld(gGameState->World);
|
|
}
|
|
|
|
ShutdownEntityManager();
|
|
}
|
|
|
|
extern "C" JULIET_API void __cdecl GameUpdate(GameData* params, [[maybe_unused]] float deltaTime)
|
|
{
|
|
gGameState = params->GameState;
|
|
if (!gGameState)
|
|
{
|
|
Arena* gameStateArena = ArenaAllocate({ .ReserveSize = Megabytes(1), .Name = "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));
|
|
|
|
// Reserve cameras. Lets go with 4 for now
|
|
ReserveCamera(4);
|
|
|
|
// Bootstrap world
|
|
auto* worldArena = ArenaAllocate({ .ReserveSize = Megabytes(1), .Name = "World Arena" });
|
|
World* world = ArenaPushStruct<World>(worldArena JULIET_DEBUG_PARAM("World"));
|
|
gameState->World = world;
|
|
InitWorld(gameState->World, worldArena);
|
|
|
|
#if JULIET_DEBUG
|
|
UnitTest::WorldUnitTest();
|
|
#endif
|
|
|
|
// Entity Use case
|
|
InitEntityManager(gameState->World);
|
|
auto& manager = GetEntityManager();
|
|
|
|
NonNullPtr mesh = MakeEntity<Inert>(manager, 1.f, 2.f, 0.f);
|
|
|
|
MeshAssetID cubeAsset = GetCubePrimitiveMeshAssetID();
|
|
MeshInstanceID meshInst = CreateMeshInstance(cubeAsset, 0, MatrixIdentity());
|
|
mesh->MeshInstance = meshInst;
|
|
|
|
NonNullPtr mesh2 = MakeEntity<Inert>(manager, 4.f, 0.f, 2.f);
|
|
|
|
MeshInstanceID meshInst2 = CreateMeshInstance(cubeAsset, 0, MatrixIdentity());
|
|
mesh2->MeshInstance = meshInst2;
|
|
|
|
// Summer at 2pm lighting
|
|
Vector3 sunDirection = { -0.2f, -0.9f, -0.3f };
|
|
Vector3 sunColor = { 1.0f, 0.95f, 0.85f };
|
|
float ambientIntensity = 0.4f;
|
|
SetGlobalLight(sunDirection, sunColor, ambientIntensity);
|
|
}
|
|
|
|
#if JULIET_DEBUG
|
|
DrawTopBar();
|
|
#endif
|
|
|
|
GameMode previousFrameMode = gGameState->Mode;
|
|
if (IsKeyPressed(ScanCode::F1))
|
|
{
|
|
if (previousFrameMode == GameMode::Play)
|
|
{
|
|
gGameState->Mode = GameMode::Debug;
|
|
}
|
|
else if (previousFrameMode == GameMode::Debug)
|
|
{
|
|
gGameState->Mode = GameMode::Play;
|
|
}
|
|
}
|
|
|
|
if (gGameState->Mode == GameMode::Editor)
|
|
{
|
|
if (IsKeyPressed(ScanCode::F5))
|
|
{
|
|
gGameState->Mode = GameMode::Play;
|
|
}
|
|
|
|
#if JULIET_EDITOR
|
|
RenderWorldEditorUI(*gGameState->World);
|
|
#endif
|
|
}
|
|
|
|
if (gGameState->Mode == GameMode::Play)
|
|
{
|
|
if (IsKeyPressed(ScanCode::Escape))
|
|
{
|
|
gGameState->Mode = GameMode::Editor;
|
|
}
|
|
|
|
// update game
|
|
}
|
|
|
|
auto& manager = GetEntityManager();
|
|
UpdateEntityManager(manager);
|
|
|
|
if (gGameState->Mode == GameMode::Debug)
|
|
{
|
|
if (previousFrameMode == GameMode::Play)
|
|
{
|
|
ActivateDebugController();
|
|
}
|
|
|
|
if (IsKeyPressed(ScanCode::Escape))
|
|
{
|
|
gGameState->Mode = GameMode::Editor;
|
|
}
|
|
|
|
UpdateDebugController(deltaTime);
|
|
|
|
#if JULIET_DEBUG
|
|
ImGui::Begin("Debug");
|
|
RenderImGuiDebugController(deltaTime);
|
|
ImGui::End();
|
|
#endif
|
|
}
|
|
if (gGameState->Mode != GameMode::Debug && previousFrameMode == GameMode::Debug)
|
|
{
|
|
DeactivateDebugController();
|
|
}
|
|
}
|