aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorheavydemon21 <nielsstunnebrink1@gmail.com>2024-10-02 15:57:59 +0200
committerheavydemon21 <nielsstunnebrink1@gmail.com>2024-10-02 15:57:59 +0200
commitde2c2593f9f272c5151d74af4ff846fdd70a9bc7 (patch)
tree4001a60809734a60b75e39da5c994d9381b6efe2
parent41bd601ef62d967c80cc0591eaf6b4290baae425 (diff)
working resource manager and textures and sprite to new standard
-rw-r--r--.gitmodules4
-rw-r--r--asset/spritesheet/spritesheet_test.pngbin0 -> 616 bytes
-rw-r--r--lib/sdl_image/CMakeLists.txt34
m---------lib/sdl_image/src0
-rw-r--r--src/CMakeLists.txt6
-rw-r--r--src/crepe/api/CMakeLists.txt13
-rw-r--r--src/crepe/api/Image_asset.cpp14
-rw-r--r--src/crepe/api/Image_asset.h18
-rw-r--r--src/crepe/api/Resource.cpp6
-rw-r--r--src/crepe/api/baseResource.h11
-rw-r--r--src/crepe/api/game.cpp18
-rw-r--r--src/crepe/api/game.h15
-rw-r--r--src/crepe/api/resource_manager.h12
-rw-r--r--src/crepe/api/spritesheet.cpp36
-rw-r--r--src/crepe/api/spritesheet.h27
-rw-r--r--src/crepe/facade/SdlContext.cpp73
-rw-r--r--src/crepe/facade/SdlContext.h15
-rw-r--r--src/crepe/facade/Sound.h3
-rw-r--r--src/crepe/facade/Texture.cpp4
-rw-r--r--src/crepe/facade/Texture.h8
-rw-r--r--src/dummy_resource_manager.cpp31
21 files changed, 272 insertions, 76 deletions
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
--- /dev/null
+++ b/asset/spritesheet/spritesheet_test.png
Binary files 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
+Subproject c6c7278b86b5de1232b10de8f612ed05cf2d11f
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 <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
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 <string>
-
-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 <filesystem>
+#include <iostream>
+#include <iterator>
#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 <vector>
+
+
+void game::render(std::vector<crepe::Texture*> & draw, std::vector<crepe::api::Spritesheet*> & 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 <vector>
+
+class game {
+
+public:
+ game(){}
+ ~game(){}
+
+ void render(std::vector<crepe::Texture*>&, std::vector<crepe::api::Spritesheet*>&);
+};
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 <unordered_map>
#include <utility>
+#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<api::Resource>> m_resources;
+ std::unordered_map< std::string, std::unique_ptr<BaseResource>> m_resources;
protected:
@@ -44,10 +42,10 @@ public:
return static_cast<T*>(m_resources[file_path].get());
}
- std::unique_ptr<api::Resource> resource = ResourceFactory::create_resource<T>(file_path);
+ auto resource = std::make_unique<T>(file_path.c_str());
if (resource) {
m_resources[file_path] = std::move(resource);
- return static_cast<T*>(m_resources[file_path].get() );
+ return static_cast<T*>(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 <memory>
-#include <string>
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<api::Resource>(src), row, col);
}
-SpriteSheet::~SpriteSheet(){
+Spritesheet::Spritesheet(std::unique_ptr<api::Resource> 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<api::Resource> 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 <string>
+#include "SDL_rect.h"
+#include "SDL_render.h"
+#include <memory>
namespace crepe::api {
+class Spritesheet{
+public:
+ Spritesheet(const char * src, const int row , const int col);
+ Spritesheet(std::unique_ptr<api::Resource> 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<api::Resource> 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 <SDL2/SDL.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_surface.h>
#include <SDL2/SDL_video.h>
#include <SDL2/SDL_image.h>
+#include <iostream>
+#include <iterator>
+#include <ostream>
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 <SDL2/SDL_render.h>
#include <SDL2/SDL_video.h>
-#include <string>
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 <memory>
#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<api::Resource> 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 <SDL2/SDL_render.h>
#include <memory>
-
namespace crepe {
-class Texture {
+class Texture : public api::BaseResource{
public:
Texture(const char * src);
Texture(std::unique_ptr<api::Resource> res);
~Texture();
+ SDL_Texture* get_texture() const;
private:
void load(std::unique_ptr<api::Resource> 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 <iostream>
-#include <ostream>
-
-
+#include "api/spritesheet.h"
+#include "facade/Texture.h"
+#include "util/log.h"
+#include <vector>
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<api::Texture>("../asset/texture/img.png");
+ auto test = c_ResMan.Load<Texture>("../asset/texture/img.png");
+
+ auto img = Texture("../asset/texture/img.png");
+
+ auto SS = api::Spritesheet("../asset/spritesheet/spritesheet_test.png", 1 , 4);
+
+ std::vector<Texture*> t = {test};
+ std::vector<api::Spritesheet*> s = {&SS};
+
+ engine.render(t, s);
- std::cout << img->getContent() << std::endl;
}