aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--resource-manager/Audio_asset.h5
-rw-r--r--resource-manager/Image_asset.cpp25
-rw-r--r--resource-manager/Image_asset.h13
-rw-r--r--resource-manager/main.cpp22
-rw-r--r--resource-manager/resource_fabricator.cpp22
-rw-r--r--resource-manager/resource_fabricator.h11
-rw-r--r--resource-manager/resource_manager.cpp2
-rw-r--r--resource-manager/resource_manager.h15
-rw-r--r--resource-manager/spritesheet.cpp19
-rw-r--r--resource-manager/spritesheet.h14
10 files changed, 82 insertions, 66 deletions
diff --git a/resource-manager/Audio_asset.h b/resource-manager/Audio_asset.h
index 87fa762..7f7f8cd 100644
--- a/resource-manager/Audio_asset.h
+++ b/resource-manager/Audio_asset.h
@@ -2,10 +2,15 @@
#include "resource.h"
+#include <string>
class Audio : public Resource {
+public:
+ Audio(const std::string& path);
+ ~Audio();
+
diff --git a/resource-manager/Image_asset.cpp b/resource-manager/Image_asset.cpp
index 10b35f8..791e988 100644
--- a/resource-manager/Image_asset.cpp
+++ b/resource-manager/Image_asset.cpp
@@ -1,21 +1,32 @@
#include "Image_asset.h"
-#include "spritesheet.h"
#include <SDL2/SDL_surface.h>
#include <SDL_image.h>
+#include <SDL_render.h>
#include <string>
-Image::Image(const std::string& path){
- surface = IMG_Load(path.c_str());
+Texture::Texture(const std::string& path){
+ m_surface = IMG_Load(path.c_str());
}
+void Texture::setTexture(SDL_Renderer& renderer){
+ m_texture = SDL_CreateTextureFromSurface(&renderer, m_surface);
+}
+
+
+Texture::~Texture(){
+ SDL_FreeSurface(m_surface);
+
+ if(m_texture)
+ SDL_DestroyTexture(m_texture);
+}
-Image::~Image(){
- SDL_FreeSurface(surface);
+SDL_Surface* Texture::getSurface() const {
+ return m_surface;
}
-SDL_Surface* Image::getSurface() const {
- return surface;
+SDL_Texture* Texture::getTexture() const{
+ return m_texture;
}
diff --git a/resource-manager/Image_asset.h b/resource-manager/Image_asset.h
index 8563352..88c0fb0 100644
--- a/resource-manager/Image_asset.h
+++ b/resource-manager/Image_asset.h
@@ -3,7 +3,7 @@
#include "resource.h"
-#include "spritesheet.h"
+#include <SDL_render.h>
#include <SDL_surface.h>
#include <string>
@@ -11,15 +11,18 @@
-class Image : public Resource {
+class Texture : public Resource {
public:
- Image(const std::string& path);
- ~Image();
+ Texture(const std::string& path);
+ ~Texture();
+ void setTexture(SDL_Renderer& renderer);
SDL_Surface* getSurface() const;
+ SDL_Texture* getTexture() const;
private:
- SDL_Surface* surface;
+ SDL_Surface* m_surface;
+ SDL_Texture* m_texture;
};
diff --git a/resource-manager/main.cpp b/resource-manager/main.cpp
index 6dc362b..dc087c6 100644
--- a/resource-manager/main.cpp
+++ b/resource-manager/main.cpp
@@ -15,10 +15,6 @@
int main() {
SDL_Init(SDL_INIT_VIDEO);
- ResourceManager * rm = new ResourceManager;
-
- //Image* img = rm->Load<Image>("../spritesheet_test.png");
- //Resource* sound = rm->Load("/sound.ogg");
bool quit = false;
@@ -28,18 +24,20 @@ int main() {
= SDL_CreateWindow("Tessting resources", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
+
SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0);
- SpriteSheet spritesheet("../spritesheet_test.png", *renderer, 1, 4);
+ ResourceManager * rm = new ResourceManager();
+
+ Texture* img = rm->Load<Texture>("../img.png");
+ img->setTexture(*renderer);
+
+ SpriteSheet* SS = rm->Load<SpriteSheet>("../spritesheet_test.png");
+
SDL_SetRenderDrawColor(renderer, 168, 230, 255, 255);
SDL_RenderClear(renderer);
while (!quit) {
- Uint32 ticks = SDL_GetTicks();
- int sprite = (ticks / 100) % 4;
-
- //SDL_Rect srcrect = { sprite * 32, 0, 32, 64 };
- SDL_Rect dstrect = {10, 10, 32, 64};
while (SDL_PollEvent(&event) != NULL) {
switch (event.type) {
@@ -49,9 +47,9 @@ int main() {
}
}
+
SDL_RenderClear(renderer);
- spritesheet.select_sprite(sprite, 0);
- spritesheet.draw_selected_sprite(renderer, &dstrect);
+ SDL_RenderCopy(renderer, img->getTexture(), NULL, NULL);
SDL_RenderPresent(renderer);
}
SDL_DestroyRenderer(renderer);
diff --git a/resource-manager/resource_fabricator.cpp b/resource-manager/resource_fabricator.cpp
index 2b1f5f7..36353e4 100644
--- a/resource-manager/resource_fabricator.cpp
+++ b/resource-manager/resource_fabricator.cpp
@@ -1,26 +1,4 @@
-#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 ) {
- return new Image(file_path);
- }
- 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
index 3489341..2d46b00 100644
--- a/resource-manager/resource_fabricator.h
+++ b/resource-manager/resource_fabricator.h
@@ -2,8 +2,13 @@
+#include "Audio_asset.h"
+#include "Image_asset.h"
#include "resource.h"
#include "constants.h"
+#include "spritesheet.h"
+#include <SDL_render.h>
+
using namespace crepe;
@@ -13,8 +18,10 @@ class ResourceFactory {
public:
- static Resource* create_resource(const Constants::FILE_PATH& file_path);
-
+ template<typename T>
+ static Resource* create_resource(const Constants::FILE_PATH& file_path){
+ return new T(file_path);
+ }
};
diff --git a/resource-manager/resource_manager.cpp b/resource-manager/resource_manager.cpp
index e8828a4..99918d1 100644
--- a/resource-manager/resource_manager.cpp
+++ b/resource-manager/resource_manager.cpp
@@ -5,6 +5,7 @@
#include "resource.h"
#include "resource_fabricator.h"
#include <SDL2/SDL_image.h>
+#include <SDL_render.h>
#include <unordered_map>
@@ -31,3 +32,4 @@ void ResourceManager::Unload(const Constants::FILE_PATH& file_path){
m_resources.erase(itr);
}
}
+
diff --git a/resource-manager/resource_manager.h b/resource-manager/resource_manager.h
index fd6657c..5570313 100644
--- a/resource-manager/resource_manager.h
+++ b/resource-manager/resource_manager.h
@@ -2,8 +2,7 @@
-#include <algorithm>
-#include <string>
+#include <SDL_render.h>
#include <unordered_map>
@@ -11,6 +10,15 @@
#include "constants.h"
#include "resource_fabricator.h"
+
+enum class asset_type{
+ TEXTURE,
+ SPRITESHEET,
+ AUDIO,
+ MAP,
+ UNKNOWN,
+};
+
using namespace crepe;
@@ -29,7 +37,7 @@ public:
return static_cast<T*>(m_resources[file_path]);
}
- Resource* resource = ResourceFactory::create_resource(file_path);
+ Resource* resource = ResourceFactory::create_resource<T>(file_path);
if (resource) {
m_resources[file_path] = std::move(resource);
}
@@ -42,5 +50,4 @@ private:
std::unordered_map<Constants::FILE_PATH, Resource*> m_resources;
-
};
diff --git a/resource-manager/spritesheet.cpp b/resource-manager/spritesheet.cpp
index c8e1b70..c0e4d06 100644
--- a/resource-manager/spritesheet.cpp
+++ b/resource-manager/spritesheet.cpp
@@ -1,6 +1,7 @@
#include "spritesheet.h"
+#include "Image_asset.h"
#include <SDL2/SDL.h>
#include <SDL_image.h>
@@ -10,17 +11,17 @@
#include <string>
-SpriteSheet::SpriteSheet(const std::string& path, SDL_Renderer& renderer, const int row, const int column){
- m_spritesheet_image = IMG_Load(path.c_str());
- texture = SDL_CreateTextureFromSurface(&renderer, m_spritesheet_image);
- m_clip.w = m_spritesheet_image->w / column;
- m_clip.h = m_spritesheet_image->h / row;
+SpriteSheet::SpriteSheet(const std::string& path){
+ m_spritesheet = new Texture(path);
+}
+void SpriteSheet::set_spritesheet_data(SDL_Renderer& renderer, const int row, const int column){
+ m_clip.w = m_spritesheet->getSurface()->w / column;
+ m_clip.h = m_spritesheet->getSurface()->h / row;
}
SpriteSheet::~SpriteSheet(){
- SDL_DestroyTexture(texture);
- SDL_FreeSurface(m_spritesheet_image);
+ delete m_spritesheet;
}
void SpriteSheet::select_sprite(const int x, const int y){
@@ -29,10 +30,10 @@ void SpriteSheet::select_sprite(const int x, const int y){
}
void SpriteSheet::draw_selected_sprite(SDL_Renderer* window_surface, SDL_Rect* position){
- SDL_RenderCopy(window_surface, texture, &m_clip, position);
+ SDL_RenderCopy(window_surface, m_spritesheet->getTexture(), &m_clip, position);
}
SDL_Surface* SpriteSheet::getSurface() const {
- return m_spritesheet_image;
+ return m_spritesheet->getSurface();
}
diff --git a/resource-manager/spritesheet.h b/resource-manager/spritesheet.h
index 022b6cf..be1a051 100644
--- a/resource-manager/spritesheet.h
+++ b/resource-manager/spritesheet.h
@@ -3,17 +3,23 @@
+#include "Image_asset.h"
+#include "resource.h"
#include <SDL_rect.h>
#include <SDL_render.h>
#include <SDL_surface.h>
#include <string>
-class SpriteSheet{
+
+
+class SpriteSheet : public Resource{
public:
- SpriteSheet(const std::string& path, SDL_Renderer& renderer, const int row, const int column);
+ SpriteSheet(const std::string& path);
~SpriteSheet();
+ void set_spritesheet_data(SDL_Renderer& renderer, const int row, const int column);
+
void select_sprite(const int x, const int y);
void draw_selected_sprite(SDL_Renderer* window_surface, SDL_Rect* position);
@@ -21,7 +27,5 @@ public:
private:
SDL_Rect m_clip;
- SDL_Surface* m_spritesheet_image;
- SDL_Texture * texture;
-
+ Texture* m_spritesheet;
};