Files
Juliet/Game/game.cpp
T

154 lines
4.2 KiB
C++

#include <Windows.h> // TODO: remove because our dll should not be platform dependant
#undef min
#undef max
#include <game.h>
#include <Controller/DebugCameraController.h>
#include <Core/Common/EnumUtils.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 <Entity/Entity.h>
#include <Entity/EntityManager.h>
#include <Graphics/Camera.h>
#include <Graphics/MeshRenderer.h>
#include <imgui.h>
GameState* gGameState = nullptr;
GameState* GetGameState()
{
return gGameState;
}
// Test code
namespace Game
{
struct Door
{
DECLARE_ENTITY()
bool IsOpened;
};
DEFINE_ENTITY(Door);
struct Rock
{
DECLARE_ENTITY()
int Health;
};
DEFINE_ENTITY(Rock);
} // namespace Game
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(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));
// Reserve cameras. Lets go with 4 for now
ReserveCamera(4);
// 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);
MeshAssetID cubeAsset = GetCubePrimitiveMeshAssetID();
MeshInstanceID meshInst = CreateMeshInstance(cubeAsset, 0, MatrixIdentity());
rock->Base->MeshInstance = meshInst;
printf("Door is %s\n", door->IsOpened ? "Opened" : "Closed");
printf("Rock has %d health points\n", rock->Health);
// 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);
}
GameMode previousFrameMode = gGameState->Mode;
if (IsKeyPressed(ScanCode::F1))
{
gGameState->Mode = static_cast<GameMode>((ToUnderlying(gGameState->Mode) + 1) % 2);
}
if (gGameState->Mode == GameMode::Play)
{
// update game
}
// Sync entity transforms to meshes
auto& manager = GetEntityManager();
for (Entity& ent : manager.Entities)
{
if (ent.MeshInstance != static_cast<index_t>(-1))
{
SetMeshInstanceTransform(ent.MeshInstance, MatrixTranslation(ent.X, ent.Y, 0.0f));
}
}
if (gGameState->Mode == GameMode::Debug)
{
if (previousFrameMode != gGameState->Mode)
{
ActivateDebugController();
}
UpdateDebugController(deltaTime);
#if JULIET_DEBUG
ImGui::Begin("Debug");
RenderImGuiDebugController(deltaTime);
ImGui::End();
#endif
}
else
{
if (previousFrameMode != gGameState->Mode)
{
DeactivateDebugController();
}
}
}