From 51f7699966ab856c873648ee25621b80421f8c8f Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Mon, 16 Sep 2024 17:34:05 +0200 Subject: loading images work through resource manager, however needs return fixing. cause right now static cast needs to be done outside --- resource-manager/Audio_asset.cpp | 0 resource-manager/Audio_asset.h | 12 +++++++ resource-manager/CMakeLists.txt | 34 ++++++++++++++++++ resource-manager/Image_asset.cpp | 12 +++++++ resource-manager/Image_asset.h | 19 +++++++++++ resource-manager/Untitled.jpeg | Bin 0 -> 6756 bytes resource-manager/constants.cpp | 10 ++++++ resource-manager/constants.h | 16 +++++++++ resource-manager/img.png | Bin 0 -> 92742 bytes resource-manager/main.cpp | 57 +++++++++++++++++++++++++++++++ resource-manager/resource.h | 11 ++++++ resource-manager/resource_fabricator.cpp | 30 ++++++++++++++++ resource-manager/resource_fabricator.h | 23 +++++++++++++ resource-manager/resource_manager.cpp | 48 ++++++++++++++++++++++++++ resource-manager/resource_manager.h | 32 +++++++++++++++++ 15 files changed, 304 insertions(+) create mode 100644 resource-manager/Audio_asset.cpp create mode 100644 resource-manager/Audio_asset.h create mode 100644 resource-manager/CMakeLists.txt create mode 100644 resource-manager/Image_asset.cpp create mode 100644 resource-manager/Image_asset.h create mode 100644 resource-manager/Untitled.jpeg create mode 100644 resource-manager/constants.cpp create mode 100644 resource-manager/constants.h create mode 100644 resource-manager/img.png create mode 100644 resource-manager/main.cpp create mode 100644 resource-manager/resource.h create mode 100644 resource-manager/resource_fabricator.cpp create mode 100644 resource-manager/resource_fabricator.h create mode 100644 resource-manager/resource_manager.cpp create mode 100644 resource-manager/resource_manager.h diff --git a/resource-manager/Audio_asset.cpp b/resource-manager/Audio_asset.cpp new file mode 100644 index 0000000..e69de29 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 + + + +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 + + + + + +class Image : public Resource { + +public: + SDL_Surface* surface = nullptr; + + ~Image(); + +}; diff --git a/resource-manager/Untitled.jpeg b/resource-manager/Untitled.jpeg new file mode 100644 index 0000000..aa4d289 Binary files /dev/null and b/resource-manager/Untitled.jpeg differ 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 + + + +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 +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 new file mode 100644 index 0000000..43b1eca Binary files /dev/null and b/resource-manager/img.png differ 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 +#include +#include +#include +#include +#include +#include +#include + + +int main(){ + SDL_Init(SDL_INIT_VIDEO); + ResourceManager* rm = new ResourceManager; + + + Image* img = static_cast(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 +#include + +#include "resource_fabricator.h" +#include "resource.h" +#include "Image_asset.h" +#include "Audio_asset.h" + +#include + + +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 +#include + + +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::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 +#include + + +#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 m_resources; + + + +}; -- cgit v1.2.3