diff options
author | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-10-01 21:22:30 +0200 |
---|---|---|
committer | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-10-01 21:22:30 +0200 |
commit | e0ea870fdfcfbe9e3f0e47215bb809d4353d88e2 (patch) | |
tree | 16f694c46ec4b76019187322da2bdd87906bf5ba /src | |
parent | 7230b630025886939c726e07eba1a0b35511f687 (diff) |
removed submodule and updating resource_manager
Diffstat (limited to 'src')
24 files changed, 183 insertions, 82 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index cb310af..27e5188 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -9,6 +9,7 @@ set(CMAKE_BUILD_TYPE Debug) add_compile_definitions(DEBUG) add_subdirectory(../lib/soloud soloud) +add_subdirectory(../lib/SDL_image SDL_image) project(crepe C CXX) @@ -21,6 +22,8 @@ target_include_directories(crepe # TODO: libraries should be linked as PRIVATE target_link_libraries(crepe PUBLIC soloud + PUBLIC SDL2 + PUBLIC SDL2_image ) add_subdirectory(crepe) @@ -30,5 +33,5 @@ install( FILE_SET HEADERS DESTINATION include/crepe ) -add_executable(dummy_rm dummy_resource_manager.cpp) +add_executable(dummy_rm dummy_audio.cpp) target_link_libraries(dummy_rm PUBLIC crepe) diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index ed6ebe5..69e67ac 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -1,14 +1,11 @@ target_sources(crepe PUBLIC - Sound.cpp - SoundContext.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES - Sound.h - SoundContext.h ) add_subdirectory(api) add_subdirectory(util) add_subdirectory(fabricator) +add_subdirectory(facade) diff --git a/src/crepe/api/AudioSource.cpp b/src/crepe/api/AudioSource.cpp index 4d1b093..f0d708a 100644 --- a/src/crepe/api/AudioSource.cpp +++ b/src/crepe/api/AudioSource.cpp @@ -1,6 +1,6 @@ #include "AudioSource.h" -#include "../Sound.h" +#include "facade/Sound.h" #include <memory> using namespace crepe::api; diff --git a/src/crepe/api/Audio_asset.cpp b/src/crepe/api/Audio_asset.cpp deleted file mode 100644 index a9b04ed..0000000 --- a/src/crepe/api/Audio_asset.cpp +++ /dev/null @@ -1,16 +0,0 @@ - - - - -#include "Audio_asset.h" -#include <string> - - -using namespace crepe::api; - -Audio::Audio(const std::string& content){ - this->m_content = content; -} - -Audio::~Audio(){ -} diff --git a/src/crepe/api/Audio_asset.h b/src/crepe/api/Audio_asset.h deleted file mode 100644 index 0b8e48e..0000000 --- a/src/crepe/api/Audio_asset.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - - -#include "resource.h" -#include <string> - - -namespace crepe::api { - - -class Audio : public Resource { - -public: - Audio(const std::string&); - ~Audio(); - -}; -} diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 4f3fada..2cf0bcc 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -1,18 +1,17 @@ target_sources(crepe PUBLIC - Image_asset.cpp - map_asset.cpp - Audio_asset.cpp - spritesheet.cpp - resource_manager.cpp + #Image_asset.cpp + #map_asset.cpp + #spritesheet.cpp + #resource_manager.cpp + Resource.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES - resource.h - Image_asset.h - map_asset.h - Audio_asset.h - spritesheet.h - resource_manager.h + Resource.h + #Image_asset.h + #map_asset.h + #spritesheet.h + #resource_manager.h Component.h AudioSource.h ) diff --git a/src/crepe/api/Image_asset.h b/src/crepe/api/Image_asset.h index 69549af..0a36b0b 100644 --- a/src/crepe/api/Image_asset.h +++ b/src/crepe/api/Image_asset.h @@ -2,7 +2,7 @@ -#include "resource.h" +#include "Resource.h" #include <string> namespace crepe::api { diff --git a/src/crepe/api/map_asset.h b/src/crepe/api/map_asset.h index a3b994f..a4f3df7 100644 --- a/src/crepe/api/map_asset.h +++ b/src/crepe/api/map_asset.h @@ -1,6 +1,6 @@ #pragma once -#include "resource.h" +#include "Resource.h" #include <string> diff --git a/src/crepe/api/resource.h b/src/crepe/api/resource.h deleted file mode 100644 index e6456f9..0000000 --- a/src/crepe/api/resource.h +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - - -#include <string> - -namespace crepe::api { - -class Resource{ - -public: - - virtual ~Resource() =default; - - const std::string& getContent() const{ - return this->m_content; - } - -protected: - std::string m_content; -}; - - -} diff --git a/src/crepe/api/resource_manager.cpp b/src/crepe/api/resource_manager.cpp index a5644ee..0ecdae5 100644 --- a/src/crepe/api/resource_manager.cpp +++ b/src/crepe/api/resource_manager.cpp @@ -6,9 +6,9 @@ using namespace crepe::api; -ResourceManager* ResourceManager::get_instance(){ +ResourceManager& ResourceManager::get_instance(){ static ResourceManager instance; - return &instance; + return instance; } diff --git a/src/crepe/api/resource_manager.h b/src/crepe/api/resource_manager.h index 1b91524..5b0e0e1 100644 --- a/src/crepe/api/resource_manager.h +++ b/src/crepe/api/resource_manager.h @@ -8,7 +8,7 @@ #include <utility> -#include "api/resource.h" +#include "Resource.h" #include "fabricator/resource_fabricator.h" diff --git a/src/crepe/api/spritesheet.h b/src/crepe/api/spritesheet.h index 7f49156..e7530c2 100644 --- a/src/crepe/api/spritesheet.h +++ b/src/crepe/api/spritesheet.h @@ -3,7 +3,7 @@ -#include "resource.h" +#include "Resource.h" #include <string> diff --git a/src/crepe/fabricator/resource_fabricator.h b/src/crepe/fabricator/resource_fabricator.h index 9299ed3..2b0030d 100644 --- a/src/crepe/fabricator/resource_fabricator.h +++ b/src/crepe/fabricator/resource_fabricator.h @@ -2,7 +2,7 @@ -#include "api/resource.h" +#include "api/Resource.h" #include <memory> #include <string> diff --git a/src/crepe/facade/CMakeLists.txt b/src/crepe/facade/CMakeLists.txt new file mode 100644 index 0000000..1263683 --- /dev/null +++ b/src/crepe/facade/CMakeLists.txt @@ -0,0 +1,13 @@ +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 new file mode 100644 index 0000000..fc68b40 --- /dev/null +++ b/src/crepe/facade/SdlContext.cpp @@ -0,0 +1,57 @@ + + +#include "SdlContext.h" +#include <SDL2/SDL.h> +#include <SDL2/SDL_render.h> +#include <SDL2/SDL_surface.h> +#include <SDL2/SDL_video.h> +#include <SDL2/SDL_image.h> + +using namespace crepe; + + +SdlContext& SdlContext::get_instance(){ + static SdlContext instance; + return instance; +} + + +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, 800, 600, 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; + } + + IMG_Init(IMG_INIT_PNG); +} + +SdlContext::~SdlContext(){ + if(m_game_renderer) + SDL_DestroyRenderer(m_game_renderer); + + if (m_game_window) { + SDL_DestroyWindow(m_game_window); + } + IMG_Quit(); +} + + +SDL_Texture* SdlContext::setTextureFromPath(const char* path){ + SDL_Surface* tmp = IMG_Load(path); + SDL_Texture* CreatedTexture = SDL_CreateTextureFromSurface(m_game_renderer, tmp); + SDL_FreeSurface(tmp); + + return CreatedTexture; +} diff --git a/src/crepe/facade/SdlContext.h b/src/crepe/facade/SdlContext.h new file mode 100644 index 0000000..c275300 --- /dev/null +++ b/src/crepe/facade/SdlContext.h @@ -0,0 +1,33 @@ +#pragma once + +#include "Texture.h" +#include <SDL2/SDL_render.h> +#include <SDL2/SDL_video.h> +#include <string> + +namespace crepe { + +class SdlContext { + +private: + SdlContext(); + virtual ~SdlContext(); + + // singleton + static SdlContext & get_instance(); + SdlContext(const SdlContext &) = delete; + SdlContext(SdlContext &&) = delete; + SdlContext & operator=(const SdlContext &) = delete; + SdlContext & operator=(SdlContext &&) = delete; + + SDL_Texture* setTextureFromPath(const char*); + +private: + friend class Texture; + + SDL_Window* m_game_window; + SDL_Renderer* m_game_renderer; +}; + +} // + diff --git a/src/crepe/Sound.cpp b/src/crepe/facade/Sound.cpp index e1150ac..e1150ac 100644 --- a/src/crepe/Sound.cpp +++ b/src/crepe/facade/Sound.cpp diff --git a/src/crepe/Sound.h b/src/crepe/facade/Sound.h index ac93991..ac93991 100644 --- a/src/crepe/Sound.h +++ b/src/crepe/facade/Sound.h diff --git a/src/crepe/SoundContext.cpp b/src/crepe/facade/SoundContext.cpp index 72047d2..72047d2 100644 --- a/src/crepe/SoundContext.cpp +++ b/src/crepe/facade/SoundContext.cpp diff --git a/src/crepe/SoundContext.h b/src/crepe/facade/SoundContext.h index 090966d..090966d 100644 --- a/src/crepe/SoundContext.h +++ b/src/crepe/facade/SoundContext.h diff --git a/src/crepe/facade/Texture.cpp b/src/crepe/facade/Texture.cpp new file mode 100644 index 0000000..c24312a --- /dev/null +++ b/src/crepe/facade/Texture.cpp @@ -0,0 +1,31 @@ + + +#include "util/log.h" + +#include "Texture.h" +#include "SdlContext.h" +#include <SDL2/SDL_render.h> + +using namespace crepe; + +Texture::Texture(std::unique_ptr<api::Resource> res) { + dbg_trace(); + this->load(std::move(res)); +} + +Texture::Texture(const char * src) { + dbg_trace(); + this->load(std::make_unique<api::Resource>(src)); +} + +Texture::~Texture(){ + dbg_trace(); + if(this->m_texture){ + SDL_DestroyTexture(m_texture); + } +} +void Texture::load(std::unique_ptr<api::Resource> res) { + SdlContext& ctx = SdlContext::get_instance(); + m_texture = ctx.setTextureFromPath(res->canonical()); +} + diff --git a/src/crepe/facade/Texture.h b/src/crepe/facade/Texture.h new file mode 100644 index 0000000..3677f6e --- /dev/null +++ b/src/crepe/facade/Texture.h @@ -0,0 +1,25 @@ +#pragma once + +#include "api/Resource.h" +#include <SDL2/SDL_render.h> +#include <memory> + + +namespace crepe { + +class Texture { + +public: + Texture(const char * src); + Texture(std::unique_ptr<api::Resource> res); + ~Texture(); + +private: + void load(std::unique_ptr<api::Resource> res); + +private: + SDL_Texture* m_texture; +}; + +} // namespace crepe + diff --git a/src/crepe/facade/touch b/src/crepe/facade/touch new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/crepe/facade/touch diff --git a/src/dummy_audio.cpp b/src/dummy_audio.cpp index 049bb49..211a503 100644 --- a/src/dummy_audio.cpp +++ b/src/dummy_audio.cpp @@ -1,4 +1,4 @@ -#include "crepe/Sound.h" +#include "crepe/facade/Sound.h" #include "crepe/util/log.h" #include <chrono> |