diff options
-rw-r--r-- | resource-manager/Audio_asset.cpp | 0 | ||||
-rw-r--r-- | resource-manager/Audio_asset.h | 12 | ||||
-rw-r--r-- | resource-manager/CMakeLists.txt | 34 | ||||
-rw-r--r-- | resource-manager/Image_asset.cpp | 12 | ||||
-rw-r--r-- | resource-manager/Image_asset.h | 19 | ||||
-rw-r--r-- | resource-manager/Untitled.jpeg | bin | 0 -> 6756 bytes | |||
-rw-r--r-- | resource-manager/constants.cpp | 10 | ||||
-rw-r--r-- | resource-manager/constants.h | 16 | ||||
-rw-r--r-- | resource-manager/img.png | bin | 0 -> 92742 bytes | |||
-rw-r--r-- | resource-manager/main.cpp | 57 | ||||
-rw-r--r-- | resource-manager/resource.h | 11 | ||||
-rw-r--r-- | resource-manager/resource_fabricator.cpp | 30 | ||||
-rw-r--r-- | resource-manager/resource_fabricator.h | 23 | ||||
-rw-r--r-- | resource-manager/resource_manager.cpp | 48 | ||||
-rw-r--r-- | resource-manager/resource_manager.h | 32 |
15 files changed, 304 insertions, 0 deletions
diff --git a/resource-manager/Audio_asset.cpp b/resource-manager/Audio_asset.cpp new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/resource-manager/Audio_asset.cpp diff --git a/resource-manager/Audio_asset.h b/resource-manager/Audio_asset.h new file mode 100644 index 0000000..87fa762 --- /dev/null +++ b/resource-manager/Audio_asset.h @@ -0,0 +1,12 @@ +#pragma once + + +#include "resource.h" + + +class Audio : public Resource { + + + + +}; diff --git a/resource-manager/CMakeLists.txt b/resource-manager/CMakeLists.txt new file mode 100644 index 0000000..5c2c962 --- /dev/null +++ b/resource-manager/CMakeLists.txt @@ -0,0 +1,34 @@ +cmake_minimum_required(VERSION 3.28) + +# Set C and C++ standards +set(CMAKE_C_STANDARD 11) +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_EXPORT_COMPILE_COMMANDS 1) + +# Enable debug features +set(CMAKE_BUILD_TYPE Debug) +add_compile_definitions(DEBUG) + +# Project name and supported languages +project(crepe C CXX) + +# Find SDL2 +find_package(SDL2 REQUIRED) +find_package(SDL2_image REQUIRED) + +# Add all source files to the executable +add_executable(main + main.cpp + Audio_asset.cpp + Image_asset.cpp + resource_fabricator.cpp + resource_manager.cpp + constants.cpp +) + +# Include directories for headers +target_include_directories(main PRIVATE ${SDL2_INCLUDE_DIRS} ${SDL2_IMAGE_INCLUDE_DIRS} .) + +# Link SDL2 library +target_link_libraries(main PRIVATE ${SDL2_LIBRARIES} SDL2_image::SDL2_image) + diff --git a/resource-manager/Image_asset.cpp b/resource-manager/Image_asset.cpp new file mode 100644 index 0000000..77eee2f --- /dev/null +++ b/resource-manager/Image_asset.cpp @@ -0,0 +1,12 @@ + + +#include "Image_asset.h" +#include <SDL2/SDL_surface.h> + + + +Image::~Image(){ + if (surface) { + SDL_FreeSurface(surface); + } +} diff --git a/resource-manager/Image_asset.h b/resource-manager/Image_asset.h new file mode 100644 index 0000000..e7d13e9 --- /dev/null +++ b/resource-manager/Image_asset.h @@ -0,0 +1,19 @@ +#pragma once + + + +#include "resource.h" +#include <SDL_surface.h> + + + + + +class Image : public Resource { + +public: + SDL_Surface* surface = nullptr; + + ~Image(); + +}; diff --git a/resource-manager/Untitled.jpeg b/resource-manager/Untitled.jpeg Binary files differnew file mode 100644 index 0000000..aa4d289 --- /dev/null +++ b/resource-manager/Untitled.jpeg diff --git a/resource-manager/constants.cpp b/resource-manager/constants.cpp new file mode 100644 index 0000000..f9fbf5b --- /dev/null +++ b/resource-manager/constants.cpp @@ -0,0 +1,10 @@ +#include "constants.h" +#include <string> + + + +namespace crepe { + + const std::string Constants::PNG_EXT = ".png"; + const std::string Constants::OGG_EXT = ".ogg"; +} diff --git a/resource-manager/constants.h b/resource-manager/constants.h new file mode 100644 index 0000000..620ddf3 --- /dev/null +++ b/resource-manager/constants.h @@ -0,0 +1,16 @@ + +#pragma once + +#include <string> +namespace crepe { + + class Constants { + public: + using FILE_PATH = std::string; + + + static const std::string PNG_EXT; + static const std::string OGG_EXT; + + }; +} diff --git a/resource-manager/img.png b/resource-manager/img.png Binary files differnew file mode 100644 index 0000000..43b1eca --- /dev/null +++ b/resource-manager/img.png diff --git a/resource-manager/main.cpp b/resource-manager/main.cpp new file mode 100644 index 0000000..62426d1 --- /dev/null +++ b/resource-manager/main.cpp @@ -0,0 +1,57 @@ + + +#include "Image_asset.h" +#include "resource_manager.h" +#include <SDL.h> +#include <SDL2/SDL_image.h> +#include <SDL_events.h> +#include <SDL_render.h> +#include <SDL_surface.h> +#include <SDL_timer.h> +#include <SDL_video.h> +#include <cstddef> + + +int main(){ + SDL_Init(SDL_INIT_VIDEO); + ResourceManager* rm = new ResourceManager; + + + Image* img = static_cast<Image*>(rm->Load("../img.png")); + //Resource* sound = rm->Load("/sound.ogg"); + + + bool quit = false; + + SDL_Event event; + + SDL_Window* window = SDL_CreateWindow("Tessting resources", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0); + + SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0); + SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, img->surface); + + + while (!quit) { + SDL_WaitEvent(&event); + + switch (event.type) { + case SDL_QUIT: + quit = true; + break; + } + + SDL_RenderCopy(renderer, texture, NULL, NULL); + SDL_RenderPresent(renderer); + + SDL_Delay(100); + + } + SDL_DestroyTexture(texture); + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + SDL_Quit(); + + + + return 0; +} diff --git a/resource-manager/resource.h b/resource-manager/resource.h new file mode 100644 index 0000000..6bc44ed --- /dev/null +++ b/resource-manager/resource.h @@ -0,0 +1,11 @@ +#pragma once + + + + +class Resource{ +public: + + virtual ~Resource() =default; + +}; diff --git a/resource-manager/resource_fabricator.cpp b/resource-manager/resource_fabricator.cpp new file mode 100644 index 0000000..03e3f74 --- /dev/null +++ b/resource-manager/resource_fabricator.cpp @@ -0,0 +1,30 @@ + + +#include <filesystem> +#include <string> + +#include "resource_fabricator.h" +#include "resource.h" +#include "Image_asset.h" +#include "Audio_asset.h" + +#include <SDL2/SDL_image.h> + + +Resource* ResourceFactory::create_resource(const Constants::FILE_PATH &file_path){ + + std::string extension = std::filesystem::path(file_path).extension(); + + + + if( extension == Constants::PNG_EXT ) { + Image* img = new Image; + img->surface = IMG_Load(file_path.c_str()); + return img; + } + else if ( extension == Constants::OGG_EXT ){ + return new Audio; + } + + return nullptr; +} diff --git a/resource-manager/resource_fabricator.h b/resource-manager/resource_fabricator.h new file mode 100644 index 0000000..3489341 --- /dev/null +++ b/resource-manager/resource_fabricator.h @@ -0,0 +1,23 @@ +#pragma once + + + +#include "resource.h" +#include "constants.h" + + +using namespace crepe; + + +class ResourceFactory { + +public: + + static Resource* create_resource(const Constants::FILE_PATH& file_path); + + +}; + + + + diff --git a/resource-manager/resource_manager.cpp b/resource-manager/resource_manager.cpp new file mode 100644 index 0000000..ce4d5dc --- /dev/null +++ b/resource-manager/resource_manager.cpp @@ -0,0 +1,48 @@ + + +#include "resource_manager.h" +#include "constants.h" +#include "resource.h" +#include "resource_fabricator.h" +#include <SDL2/SDL_image.h> +#include <unordered_map> + + +ResourceManager::ResourceManager(){ + IMG_Init(IMG_INIT_PNG); +} + +ResourceManager::~ResourceManager(){ + + for(auto pair : m_resources){ + delete pair.second; + } + + m_resources.clear(); + + IMG_Quit(); +} + + +Resource* ResourceManager::Load(const Constants::FILE_PATH& file_path){ + + if(m_resources.find(file_path) != m_resources.end( )){ + return m_resources[file_path]; + } + + Resource* res = ResourceFactory::create_resource(file_path); + if(res){ + m_resources[file_path] = std::move(res); + } + + return m_resources[file_path]; +} + + +void ResourceManager::Unload(const Constants::FILE_PATH& file_path){ + std::unordered_map<Constants::FILE_PATH, Resource* >::iterator itr = m_resources.find(file_path); + if(itr != m_resources.end()){ + delete itr->second; + m_resources.erase(itr); + } +} diff --git a/resource-manager/resource_manager.h b/resource-manager/resource_manager.h new file mode 100644 index 0000000..7a86360 --- /dev/null +++ b/resource-manager/resource_manager.h @@ -0,0 +1,32 @@ +#pragma once + + + +#include <string> +#include <unordered_map> + + +#include "resource.h" +#include "constants.h" + +using namespace crepe; + + + +class ResourceManager{ + +public: + + ResourceManager(); + ~ResourceManager(); + + Resource* Load(const Constants::FILE_PATH& file_path); + void Unload(const Constants::FILE_PATH& file_path); + + +private: + std::unordered_map<Constants::FILE_PATH, Resource*> m_resources; + + + +}; |