Cleaning game.cpp removing old test code.
Created static mesh entity Created debug bar
This commit is contained in:
@@ -0,0 +1,9 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <Entity/Entity.h>
|
||||||
|
|
||||||
|
struct StaticMesh
|
||||||
|
{
|
||||||
|
DECLARE_ENTITY()
|
||||||
|
};
|
||||||
|
DEFINE_ENTITY(StaticMesh);
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
#include <Debug/DebugTopBar.h>
|
||||||
|
|
||||||
|
#include <game.h>
|
||||||
|
#include <imgui.h>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
Juliet::String GetGameModeName(GameMode gameMode)
|
||||||
|
{
|
||||||
|
using namespace Juliet;
|
||||||
|
switch (gameMode)
|
||||||
|
{
|
||||||
|
case GameMode::Editor: return WrapString("Editor");
|
||||||
|
case GameMode::Play: return WrapString("Play");
|
||||||
|
case GameMode::Debug: return WrapString("Debug");
|
||||||
|
default: return WrapString("Unknown ERROR");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
void DrawTopBar()
|
||||||
|
{
|
||||||
|
auto* gameState = GetGameState();
|
||||||
|
|
||||||
|
ImGuiViewport* viewport = ImGui::GetMainViewport();
|
||||||
|
ImGui::SetNextWindowPos(viewport->Pos);
|
||||||
|
ImGui::SetNextWindowSize(ImVec2(viewport->Size.x, 10.0f));
|
||||||
|
|
||||||
|
ImGuiWindowFlags flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings |
|
||||||
|
ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoNav;
|
||||||
|
|
||||||
|
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(0.0f, 0.0f, 0.0f, 0.6f));
|
||||||
|
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(10.0f, 5.0f));
|
||||||
|
|
||||||
|
if (ImGui::Begin("TopBarOverlay", nullptr, flags))
|
||||||
|
{
|
||||||
|
ImGui::Text("Game Mode: %s", CStr(GetGameModeName(gameState->Mode)));
|
||||||
|
|
||||||
|
char fps_text[32];
|
||||||
|
snprintf(fps_text, sizeof(fps_text), "FPS: %.1f (%.2f ms)", ImGui::GetIO().Framerate, 1000.0f / ImGui::GetIO().Framerate);
|
||||||
|
float text_width = ImGui::CalcTextSize(fps_text).x;
|
||||||
|
ImGui::SameLine(viewport->Size.x - text_width - 15.0f); // 15px right padding
|
||||||
|
// Render FPS text
|
||||||
|
ImGui::Text("%s", fps_text);
|
||||||
|
}
|
||||||
|
ImGui::End();
|
||||||
|
|
||||||
|
ImGui::PopStyleVar();
|
||||||
|
ImGui::PopStyleColor();
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if JULIET_DEBUG
|
||||||
|
void DrawTopBar();
|
||||||
|
#endif
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
#include <Entity/EntityManager.h>
|
#include <Entity/EntityManager.h>
|
||||||
|
|
||||||
#include <Entity/Entity.h>
|
#include <Entity/Entity.h>
|
||||||
|
#include <Graphics/MeshRenderer.h>
|
||||||
|
|
||||||
EntityID EntityManager::ID = 0;
|
EntityID EntityManager::ID = 0;
|
||||||
|
|
||||||
@@ -29,3 +30,14 @@ void RegisterEntity(EntityManager& /*manager*/, Entity* entity)
|
|||||||
{
|
{
|
||||||
entity->ID = EntityManager::ID++;
|
entity->ID = EntityManager::ID++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void UpdateEntityManager(EntityManager& manager)
|
||||||
|
{
|
||||||
|
for (Entity& ent : manager.Entities)
|
||||||
|
{
|
||||||
|
if (ent.MeshInstance != indexMax)
|
||||||
|
{
|
||||||
|
Juliet::SetMeshInstanceTransform(ent.MeshInstance, Juliet::MatrixTranslation(ent.X, ent.Y, 0.0f));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -21,3 +21,4 @@ void InitEntityManager(Juliet::NonNullPtr<World> world);
|
|||||||
void ShutdownEntityManager();
|
void ShutdownEntityManager();
|
||||||
EntityManager& GetEntityManager();
|
EntityManager& GetEntityManager();
|
||||||
void RegisterEntity(EntityManager& manager, Entity* entity);
|
void RegisterEntity(EntityManager& manager, Entity* entity);
|
||||||
|
void UpdateEntityManager(EntityManager& manager);
|
||||||
|
|||||||
+53
-62
@@ -1,53 +1,37 @@
|
|||||||
|
|
||||||
#include <Windows.h> // TODO: remove because our dll should not be platform dependant
|
|
||||||
#undef min
|
|
||||||
#undef max
|
|
||||||
|
|
||||||
#include <game.h>
|
#include <game.h>
|
||||||
|
|
||||||
#include <Controller/DebugCameraController.h>
|
#include <Controller/DebugCameraController.h>
|
||||||
#include <Core/Common/EnumUtils.h>
|
|
||||||
#include <Core/HAL/Filesystem/Filesystem.h>
|
#include <Core/HAL/Filesystem/Filesystem.h>
|
||||||
#include <Core/HAL/Keyboard/Keyboard.h>
|
#include <Core/HAL/Keyboard/Keyboard.h>
|
||||||
#include <Core/JulietInit.h>
|
#include <Core/JulietInit.h>
|
||||||
#include <Core/Logging/LogManager.h>
|
#include <Core/Logging/LogManager.h>
|
||||||
#include <Core/Memory/MemoryArena.h>
|
#include <Core/Memory/MemoryArena.h>
|
||||||
|
#include <Data/StaticMesh.h>
|
||||||
#include <Entity/Entity.h>
|
#include <Entity/Entity.h>
|
||||||
#include <Entity/EntityManager.h>
|
#include <Entity/EntityManager.h>
|
||||||
#include <Graphics/Camera.h>
|
#include <Graphics/Camera.h>
|
||||||
#include <Graphics/MeshRenderer.h>
|
#include <Graphics/MeshRenderer.h>
|
||||||
#include <imgui.h>
|
#include <imgui.h>
|
||||||
|
|
||||||
GameState* gGameState = nullptr;
|
#if JULIET_DEBUG
|
||||||
|
#include <Debug/DebugTopBar.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
GameState* gGameState = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
GameState* GetGameState()
|
GameState* GetGameState()
|
||||||
{
|
{
|
||||||
return gGameState;
|
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()
|
extern "C" JULIET_API void __cdecl GameShutdown()
|
||||||
{
|
{
|
||||||
printf("Shutting down game...\n");
|
printf("Shutting down game...\n");
|
||||||
|
|
||||||
using namespace Juliet;
|
using namespace Juliet;
|
||||||
using namespace Game;
|
|
||||||
|
|
||||||
ShutdownEntityManager();
|
ShutdownEntityManager();
|
||||||
}
|
}
|
||||||
@@ -55,7 +39,6 @@ extern "C" JULIET_API void __cdecl GameShutdown()
|
|||||||
extern "C" JULIET_API void __cdecl GameUpdate(Juliet::GameData* params, [[maybe_unused]] float deltaTime)
|
extern "C" JULIET_API void __cdecl GameUpdate(Juliet::GameData* params, [[maybe_unused]] float deltaTime)
|
||||||
{
|
{
|
||||||
using namespace Juliet;
|
using namespace Juliet;
|
||||||
using namespace Game;
|
|
||||||
|
|
||||||
gGameState = params->GameState;
|
gGameState = params->GameState;
|
||||||
if (!gGameState)
|
if (!gGameState)
|
||||||
@@ -81,60 +64,71 @@ extern "C" JULIET_API void __cdecl GameUpdate(Juliet::GameData* params, [[maybe_
|
|||||||
|
|
||||||
// Entity Use case
|
// Entity Use case
|
||||||
InitEntityManager(gameState->World);
|
InitEntityManager(gameState->World);
|
||||||
auto& manager = GetEntityManager();
|
auto& manager = GetEntityManager();
|
||||||
Door* door = MakeEntity<Door>(manager, 10.0f, 2.0f);
|
|
||||||
door->IsOpened = true;
|
|
||||||
|
|
||||||
Entity* ent = door->Base;
|
NonNullPtr mesh = MakeEntity<StaticMesh>(manager, 1.f, 2.f);
|
||||||
[[maybe_unused]] Door* stillDoor = DownCast<Door>(ent);
|
|
||||||
Assert(door == stillDoor);
|
|
||||||
|
|
||||||
Rock* rock = MakeEntity<Rock>(manager, 1.f, 2.f);
|
MeshAssetID cubeAsset = GetCubePrimitiveMeshAssetID();
|
||||||
rock->Health = 100;
|
MeshInstanceID meshInst = CreateMeshInstance(cubeAsset, 0, MatrixIdentity());
|
||||||
Assert(door->Base != rock->Base);
|
mesh->Base->MeshInstance = meshInst;
|
||||||
|
|
||||||
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
|
// Summer at 2pm lighting
|
||||||
Vector3 sunDirection = { -0.2f, -0.9f, -0.3f };
|
Vector3 sunDirection = { -0.2f, -0.9f, -0.3f };
|
||||||
Vector3 sunColor = { 1.0f, 0.95f, 0.85f };
|
Vector3 sunColor = { 1.0f, 0.95f, 0.85f };
|
||||||
float ambientIntensity = 0.4f;
|
float ambientIntensity = 0.4f;
|
||||||
SetGlobalLight(sunDirection, sunColor, ambientIntensity);
|
SetGlobalLight(sunDirection, sunColor, ambientIntensity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if JULIET_DEBUG
|
||||||
|
DrawTopBar();
|
||||||
|
#endif
|
||||||
|
|
||||||
GameMode previousFrameMode = gGameState->Mode;
|
GameMode previousFrameMode = gGameState->Mode;
|
||||||
if (IsKeyPressed(ScanCode::F1))
|
if (IsKeyPressed(ScanCode::F1))
|
||||||
{
|
{
|
||||||
gGameState->Mode = static_cast<GameMode>((ToUnderlying(gGameState->Mode) + 1) % 2);
|
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 (gGameState->Mode == GameMode::Play)
|
if (gGameState->Mode == GameMode::Play)
|
||||||
{
|
{
|
||||||
|
if (IsKeyPressed(ScanCode::Escape))
|
||||||
|
{
|
||||||
|
gGameState->Mode = GameMode::Editor;
|
||||||
|
}
|
||||||
|
|
||||||
// update game
|
// update game
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sync entity transforms to meshes
|
|
||||||
auto& manager = GetEntityManager();
|
auto& manager = GetEntityManager();
|
||||||
for (Entity& ent : manager.Entities)
|
UpdateEntityManager(manager);
|
||||||
{
|
|
||||||
if (ent.MeshInstance != static_cast<index_t>(-1))
|
|
||||||
{
|
|
||||||
SetMeshInstanceTransform(ent.MeshInstance, MatrixTranslation(ent.X, ent.Y, 0.0f));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (gGameState->Mode == GameMode::Debug)
|
if (gGameState->Mode == GameMode::Debug)
|
||||||
{
|
{
|
||||||
if (previousFrameMode != gGameState->Mode)
|
if (previousFrameMode == GameMode::Play)
|
||||||
{
|
{
|
||||||
ActivateDebugController();
|
ActivateDebugController();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (IsKeyPressed(ScanCode::Escape))
|
||||||
|
{
|
||||||
|
gGameState->Mode = GameMode::Editor;
|
||||||
|
}
|
||||||
|
|
||||||
UpdateDebugController(deltaTime);
|
UpdateDebugController(deltaTime);
|
||||||
|
|
||||||
#if JULIET_DEBUG
|
#if JULIET_DEBUG
|
||||||
@@ -143,11 +137,8 @@ extern "C" JULIET_API void __cdecl GameUpdate(Juliet::GameData* params, [[maybe_
|
|||||||
ImGui::End();
|
ImGui::End();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
else
|
if (gGameState->Mode != GameMode::Debug && previousFrameMode == GameMode::Debug)
|
||||||
{
|
{
|
||||||
if (previousFrameMode != gGameState->Mode)
|
DeactivateDebugController();
|
||||||
{
|
|
||||||
DeactivateDebugController();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-1
@@ -12,6 +12,7 @@ struct World
|
|||||||
|
|
||||||
enum class GameMode
|
enum class GameMode
|
||||||
{
|
{
|
||||||
|
Editor,
|
||||||
Play,
|
Play,
|
||||||
Debug
|
Debug
|
||||||
};
|
};
|
||||||
@@ -22,7 +23,7 @@ struct GameState
|
|||||||
|
|
||||||
World* World;
|
World* World;
|
||||||
|
|
||||||
GameMode Mode = GameMode::Play;
|
GameMode Mode = GameMode::Editor;
|
||||||
|
|
||||||
float TotalTime;
|
float TotalTime;
|
||||||
int Score;
|
int Score;
|
||||||
|
|||||||
Reference in New Issue
Block a user