Skip to content

Commit

Permalink
ownership of textures and resources, and shutdown changed
Browse files Browse the repository at this point in the history
  • Loading branch information
beaumanvienna committed Jun 17, 2024
1 parent db31a19 commit 7be73a4
Show file tree
Hide file tree
Showing 24 changed files with 89 additions and 149 deletions.
11 changes: 4 additions & 7 deletions application/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@
#include "application.h"
#include "lucre.h"

std::shared_ptr<Application> Application::Create()
std::unique_ptr<Application> Application::Create()
{

if (!LucreApp::Lucre::m_Application)
{
LucreApp::Lucre::m_Application = std::make_shared<LucreApp::Lucre>();
}
return LucreApp::Lucre::m_Application;
std::unique_ptr<Application> application = std::make_unique<LucreApp::Lucre>();
LucreApp::Lucre::m_Application = reinterpret_cast<LucreApp::Lucre*>(application.get());
return application;
}
46 changes: 3 additions & 43 deletions application/lucre/gameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,59 +143,19 @@ namespace LucreApp
LoadNextState();
break;
}
case State::MAIN:
case State::SETTINGS:
{
if (GetScene()->IsFinished())
{
// game over
Engine::m_Engine->Shutdown();
SetState(m_LastState);
}
break;
}
case State::MAIN:
case State::BEACH:
{
if (GetScene()->IsFinished())
{
// game over
Engine::m_Engine->Shutdown();
}
break;
}
case State::NIGHT:
{
if (GetScene()->IsFinished())
{
// game over
Engine::m_Engine->Shutdown();
}
break;
}
case State::DESSERT:
{
if (GetScene()->IsFinished())
{
// game over
Engine::m_Engine->Shutdown();
}
break;
}
case State::TERRAIN:
{
if (GetScene()->IsFinished())
{
// game over
Engine::m_Engine->Shutdown();
}
break;
}
case State::SETTINGS:
{
if (GetScene()->IsFinished())
{
SetState(m_LastState);
}
break;
}
case State::NULL_STATE:
case State::MAX_STATES:
{
Expand Down
12 changes: 6 additions & 6 deletions application/lucre/gameState.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ namespace LucreApp

public:
GameState();
~GameState() {}

void Start();
void Stop();
Scene *OnUpdate();
void OnEvent(Event &event);
Scene* OnUpdate();

void EnableUserInput(bool enable);

Expand All @@ -72,11 +72,11 @@ namespace LucreApp
State GetNextState() const { return m_NextState; }
bool UserInputIsInabled() const { return m_UserInputEnabled; }

Scene *GetScene();
Scene *GetScene(State state);
Scene *GetNextScene();
Scene* GetScene();
Scene* GetScene(State state);
Scene* GetNextScene();
void DestroyScene(const State state);
void SetupScene(const State state, const std::shared_ptr<Scene> &scene);
void SetupScene(const State state, const std::shared_ptr<Scene>& scene);

private:
void Load(State state);
Expand Down
4 changes: 2 additions & 2 deletions application/lucre/lucre.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
namespace LucreApp
{

std::shared_ptr<Lucre> Lucre::m_Application;
Lucre* Lucre::m_Application;
SpriteSheet* Lucre::m_Spritesheet;

Lucre::Lucre() : m_CurrentScene{nullptr}, m_InGameGuiIsRunning{false}, m_DebugWindowIsRunning{false} {}
Expand Down Expand Up @@ -64,7 +64,7 @@ namespace LucreApp
m_Renderer = Engine::m_Engine->GetRenderer();

// create orthogonal camera

OrthographicCameraComponent orthographicCameraComponent(1.0f /*m_XMag*/, 1.0f /*m_YMag*/, 2.0f /*m_ZNear*/,
-2.0f /*ZFar*/);
m_CameraController = std::make_shared<CameraController>(orthographicCameraComponent);
Expand Down
14 changes: 7 additions & 7 deletions application/lucre/lucre.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,25 @@ namespace LucreApp

public:
Lucre();
~Lucre() {}
virtual ~Lucre() {}

bool Start() override;
void Shutdown() override;
void OnUpdate(const Timestep& timestep) override;
void OnEvent(Event& event) override;
virtual bool Start() override;
virtual void Shutdown() override;
virtual void OnUpdate(const Timestep& timestep) override;
virtual void OnEvent(Event& event) override;
void OnAppEvent(AppEvent& event);
void OnResize();

void PlaySound(int resourceID);
UIControllerIcon* GetUI() const { return m_UIControllerIcon; }
Scene* GetScene() override { return m_GameState.GetScene(); }
virtual Scene* GetScene() override { return m_GameState.GetScene(); }
GameState::State GetState() const { return m_GameState.GetState(); }
bool KeyboardInputIsReleased() const { return !m_InGameGuiIsRunning; }
bool DebugWindowIsRunning() const { return m_DebugWindowIsRunning; }
bool InGameGuiIsRunning() const { return m_InGameGuiIsRunning; }

public:
static std::shared_ptr<Lucre> m_Application;
static Lucre* m_Application;
static SpriteSheet* m_Spritesheet;

private:
Expand Down
2 changes: 1 addition & 1 deletion application/lucre/scenes/terrainScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace LucreApp

public:
TerrainScene(const std::string& filepath, const std::string& alternativeFilepath);
~TerrainScene() override {}
virtual ~TerrainScene() override {}

virtual void Start() override;
virtual void Stop() override;
Expand Down
2 changes: 1 addition & 1 deletion engine/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ namespace GfxRenderEngine
virtual void OnEvent(Event& event) = 0;
virtual Scene* GetScene() = 0;

static std::shared_ptr<Application> Create();
static std::unique_ptr<Application> Create();
};
} // namespace GfxRenderEngine
11 changes: 4 additions & 7 deletions engine/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,9 @@ namespace GfxRenderEngine
// init audio
m_Audio = Audio::Create();
m_Audio->Start();
#ifdef PULSEAUDIO
Sound::SetCallback([this](const LibPAmanager::Event& event)
{
AudioCallback((int)event.GetType());
});
#endif
#ifdef PULSEAUDIO
Sound::SetCallback([this](const LibPAmanager::Event& event) { AudioCallback((int)event.GetType()); });
#endif

// init controller
if (!m_Controller.Start())
Expand Down Expand Up @@ -330,7 +327,7 @@ namespace GfxRenderEngine
return std::chrono::high_resolution_clock::now();
}

void Engine::RunScripts(std::shared_ptr<GfxRenderEngine::Application> application)
void Engine::RunScripts(Application* application)
{
auto currentScene = application->GetScene();
auto& registry = currentScene->GetRegistry();
Expand Down
2 changes: 1 addition & 1 deletion engine/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ namespace GfxRenderEngine
bool IsPaused() const { return m_Paused; }
bool IsInitialized() const { return m_GraphicsContextInitialized; }
bool IsRunning() const { return m_Running; }
void RunScripts(std::shared_ptr<GfxRenderEngine::Application> application);
void RunScripts(Application* application);

std::string& GetHomeDirectory() { return m_HomeDir; }
std::string GetConfigFilePath() const { return m_ConfigFilePath; }
Expand Down
6 changes: 4 additions & 2 deletions engine/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ int engine(int argc, char* argv[])
PROFILE_BEGIN_SESSION("RunTime", "profiling (open with chrome tracing).json");

std::unique_ptr<GfxRenderEngine::Engine> engine;
std::shared_ptr<GfxRenderEngine::Application> application;
std::unique_ptr<GfxRenderEngine::Application> application;

{
PROFILE_SCOPE("engine startup");
Expand Down Expand Up @@ -83,7 +83,7 @@ int engine(int argc, char* argv[])
{
PROFILE_SCOPE("application->OnUpdate()");
application->OnUpdate(engine->GetTimestep());
engine->RunScripts(application);
engine->RunScripts(application.get());
}
engine->OnRender();
}
Expand All @@ -95,6 +95,8 @@ int engine(int argc, char* argv[])
}

application->Shutdown();
engine->Shutdown();
application.reset();
engine->Quit();

PROFILE_END_SESSION();
Expand Down
3 changes: 2 additions & 1 deletion engine/platform/Vulkan/VKbuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ namespace GfxRenderEngine
mappedRange.memory = m_Memory;
mappedRange.offset = offset;
mappedRange.size = size;
return vkFlushMappedMemoryRanges(m_Device->Device(), 1, &mappedRange);
VkResult result = vkFlushMappedMemoryRanges(m_Device->Device(), 1, &mappedRange);
return result;
}

/**
Expand Down
36 changes: 14 additions & 22 deletions engine/platform/Vulkan/VKmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,56 +57,48 @@ namespace GfxRenderEngine
return attributeDescriptions;
}

#define INIT_GLTF_AND_FBX_MODEL(x) \
CopySubmeshes(builder.m_Submeshes); \
m_Textures = std::move(builder.m_Textures); /*used to manage lifetime of textures*/ \
m_Skeleton = std::move(builder.m_Skeleton); \
m_Animations = std::move(builder.m_Animations); \
m_ShaderDataUbo = builder.m_ShaderData; \
CreateVertexBuffer(std::move(builder.m_Vertices)); \
#define INIT_MODEL() \
CopySubmeshes(builder.m_Submeshes); \
CreateVertexBuffer(std::move(builder.m_Vertices)); \
CreateIndexBuffer(std::move(builder.m_Indices));

// VK_Model
#define INIT_GLTF_AND_FBX_MODEL() \
CopySubmeshes(builder.m_Submeshes); \
CreateVertexBuffer(std::move(builder.m_Vertices)); \
CreateIndexBuffer(std::move(builder.m_Indices)); \
m_Skeleton = std::move(builder.m_Skeleton); \
m_Animations = std::move(builder.m_Animations); \
m_ShaderDataUbo = builder.m_ShaderData;

VK_Model::VK_Model(std::shared_ptr<VK_Device> device, const FastgltfBuilder& builder)
: m_Device(device), m_HasIndexBuffer{false}
{
INIT_GLTF_AND_FBX_MODEL();
}

VK_Model::VK_Model(std::shared_ptr<VK_Device> device, const UFbxBuilder& builder)
: m_Device(device), m_HasIndexBuffer{false}
{
INIT_GLTF_AND_FBX_MODEL();
}

VK_Model::VK_Model(std::shared_ptr<VK_Device> device, const GltfBuilder& builder)
: m_Device(device), m_HasIndexBuffer{false}
{
INIT_GLTF_AND_FBX_MODEL();
}

VK_Model::VK_Model(std::shared_ptr<VK_Device> device, const FbxBuilder& builder)
: m_Device(device), m_HasIndexBuffer{false}
{
INIT_GLTF_AND_FBX_MODEL();
}

VK_Model::VK_Model(std::shared_ptr<VK_Device> device, const Builder& builder) : m_Device(device), m_HasIndexBuffer{false}
{
CopySubmeshes(builder.m_Submeshes);
m_Cubemaps = std::move(builder.m_Cubemaps);

CreateVertexBuffer(std::move(builder.m_Vertices));
CreateIndexBuffer(std::move(builder.m_Indices));
INIT_MODEL();
m_Cubemaps = std::move(builder.m_Cubemaps); // used to manage lifetime
}

VK_Model::VK_Model(std::shared_ptr<VK_Device> device, const TerrainBuilder& builder)
: m_Device(device), m_HasIndexBuffer{false}
{
CopySubmeshes(builder.m_Submeshes);

CreateVertexBuffer(std::move(builder.m_Vertices));
CreateIndexBuffer(std::move(builder.m_Indices));
INIT_MODEL();
}

VK_Model::~VK_Model() {}
Expand Down
25 changes: 13 additions & 12 deletions engine/renderer/builder/fastgltfBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ namespace GfxRenderEngine
m_Basepath = EngineCore::GetPathWithoutFilename(filepath);
if (resourceBuffers)
{
m_ResourceBuffers = *resourceBuffers;
// optionally: additional resources not originating from a 3D file can be provided here
// e.g. a height map for terrain required in the vertex shader
// these additional resource go into descriptor set 2,
// along with the instance and skeletal animation buffer
m_ResourceBuffersPre = *resourceBuffers;
}
}

Expand Down Expand Up @@ -815,33 +819,30 @@ namespace GfxRenderEngine
LOG_CORE_CRITICAL("AssignMaterial: materialIndex must be less than m_Materials.size()");
}

Material material{}; // create from defaults
Material::MaterialTextures materialTextures;
Material& material = submesh.m_Material;

// material
if (materialIndex != Gltf::GLTF_NOT_USED)
{
material = m_Materials[materialIndex];
materialTextures = m_MaterialTextures[materialIndex];
material.m_MaterialTextures = m_MaterialTextures[materialIndex];
}

// create material descriptor

material.m_MaterialDescriptor =
MaterialDescriptor::Create(MaterialDescriptor::MaterialType::MtPbr, materialTextures);

// assign
submesh.m_Material = material;
MaterialDescriptor::Create(MaterialDescriptor::MaterialType::MtPbr, material.m_MaterialTextures);
}

{ // resources
Resources::ResourceBuffers& resourceBuffers = submesh.m_Resources.m_ResourceBuffers;
resourceBuffers = m_ResourceBuffersPre;
std::shared_ptr<Buffer> instanceUbo{m_InstanceBuffer->GetBuffer()};
m_ResourceBuffers[Resources::INSTANCE_BUFFER_INDEX] = instanceUbo;
resourceBuffers[Resources::INSTANCE_BUFFER_INDEX] = instanceUbo;
if (m_SkeletalAnimation)
{
m_ResourceBuffers[Resources::SKELETAL_ANIMATION_BUFFER_INDEX] = m_ShaderData;
resourceBuffers[Resources::SKELETAL_ANIMATION_BUFFER_INDEX] = m_ShaderData;
}
submesh.m_Resources.m_ResourceDescriptor = ResourceDescriptor::Create(m_ResourceBuffers);
submesh.m_Resources.m_ResourceDescriptor = ResourceDescriptor::Create(resourceBuffers);
}

LOG_CORE_INFO("material assigned (fastgltf): material index {0}", materialIndex);
Expand Down
4 changes: 2 additions & 2 deletions engine/renderer/builder/fastgltfBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ namespace GfxRenderEngine
public:
std::vector<uint> m_Indices{};
std::vector<Vertex> m_Vertices{};
std::vector<std::shared_ptr<Texture>> m_Textures{};
std::vector<Submesh> m_Submeshes{};

private:
Expand Down Expand Up @@ -119,6 +118,7 @@ namespace GfxRenderEngine
std::shared_ptr<Model> m_Model;
std::vector<Material> m_Materials;
std::vector<Material::MaterialTextures> m_MaterialTextures{};
std::vector<std::shared_ptr<Texture>> m_Textures{};

// scene graph
uint m_InstanceCount;
Expand All @@ -128,7 +128,7 @@ namespace GfxRenderEngine

std::vector<entt::entity> m_InstancedObjects;
std::shared_ptr<InstanceBuffer> m_InstanceBuffer;
Resources::ResourceBuffers m_ResourceBuffers;
Resources::ResourceBuffers m_ResourceBuffersPre;
uint m_RenderObject;

entt::registry& m_Registry;
Expand Down
Loading

0 comments on commit 7be73a4

Please sign in to comment.