From ef44da3f5e9ca533782da5e185e69e28c295d226 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Tue, 1 Oct 2024 19:53:56 +0200 Subject: Added resources to crepe --- src/build.sh | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100755 src/build.sh (limited to 'src/build.sh') diff --git a/src/build.sh b/src/build.sh new file mode 100755 index 0000000..e987bc1 --- /dev/null +++ b/src/build.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +# creates the build dir and runs CMake with Ninja +cmake -B build -G Ninja + +# build the project +cmake --build build \ No newline at end of file -- cgit v1.2.3 From f1857fc2d4ddec71b3f0395903f8446cf96b8d0c Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Tue, 22 Oct 2024 15:37:00 +0200 Subject: fixed everything and can now work with new compiler, example rendering and made it work with component manager --- src/CMakeLists.txt | 4 +- src/build.sh | 5 +- src/crepe/Asset.cpp | 3 +- src/crepe/CMakeLists.txt | 6 +- src/crepe/RenderSystem.cpp | 41 +++++++++++ src/crepe/RenderSystem.h | 18 +++++ src/crepe/SdlContext.cpp | 156 ++++++++++++------------------------------ src/crepe/SdlContext.h | 45 +++++++----- src/crepe/SoundContext.cpp | 20 ++++++ src/crepe/SoundContext.h | 26 +++++++ src/crepe/Sprite.cpp | 8 --- src/crepe/Sprite.h | 16 ----- src/crepe/api/AudioSource.cpp | 2 +- src/crepe/api/CMakeLists.txt | 9 +++ src/crepe/api/Sprite.cpp | 18 +++++ src/crepe/api/Sprite.h | 18 ++--- src/crepe/api/Texture.cpp | 29 ++++---- src/crepe/api/Texture.h | 25 +++---- src/crepe/api/Transform.cpp | 13 ++++ src/crepe/api/Transform.h | 4 +- src/crepe/renderSystem.cpp | 37 ---------- src/crepe/renderSystem.h | 13 ---- src/dummy_rendering.cpp | 19 ----- src/example/CMakeLists.txt | 1 + src/example/rendering.cpp | 52 ++++++++++++++ 25 files changed, 320 insertions(+), 268 deletions(-) create mode 100644 src/crepe/RenderSystem.cpp create mode 100644 src/crepe/RenderSystem.h create mode 100644 src/crepe/SoundContext.cpp create mode 100644 src/crepe/SoundContext.h delete mode 100644 src/crepe/Sprite.cpp delete mode 100644 src/crepe/Sprite.h create mode 100644 src/crepe/api/Sprite.cpp create mode 100644 src/crepe/api/Transform.cpp delete mode 100644 src/crepe/renderSystem.cpp delete mode 100644 src/crepe/renderSystem.h delete mode 100644 src/dummy_rendering.cpp create mode 100644 src/example/rendering.cpp (limited to 'src/build.sh') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 09c60bd..8a47905 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -8,6 +8,7 @@ set(CMAKE_BUILD_TYPE Debug) project(crepe C CXX) find_package(SDL2 REQUIRED) +find_package(SDL2_image REQUIRED) find_package(SoLoud REQUIRED) find_package(GTest REQUIRED) @@ -20,7 +21,8 @@ target_include_directories(crepe target_link_libraries(crepe PRIVATE soloud - PRIVATE SDL2 + PUBLIC SDL2 + PUBLIC SDL2_image ) add_subdirectory(crepe) diff --git a/src/build.sh b/src/build.sh index e987bc1..827c7d3 100755 --- a/src/build.sh +++ b/src/build.sh @@ -3,5 +3,6 @@ # creates the build dir and runs CMake with Ninja cmake -B build -G Ninja -# build the project -cmake --build build \ No newline at end of file +ninja -C build + +ninja -C build/ rendering diff --git a/src/crepe/Asset.cpp b/src/crepe/Asset.cpp index 15ddc27..3cbed0b 100644 --- a/src/crepe/Asset.cpp +++ b/src/crepe/Asset.cpp @@ -5,7 +5,8 @@ using namespace crepe; Asset::Asset(const std::string & src) { - this->src = std::filesystem::canonical(src); + //this->src = std::filesystem::canonical(src); + this->src = src; this->file = std::ifstream(this->src, std::ios::in | std::ios::binary); } diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 8323490..6633696 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -2,19 +2,21 @@ target_sources(crepe PUBLIC Asset.cpp Sound.cpp SoundContext.cpp + SdlContext.cpp ComponentManager.cpp Component.cpp GameObject.cpp Collider.cpp Rigidbody.cpp - Sprite.cpp ScriptSystem.cpp + RenderSystem.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES Asset.h Sound.h SoundContext.h + SdlContext.h ComponentManager.h ComponentManager.hpp Component.h @@ -22,9 +24,9 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES GameObject.hpp Collider.h Rigidbody.h - Sprite.h System.h ScriptSystem.h + RenderSystem.h ) add_subdirectory(api) diff --git a/src/crepe/RenderSystem.cpp b/src/crepe/RenderSystem.cpp new file mode 100644 index 0000000..6aae3bb --- /dev/null +++ b/src/crepe/RenderSystem.cpp @@ -0,0 +1,41 @@ + + +#include "RenderSystem.h" +#include "ComponentManager.h" +#include "SdlContext.h" +#include "api/Sprite.h" +#include "api/Transform.h" +#include "util/log.h" +#include +#include +#include + +using namespace crepe; +using namespace crepe::api; + +RenderSystem::RenderSystem() { dbg_trace(); } + +RenderSystem::~RenderSystem() { dbg_trace(); } + +RenderSystem& RenderSystem::get_instance(){ + static RenderSystem instance; + return instance; +} + +void RenderSystem::update() { + + ComponentManager& mgr = ComponentManager::get_instance(); + + std::vector> sprites = mgr.get_components_by_type(); + std::vector> transforms = mgr.get_components_by_type(); + + SdlContext& render = SdlContext::get_instance(); + render.clear_screen(); + + for (size_t i = 0; i < sprites.size(); ++i) { + render.draw(sprites[i].get(), transforms[i].get()); + } + + render.present_screen(); + +} diff --git a/src/crepe/RenderSystem.h b/src/crepe/RenderSystem.h new file mode 100644 index 0000000..5e86dce --- /dev/null +++ b/src/crepe/RenderSystem.h @@ -0,0 +1,18 @@ + +#pragma once + +#include "System.h" + +namespace crepe { + +class RenderSystem : public System { + +public: + static RenderSystem & get_instance(); + void update(); + +private: + RenderSystem(); + ~RenderSystem(); +}; +} // namespace crepe diff --git a/src/crepe/SdlContext.cpp b/src/crepe/SdlContext.cpp index 44d1bdf..17edfbc 100644 --- a/src/crepe/SdlContext.cpp +++ b/src/crepe/SdlContext.cpp @@ -1,12 +1,10 @@ #include "SdlContext.h" -#include "SDL_hints.h" -#include "SDL_rect.h" -#include "SDL_stdinc.h" + #include "api/Sprite.h" +#include "api/Texture.h" #include "api/Transform.h" -#include "facade/Texture.h" #include "util/log.h" #include #include @@ -15,7 +13,6 @@ #include #include #include -#include using namespace crepe; @@ -24,7 +21,7 @@ SdlContext & SdlContext::get_instance() { return instance; } -void SdlContext::handleEvents(bool & running) { +void SdlContext::handle_events(bool & running) { SDL_Event event; while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT) { @@ -32,32 +29,25 @@ void SdlContext::handleEvents(bool & running) { } } } -void SdlContext::clearScreen() { SDL_RenderClear(this->m_game_renderer); } -void SdlContext::presentScreen() { SDL_RenderPresent(this->m_game_renderer); } +SdlContext::~SdlContext() { + dbg_trace(); -void SdlContext::draw(const api::Sprite & sprite, - const api::Transform & transform) { - static SDL_RendererFlip renderFlip - = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flipX) - | (SDL_FLIP_VERTICAL * sprite.flip.flipY)); + if (m_game_renderer) SDL_DestroyRenderer(m_game_renderer); - // needs maybe camera for position - static SDL_Rect dstrect = { - .x = static_cast(transform.position.x), - .y = static_cast(transform.position.y), - .w - = static_cast(sprite.sprite_image->get_rect().w * transform.scale), - .h - = static_cast(sprite.sprite_image->get_rect().h * transform.scale), - }; + if (m_game_window) { + SDL_DestroyWindow(m_game_window); + } - SDL_RenderCopyEx(this->m_game_renderer, sprite.sprite_image->get_texture(), - &sprite.sprite_image->get_rect(), &dstrect, 0, NULL, - renderFlip); + IMG_Quit(); + SDL_Quit(); } +void SdlContext::clear_screen() { SDL_RenderClear(this->m_game_renderer); } + SdlContext::SdlContext() { + dbg_trace(); + if (SDL_Init(SDL_INIT_VIDEO) < 0) { std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl; @@ -80,95 +70,37 @@ SdlContext::SdlContext() { SDL_DestroyWindow(m_game_window); return; } - int imgFlags = IMG_INIT_PNG; - if (!(IMG_Init(imgFlags) & imgFlags)) { + + int img_flags = IMG_INIT_PNG; + if (!(IMG_Init(img_flags) & img_flags)) { std::cout << "SDL_image could not initialize! SDL_image Error: " << IMG_GetError() << std::endl; } +} +void SdlContext::present_screen() { SDL_RenderPresent(this->m_game_renderer); } - SDL_SetHint(SDL_HINT_RENDER_BATCHING, "1"); - SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl"); - //SDL_SetHint(SDL_HINT_RENDER_OPENGL_SHADERS, "1"); - SDL_SetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION, "X"); - - - - - const char * hint = SDL_GetHint(SDL_HINT_RENDER_BATCHING); - if (hint != NULL) { - std::cout << "SDL_HINT_RENDER_BATCHING: " << hint << std::endl; - } - - hint = SDL_GetHint(SDL_HINT_RENDER_DRIVER); - if (hint != NULL) { - std::cout << "SDL_HINT_RENDER_DRIVER: " << hint << std::endl; - } - - hint = SDL_GetHint(SDL_HINT_RENDER_OPENGL_SHADERS); - if (hint != NULL) { - std::cout << "SDL_HINT_RENDER_OPENGL_SHADERS: " << hint << std::endl; - } - - hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY); - if (hint != NULL) { - std::cout << "SDL_HINT_RENDER_SCALE_QUALITY: " << hint << std::endl; - } - - hint = SDL_GetHint(SDL_HINT_RENDER_VSYNC); - if (hint != NULL) { - std::cout << "SDL_HINT_RENDER_VSYNC: " << hint << std::endl; - } - - hint = SDL_GetHint(SDL_HINT_TIMER_RESOLUTION); - if (hint != NULL) { - std::cout << "SDL_HINT_TIMER_RESOLUTION: " << hint << std::endl; - } - - hint = SDL_GetHint(SDL_HINT_VIDEO_WINDOW_SHARE_PIXEL_FORMAT); - if (hint != NULL) { - std::cout << "SDL_HINT_VIDEO_WINDOW_SHARE_PIXEL_FORMAT: " << hint - << std::endl; - } - - hint = SDL_GetHint(SDL_HINT_WINDOW_FRAME_USABLE_WHILE_CURSOR_HIDDEN); - if (hint != NULL) { - std::cout << "SDL_HINT_WINDOW_FRAME_USABLE_WHILE_CURSOR_HIDDEN: " - << hint << std::endl; - } - - hint = SDL_GetHint(SDL_HINT_RENDER_LOGICAL_SIZE_MODE); - if (hint != NULL) { - std::cout << "SDL_HINT_RENDER_LOGICAL_SIZE_MODE: " << hint << std::endl; - } - - hint = SDL_GetHint(SDL_HINT_VIDEO_DOUBLE_BUFFER); - if (hint != NULL) { - std::cout << "SDL_HINT_VIDEO_DOUBLE_BUFFER: " << hint << std::endl; - } - - hint = SDL_GetHint(SDL_HINT_OPENGL_ES_DRIVER); - if (hint != NULL) { - std::cout << "SDL_HINT_OPENGL_ES_DRIVER: " << hint << std::endl; - } +void SdlContext::draw(const api::Sprite & sprite, + const api::Transform & transform) { - hint = SDL_GetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION); - if (hint != NULL) { - std::cout << "SDL_HINT_FRAMEBUFFER_ACCELERATION: " << hint << std::endl; - } + static SDL_RendererFlip render_flip + = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) + | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); - std::cout << "HALLO " << std::endl; -} - -SdlContext::~SdlContext() { - if (m_game_renderer) SDL_DestroyRenderer(m_game_renderer); + int w, h; + SDL_QueryTexture(sprite.sprite_image->m_texture, NULL, NULL, &w, &h); + // needs maybe camera for position + SDL_Rect dstrect = { + .x = static_cast(transform.position.x), + .y = static_cast(transform.position.y), + .w = static_cast(w * transform.scale), + .h = static_cast(h * transform.scale), + }; - if (m_game_window) { - SDL_DestroyWindow(m_game_window); - } - IMG_Quit(); - SDL_Quit(); + SDL_RenderCopyEx(this->m_game_renderer, sprite.sprite_image->m_texture, + NULL, &dstrect, 0, NULL, render_flip); } +/* SDL_Texture * SdlContext::setTextureFromPath(const char * path, SDL_Rect & clip, const int row, const int col) { dbg_trace(); @@ -178,7 +110,8 @@ SDL_Texture * SdlContext::setTextureFromPath(const char * path, SDL_Rect & clip, std::cerr << "Error surface " << IMG_GetError << std::endl; } - clip.w = tmp->w / col; + clip. + w = tmp->w / col; clip.h = tmp->h / row; SDL_Texture * CreatedTexture @@ -192,22 +125,23 @@ SDL_Texture * SdlContext::setTextureFromPath(const char * path, SDL_Rect & clip, return CreatedTexture; } +*/ -SDL_Texture * SdlContext::setTextureFromPath(const char * path) { +SDL_Texture * SdlContext::texture_from_path(const char * path) { dbg_trace(); - + SDL_Surface * tmp = IMG_Load(path); if (!tmp) { std::cerr << "Error surface " << IMG_GetError << std::endl; } - SDL_Texture * CreatedTexture + SDL_Texture * created_texture = SDL_CreateTextureFromSurface(m_game_renderer, tmp); - if (!CreatedTexture) { + if (!created_texture) { std::cerr << "Error could not create texture " << IMG_GetError << std::endl; } SDL_FreeSurface(tmp); - return CreatedTexture; + return created_texture; } diff --git a/src/crepe/SdlContext.h b/src/crepe/SdlContext.h index c8f1304..a6c85f1 100644 --- a/src/crepe/SdlContext.h +++ b/src/crepe/SdlContext.h @@ -1,43 +1,50 @@ #pragma once -#include "SDL_rect.h" +#include "RenderSystem.h" #include "api/Sprite.h" #include "api/Transform.h" #include #include -namespace crepe { +namespace crepe::api { +class Texture; +} +namespace crepe { class SdlContext { public: - - void handleEvents(bool& running); - void clearScreen(); - void presentScreen(); - void draw(const api::Sprite&, const api::Transform&); - // singleton static SdlContext & get_instance(); - SDL_Texture* setTextureFromPath(const char*); - SDL_Texture* setTextureFromPath(const char*, SDL_Rect& clip, const int row, const int col); - -private: - SdlContext(); - virtual ~SdlContext(); - SdlContext(const SdlContext &) = delete; SdlContext(SdlContext &&) = delete; SdlContext & operator=(const SdlContext &) = delete; SdlContext & operator=(SdlContext &&) = delete; + //TODO decide events wouter? private: + void handle_events(bool & running); - SDL_Window* m_game_window; - SDL_Renderer* m_game_renderer; -}; +private: + SdlContext(); + virtual ~SdlContext(); + +private: + friend class api::Texture; + SDL_Texture * texture_from_path(const char *); + //SDL_Texture* setTextureFromPath(const char*, SDL_Rect& clip, const int row, const int col); -} // +private: + friend class RenderSystem; + void draw(const api::Sprite &, const api::Transform &); + void clear_screen(); + void present_screen(); + +private: + SDL_Window * m_game_window; + SDL_Renderer * m_game_renderer; +}; +} // namespace crepe diff --git a/src/crepe/SoundContext.cpp b/src/crepe/SoundContext.cpp new file mode 100644 index 0000000..72047d2 --- /dev/null +++ b/src/crepe/SoundContext.cpp @@ -0,0 +1,20 @@ +#include "util/log.h" + +#include "SoundContext.h" + +using namespace crepe; + +SoundContext & SoundContext::get_instance() { + static SoundContext instance; + return instance; +} + +SoundContext::SoundContext() { + dbg_trace(); + engine.init(); +} + +SoundContext::~SoundContext() { + dbg_trace(); + engine.deinit(); +} diff --git a/src/crepe/SoundContext.h b/src/crepe/SoundContext.h new file mode 100644 index 0000000..d3123d2 --- /dev/null +++ b/src/crepe/SoundContext.h @@ -0,0 +1,26 @@ +#pragma once + +#include + +#include "Sound.h" + +namespace crepe { + +class SoundContext { +private: + SoundContext(); + virtual ~SoundContext(); + + // singleton + static SoundContext & get_instance(); + SoundContext(const SoundContext &) = delete; + SoundContext(SoundContext &&) = delete; + SoundContext & operator=(const SoundContext &) = delete; + SoundContext & operator=(SoundContext &&) = delete; + +private: + SoLoud::Soloud engine; + friend class Sound; +}; + +} // namespace crepe diff --git a/src/crepe/Sprite.cpp b/src/crepe/Sprite.cpp deleted file mode 100644 index a5a5e68..0000000 --- a/src/crepe/Sprite.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include - -#include "Sprite.h" - -using namespace crepe; -using namespace std; - -Sprite::Sprite(string path) : path(path) {} diff --git a/src/crepe/Sprite.h b/src/crepe/Sprite.h deleted file mode 100644 index 143e702..0000000 --- a/src/crepe/Sprite.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include - -#include "Component.h" - -namespace crepe { - -class Sprite : public Component { -public: - Sprite(std::string path); - - std::string path; -}; - -} // namespace crepe diff --git a/src/crepe/api/AudioSource.cpp b/src/crepe/api/AudioSource.cpp index a5b6d6a..90c1b07 100644 --- a/src/crepe/api/AudioSource.cpp +++ b/src/crepe/api/AudioSource.cpp @@ -1,6 +1,6 @@ #include "AudioSource.h" -#include "facade/Sound.h" +#include "Sound.h" #include using namespace crepe::api; diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 9a02580..b046301 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -2,10 +2,19 @@ target_sources(crepe PUBLIC # AudioSource.cpp BehaviorScript.cpp Script.cpp + Color.cpp + Texture.cpp + Sprite.cpp + Transform.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES # AudioSource.h BehaviorScript.h Script.h + Point.h + Transform.h + Color.h + Sprite.h + Texture.h ) diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp new file mode 100644 index 0000000..b0c0971 --- /dev/null +++ b/src/crepe/api/Sprite.cpp @@ -0,0 +1,18 @@ + + +#include "Sprite.h" +#include "api/Texture.h" +#include "util/log.h" +#include +#include + +using namespace std; +using namespace crepe; +using namespace crepe::api; + +Sprite::Sprite(unique_ptr image, const Color & color, + const flip_settings & flip) : color(color), flip(flip), sprite_image(std::move(image)) { + dbg_trace(); +} + +Sprite::~Sprite() { dbg_trace(); } diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index 84eeb83..3dd9b4a 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -2,26 +2,28 @@ #include "Component.h" #include "api/Color.h" -#include "facade/Texture.h" +#include "api/Texture.h" +#include #include +#include namespace crepe::api { struct flip_settings{ - bool flipX : 1; - bool flipY : 1; + bool flip_x: 1; + bool flip_y : 1; }; class Sprite : public Component { public: - Sprite(crepe::Texture& image, const Color& color, const flip_settings& flip ) : sprite_image(&image), color(color), flip(flip){} - crepe::Texture* sprite_image; + Sprite(std::unique_ptr image, const Color& color, const flip_settings& flip ); + ~Sprite(); + std::unique_ptr sprite_image; Color color; flip_settings flip; - uint8_t sortingLayer; - uint8_t orderInLayer; - + uint8_t sorting_in_layer; + uint8_t order_in_layer; }; diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp index b4e3aa8..2d170c3 100644 --- a/src/crepe/api/Texture.cpp +++ b/src/crepe/api/Texture.cpp @@ -1,39 +1,34 @@ +#include "Asset.h" +#include "SdlContext.h" #include "util/log.h" #include "Texture.h" -#include "SdlContext.h" #include +#include +#include -using namespace crepe; +using namespace crepe::api; -Texture::Texture(std::unique_ptr res) { +Texture::Texture(std::unique_ptr res) { dbg_trace(); this->load(std::move(res)); } Texture::Texture(const char * src) { dbg_trace(); - this->load(std::make_unique(src)); + this->load(std::make_unique(src)); } -Texture::~Texture(){ +Texture::~Texture() { dbg_trace(); - if(this->m_texture){ + if (this->m_texture != nullptr) { SDL_DestroyTexture(m_texture); } } -void Texture::load(std::unique_ptr res) { - dbg_trace(); - SdlContext& ctx = SdlContext::get_instance(); - m_texture = ctx.setTextureFromPath(res->canonical(), srcrect, 1, 1); -} - -SDL_Texture* Texture::get_texture() const{ - return m_texture; -} +void Texture::load(std::unique_ptr res) { + SdlContext & ctx = SdlContext::get_instance(); + m_texture = ctx.texture_from_path(res->canonical()); -SDL_Rect& Texture::get_rect() { - return srcrect; } diff --git a/src/crepe/api/Texture.h b/src/crepe/api/Texture.h index db2f1f9..b376b44 100644 --- a/src/crepe/api/Texture.h +++ b/src/crepe/api/Texture.h @@ -1,31 +1,32 @@ #pragma once -#include "SDL_rect.h" -#include "api/baseResource.h" -#include "api/Resource.h" +#include "Asset.h" #include #include -namespace crepe { +namespace crepe { + class SdlContext; +} +namespace crepe::api { -class Texture : public api::BaseResource{ +class Texture { public: Texture(const char * src); - Texture(std::unique_ptr res); + Texture(std::unique_ptr res); ~Texture(); - SDL_Texture* get_texture() const; - SDL_Rect& get_rect() ; + private: - void load(std::unique_ptr res); + void load(std::unique_ptr res); + private: - SDL_Texture* m_texture; - SDL_Rect srcrect; + SDL_Texture * m_texture = nullptr; + + friend class crepe::SdlContext; }; } // namespace crepe - diff --git a/src/crepe/api/Transform.cpp b/src/crepe/api/Transform.cpp new file mode 100644 index 0000000..c83461f --- /dev/null +++ b/src/crepe/api/Transform.cpp @@ -0,0 +1,13 @@ + + +#include "Transform.h" +#include "api/Point.h" +#include "util/log.h" + +using namespace crepe::api; + +Transform::Transform(Point & point, double rot, double scale) + : position(point), rotation(rot), scale(scale) { + dbg_trace(); +} +Transform::~Transform() { dbg_trace(); } diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h index d4dfafc..a34ebb1 100644 --- a/src/crepe/api/Transform.h +++ b/src/crepe/api/Transform.h @@ -1,11 +1,13 @@ #pragma once -#include "api/Component.h" +#include "Component.h" #include "api/Point.h" namespace crepe::api { class Transform : public Component { public: + Transform(Point&, double, double); + ~Transform(); Point position; // Translation (shift) double rotation; // Rotation, in radians double scale; // Multiplication factoh diff --git a/src/crepe/renderSystem.cpp b/src/crepe/renderSystem.cpp deleted file mode 100644 index a06aeba..0000000 --- a/src/crepe/renderSystem.cpp +++ /dev/null @@ -1,37 +0,0 @@ - - - -#include "renderSystem.h" -#include - -#include "api/Color.h" -#include "api/Sprite.h" -#include "api/Transform.h" -#include "facade/SdlContext.h" -#include "facade/Texture.h" - -using namespace crepe::api; - - -static crepe::Texture player("../asset/texture/img.png"); - - -void RenderSystem::render(){ - - Sprite sprite(player, Color::get_red(), {1,1}); - Transform transform ={ - .position = {0,0}, - .rotation = 0, - .scale = 1, - }; - - // this will get changed to ecs getter of componets - crepe::SdlContext& ctx = crepe::SdlContext::get_instance(); - - ctx.draw(sprite, transform); - /* - for(const auto& S : test_objects){ - ctx.draw(S, const api::Transform &) - } - */ -} diff --git a/src/crepe/renderSystem.h b/src/crepe/renderSystem.h deleted file mode 100644 index 9011b30..0000000 --- a/src/crepe/renderSystem.h +++ /dev/null @@ -1,13 +0,0 @@ - -#pragma once - - - -class RenderSystem { - -public: - RenderSystem() = default; - ~RenderSystem() = default; - - void render(); -}; diff --git a/src/dummy_rendering.cpp b/src/dummy_rendering.cpp deleted file mode 100644 index e00ab7f..0000000 --- a/src/dummy_rendering.cpp +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - -#include "api/game.h" -#include -int main(){ - - - - Engine engine(800,600); - - // engine.loop(); - -} diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 6df4ce7..9d82827 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -15,4 +15,5 @@ endfunction() add_example(audio_internal) add_example(components_internal) add_example(script) +add_example(rendering) diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp new file mode 100644 index 0000000..9ca12b7 --- /dev/null +++ b/src/example/rendering.cpp @@ -0,0 +1,52 @@ + + + +#include +#include +#include +#include + + +#include +#include +#include +#include +#include + + +#include +#include + + +using namespace std; +using namespace crepe; +using namespace crepe::api; + +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(point, 0 ,1); + obj.add_component(make_unique("../asset/texture/img.png"),color, flip_settings{false,false}); + + auto& sys = crepe::RenderSystem::get_instance(); + + // scene example + auto start = std::chrono::steady_clock::now(); + while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) { + sys.update(); + } + +} + -- cgit v1.2.3 From eaa05e7a981b0f581f5393882e4753d9294a3dba Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 23 Oct 2024 13:49:20 +0200 Subject: rendering and asset_manager poc : --- src/build.sh | 1 + src/crepe/SdlContext.cpp | 12 ++++--- src/crepe/SdlContext.h | 4 +-- src/crepe/api/AssetManager.cpp | 22 ++++++------ src/crepe/api/AssetManager.h | 47 +++++++++++------------- src/crepe/api/CMakeLists.txt | 4 ++- src/crepe/api/Sprite.cpp | 4 +-- src/crepe/api/Sprite.h | 4 +-- src/crepe/api/Texture.cpp | 2 -- src/dummy_resource_manager.cpp | 38 -------------------- src/example/CMakeLists.txt | 1 + src/example/asset_manager.cpp | 35 ++++++++++++++++++ src/example/rendering.cpp | 82 ++++++++++++++++++++++++++---------------- 13 files changed, 135 insertions(+), 121 deletions(-) delete mode 100644 src/dummy_resource_manager.cpp create mode 100644 src/example/asset_manager.cpp (limited to 'src/build.sh') 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 #include #include +#include #include #include @@ -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(w * transform.scale), .h = static_cast(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 -#include +#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 #include #include #include #include -#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> 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 - T* Load(const std::string& file_path){ - - if (m_resources.find(file_path) != m_resources.end()) { - return static_cast(m_resources[file_path].get()); - } + template + std::shared_ptr cache(const std::string& file_path, bool reload = false){ + auto it = asset_cache.find(file_path); - auto resource = std::make_unique(file_path.c_str()); - if (resource) { - m_resources[file_path] = std::move(resource); - return static_cast(m_resources[file_path].get() ); + if (!reload && it != asset_cache.end()) { + return std::any_cast>(it->second); } - return nullptr; - } + std::shared_ptr new_asset = std::make_shared(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 image, const Color & color, - const flip_settings & flip) : color(color), flip(flip), sprite_image(std::move(image)) { +Sprite::Sprite(shared_ptr 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 image, const Color& color, const flip_settings& flip ); + Sprite(std::shared_ptr image, const Color& color, const flip_settings& flip ); ~Sprite(); - std::unique_ptr sprite_image; + std::shared_ptr 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 -#include -#include 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 - -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("../asset/texture/img.png"); - - auto img = Texture("../asset/texture/img.png"); - - auto SS = api::Spritesheet("../asset/spritesheet/spritesheet_test.png", 1 , 4); - - std::vector t = {test}; - std::vector 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 +#include +#include + +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("../mwe/audio/bgm.ogg"); + auto sfx1 = mgr.cache("../mwe/audio/sfx1.wav"); + auto sfx2 = mgr.cache("../mwe/audio/sfx2.wav"); + + auto img = mgr.cache("../asset/texture/img.png"); + auto img1 = mgr.cache("../asset/texture/second.png"); + } + + { + auto bgm = mgr.cache("../mwe/audio/bgm.ogg"); + auto sfx1 = mgr.cache("../mwe/audio/sfx1.wav"); + auto sfx2 = mgr.cache("../mwe/audio/sfx2.wav"); + + auto img = mgr.cache("../asset/texture/img.png"); + auto img1 = mgr.cache("../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 -#include #include +#include #include - +#include #include +#include #include #include #include -#include +#include - -#include #include - +#include 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(point, 0 ,1); - obj.add_component(make_unique("../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(point, 1, 1); + obj.add_component( + make_shared("../asset/texture/img.png"), color, + flip_settings{true, true}); + } + { + Color color(0, 0, 0, 0); + Point point = { + .x = 500, + .y = 0, + }; + obj.add_component(point, 0, 0.1); + auto img = mgr.cache("../asset/texture/second.png"); + obj.add_component(img, color, + flip_settings{true, true}); + } + { + Color color(0, 0, 0, 0); + Point point = { + .x = 800, + .y = 0, + }; + //obj.add_component(point, 0, 0.1); + auto img = mgr.cache("../asset/texture/second.png"); + obj.add_component(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(); - } - + } } - -- cgit v1.2.3