aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorheavydemon21 <nielsstunnebrink1@gmail.com>2024-10-23 13:49:20 +0200
committerheavydemon21 <nielsstunnebrink1@gmail.com>2024-10-23 13:49:20 +0200
commiteaa05e7a981b0f581f5393882e4753d9294a3dba (patch)
tree4f55cbacf8a70229ec523a45848fe89915d707d5 /src
parentf1857fc2d4ddec71b3f0395903f8446cf96b8d0c (diff)
rendering and asset_manager poc
:
Diffstat (limited to 'src')
-rwxr-xr-xsrc/build.sh1
-rw-r--r--src/crepe/SdlContext.cpp12
-rw-r--r--src/crepe/SdlContext.h4
-rw-r--r--src/crepe/api/AssetManager.cpp22
-rw-r--r--src/crepe/api/AssetManager.h47
-rw-r--r--src/crepe/api/CMakeLists.txt4
-rw-r--r--src/crepe/api/Sprite.cpp4
-rw-r--r--src/crepe/api/Sprite.h4
-rw-r--r--src/crepe/api/Texture.cpp2
-rw-r--r--src/dummy_resource_manager.cpp38
-rw-r--r--src/example/CMakeLists.txt1
-rw-r--r--src/example/asset_manager.cpp35
-rw-r--r--src/example/rendering.cpp82
13 files changed, 135 insertions, 121 deletions
diff --git a/src/build.sh b/src/build.sh
index 827c7d3..05d086c 100755
--- a/src/build.sh
+++ b/src/build.sh
@@ -6,3 +6,4 @@ cmake -B build -G Ninja
ninja -C build
ninja -C build/ rendering
+ninja -C build/ asset_manager
diff --git a/src/crepe/SdlContext.cpp b/src/crepe/SdlContext.cpp
index 17edfbc..cc5148c 100644
--- a/src/crepe/SdlContext.cpp
+++ b/src/crepe/SdlContext.cpp
@@ -11,6 +11,7 @@
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_surface.h>
#include <SDL2/SDL_video.h>
+#include <cmath>
#include <cstddef>
#include <iostream>
@@ -33,9 +34,9 @@ void SdlContext::handle_events(bool & running) {
SdlContext::~SdlContext() {
dbg_trace();
- if (m_game_renderer) SDL_DestroyRenderer(m_game_renderer);
+ if (m_game_renderer != nullptr) SDL_DestroyRenderer(m_game_renderer);
- if (m_game_window) {
+ if (m_game_window != nullptr) {
SDL_DestroyWindow(m_game_window);
}
@@ -56,7 +57,7 @@ SdlContext::SdlContext() {
m_game_window = SDL_CreateWindow(
"Crepe Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
- 1920, 1080, SDL_WINDOW_SHOWN);
+ 1920, 1080, SDL_WINDOW_HIDDEN);
if (!m_game_window) {
std::cerr << "Window could not be created! SDL_Error: "
<< SDL_GetError() << std::endl;
@@ -95,9 +96,10 @@ void SdlContext::draw(const api::Sprite & sprite,
.w = static_cast<int>(w * transform.scale),
.h = static_cast<int>(h * transform.scale),
};
-
+
+ double degrees = transform.rotation * 180 / M_PI;
SDL_RenderCopyEx(this->m_game_renderer, sprite.sprite_image->m_texture,
- NULL, &dstrect, 0, NULL, render_flip);
+ NULL, &dstrect, degrees, NULL, render_flip);
}
/*
diff --git a/src/crepe/SdlContext.h b/src/crepe/SdlContext.h
index a6c85f1..97adfa2 100644
--- a/src/crepe/SdlContext.h
+++ b/src/crepe/SdlContext.h
@@ -43,8 +43,8 @@ private:
void present_screen();
private:
- SDL_Window * m_game_window;
- SDL_Renderer * m_game_renderer;
+ SDL_Window * m_game_window = nullptr;
+ SDL_Renderer * m_game_renderer = nullptr;
};
} // namespace crepe
diff --git a/src/crepe/api/AssetManager.cpp b/src/crepe/api/AssetManager.cpp
index 0ecdae5..f6cc369 100644
--- a/src/crepe/api/AssetManager.cpp
+++ b/src/crepe/api/AssetManager.cpp
@@ -1,25 +1,23 @@
-#include "resource_manager.h"
-#include <string>
-#include <unordered_map>
+#include "AssetManager.h"
+#include "util/log.h"
+
using namespace crepe::api;
-ResourceManager& ResourceManager::get_instance(){
- static ResourceManager instance;
+AssetManager& AssetManager::get_instance(){
+ static AssetManager instance;
return instance;
}
-ResourceManager::~ResourceManager(){
- m_resources.clear();
+AssetManager::~AssetManager(){
+ dbg_trace();
+ this->asset_cache.clear();
}
-
-void ResourceManager::Unload(const std::string& file_path){
- if(m_resources.find(file_path) != m_resources.end()){
- m_resources.erase(file_path);
- }
+AssetManager::AssetManager(){
+ dbg_trace();
}
diff --git a/src/crepe/api/AssetManager.h b/src/crepe/api/AssetManager.h
index a646d95..1b8b86f 100644
--- a/src/crepe/api/AssetManager.h
+++ b/src/crepe/api/AssetManager.h
@@ -2,56 +2,49 @@
+#include <any>
#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
-#include "api/baseResource.h"
-
namespace crepe::api{
-class ResourceManager{
+class AssetManager{
private:
+ std::unordered_map< std::string, std::any> asset_cache;
- std::unordered_map< std::string, std::unique_ptr<BaseResource>> m_resources;
-
-
-protected:
- ResourceManager() = default;
- ~ResourceManager();
+private:
+ AssetManager();
+ virtual ~AssetManager();
public:
- ResourceManager(const ResourceManager &) = delete;
- ResourceManager(ResourceManager &&) = delete;
- ResourceManager &operator=(const ResourceManager &) = delete;
- ResourceManager &operator=(ResourceManager &&) = delete;
+ AssetManager(const AssetManager &) = delete;
+ AssetManager(AssetManager &&) = delete;
+ AssetManager &operator=(const AssetManager &) = delete;
+ AssetManager &operator=(AssetManager &&) = delete;
- static ResourceManager& get_instance();
+ static AssetManager& get_instance();
public:
- template<typename T>
- T* Load(const std::string& file_path){
-
- if (m_resources.find(file_path) != m_resources.end()) {
- return static_cast<T*>(m_resources[file_path].get());
- }
+ template<typename asset>
+ std::shared_ptr<asset> cache(const std::string& file_path, bool reload = false){
+ auto it = asset_cache.find(file_path);
- auto resource = std::make_unique<T>(file_path.c_str());
- if (resource) {
- m_resources[file_path] = std::move(resource);
- return static_cast<T*>(m_resources[file_path].get() );
+ if (!reload && it != asset_cache.end()) {
+ return std::any_cast<std::shared_ptr<asset>>(it->second);
}
- return nullptr;
- }
+ std::shared_ptr<asset> new_asset = std::make_shared<asset>(file_path.c_str());
- void Unload(const std::string& file_path);
+ asset_cache[file_path] = new_asset;
+ return new_asset;
+ }
};
}
diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt
index b046301..d29a771 100644
--- a/src/crepe/api/CMakeLists.txt
+++ b/src/crepe/api/CMakeLists.txt
@@ -6,6 +6,7 @@ target_sources(crepe PUBLIC
Texture.cpp
Sprite.cpp
Transform.cpp
+ AssetManager.cpp
)
target_sources(crepe PUBLIC FILE_SET HEADERS FILES
@@ -16,5 +17,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES
Transform.h
Color.h
Sprite.h
- Texture.h
+ Texture.h
+ AssetManager.h
)
diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp
index b0c0971..d9e26ab 100644
--- a/src/crepe/api/Sprite.cpp
+++ b/src/crepe/api/Sprite.cpp
@@ -10,8 +10,8 @@ using namespace std;
using namespace crepe;
using namespace crepe::api;
-Sprite::Sprite(unique_ptr<Texture> image, const Color & color,
- const flip_settings & flip) : color(color), flip(flip), sprite_image(std::move(image)) {
+Sprite::Sprite(shared_ptr<Texture> image, const Color & color,
+ const flip_settings & flip) : color(color), flip(flip), sprite_image(image) {
dbg_trace();
}
diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h
index 3dd9b4a..920f91e 100644
--- a/src/crepe/api/Sprite.h
+++ b/src/crepe/api/Sprite.h
@@ -17,9 +17,9 @@ struct flip_settings{
class Sprite : public Component {
public:
- Sprite(std::unique_ptr<Texture> image, const Color& color, const flip_settings& flip );
+ Sprite(std::shared_ptr<Texture> image, const Color& color, const flip_settings& flip );
~Sprite();
- std::unique_ptr<Texture> sprite_image;
+ std::shared_ptr<Texture> sprite_image;
Color color;
flip_settings flip;
uint8_t sorting_in_layer;
diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp
index 2d170c3..ba06c6d 100644
--- a/src/crepe/api/Texture.cpp
+++ b/src/crepe/api/Texture.cpp
@@ -6,8 +6,6 @@
#include "Texture.h"
#include <SDL2/SDL_render.h>
-#include <iostream>
-#include <string>
using namespace crepe::api;
diff --git a/src/dummy_resource_manager.cpp b/src/dummy_resource_manager.cpp
deleted file mode 100644
index bb0b7af..0000000
--- a/src/dummy_resource_manager.cpp
+++ /dev/null
@@ -1,38 +0,0 @@
-
-
-
-
-#include "api/game.h"
-#include "api/resource_manager.h"
-#include "api/spritesheet.h"
-#include "facade/Texture.h"
-#include "util/log.h"
-#include <vector>
-
-using namespace crepe;
-
-int main(){
-
-
- dbg_trace();
-
- // get instance of resource manager
- //api::ResourceManager& c_ResMan = api::ResourceManager::get_instance();
-
-
- game engine;
-
- api::ResourceManager& c_ResMan = api::ResourceManager::get_instance();
-
- auto test = c_ResMan.Load<Texture>("../asset/texture/img.png");
-
- auto img = Texture("../asset/texture/img.png");
-
- auto SS = api::Spritesheet("../asset/spritesheet/spritesheet_test.png", 1 , 4);
-
- std::vector<Texture*> t = {test};
- std::vector<api::Spritesheet*> s = {&SS};
-
- engine.render(t, s);
-
-}
diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt
index 9d82827..1b148b3 100644
--- a/src/example/CMakeLists.txt
+++ b/src/example/CMakeLists.txt
@@ -16,4 +16,5 @@ add_example(audio_internal)
add_example(components_internal)
add_example(script)
add_example(rendering)
+add_example(asset_manager)
diff --git a/src/example/asset_manager.cpp b/src/example/asset_manager.cpp
new file mode 100644
index 0000000..9b41c2f
--- /dev/null
+++ b/src/example/asset_manager.cpp
@@ -0,0 +1,35 @@
+
+
+#include <crepe/Sound.h>
+#include <crepe/api/AssetManager.h>
+#include <crepe/api/Texture.h>
+
+using namespace crepe;
+using namespace crepe::api;
+int main() {
+
+ // this needs to be called before the asset manager otherwise the destructor of sdl is not in the right order
+ {
+ Texture test("../asset/texture/img.png");
+ }
+
+ auto & mgr = AssetManager::get_instance();
+
+ {
+ auto bgm = mgr.cache<Sound>("../mwe/audio/bgm.ogg");
+ auto sfx1 = mgr.cache<Sound>("../mwe/audio/sfx1.wav");
+ auto sfx2 = mgr.cache<Sound>("../mwe/audio/sfx2.wav");
+
+ auto img = mgr.cache<Texture>("../asset/texture/img.png");
+ auto img1 = mgr.cache<Texture>("../asset/texture/second.png");
+ }
+
+ {
+ auto bgm = mgr.cache<Sound>("../mwe/audio/bgm.ogg");
+ auto sfx1 = mgr.cache<Sound>("../mwe/audio/sfx1.wav");
+ auto sfx2 = mgr.cache<Sound>("../mwe/audio/sfx2.wav");
+
+ auto img = mgr.cache<Texture>("../asset/texture/img.png");
+ auto img1 = mgr.cache<Texture>("../asset/texture/second.png");
+ }
+}
diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp
index 9ca12b7..34d9f66 100644
--- a/src/example/rendering.cpp
+++ b/src/example/rendering.cpp
@@ -1,52 +1,74 @@
-
-#include <crepe/util/log.h>
-#include <crepe/GameObject.h>
#include <crepe/ComponentManager.h>
+#include <crepe/GameObject.h>
#include <crepe/RenderSystem.h>
-
+#include <crepe/util/log.h>
#include <crepe/api/Color.h>
+#include <crepe/api/Point.h>
#include <crepe/api/Sprite.h>
#include <crepe/api/Texture.h>
#include <crepe/api/Transform.h>
-#include <crepe/api/Point.h>
+#include <crepe/api/AssetManager.h>
-
-#include <memory>
#include <chrono>
-
+#include <memory>
using namespace std;
using namespace crepe;
using namespace crepe::api;
-int main(){
+int main() {
dbg_trace();
-
- auto obj = GameObject(0, "name" , "tag", 0);
-
- Color color(0,0,0,0);
- //Sprite sprite(std::move(texture), color, {false,false});
-
- Point point = {
- .x = 0,
- .y = 0,
- };
-
- obj.add_component<Transform>(point, 0 ,1);
- obj.add_component<Sprite>(make_unique<Texture>("../asset/texture/img.png"),color, flip_settings{false,false});
-
- auto& sys = crepe::RenderSystem::get_instance();
-
- // scene example
+ auto obj = GameObject(0, "name", "tag", 0);
+ auto obj1= GameObject(0, "name", "tag", 0);
+ auto obj2 = GameObject(0, "name", "tag", 0);
+
+ auto& mgr = AssetManager::get_instance();
+ // Normal adding components
+ {
+ Color color(0, 0, 0, 0);
+ Point point = {
+ .x = 0,
+ .y = 0,
+ };
+ obj.add_component<Transform>(point, 1, 1);
+ obj.add_component<Sprite>(
+ make_shared<Texture>("../asset/texture/img.png"), color,
+ flip_settings{true, true});
+ }
+ {
+ Color color(0, 0, 0, 0);
+ Point point = {
+ .x = 500,
+ .y = 0,
+ };
+ obj.add_component<Transform>(point, 0, 0.1);
+ auto img = mgr.cache<Texture>("../asset/texture/second.png");
+ obj.add_component<Sprite>(img, color,
+ flip_settings{true, true});
+ }
+ {
+ Color color(0, 0, 0, 0);
+ Point point = {
+ .x = 800,
+ .y = 0,
+ };
+ //obj.add_component<Transform>(point, 0, 0.1);
+ auto img = mgr.cache<Texture>("../asset/texture/second.png");
+ obj.add_component<Sprite>(img, color,
+ flip_settings{true, true});
+ }
+
+
+
+
+ auto & sys = crepe::RenderSystem::get_instance();
auto start = std::chrono::steady_clock::now();
- while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) {
+ while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) {
sys.update();
- }
-
+ }
}
-