From 2d13805218eb34b6e06205c1b65c341ebcdad504 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Tue, 1 Oct 2024 11:03:50 +0200 Subject: changed resource manager to singleton --- asset/tiled/demo.tmx | 21 +++++++ asset/tiled/images/tilemap/platform.tsx | 18 ++++++ asset/tiled/images/tilemap/tileset.png | Bin 0 -> 29737 bytes asset/tiled/images/tilemap/tileset.txt | 2 + asset/tiled/images/tilemap/tileset02.png | Bin 0 -> 6478 bytes asset/tiled/platform.tmx | 94 +++++++++++++++++++++++++++++++ 6 files changed, 135 insertions(+) create mode 100644 asset/tiled/demo.tmx create mode 100644 asset/tiled/images/tilemap/platform.tsx create mode 100644 asset/tiled/images/tilemap/tileset.png create mode 100644 asset/tiled/images/tilemap/tileset.txt create mode 100644 asset/tiled/images/tilemap/tileset02.png create mode 100644 asset/tiled/platform.tmx (limited to 'asset') diff --git a/asset/tiled/demo.tmx b/asset/tiled/demo.tmx new file mode 100644 index 0000000..8183148 --- /dev/null +++ b/asset/tiled/demo.tmx @@ -0,0 +1,21 @@ + + + + + + + + eJxjYBj6gBeIeZAwH5H6OIFYBQvmooEbBwoAAO2gAQc= + + + + + eJxjYBhcgIUCvcxIbH5KHTJMAAAZ1AAX + + + + + eJxjYBjaQBCIBaCYG4k9CiAAAGXcAF0= + + + diff --git a/asset/tiled/images/tilemap/platform.tsx b/asset/tiled/images/tilemap/platform.tsx new file mode 100644 index 0000000..6a48b12 --- /dev/null +++ b/asset/tiled/images/tilemap/platform.tsx @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + diff --git a/asset/tiled/images/tilemap/tileset.png b/asset/tiled/images/tilemap/tileset.png new file mode 100644 index 0000000..f13825d Binary files /dev/null and b/asset/tiled/images/tilemap/tileset.png differ diff --git a/asset/tiled/images/tilemap/tileset.txt b/asset/tiled/images/tilemap/tileset.txt new file mode 100644 index 0000000..1676dad --- /dev/null +++ b/asset/tiled/images/tilemap/tileset.txt @@ -0,0 +1,2 @@ +tileset by Alucard +http://opengameart.org/content/pixel-art-platformer-complete-pack \ No newline at end of file diff --git a/asset/tiled/images/tilemap/tileset02.png b/asset/tiled/images/tilemap/tileset02.png new file mode 100644 index 0000000..9f594e4 Binary files /dev/null and b/asset/tiled/images/tilemap/tileset02.png differ diff --git a/asset/tiled/platform.tmx b/asset/tiled/platform.tmx new file mode 100644 index 0000000..2b40b85 --- /dev/null +++ b/asset/tiled/platform.tmx @@ -0,0 +1,94 @@ + + + + + + + + + + + + eJzt2VsKwyAQBdCsoo8VNN3/BvvbQC1Em9iZOQeE/AQcyFXjLAuc69YxIKuePMgLVckCtNkz4LvrjgGVXHYMqGZPPuSFSuQCPrt3Dshudt/jOfAuZLfOngD8MfmANvkY43ya22P2BIKzvkCbfECbfECb8ykAAABnqNb/qlYvY6rdX0avV77PVe3+Mnq90fMNv/a+Z8gHbK2NZ2B7Jox+PqQO/8rQZi0HAAAAiCf7nWf2+jhW9v5u9vo41sj3E2Ftlg9GjPTMIvTbIswRYKYXswgSvA== + + + + + + + + eJztm9tOw0AMBc0DPAEfAKIY6P9/I6kgqA25NInXa3dnpCOeqJxmZ+89CgCMcfwNAPwHPwCmwQ+AafDDlrvaBYAp+GHLZ5fDIF9VK4I94AfU4qXL60LeqlX3A34ATIMfvrA+yQV++MC6JCf4AdkpOSbjB2RmbFy2HJvxw5bh+2IOlRv8iA9rehueujwO8rzwP/gRk6l5A+PSNua+z7nvEj/KsqXPuhW2jHvRxkr8KMfWPmsN0dqTdnkvlA+/x/ijZT/G+vYM/XuksxSVyzZ8LBxvT1r1g/n9PlR8fJiKlyOt+gHbUKnrhbcj+AFLqPjNn6LNt/AjLyXniCpxnfAcS/ADelRyOeHhCH7ACZX67dvSkdO+99T+5FzO9y7vzz63BaKdFURBpX67tozVecv5Z94Kpzl5/3yHkb/s3V6iUr89Zwi0h0r9dpclUI+18+S58/1r5pAqedfg+NEOKuXuKl07ryb4sYZr+nKL+1kq9d85wY81LJ23WZy9qdCHZ0x2ou/dquBF5mRGJeZvB3pU6r9f0p4fKuv6ZG9H1tZH4iYTKtvbnYcje+ojMZMFlf3PWtIRi/pIvERHxbZPLuGIGtZHYiUiKmXPtKzW7X2dtd8hKZcH2XYf2CrDMzd1fPY9jnjWSeqmxl2HKHcgstRJ/PMNRu5JzA== + + + + + eJzt2+FqwjAUBtDihO2fbnPv/6rbYGUxNOuqbW9izoEgIsqF+jVpczsMAAAAAACkPqILgIq9RxdA1U7RBQSTDyiTjz4dowtoxJr5eP0a52T0PjfX7PDzuuUxajmDY+3mD7byFF3AHcba5YN7nSfGt0PxG/WLzke6FnsLqgFKovNBXVwrXhvnvsswPT9ODed5lvC/YU/5+t45HwCgX+la0HUJsIaWexBga3M9CM+7VAH1SNedf/Ug9JCNUm8G9Mxzm1CmrwfKWsrHXN/RlP/s7S/9TfqR58N9PPiV5+PWZ4n0YPGI1soHPKJL9r7lZ+0AAAAgZQ+Bnr3MfO4eKb3LM5Lujy29Rzq3t3ZLX8VeTkPd9QHU4hO/Kwa1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -- cgit v1.2.3 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 --- asset/texture/img.png | Bin 0 -> 92742 bytes src/CMakeLists.txt | 21 +++++++++- src/build.sh | 7 ++++ src/crepe/CMakeLists.txt | 9 +++- src/crepe/api/Audio_asset.cpp | 16 ++++++++ src/crepe/api/Audio_asset.h | 18 ++++++++ src/crepe/api/CMakeLists.txt | 17 ++++++++ src/crepe/api/Image_asset.cpp | 14 +++++++ src/crepe/api/Image_asset.h | 18 ++++++++ src/crepe/api/map_asset.cpp | 12 ++++++ src/crepe/api/map_asset.h | 14 +++++++ src/crepe/api/resource.h | 23 +++++++++++ src/crepe/api/resource_manager.cpp | 25 ++++++++++++ src/crepe/api/resource_manager.h | 59 +++++++++++++++++++++++++++ src/crepe/api/spritesheet.cpp | 16 ++++++++ src/crepe/api/spritesheet.h | 21 ++++++++++ src/crepe/fabricator/CMakeLists.txt | 8 ++++ src/crepe/fabricator/resource_fabricator.cpp | 26 ++++++++++++ src/crepe/fabricator/resource_fabricator.h | 29 +++++++++++++ src/crepe/main.cpp | 3 -- src/dummy_resource_manager.cpp | 23 +++++++++++ 21 files changed, 373 insertions(+), 6 deletions(-) create mode 100644 asset/texture/img.png create mode 100755 src/build.sh create mode 100644 src/crepe/api/Audio_asset.cpp create mode 100644 src/crepe/api/Audio_asset.h create mode 100644 src/crepe/api/CMakeLists.txt create mode 100644 src/crepe/api/Image_asset.cpp create mode 100644 src/crepe/api/Image_asset.h create mode 100644 src/crepe/api/map_asset.cpp create mode 100644 src/crepe/api/map_asset.h create mode 100644 src/crepe/api/resource.h create mode 100644 src/crepe/api/resource_manager.cpp create mode 100644 src/crepe/api/resource_manager.h create mode 100644 src/crepe/api/spritesheet.cpp create mode 100644 src/crepe/api/spritesheet.h create mode 100644 src/crepe/fabricator/CMakeLists.txt create mode 100644 src/crepe/fabricator/resource_fabricator.cpp create mode 100644 src/crepe/fabricator/resource_fabricator.h delete mode 100644 src/crepe/main.cpp create mode 100644 src/dummy_resource_manager.cpp (limited to 'asset') diff --git a/asset/texture/img.png b/asset/texture/img.png new file mode 100644 index 0000000..43b1eca Binary files /dev/null and b/asset/texture/img.png differ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0090188..87330d4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -8,9 +8,28 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS 1) set(CMAKE_BUILD_TYPE Debug) add_compile_definitions(DEBUG) +#add_subdirectory(../lib/soloud soloud) + project(crepe C CXX) -add_executable(main) +add_library(crepe SHARED) + +target_include_directories(crepe + PUBLIC SYSTEM INTERFACE . +) + +# TODO: libraries should be linked as PRIVATE +target_link_libraries(crepe + #PUBLIC soloud +) add_subdirectory(crepe) +install( + TARGETS crepe + FILE_SET HEADERS DESTINATION include/crepe +) + + +add_executable(dummy_rm dummy_resource_manager.cpp) +target_link_libraries(dummy_rm PUBLIC crepe) 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 diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 392b7d7..68aa072 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -1,3 +1,8 @@ -target_sources(main PUBLIC - main.cpp +target_sources(crepe PUBLIC ) + +target_sources(crepe PUBLIC FILE_SET HEADERS FILES +) + +add_subdirectory(api) +add_subdirectory(fabricator) diff --git a/src/crepe/api/Audio_asset.cpp b/src/crepe/api/Audio_asset.cpp new file mode 100644 index 0000000..a9b04ed --- /dev/null +++ b/src/crepe/api/Audio_asset.cpp @@ -0,0 +1,16 @@ + + + + +#include "Audio_asset.h" +#include + + +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 new file mode 100644 index 0000000..0b8e48e --- /dev/null +++ b/src/crepe/api/Audio_asset.h @@ -0,0 +1,18 @@ +#pragma once + + +#include "resource.h" +#include + + +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 new file mode 100644 index 0000000..7b16fb1 --- /dev/null +++ b/src/crepe/api/CMakeLists.txt @@ -0,0 +1,17 @@ +target_sources(crepe PUBLIC + Image_asset.cpp + map_asset.cpp + Audio_asset.cpp + spritesheet.cpp + resource_manager.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 +) + diff --git a/src/crepe/api/Image_asset.cpp b/src/crepe/api/Image_asset.cpp new file mode 100644 index 0000000..57431c4 --- /dev/null +++ b/src/crepe/api/Image_asset.cpp @@ -0,0 +1,14 @@ + + +#include "Image_asset.h" +#include + +using namespace crepe::api; + +Texture::Texture(const std::string& content){ + this->m_content = content; +} + + +Texture::~Texture(){ +} diff --git a/src/crepe/api/Image_asset.h b/src/crepe/api/Image_asset.h new file mode 100644 index 0000000..69549af --- /dev/null +++ b/src/crepe/api/Image_asset.h @@ -0,0 +1,18 @@ +#pragma once + + + +#include "resource.h" +#include + +namespace crepe::api { + + +class Texture : public Resource { + +public: + Texture(const std::string&); + ~Texture(); +}; + +} diff --git a/src/crepe/api/map_asset.cpp b/src/crepe/api/map_asset.cpp new file mode 100644 index 0000000..bbabe2b --- /dev/null +++ b/src/crepe/api/map_asset.cpp @@ -0,0 +1,12 @@ + + + + +#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 new file mode 100644 index 0000000..a3b994f --- /dev/null +++ b/src/crepe/api/map_asset.h @@ -0,0 +1,14 @@ +#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.h b/src/crepe/api/resource.h new file mode 100644 index 0000000..e6456f9 --- /dev/null +++ b/src/crepe/api/resource.h @@ -0,0 +1,23 @@ +#pragma once + + +#include + +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 new file mode 100644 index 0000000..a5644ee --- /dev/null +++ b/src/crepe/api/resource_manager.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/resource_manager.h b/src/crepe/api/resource_manager.h new file mode 100644 index 0000000..1b91524 --- /dev/null +++ b/src/crepe/api/resource_manager.h @@ -0,0 +1,59 @@ +#pragma once + + + +#include +#include +#include +#include + + +#include "api/resource.h" +#include "fabricator/resource_fabricator.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()); + } + + std::unique_ptr resource = ResourceFactory::create_resource(file_path); + 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 new file mode 100644 index 0000000..f42a782 --- /dev/null +++ b/src/crepe/api/spritesheet.cpp @@ -0,0 +1,16 @@ + + +#include "spritesheet.h" + +#include + +using namespace crepe::api; + +SpriteSheet::SpriteSheet(const std::string& content){ + this->m_content = content; +} + +SpriteSheet::~SpriteSheet(){ +} + + diff --git a/src/crepe/api/spritesheet.h b/src/crepe/api/spritesheet.h new file mode 100644 index 0000000..7f49156 --- /dev/null +++ b/src/crepe/api/spritesheet.h @@ -0,0 +1,21 @@ +#pragma once + + + + +#include "resource.h" +#include + + +namespace crepe::api { + + + +class SpriteSheet : public Resource{ + +public: + SpriteSheet(const std::string&); + ~SpriteSheet(); + +}; +} diff --git a/src/crepe/fabricator/CMakeLists.txt b/src/crepe/fabricator/CMakeLists.txt new file mode 100644 index 0000000..4fd7eea --- /dev/null +++ b/src/crepe/fabricator/CMakeLists.txt @@ -0,0 +1,8 @@ +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 new file mode 100644 index 0000000..0633a40 --- /dev/null +++ b/src/crepe/fabricator/resource_fabricator.cpp @@ -0,0 +1,26 @@ + + +#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 new file mode 100644 index 0000000..9299ed3 --- /dev/null +++ b/src/crepe/fabricator/resource_fabricator.h @@ -0,0 +1,29 @@ +#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/main.cpp b/src/crepe/main.cpp deleted file mode 100644 index 8e9a184..0000000 --- a/src/crepe/main.cpp +++ /dev/null @@ -1,3 +0,0 @@ -#include - -int main() { printf("Hello World!\n"); } diff --git a/src/dummy_resource_manager.cpp b/src/dummy_resource_manager.cpp new file mode 100644 index 0000000..214c617 --- /dev/null +++ b/src/dummy_resource_manager.cpp @@ -0,0 +1,23 @@ + + + + +#include "api/Image_asset.h" +#include "api/resource_manager.h" +#include +#include + + + +using namespace crepe; + +int main(){ + + // get instance of resource manager + api::ResourceManager& c_ResMan = api::ResourceManager::get_instance(); + + // make a resouce from the file path + api::Texture* img = c_ResMan.Load("../asset/texture/img.png"); + + std::cout << img->getContent() << std::endl; +} -- cgit v1.2.3 From de2c2593f9f272c5151d74af4ff846fdd70a9bc7 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 2 Oct 2024 15:57:59 +0200 Subject: working resource manager and textures and sprite to new standard --- .gitmodules | 4 ++ asset/spritesheet/spritesheet_test.png | Bin 0 -> 616 bytes lib/sdl_image/CMakeLists.txt | 34 ++++++++++++++- lib/sdl_image/src | 1 + src/CMakeLists.txt | 6 +-- src/crepe/api/CMakeLists.txt | 13 +++--- src/crepe/api/Image_asset.cpp | 14 ------- src/crepe/api/Image_asset.h | 18 -------- src/crepe/api/Resource.cpp | 6 ++- src/crepe/api/baseResource.h | 11 +++++ src/crepe/api/game.cpp | 18 ++++++++ src/crepe/api/game.h | 15 +++++++ src/crepe/api/resource_manager.h | 12 +++--- src/crepe/api/spritesheet.cpp | 36 ++++++++++++++-- src/crepe/api/spritesheet.h | 27 +++++++++--- src/crepe/facade/SdlContext.cpp | 73 ++++++++++++++++++++++++++++++++- src/crepe/facade/SdlContext.h | 15 +++++-- src/crepe/facade/Sound.h | 3 +- src/crepe/facade/Texture.cpp | 4 ++ src/crepe/facade/Texture.h | 8 +++- src/dummy_resource_manager.cpp | 31 ++++++++++---- 21 files changed, 273 insertions(+), 76 deletions(-) create mode 100644 asset/spritesheet/spritesheet_test.png create mode 160000 lib/sdl_image/src delete mode 100644 src/crepe/api/Image_asset.cpp delete mode 100644 src/crepe/api/Image_asset.h create mode 100644 src/crepe/api/baseResource.h create mode 100644 src/crepe/api/game.cpp create mode 100644 src/crepe/api/game.h (limited to 'asset') diff --git a/.gitmodules b/.gitmodules index bb860c0..d7a6ebf 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,3 +10,7 @@ path = lib/sdl2 url = https://github.com/libsdl-org/SDL shallow = true +[submodule "lib/sdl_image/src"] + path = lib/sdl_image/src + url = https://github.com/libsdl-org/SDL_image + branch = SDL2 diff --git a/asset/spritesheet/spritesheet_test.png b/asset/spritesheet/spritesheet_test.png new file mode 100644 index 0000000..d68a72a Binary files /dev/null and b/asset/spritesheet/spritesheet_test.png differ diff --git a/lib/sdl_image/CMakeLists.txt b/lib/sdl_image/CMakeLists.txt index 65f7598..d1bee81 100644 --- a/lib/sdl_image/CMakeLists.txt +++ b/lib/sdl_image/CMakeLists.txt @@ -11,8 +11,38 @@ include_directories(src/include/SDL3_image) project(sdl_image C CXX) add_library(sdl_image SHARED - src/src/IMG.c - src/src/IMG_png.c + src/src/IMG.c + src/src/IMG_WIC.c + src/src/IMG_avif.c + src/src/IMG_bmp.c + src/src/IMG_gif.c + src/src/IMG_jpg.c + src/src/IMG_jxl.c + src/src/IMG_lbm.c + src/src/IMG_pcx.c + src/src/IMG_png.c + src/src/IMG_pnm.c + src/src/IMG_qoi.c + src/src/IMG_stb.c + src/src/IMG_svg.c + src/src/IMG_tga.c + src/src/IMG_tif.c + src/src/IMG_webp.c + src/src/IMG_xcf.c + src/src/IMG_xpm.c + src/src/IMG_xv.c + + + + + + + + + + + + ) target_include_directories(sdl_image PRIVATE src/include/) target_include_directories(sdl_image SYSTEM INTERFACE src/include/) diff --git a/lib/sdl_image/src b/lib/sdl_image/src new file mode 160000 index 0000000..c6c7278 --- /dev/null +++ b/lib/sdl_image/src @@ -0,0 +1 @@ +Subproject commit c6c7278b86b5de1232b10de8f612ed05cf2d11f6 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 40d6644..b47be8f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -9,7 +9,7 @@ set(CMAKE_BUILD_TYPE Debug) add_compile_definitions(DEBUG) add_subdirectory(../lib/soloud soloud) -add_subdirectory(../lib/sdl_image sdl_image) +add_subdirectory(../lib/sdl_image/src SDL2_image) project(crepe C CXX) @@ -23,7 +23,7 @@ target_include_directories(crepe target_link_libraries(crepe PUBLIC soloud PUBLIC SDL2 - PUBLIC sdl_image + PUBLIC SDL2_image ) add_subdirectory(crepe) @@ -33,5 +33,5 @@ install( FILE_SET HEADERS DESTINATION include/crepe ) -add_executable(dummy_rm dummy_audio.cpp) +add_executable(dummy_rm dummy_resource_manager.cpp) target_link_libraries(dummy_rm PUBLIC crepe) diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 2cf0bcc..96b55cf 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -1,17 +1,18 @@ target_sources(crepe PUBLIC - #Image_asset.cpp #map_asset.cpp - #spritesheet.cpp - #resource_manager.cpp + spritesheet.cpp + resource_manager.cpp Resource.cpp + game.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES Resource.h - #Image_asset.h + baseResource.h + game.h #map_asset.h - #spritesheet.h - #resource_manager.h + spritesheet.h + resource_manager.h Component.h AudioSource.h ) diff --git a/src/crepe/api/Image_asset.cpp b/src/crepe/api/Image_asset.cpp deleted file mode 100644 index 57431c4..0000000 --- a/src/crepe/api/Image_asset.cpp +++ /dev/null @@ -1,14 +0,0 @@ - - -#include "Image_asset.h" -#include - -using namespace crepe::api; - -Texture::Texture(const std::string& content){ - this->m_content = content; -} - - -Texture::~Texture(){ -} diff --git a/src/crepe/api/Image_asset.h b/src/crepe/api/Image_asset.h deleted file mode 100644 index 0a36b0b..0000000 --- a/src/crepe/api/Image_asset.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - - - -#include "Resource.h" -#include - -namespace crepe::api { - - -class Texture : public Resource { - -public: - Texture(const std::string&); - ~Texture(); -}; - -} diff --git a/src/crepe/api/Resource.cpp b/src/crepe/api/Resource.cpp index 1a647ce..f7f2516 100644 --- a/src/crepe/api/Resource.cpp +++ b/src/crepe/api/Resource.cpp @@ -1,11 +1,15 @@ #include +#include +#include #include "Resource.h" +#include "util/log.h" using namespace crepe::api; Resource::Resource(const std::string & src) { - this->src = std::filesystem::canonical(src); + dbg_trace(); + this->src = std::filesystem::path(src); this->file = std::ifstream(this->src, std::ios::in | std::ios::binary); } diff --git a/src/crepe/api/baseResource.h b/src/crepe/api/baseResource.h new file mode 100644 index 0000000..2513f4d --- /dev/null +++ b/src/crepe/api/baseResource.h @@ -0,0 +1,11 @@ +#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 new file mode 100644 index 0000000..02a0132 --- /dev/null +++ b/src/crepe/api/game.cpp @@ -0,0 +1,18 @@ + + + +#include "game.h" +#include "api/spritesheet.h" +#include "facade/SdlContext.h" +#include "facade/Texture.h" +#include + + +void game::render(std::vector & draw, std::vector & ss){ + auto& ctx = crepe::SdlContext::get_instance(); + + ctx.loop(*draw[0], *ss[0]); +} + + + diff --git a/src/crepe/api/game.h b/src/crepe/api/game.h new file mode 100644 index 0000000..7cde954 --- /dev/null +++ b/src/crepe/api/game.h @@ -0,0 +1,15 @@ +#pragma once + + +#include "api/spritesheet.h" +#include "facade/Texture.h" +#include + +class game { + +public: + game(){} + ~game(){} + + void render(std::vector&, std::vector&); +}; diff --git a/src/crepe/api/resource_manager.h b/src/crepe/api/resource_manager.h index 5b0e0e1..a646d95 100644 --- a/src/crepe/api/resource_manager.h +++ b/src/crepe/api/resource_manager.h @@ -7,19 +7,17 @@ #include #include +#include "api/baseResource.h" -#include "Resource.h" -#include "fabricator/resource_fabricator.h" - - namespace crepe::api{ +namespace crepe::api{ class ResourceManager{ private: - std::unordered_map< std::string, std::unique_ptr> m_resources; + std::unordered_map< std::string, std::unique_ptr> m_resources; protected: @@ -44,10 +42,10 @@ public: return static_cast(m_resources[file_path].get()); } - std::unique_ptr resource = ResourceFactory::create_resource(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() ); + return static_cast(m_resources[file_path].get() ); } return nullptr; diff --git a/src/crepe/api/spritesheet.cpp b/src/crepe/api/spritesheet.cpp index f42a782..93a2b65 100644 --- a/src/crepe/api/spritesheet.cpp +++ b/src/crepe/api/spritesheet.cpp @@ -1,16 +1,44 @@ #include "spritesheet.h" +#include "SDL_rect.h" +#include "SDL_render.h" +#include "api/Resource.h" +#include "facade/SdlContext.h" +#include -#include using namespace crepe::api; -SpriteSheet::SpriteSheet(const std::string& content){ - this->m_content = content; +Spritesheet::Spritesheet(const char* src, const int row, const int col){ + this->load(std::make_unique(src), row, col); } -SpriteSheet::~SpriteSheet(){ +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::draw_selected_sprite(const int x, const int y){ + auto& ctx = SdlContext::get_instance(); + SDL_Rect tmp = { x, y, m_clip.w, m_clip.h}; + SDL_RenderCopy(ctx.m_game_renderer, this->m_spritesheet, &this->m_clip, &tmp); +} + +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 index e7530c2..7f46296 100644 --- a/src/crepe/api/spritesheet.h +++ b/src/crepe/api/spritesheet.h @@ -4,18 +4,35 @@ #include "Resource.h" -#include +#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(); -class SpriteSheet : public Resource{ + 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; -public: - SpriteSheet(const std::string&); - ~SpriteSheet(); + +private: + + SDL_Texture* m_spritesheet; + SDL_Rect m_clip; + + friend class SdlContext; }; + } + diff --git a/src/crepe/facade/SdlContext.cpp b/src/crepe/facade/SdlContext.cpp index fc68b40..7e2d79f 100644 --- a/src/crepe/facade/SdlContext.cpp +++ b/src/crepe/facade/SdlContext.cpp @@ -1,11 +1,18 @@ #include "SdlContext.h" +#include "SDL_rect.h" +#include "api/spritesheet.h" +#include "facade/Texture.h" +#include "util/log.h" #include #include #include #include #include +#include +#include +#include using namespace crepe; @@ -33,8 +40,11 @@ SdlContext::SdlContext(){ SDL_DestroyWindow(m_game_window); return; } - - IMG_Init(IMG_INIT_PNG); + int imgFlags = IMG_INIT_PNG; + if( !( IMG_Init( imgFlags ) & imgFlags ) ) + { + std::cout << "SDL_image could not initialize! SDL_image Error: " << IMG_GetError() << std::endl; + } } SdlContext::~SdlContext(){ @@ -45,13 +55,72 @@ SdlContext::~SdlContext(){ 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; } + + +void SdlContext::loop(const Texture& texture, api::Spritesheet& ss){ + SDL_RenderClear(m_game_renderer); + bool quit = false; + SDL_Event event; + + while (!quit) { + Uint32 ticks = SDL_GetTicks(); + int sprite = (ticks / 100) % 4; + ss.select_sprite(sprite, 0); + + while (SDL_PollEvent(&event) != NULL) { + switch (event.type) { + case SDL_QUIT: + quit = true; + break; + } + + } + + SDL_RenderClear(m_game_renderer); + SDL_RenderCopy(m_game_renderer, texture.get_texture(), NULL, NULL); + ss.draw_selected_sprite(10, 10); + SDL_RenderPresent(m_game_renderer); + } +} + + diff --git a/src/crepe/facade/SdlContext.h b/src/crepe/facade/SdlContext.h index c275300..329a374 100644 --- a/src/crepe/facade/SdlContext.h +++ b/src/crepe/facade/SdlContext.h @@ -1,29 +1,38 @@ #pragma once +#include "SDL_rect.h" #include "Texture.h" +#include "api/spritesheet.h" #include #include -#include namespace crepe { +class Texture; +class Spritesheet; + class SdlContext { +public: + void loop(const Texture& , api::Spritesheet&); + + // singleton + static SdlContext & get_instance(); 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*); + SDL_Texture* setTextureFromPath(const char*, SDL_Rect& clip, const int row, const int col); private: friend class Texture; + friend class api::Spritesheet; SDL_Window* m_game_window; SDL_Renderer* m_game_renderer; diff --git a/src/crepe/facade/Sound.h b/src/crepe/facade/Sound.h index ac93991..06e1932 100644 --- a/src/crepe/facade/Sound.h +++ b/src/crepe/facade/Sound.h @@ -6,10 +6,11 @@ #include #include "api/Resource.h" +#include "api/baseResource.h" namespace crepe { -class Sound { +class Sound : public api::BaseResource{ public: /** * \brief Pause this sample diff --git a/src/crepe/facade/Texture.cpp b/src/crepe/facade/Texture.cpp index c24312a..220ef2e 100644 --- a/src/crepe/facade/Texture.cpp +++ b/src/crepe/facade/Texture.cpp @@ -25,7 +25,11 @@ Texture::~Texture(){ } } void Texture::load(std::unique_ptr res) { + dbg_trace(); SdlContext& ctx = SdlContext::get_instance(); m_texture = ctx.setTextureFromPath(res->canonical()); } +SDL_Texture* Texture::get_texture() const{ + return m_texture; +} diff --git a/src/crepe/facade/Texture.h b/src/crepe/facade/Texture.h index 3677f6e..a5fcca9 100644 --- a/src/crepe/facade/Texture.h +++ b/src/crepe/facade/Texture.h @@ -1,24 +1,28 @@ #pragma once +#include "api/baseResource.h" +#include "facade/SdlContext.h" #include "api/Resource.h" #include #include - namespace crepe { -class Texture { +class Texture : public api::BaseResource{ public: Texture(const char * src); Texture(std::unique_ptr res); ~Texture(); + SDL_Texture* get_texture() const; private: void load(std::unique_ptr res); private: SDL_Texture* m_texture; + + friend class SdlContext; }; } // namespace crepe diff --git a/src/dummy_resource_manager.cpp b/src/dummy_resource_manager.cpp index 214c617..bb0b7af 100644 --- a/src/dummy_resource_manager.cpp +++ b/src/dummy_resource_manager.cpp @@ -2,22 +2,37 @@ -#include "api/Image_asset.h" +#include "api/game.h" #include "api/resource_manager.h" -#include -#include - - +#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(); - // make a resouce from the file path - api::Texture* img = c_ResMan.Load("../asset/texture/img.png"); + 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); - std::cout << img->getContent() << std::endl; } -- cgit v1.2.3