-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add a resource manager for loading textures #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
dfa5f78
feat(resource manager): adds manager for textures
Delta18-Git e92cd7b
test: add interactive sprite layer test
Delta18-Git b101b24
fix: replace logging with spdlog-based logging
Delta18-Git abc793a
fix: remove example code from main
Delta18-Git 3d1f6a2
fix(test/sprite_layer): make logging same as main
Delta18-Git 4207b47
feat(test/collision): add AABB based collision example
Delta18-Git 5d45fdd
fix: prevent UB in the texture allocation
Delta18-Git File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| #ifndef RESOURCE_HPP | ||
| #define RESOURCE_HPP | ||
| #include <engine/core/renderer.hpp> | ||
| #include <engine/core/texture.hpp> | ||
| #include <memory> | ||
| #include <string_view> | ||
| #include <unordered_map> | ||
| namespace Engine { | ||
| // add more resource types later (font, audio) | ||
| enum class ResourceType { Texture }; | ||
| class ResourceManager { | ||
| public: | ||
| explicit ResourceManager(Renderer &renderer); | ||
| ~ResourceManager(); | ||
|
|
||
| void AddResource(const std::string &path, ResourceType type); | ||
| bool RemoveResource(const std::string_view path, ResourceType type); | ||
|
|
||
| std::shared_ptr<Texture> FindTexture(const std::string_view path); | ||
|
|
||
| private: | ||
| void CreateTexture(const std::string_view path); | ||
|
|
||
| Renderer &m_renderer; | ||
| std::unordered_map<std::string, ResourceType> m_resourcePaths; | ||
| std::unordered_map<std::string_view, std::shared_ptr<Texture>> m_textureMap; | ||
| }; | ||
| } // namespace Engine | ||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| #include <SDL3_image/SDL_image.h> | ||
| #include <engine/core/resource.hpp> | ||
| #include <engine/core/texture.hpp> | ||
| #include <memory> | ||
| #include <string_view> | ||
| namespace Engine { | ||
|
|
||
| ResourceManager::ResourceManager(Renderer &renderer) : m_renderer(renderer) {} | ||
|
|
||
| ResourceManager::~ResourceManager() { | ||
| m_resourcePaths.clear(); | ||
| m_textureMap.clear(); | ||
| } | ||
|
|
||
| void ResourceManager::AddResource(const std::string &path, ResourceType type) { | ||
| m_resourcePaths.emplace(path, type); | ||
| } | ||
|
|
||
| bool ResourceManager::RemoveResource(const std::string_view path, | ||
| ResourceType type) { | ||
| auto it_resPath = m_resourcePaths.find(std::string(path)); | ||
| if (it_resPath == m_resourcePaths.end() || it_resPath->second != type) { | ||
| return false; | ||
| } | ||
| m_resourcePaths.erase(it_resPath); | ||
| switch (type) { | ||
| case ResourceType::Texture: { | ||
| auto it_texcache = m_textureMap.find(path); | ||
| if (it_texcache != m_textureMap.end()) { | ||
| m_textureMap.erase(it_texcache); | ||
| } | ||
| return true; | ||
| } | ||
| default: | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| std::shared_ptr<Texture> | ||
| ResourceManager::FindTexture(const std::string_view path) { | ||
| auto it_tex = m_textureMap.find(path); | ||
| auto it_res = m_resourcePaths.find(std::string(path)); | ||
| // if texture not loaded, and imported as asset and actually a texture | ||
| if (it_tex == m_textureMap.end() && it_res != m_resourcePaths.end() && | ||
| it_res->second == ResourceType::Texture) { | ||
| CreateTexture(path); | ||
| return FindTexture(path); | ||
| } else if (it_tex == m_textureMap.end()) { | ||
| return nullptr; // else if resource not loaded but not (a texture or | ||
| // imported as asset) | ||
| } | ||
| return it_tex->second; | ||
| } | ||
|
|
||
| void ResourceManager::CreateTexture(const std::string_view path) { | ||
| std::shared_ptr<Texture> texture = std::make_shared<Texture>(); | ||
| SDL_Texture *sdlTex = | ||
| IMG_LoadTexture(m_renderer.GetSDLRenderer(), std::string(path).c_str()); | ||
| if (sdlTex == nullptr) { | ||
| // TODO: add logging here | ||
| return; | ||
| } | ||
| texture->SetTexture(sdlTex); | ||
| m_textureMap.emplace(path, std::move(texture)); | ||
| } | ||
|
|
||
| } // namespace Engine | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.