From 95171100beba7bc36ae4421652f6c4fbb34774a8 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 23 Oct 2024 22:26:05 +0200 Subject: rename SdlContext -> SDLContext --- src/crepe/CMakeLists.txt | 6 +- src/crepe/RenderSystem.cpp | 4 +- src/crepe/SDLContext.cpp | 150 +++++++++++++++++++++++++++++++++++++++++++++ src/crepe/SDLContext.h | 52 ++++++++++++++++ src/crepe/SdlContext.cpp | 150 --------------------------------------------- src/crepe/SdlContext.h | 52 ---------------- src/crepe/api/Texture.cpp | 4 +- src/crepe/api/Texture.h | 4 +- 8 files changed, 211 insertions(+), 211 deletions(-) create mode 100644 src/crepe/SDLContext.cpp create mode 100644 src/crepe/SDLContext.h delete mode 100644 src/crepe/SdlContext.cpp delete mode 100644 src/crepe/SdlContext.h (limited to 'src') diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index fcd169c..d938eb8 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -11,7 +11,7 @@ target_sources(crepe PUBLIC PhysicsSystem.cpp CollisionSystem.cpp Collider.cpp - SdlContext.cpp + SDLContext.cpp RenderSystem.cpp ) @@ -20,7 +20,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Asset.h Sound.h SoundContext.h - SdlContext.h + SDLContext.h ComponentManager.h ComponentManager.hpp Component.h @@ -29,7 +29,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES PhysicsSystem.h CollisionSystem.h Collider.h - SdlContext.h + SDLContext.h RenderSystem.h ) diff --git a/src/crepe/RenderSystem.cpp b/src/crepe/RenderSystem.cpp index 1139359..fae93f0 100644 --- a/src/crepe/RenderSystem.cpp +++ b/src/crepe/RenderSystem.cpp @@ -7,7 +7,7 @@ #include "ComponentManager.h" #include "RenderSystem.h" -#include "SdlContext.h" +#include "SDLContext.h" using namespace crepe; using namespace crepe::api; @@ -28,7 +28,7 @@ void RenderSystem::update() { std::vector> sprites = mgr.get_components_by_type(); - SdlContext & render = SdlContext::get_instance(); + SDLContext & render = SDLContext::get_instance(); render.clear_screen(); for (const Sprite & sprite : sprites) { diff --git a/src/crepe/SDLContext.cpp b/src/crepe/SDLContext.cpp new file mode 100644 index 0000000..5a93679 --- /dev/null +++ b/src/crepe/SDLContext.cpp @@ -0,0 +1,150 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "api/Sprite.h" +#include "api/Texture.h" +#include "api/Transform.h" +#include "util/log.h" + +#include "SDLContext.h" + +using namespace crepe; + +SDLContext & SDLContext::get_instance() { + static SDLContext instance; + return instance; +} + +void SDLContext::handle_events(bool & running) { + SDL_Event event; + while (SDL_PollEvent(&event)) { + if (event.type == SDL_QUIT) { + running = false; + } + } +} + +SDLContext::~SDLContext() { + dbg_trace(); + + if (this->game_renderer != nullptr) + SDL_DestroyRenderer(this->game_renderer); + + if (this->game_window != nullptr) { + SDL_DestroyWindow(this->game_window); + } + + IMG_Quit(); + SDL_Quit(); +} + +void SDLContext::clear_screen() { SDL_RenderClear(this->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; + return; + } + + this->game_window = SDL_CreateWindow( + "Crepe Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, + 1920, 1080, SDL_WINDOW_SHOWN); + if (!this->game_window) { + std::cerr << "Window could not be created! SDL_Error: " + << SDL_GetError() << std::endl; + } + + this->game_renderer + = SDL_CreateRenderer(this->game_window, -1, SDL_RENDERER_ACCELERATED); + if (!this->game_renderer) { + std::cerr << "Renderer could not be created! SDL_Error: " + << SDL_GetError() << std::endl; + SDL_DestroyWindow(this->game_window); + return; + } + + 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->game_renderer); } + +void SDLContext::draw(const api::Sprite & sprite, + const api::Transform & transform) { + + static SDL_RendererFlip render_flip + = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) + | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); + + int w, h; + SDL_QueryTexture(sprite.sprite_image->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), + }; + + double degrees = transform.rotation * 180 / M_PI; + SDL_RenderCopyEx(this->game_renderer, sprite.sprite_image->texture, NULL, + &dstrect, degrees, NULL, render_flip); +} + +/* +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(this->game_renderer, tmp); + + if (!CreatedTexture) { + std::cerr << "Error could not create texture " << IMG_GetError + << std::endl; + } + SDL_FreeSurface(tmp); + + return CreatedTexture; +} +*/ + +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 * created_texture + = SDL_CreateTextureFromSurface(this->game_renderer, tmp); + + if (!created_texture) { + std::cerr << "Error could not create texture " << IMG_GetError + << std::endl; + } + SDL_FreeSurface(tmp); + + return created_texture; +} diff --git a/src/crepe/SDLContext.h b/src/crepe/SDLContext.h new file mode 100644 index 0000000..4d9c1bc --- /dev/null +++ b/src/crepe/SDLContext.h @@ -0,0 +1,52 @@ +#pragma once + +#include +#include + +#include "api/Sprite.h" +#include "api/Transform.h" + +#include "RenderSystem.h" + +namespace crepe::api { +class Texture; +} + +namespace crepe { + +class SDLContext { + +public: + // singleton + static SDLContext & get_instance(); + 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); + +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 * game_window = nullptr; + SDL_Renderer * game_renderer = nullptr; +}; + +} // namespace crepe diff --git a/src/crepe/SdlContext.cpp b/src/crepe/SdlContext.cpp deleted file mode 100644 index 2b49283..0000000 --- a/src/crepe/SdlContext.cpp +++ /dev/null @@ -1,150 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -#include "api/Sprite.h" -#include "api/Texture.h" -#include "api/Transform.h" -#include "util/log.h" - -#include "SdlContext.h" - -using namespace crepe; - -SdlContext & SdlContext::get_instance() { - static SdlContext instance; - return instance; -} - -void SdlContext::handle_events(bool & running) { - SDL_Event event; - while (SDL_PollEvent(&event)) { - if (event.type == SDL_QUIT) { - running = false; - } - } -} - -SdlContext::~SdlContext() { - dbg_trace(); - - if (this->game_renderer != nullptr) - SDL_DestroyRenderer(this->game_renderer); - - if (this->game_window != nullptr) { - SDL_DestroyWindow(this->game_window); - } - - IMG_Quit(); - SDL_Quit(); -} - -void SdlContext::clear_screen() { SDL_RenderClear(this->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; - return; - } - - this->game_window = SDL_CreateWindow( - "Crepe Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, - 1920, 1080, SDL_WINDOW_SHOWN); - if (!this->game_window) { - std::cerr << "Window could not be created! SDL_Error: " - << SDL_GetError() << std::endl; - } - - this->game_renderer - = SDL_CreateRenderer(this->game_window, -1, SDL_RENDERER_ACCELERATED); - if (!this->game_renderer) { - std::cerr << "Renderer could not be created! SDL_Error: " - << SDL_GetError() << std::endl; - SDL_DestroyWindow(this->game_window); - return; - } - - 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->game_renderer); } - -void SdlContext::draw(const api::Sprite & sprite, - const api::Transform & transform) { - - static SDL_RendererFlip render_flip - = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) - | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); - - int w, h; - SDL_QueryTexture(sprite.sprite_image->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), - }; - - double degrees = transform.rotation * 180 / M_PI; - SDL_RenderCopyEx(this->game_renderer, sprite.sprite_image->texture, NULL, - &dstrect, degrees, NULL, render_flip); -} - -/* -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(this->game_renderer, tmp); - - if (!CreatedTexture) { - std::cerr << "Error could not create texture " << IMG_GetError - << std::endl; - } - SDL_FreeSurface(tmp); - - return CreatedTexture; -} -*/ - -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 * created_texture - = SDL_CreateTextureFromSurface(this->game_renderer, tmp); - - if (!created_texture) { - std::cerr << "Error could not create texture " << IMG_GetError - << std::endl; - } - SDL_FreeSurface(tmp); - - return created_texture; -} diff --git a/src/crepe/SdlContext.h b/src/crepe/SdlContext.h deleted file mode 100644 index 31ba3a6..0000000 --- a/src/crepe/SdlContext.h +++ /dev/null @@ -1,52 +0,0 @@ -#pragma once - -#include -#include - -#include "api/Sprite.h" -#include "api/Transform.h" - -#include "RenderSystem.h" - -namespace crepe::api { -class Texture; -} - -namespace crepe { - -class SdlContext { - -public: - // singleton - static SdlContext & get_instance(); - 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); - -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 * game_window = nullptr; - SDL_Renderer * game_renderer = nullptr; -}; - -} // namespace crepe diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp index 7791b5b..481ef7c 100644 --- a/src/crepe/api/Texture.cpp +++ b/src/crepe/api/Texture.cpp @@ -3,7 +3,7 @@ #include "util/log.h" #include "Asset.h" -#include "SdlContext.h" +#include "SDLContext.h" #include "Texture.h" using namespace crepe::api; @@ -27,6 +27,6 @@ Texture::~Texture() { } void Texture::load(unique_ptr res) { - SdlContext & ctx = SdlContext::get_instance(); + SDLContext & ctx = SDLContext::get_instance(); this->texture = ctx.texture_from_path(res->canonical()); } diff --git a/src/crepe/api/Texture.h b/src/crepe/api/Texture.h index 993f72b..7a2c755 100644 --- a/src/crepe/api/Texture.h +++ b/src/crepe/api/Texture.h @@ -6,7 +6,7 @@ #include "Asset.h" namespace crepe { -class SdlContext; +class SDLContext; } namespace crepe::api { @@ -24,7 +24,7 @@ private: private: SDL_Texture * texture = nullptr; - friend class crepe::SdlContext; + friend class crepe::SDLContext; }; } // namespace crepe::api -- cgit v1.2.3