From 9037aca03bfa4312794a6954752628381256f777 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Tue, 22 Oct 2024 12:15:03 +0200 Subject: merged further and changed to standard --- src/crepe/CMakeLists.txt | 3 - src/crepe/SdlContext.cpp | 213 +++++++++++++++++++++++++++ src/crepe/SdlContext.h | 43 ++++++ src/crepe/api/AssetManager.cpp | 25 ++++ src/crepe/api/AssetManager.h | 57 +++++++ src/crepe/api/CMakeLists.txt | 2 + src/crepe/api/Texture.cpp | 39 +++++ src/crepe/api/Texture.h | 31 ++++ src/crepe/api/baseResource.h | 11 -- src/crepe/api/game.cpp | 30 ---- src/crepe/api/game.h | 17 --- src/crepe/api/map_asset.cpp | 12 -- src/crepe/api/map_asset.h | 14 -- src/crepe/api/resource_manager.cpp | 25 ---- src/crepe/api/resource_manager.h | 57 ------- src/crepe/api/spritesheet.cpp | 38 ----- src/crepe/api/spritesheet.h | 34 ----- src/crepe/core/CMakeLists.txt | 8 - src/crepe/core/renderSystem.cpp | 37 ----- src/crepe/core/renderSystem.h | 13 -- src/crepe/fabricator/CMakeLists.txt | 8 - src/crepe/fabricator/resource_fabricator.cpp | 26 ---- src/crepe/fabricator/resource_fabricator.h | 29 ---- src/crepe/facade/CMakeLists.txt | 13 -- src/crepe/facade/SdlContext.cpp | 213 --------------------------- src/crepe/facade/SdlContext.h | 43 ------ src/crepe/facade/Sound.cpp | 60 -------- src/crepe/facade/Sound.h | 82 ----------- src/crepe/facade/SoundContext.cpp | 20 --- src/crepe/facade/SoundContext.h | 26 ---- src/crepe/facade/Texture.cpp | 39 ----- src/crepe/facade/Texture.h | 31 ---- src/crepe/facade/touch | 0 src/crepe/renderSystem.cpp | 37 +++++ src/crepe/renderSystem.h | 13 ++ 35 files changed, 460 insertions(+), 889 deletions(-) create mode 100644 src/crepe/SdlContext.cpp create mode 100644 src/crepe/SdlContext.h create mode 100644 src/crepe/api/AssetManager.cpp create mode 100644 src/crepe/api/AssetManager.h create mode 100644 src/crepe/api/Texture.cpp create mode 100644 src/crepe/api/Texture.h delete mode 100644 src/crepe/api/baseResource.h delete mode 100644 src/crepe/api/game.cpp delete mode 100644 src/crepe/api/game.h delete mode 100644 src/crepe/api/map_asset.cpp delete mode 100644 src/crepe/api/map_asset.h delete mode 100644 src/crepe/api/resource_manager.cpp delete mode 100644 src/crepe/api/resource_manager.h delete mode 100644 src/crepe/api/spritesheet.cpp delete mode 100644 src/crepe/api/spritesheet.h delete mode 100644 src/crepe/core/CMakeLists.txt delete mode 100644 src/crepe/core/renderSystem.cpp delete mode 100644 src/crepe/core/renderSystem.h delete mode 100644 src/crepe/fabricator/CMakeLists.txt delete mode 100644 src/crepe/fabricator/resource_fabricator.cpp delete mode 100644 src/crepe/fabricator/resource_fabricator.h delete mode 100644 src/crepe/facade/CMakeLists.txt delete mode 100644 src/crepe/facade/SdlContext.cpp delete mode 100644 src/crepe/facade/SdlContext.h delete mode 100644 src/crepe/facade/Sound.cpp delete mode 100644 src/crepe/facade/Sound.h delete mode 100644 src/crepe/facade/SoundContext.cpp delete mode 100644 src/crepe/facade/SoundContext.h delete mode 100644 src/crepe/facade/Texture.cpp delete mode 100644 src/crepe/facade/Texture.h delete mode 100644 src/crepe/facade/touch create mode 100644 src/crepe/renderSystem.cpp create mode 100644 src/crepe/renderSystem.h diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index addb9dd..d85aef0 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -30,7 +30,4 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES add_subdirectory(api) add_subdirectory(util) -add_subdirectory(fabricator) -add_subdirectory(facade) -add_subdirectory(core) diff --git a/src/crepe/SdlContext.cpp b/src/crepe/SdlContext.cpp new file mode 100644 index 0000000..44d1bdf --- /dev/null +++ b/src/crepe/SdlContext.cpp @@ -0,0 +1,213 @@ + + +#include "SdlContext.h" +#include "SDL_hints.h" +#include "SDL_rect.h" +#include "SDL_stdinc.h" +#include "api/Sprite.h" +#include "api/Transform.h" +#include "facade/Texture.h" +#include "util/log.h" +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace crepe; + +SdlContext & SdlContext::get_instance() { + static SdlContext instance; + return instance; +} + +void SdlContext::handleEvents(bool & running) { + SDL_Event event; + while (SDL_PollEvent(&event)) { + if (event.type == SDL_QUIT) { + running = false; + } + } +} +void SdlContext::clearScreen() { SDL_RenderClear(this->m_game_renderer); } + +void SdlContext::presentScreen() { SDL_RenderPresent(this->m_game_renderer); } + +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)); + + // 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), + }; + + SDL_RenderCopyEx(this->m_game_renderer, sprite.sprite_image->get_texture(), + &sprite.sprite_image->get_rect(), &dstrect, 0, NULL, + renderFlip); +} + +SdlContext::SdlContext() { + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() + << std::endl; + return; + } + + m_game_window = SDL_CreateWindow( + "Crepe Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, + 1920, 1080, SDL_WINDOW_SHOWN); + if (!m_game_window) { + std::cerr << "Window could not be created! SDL_Error: " + << SDL_GetError() << std::endl; + } + + m_game_renderer + = SDL_CreateRenderer(m_game_window, -1, SDL_RENDERER_ACCELERATED); + if (!m_game_renderer) { + std::cerr << "Renderer could not be created! SDL_Error: " + << SDL_GetError() << std::endl; + SDL_DestroyWindow(m_game_window); + return; + } + int imgFlags = IMG_INIT_PNG; + if (!(IMG_Init(imgFlags) & imgFlags)) { + std::cout << "SDL_image could not initialize! SDL_image Error: " + << IMG_GetError() << std::endl; + } + + 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; + } + + hint = SDL_GetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION); + if (hint != NULL) { + std::cout << "SDL_HINT_FRAMEBUFFER_ACCELERATION: " << hint << std::endl; + } + + std::cout << "HALLO " << std::endl; +} + +SdlContext::~SdlContext() { + if (m_game_renderer) SDL_DestroyRenderer(m_game_renderer); + + if (m_game_window) { + SDL_DestroyWindow(m_game_window); + } + IMG_Quit(); + SDL_Quit(); +} + +SDL_Texture * SdlContext::setTextureFromPath(const char * path, SDL_Rect & clip, + const int row, const int col) { + dbg_trace(); + + SDL_Surface * tmp = IMG_Load(path); + if (!tmp) { + std::cerr << "Error surface " << IMG_GetError << std::endl; + } + + clip.w = tmp->w / col; + clip.h = tmp->h / row; + + SDL_Texture * CreatedTexture + = SDL_CreateTextureFromSurface(m_game_renderer, tmp); + + if (!CreatedTexture) { + std::cerr << "Error could not create texture " << IMG_GetError + << std::endl; + } + SDL_FreeSurface(tmp); + + return CreatedTexture; +} + +SDL_Texture * SdlContext::setTextureFromPath(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_CreateTextureFromSurface(m_game_renderer, tmp); + + if (!CreatedTexture) { + std::cerr << "Error could not create texture " << IMG_GetError + << std::endl; + } + SDL_FreeSurface(tmp); + + return CreatedTexture; +} diff --git a/src/crepe/SdlContext.h b/src/crepe/SdlContext.h new file mode 100644 index 0000000..c8f1304 --- /dev/null +++ b/src/crepe/SdlContext.h @@ -0,0 +1,43 @@ +#pragma once + +#include "SDL_rect.h" +#include "api/Sprite.h" +#include "api/Transform.h" +#include +#include + +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; + + +private: + + SDL_Window* m_game_window; + SDL_Renderer* m_game_renderer; +}; + +} // + diff --git a/src/crepe/api/AssetManager.cpp b/src/crepe/api/AssetManager.cpp new file mode 100644 index 0000000..0ecdae5 --- /dev/null +++ b/src/crepe/api/AssetManager.cpp @@ -0,0 +1,25 @@ + + +#include "resource_manager.h" +#include +#include + +using namespace crepe::api; + +ResourceManager& ResourceManager::get_instance(){ + static ResourceManager instance; + return instance; +} + + +ResourceManager::~ResourceManager(){ + m_resources.clear(); +} + + +void ResourceManager::Unload(const std::string& file_path){ + if(m_resources.find(file_path) != m_resources.end()){ + m_resources.erase(file_path); + } +} + diff --git a/src/crepe/api/AssetManager.h b/src/crepe/api/AssetManager.h new file mode 100644 index 0000000..a646d95 --- /dev/null +++ b/src/crepe/api/AssetManager.h @@ -0,0 +1,57 @@ +#pragma once + + + +#include +#include +#include +#include + +#include "api/baseResource.h" + + +namespace crepe::api{ + +class ResourceManager{ + + +private: + + std::unordered_map< std::string, std::unique_ptr> m_resources; + + +protected: + ResourceManager() = default; + ~ResourceManager(); + +public: + ResourceManager(const ResourceManager &) = delete; + ResourceManager(ResourceManager &&) = delete; + ResourceManager &operator=(const ResourceManager &) = delete; + ResourceManager &operator=(ResourceManager &&) = delete; + + static ResourceManager& 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()); + } + + 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() ); + } + + return nullptr; + } + + void Unload(const std::string& file_path); + +}; +} diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index cecc2f1..9a02580 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -1,9 +1,11 @@ target_sources(crepe PUBLIC # AudioSource.cpp BehaviorScript.cpp + Script.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES # AudioSource.h BehaviorScript.h + Script.h ) diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp new file mode 100644 index 0000000..b4e3aa8 --- /dev/null +++ b/src/crepe/api/Texture.cpp @@ -0,0 +1,39 @@ + + +#include "util/log.h" + +#include "Texture.h" +#include "SdlContext.h" +#include + +using namespace crepe; + +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)); +} + +Texture::~Texture(){ + dbg_trace(); + if(this->m_texture){ + 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; +} + +SDL_Rect& Texture::get_rect() { + return srcrect; +} diff --git a/src/crepe/api/Texture.h b/src/crepe/api/Texture.h new file mode 100644 index 0000000..db2f1f9 --- /dev/null +++ b/src/crepe/api/Texture.h @@ -0,0 +1,31 @@ +#pragma once + +#include "SDL_rect.h" +#include "api/baseResource.h" +#include "api/Resource.h" +#include +#include + +namespace crepe { + + + +class Texture : public api::BaseResource{ + +public: + Texture(const char * src); + Texture(std::unique_ptr res); + ~Texture(); + + SDL_Texture* get_texture() const; + SDL_Rect& get_rect() ; +private: + void load(std::unique_ptr res); + +private: + SDL_Texture* m_texture; + SDL_Rect srcrect; +}; + +} // namespace crepe + diff --git a/src/crepe/api/baseResource.h b/src/crepe/api/baseResource.h deleted file mode 100644 index 2513f4d..0000000 --- a/src/crepe/api/baseResource.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -namespace crepe::api { - -class BaseResource { -public: - virtual ~BaseResource() = default; -}; - -} - diff --git a/src/crepe/api/game.cpp b/src/crepe/api/game.cpp deleted file mode 100644 index 01920a9..0000000 --- a/src/crepe/api/game.cpp +++ /dev/null @@ -1,30 +0,0 @@ - - - -#include "game.h" -#include "core/renderSystem.h" -#include "facade/SdlContext.h" - - -Engine::Engine(int windowHeight, int window_with){ - crepe::SdlContext& ctx = crepe::SdlContext::get_instance(); -} - - - -void Engine::loop() { - - bool running = true; - crepe::SdlContext& ctx = crepe::SdlContext::get_instance(); - RenderSystem rendering; - - while (running) { - ctx.handleEvents(running); - - ctx.clearScreen(); - - rendering.render(); - - ctx.presentScreen(); - } -} diff --git a/src/crepe/api/game.h b/src/crepe/api/game.h deleted file mode 100644 index 64027fa..0000000 --- a/src/crepe/api/game.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - - - -class Engine{ - -public: - Engine(int windowWith, int windowHeight); - ~Engine() = default; - - void loop(); - - -private: - int window_height; - int window_width; -}; diff --git a/src/crepe/api/map_asset.cpp b/src/crepe/api/map_asset.cpp deleted file mode 100644 index bbabe2b..0000000 --- a/src/crepe/api/map_asset.cpp +++ /dev/null @@ -1,12 +0,0 @@ - - - - -#include "map_asset.h" - -Map::Map(const std::string& content){ - this->m_content = content; -} - -Map::~Map(){ -} diff --git a/src/crepe/api/map_asset.h b/src/crepe/api/map_asset.h deleted file mode 100644 index a4f3df7..0000000 --- a/src/crepe/api/map_asset.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -#include "Resource.h" -#include - - -using namespace crepe::api; - -class Map : public Resource { - -public: - Map(const std::string& ); - ~Map(); -}; diff --git a/src/crepe/api/resource_manager.cpp b/src/crepe/api/resource_manager.cpp deleted file mode 100644 index 0ecdae5..0000000 --- a/src/crepe/api/resource_manager.cpp +++ /dev/null @@ -1,25 +0,0 @@ - - -#include "resource_manager.h" -#include -#include - -using namespace crepe::api; - -ResourceManager& ResourceManager::get_instance(){ - static ResourceManager instance; - return instance; -} - - -ResourceManager::~ResourceManager(){ - m_resources.clear(); -} - - -void ResourceManager::Unload(const std::string& file_path){ - if(m_resources.find(file_path) != m_resources.end()){ - m_resources.erase(file_path); - } -} - diff --git a/src/crepe/api/resource_manager.h b/src/crepe/api/resource_manager.h deleted file mode 100644 index a646d95..0000000 --- a/src/crepe/api/resource_manager.h +++ /dev/null @@ -1,57 +0,0 @@ -#pragma once - - - -#include -#include -#include -#include - -#include "api/baseResource.h" - - -namespace crepe::api{ - -class ResourceManager{ - - -private: - - std::unordered_map< std::string, std::unique_ptr> m_resources; - - -protected: - ResourceManager() = default; - ~ResourceManager(); - -public: - ResourceManager(const ResourceManager &) = delete; - ResourceManager(ResourceManager &&) = delete; - ResourceManager &operator=(const ResourceManager &) = delete; - ResourceManager &operator=(ResourceManager &&) = delete; - - static ResourceManager& 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()); - } - - 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() ); - } - - return nullptr; - } - - void Unload(const std::string& file_path); - -}; -} diff --git a/src/crepe/api/spritesheet.cpp b/src/crepe/api/spritesheet.cpp deleted file mode 100644 index 7f5da38..0000000 --- a/src/crepe/api/spritesheet.cpp +++ /dev/null @@ -1,38 +0,0 @@ - - -#include "spritesheet.h" -#include "SDL_rect.h" -#include "SDL_render.h" -#include "api/Resource.h" -#include "facade/SdlContext.h" -#include - - -using namespace crepe::api; - -Spritesheet::Spritesheet(const char* src, const int row, const int col){ - this->load(std::make_unique(src), row, col); -} - -Spritesheet::Spritesheet(std::unique_ptr res, const int row, const int col){ - this->load(std::move(res), row, col); -} - -Spritesheet::~Spritesheet(){ - - if (this->m_spritesheet) { - SDL_DestroyTexture(this->m_spritesheet); - } -} - -void Spritesheet::select_sprite(const int x, const int y){ - m_clip.x = x * m_clip.w; - m_clip.y = y * m_clip.h; -} - -void Spritesheet::load(std::unique_ptr res, const int row, const int col){ - auto& ctx = SdlContext::get_instance(); - - this->m_spritesheet = ctx.setTextureFromPath(res->canonical(), this->m_clip, row, col); -} - diff --git a/src/crepe/api/spritesheet.h b/src/crepe/api/spritesheet.h deleted file mode 100644 index 503dcef..0000000 --- a/src/crepe/api/spritesheet.h +++ /dev/null @@ -1,34 +0,0 @@ -#pragma once - - - - -#include "Resource.h" -#include "SDL_rect.h" -#include "SDL_render.h" -#include - - -namespace crepe::api { - -class Spritesheet{ - -public: - Spritesheet(const char * src, const int row , const int col); - Spritesheet(std::unique_ptr res, const int row, const int col); - ~Spritesheet(); - - void select_sprite(const int x, const int y); - void draw_selected_sprite(const int x, const int y); -private: - void load(std::unique_ptr res, const int row, const int col);; - SDL_Texture* get_texture() const; - -private: - - SDL_Texture* m_spritesheet; - SDL_Rect m_clip; -}; - -} - diff --git a/src/crepe/core/CMakeLists.txt b/src/crepe/core/CMakeLists.txt deleted file mode 100644 index c44f0f6..0000000 --- a/src/crepe/core/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -target_sources(crepe PUBLIC - renderSystem.cpp -) - -target_sources(crepe PUBLIC FILE_SET HEADERS FILES - renderSystem.h -) - diff --git a/src/crepe/core/renderSystem.cpp b/src/crepe/core/renderSystem.cpp deleted file mode 100644 index a06aeba..0000000 --- a/src/crepe/core/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/core/renderSystem.h b/src/crepe/core/renderSystem.h deleted file mode 100644 index 9011b30..0000000 --- a/src/crepe/core/renderSystem.h +++ /dev/null @@ -1,13 +0,0 @@ - -#pragma once - - - -class RenderSystem { - -public: - RenderSystem() = default; - ~RenderSystem() = default; - - void render(); -}; diff --git a/src/crepe/fabricator/CMakeLists.txt b/src/crepe/fabricator/CMakeLists.txt deleted file mode 100644 index 4fd7eea..0000000 --- a/src/crepe/fabricator/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -target_sources(crepe PUBLIC - resource_fabricator.cpp -) - -target_sources(crepe PUBLIC FILE_SET HEADERS FILES - resource_fabricator.h -) - diff --git a/src/crepe/fabricator/resource_fabricator.cpp b/src/crepe/fabricator/resource_fabricator.cpp deleted file mode 100644 index 0633a40..0000000 --- a/src/crepe/fabricator/resource_fabricator.cpp +++ /dev/null @@ -1,26 +0,0 @@ - - -#include "resource_fabricator.h" -#include -#include -#include -#include - - - - -std::string ResourceFactory::convert_file_to_string(const std::string& path){ - std::ifstream file(path, std::ios::binary | std::ios::ate); - if (!file.is_open()) { - std::cerr << "Failed to open file: " << path << std::endl; - return ""; - } - - std::ifstream::pos_type fileSize = file.tellg(); - file.seekg(0, std::ios::beg); - - std::vector bytes(fileSize); - file.read(bytes.data(), fileSize); - - return std::string(bytes.begin(), bytes.end()); -} diff --git a/src/crepe/fabricator/resource_fabricator.h b/src/crepe/fabricator/resource_fabricator.h deleted file mode 100644 index 2b0030d..0000000 --- a/src/crepe/fabricator/resource_fabricator.h +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once - - - -#include "api/Resource.h" -#include -#include - - - - -class ResourceFactory { - -public: - - template - static std::unique_ptr create_resource(const std::string& file_path){ - - return std::make_unique(convert_file_to_string(file_path)); - } - -private: - static std::string convert_file_to_string(const std::string& path); - -}; - - - - diff --git a/src/crepe/facade/CMakeLists.txt b/src/crepe/facade/CMakeLists.txt deleted file mode 100644 index 1263683..0000000 --- a/src/crepe/facade/CMakeLists.txt +++ /dev/null @@ -1,13 +0,0 @@ -target_sources(crepe PUBLIC - Sound.cpp - SoundContext.cpp - Texture.cpp - SdlContext.cpp -) - -target_sources(crepe PUBLIC FILE_SET HEADERS FILES - Sound.h - SoundContext.h - Texture.h - SdlContext.h -) diff --git a/src/crepe/facade/SdlContext.cpp b/src/crepe/facade/SdlContext.cpp deleted file mode 100644 index 44d1bdf..0000000 --- a/src/crepe/facade/SdlContext.cpp +++ /dev/null @@ -1,213 +0,0 @@ - - -#include "SdlContext.h" -#include "SDL_hints.h" -#include "SDL_rect.h" -#include "SDL_stdinc.h" -#include "api/Sprite.h" -#include "api/Transform.h" -#include "facade/Texture.h" -#include "util/log.h" -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace crepe; - -SdlContext & SdlContext::get_instance() { - static SdlContext instance; - return instance; -} - -void SdlContext::handleEvents(bool & running) { - SDL_Event event; - while (SDL_PollEvent(&event)) { - if (event.type == SDL_QUIT) { - running = false; - } - } -} -void SdlContext::clearScreen() { SDL_RenderClear(this->m_game_renderer); } - -void SdlContext::presentScreen() { SDL_RenderPresent(this->m_game_renderer); } - -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)); - - // 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), - }; - - SDL_RenderCopyEx(this->m_game_renderer, sprite.sprite_image->get_texture(), - &sprite.sprite_image->get_rect(), &dstrect, 0, NULL, - renderFlip); -} - -SdlContext::SdlContext() { - if (SDL_Init(SDL_INIT_VIDEO) < 0) { - std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() - << std::endl; - return; - } - - m_game_window = SDL_CreateWindow( - "Crepe Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, - 1920, 1080, SDL_WINDOW_SHOWN); - if (!m_game_window) { - std::cerr << "Window could not be created! SDL_Error: " - << SDL_GetError() << std::endl; - } - - m_game_renderer - = SDL_CreateRenderer(m_game_window, -1, SDL_RENDERER_ACCELERATED); - if (!m_game_renderer) { - std::cerr << "Renderer could not be created! SDL_Error: " - << SDL_GetError() << std::endl; - SDL_DestroyWindow(m_game_window); - return; - } - int imgFlags = IMG_INIT_PNG; - if (!(IMG_Init(imgFlags) & imgFlags)) { - std::cout << "SDL_image could not initialize! SDL_image Error: " - << IMG_GetError() << std::endl; - } - - 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; - } - - hint = SDL_GetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION); - if (hint != NULL) { - std::cout << "SDL_HINT_FRAMEBUFFER_ACCELERATION: " << hint << std::endl; - } - - std::cout << "HALLO " << std::endl; -} - -SdlContext::~SdlContext() { - if (m_game_renderer) SDL_DestroyRenderer(m_game_renderer); - - if (m_game_window) { - SDL_DestroyWindow(m_game_window); - } - IMG_Quit(); - SDL_Quit(); -} - -SDL_Texture * SdlContext::setTextureFromPath(const char * path, SDL_Rect & clip, - const int row, const int col) { - dbg_trace(); - - SDL_Surface * tmp = IMG_Load(path); - if (!tmp) { - std::cerr << "Error surface " << IMG_GetError << std::endl; - } - - clip.w = tmp->w / col; - clip.h = tmp->h / row; - - SDL_Texture * CreatedTexture - = SDL_CreateTextureFromSurface(m_game_renderer, tmp); - - if (!CreatedTexture) { - std::cerr << "Error could not create texture " << IMG_GetError - << std::endl; - } - SDL_FreeSurface(tmp); - - return CreatedTexture; -} - -SDL_Texture * SdlContext::setTextureFromPath(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_CreateTextureFromSurface(m_game_renderer, tmp); - - if (!CreatedTexture) { - std::cerr << "Error could not create texture " << IMG_GetError - << std::endl; - } - SDL_FreeSurface(tmp); - - return CreatedTexture; -} diff --git a/src/crepe/facade/SdlContext.h b/src/crepe/facade/SdlContext.h deleted file mode 100644 index c8f1304..0000000 --- a/src/crepe/facade/SdlContext.h +++ /dev/null @@ -1,43 +0,0 @@ -#pragma once - -#include "SDL_rect.h" -#include "api/Sprite.h" -#include "api/Transform.h" -#include -#include - -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; - - -private: - - SDL_Window* m_game_window; - SDL_Renderer* m_game_renderer; -}; - -} // - diff --git a/src/crepe/facade/Sound.cpp b/src/crepe/facade/Sound.cpp deleted file mode 100644 index 64fa281..0000000 --- a/src/crepe/facade/Sound.cpp +++ /dev/null @@ -1,60 +0,0 @@ -#include "util/log.h" - -#include "Sound.h" -#include "SoundContext.h" - -using namespace crepe; - -Sound::Sound(std::unique_ptr res) { - dbg_trace(); - this->load(std::move(res)); -} - -Sound::Sound(const char * src) { - dbg_trace(); - this->load(std::make_unique(src)); -} - -void Sound::load(std::unique_ptr res) { - this->sample.load(res->canonical()); -} - -void Sound::play() { - SoundContext & ctx = SoundContext::get_instance(); - if (ctx.engine.getPause(this->handle)) { - // resume if paused - ctx.engine.setPause(this->handle, false); - } else { - // or start new sound - this->handle = ctx.engine.play(this->sample, this->volume); - ctx.engine.setLooping(this->handle, this->looping); - } -} - -void Sound::pause() { - SoundContext & ctx = SoundContext::get_instance(); - if (ctx.engine.getPause(this->handle)) return; - ctx.engine.setPause(this->handle, true); -} - -void Sound::rewind() { - SoundContext & ctx = SoundContext::get_instance(); - if (!ctx.engine.isValidVoiceHandle(this->handle)) return; - ctx.engine.seek(this->handle, 0); -} - -void Sound::set_volume(float volume) { - this->volume = volume; - - SoundContext & ctx = SoundContext::get_instance(); - if (!ctx.engine.isValidVoiceHandle(this->handle)) return; - ctx.engine.setVolume(this->handle, this->volume); -} - -void Sound::set_looping(bool looping) { - this->looping = looping; - - SoundContext & ctx = SoundContext::get_instance(); - if (!ctx.engine.isValidVoiceHandle(this->handle)) return; - ctx.engine.setLooping(this->handle, this->looping); -} diff --git a/src/crepe/facade/Sound.h b/src/crepe/facade/Sound.h deleted file mode 100644 index b11f871..0000000 --- a/src/crepe/facade/Sound.h +++ /dev/null @@ -1,82 +0,0 @@ -#pragma once - -#include -#include - -#include - -#include "Asset.h" - -namespace crepe { - -class Sound{ -public: - /** - * \brief Pause this sample - * - * Pauses this sound if it is playing, or does nothing if it is already - * paused. The playhead position is saved, such that calling \c play() after - * this function makes the sound resume. - */ - void pause(); - /** - * \brief Play this sample - * - * Resume playback if this sound is paused, or start from the beginning of - * the sample. - * - * \note This class only saves a reference to the most recent 'voice' of this - * sound. Calling \c play() while the sound is already playing causes - * multiple instances of the sample to play simultaniously. The sample - * started last is the one that is controlled afterwards. - */ - void play(); - /** - * \brief Reset playhead position - * - * Resets the playhead position so that calling \c play() after this function - * makes it play from the start of the sample. If the sound is not paused - * before calling this function, this function will stop playback. - */ - void rewind(); - /** - * \brief Set playback volume / gain - * - * \param volume Volume (0 = muted, 1 = full volume) - */ - void set_volume(float volume); - /** - * \brief Get playback volume / gain - * - * \return Volume - */ - float get_volume() const { return this->volume; } - /** - * \brief Set looping behavior for this sample - * - * \param looping Looping behavior (false = one-shot, true = loop) - */ - void set_looping(bool looping); - /** - * \brief Get looping behavior - * - * \return true if looping, false if one-shot - */ - bool get_looping() const { return this->looping; } - -public: - Sound(const char * src); - Sound(std::unique_ptr res); - -private: - void load(std::unique_ptr res); - -private: - SoLoud::Wav sample; - SoLoud::handle handle; - - float volume = 1.0f; - bool looping = false; -}; - -} // namespace crepe diff --git a/src/crepe/facade/SoundContext.cpp b/src/crepe/facade/SoundContext.cpp deleted file mode 100644 index 72047d2..0000000 --- a/src/crepe/facade/SoundContext.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#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/facade/SoundContext.h b/src/crepe/facade/SoundContext.h deleted file mode 100644 index d3123d2..0000000 --- a/src/crepe/facade/SoundContext.h +++ /dev/null @@ -1,26 +0,0 @@ -#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/facade/Texture.cpp b/src/crepe/facade/Texture.cpp deleted file mode 100644 index b4e3aa8..0000000 --- a/src/crepe/facade/Texture.cpp +++ /dev/null @@ -1,39 +0,0 @@ - - -#include "util/log.h" - -#include "Texture.h" -#include "SdlContext.h" -#include - -using namespace crepe; - -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)); -} - -Texture::~Texture(){ - dbg_trace(); - if(this->m_texture){ - 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; -} - -SDL_Rect& Texture::get_rect() { - return srcrect; -} diff --git a/src/crepe/facade/Texture.h b/src/crepe/facade/Texture.h deleted file mode 100644 index db2f1f9..0000000 --- a/src/crepe/facade/Texture.h +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once - -#include "SDL_rect.h" -#include "api/baseResource.h" -#include "api/Resource.h" -#include -#include - -namespace crepe { - - - -class Texture : public api::BaseResource{ - -public: - Texture(const char * src); - Texture(std::unique_ptr res); - ~Texture(); - - SDL_Texture* get_texture() const; - SDL_Rect& get_rect() ; -private: - void load(std::unique_ptr res); - -private: - SDL_Texture* m_texture; - SDL_Rect srcrect; -}; - -} // namespace crepe - diff --git a/src/crepe/facade/touch b/src/crepe/facade/touch deleted file mode 100644 index e69de29..0000000 diff --git a/src/crepe/renderSystem.cpp b/src/crepe/renderSystem.cpp new file mode 100644 index 0000000..a06aeba --- /dev/null +++ b/src/crepe/renderSystem.cpp @@ -0,0 +1,37 @@ + + + +#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 new file mode 100644 index 0000000..9011b30 --- /dev/null +++ b/src/crepe/renderSystem.h @@ -0,0 +1,13 @@ + +#pragma once + + + +class RenderSystem { + +public: + RenderSystem() = default; + ~RenderSystem() = default; + + void render(); +}; -- cgit v1.2.3