diff options
author | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-10-01 19:53:56 +0200 |
---|---|---|
committer | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-10-01 19:53:56 +0200 |
commit | ef44da3f5e9ca533782da5e185e69e28c295d226 (patch) | |
tree | d44f4f849245f640eff874d53ab610cff2fbe53b /src | |
parent | 8e66301577551bc5b8a2e169be173e71de2f5e4e (diff) |
Added resources to crepe
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 21 | ||||
-rwxr-xr-x | src/build.sh | 7 | ||||
-rw-r--r-- | src/crepe/CMakeLists.txt | 9 | ||||
-rw-r--r-- | src/crepe/api/Audio_asset.cpp | 16 | ||||
-rw-r--r-- | src/crepe/api/Audio_asset.h | 18 | ||||
-rw-r--r-- | src/crepe/api/CMakeLists.txt | 17 | ||||
-rw-r--r-- | src/crepe/api/Image_asset.cpp | 14 | ||||
-rw-r--r-- | src/crepe/api/Image_asset.h | 18 | ||||
-rw-r--r-- | src/crepe/api/map_asset.cpp | 12 | ||||
-rw-r--r-- | src/crepe/api/map_asset.h | 14 | ||||
-rw-r--r-- | src/crepe/api/resource.h | 23 | ||||
-rw-r--r-- | src/crepe/api/resource_manager.cpp | 25 | ||||
-rw-r--r-- | src/crepe/api/resource_manager.h | 59 | ||||
-rw-r--r-- | src/crepe/api/spritesheet.cpp | 16 | ||||
-rw-r--r-- | src/crepe/api/spritesheet.h | 21 | ||||
-rw-r--r-- | src/crepe/fabricator/CMakeLists.txt | 8 | ||||
-rw-r--r-- | src/crepe/fabricator/resource_fabricator.cpp | 26 | ||||
-rw-r--r-- | src/crepe/fabricator/resource_fabricator.h | 29 | ||||
-rw-r--r-- | src/crepe/main.cpp | 3 | ||||
-rw-r--r-- | src/dummy_resource_manager.cpp | 23 |
20 files changed, 373 insertions, 6 deletions
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 <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 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 <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 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 <string> + +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 <string> + +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 <string> + + +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 <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 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 <string> +#include <unordered_map> + +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 <memory> +#include <string> +#include <unordered_map> +#include <utility> + + +#include "api/resource.h" +#include "fabricator/resource_fabricator.h" + + + namespace crepe::api{ + +class ResourceManager{ + + +private: + + std::unordered_map< std::string, std::unique_ptr<api::Resource>> 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<typename T> + T* Load(const std::string& file_path){ + + if (m_resources.find(file_path) != m_resources.end()) { + return static_cast<T*>(m_resources[file_path].get()); + } + + std::unique_ptr<api::Resource> resource = ResourceFactory::create_resource<T>(file_path); + if (resource) { + m_resources[file_path] = std::move(resource); + return static_cast<T*>(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 <string> + +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 <string> + + +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 <fstream> +#include <iostream> +#include <string> +#include <vector> + + + + +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<char> 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 <memory> +#include <string> + + + + +class ResourceFactory { + +public: + + template<typename T> + static std::unique_ptr<crepe::api::Resource> create_resource(const std::string& file_path){ + + return std::make_unique<T>(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 <stdio.h> - -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 <iostream> +#include <ostream> + + + +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<api::Texture>("../asset/texture/img.png"); + + std::cout << img->getContent() << std::endl; +} |