aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorheavydemon21 <nielsstunnebrink1@gmail.com>2024-09-22 10:58:21 +0200
committerheavydemon21 <nielsstunnebrink1@gmail.com>2024-09-22 10:58:21 +0200
commit765485ced528ca2f4cf644a1503b9446c5826731 (patch)
treee9d35c04a8768afb38873daa31d1b47a3da93e54
parent507a97739726feb74ffa91e317ead1773183ccbf (diff)
spritesheet werkt
-rw-r--r--resource-manager/CMakeLists.txt1
-rw-r--r--resource-manager/Image_asset.cpp15
-rw-r--r--resource-manager/Image_asset.h10
-rw-r--r--resource-manager/main.cpp51
-rw-r--r--resource-manager/resource_fabricator.cpp6
-rw-r--r--resource-manager/resource_manager.cpp15
-rw-r--r--resource-manager/resource_manager.h18
-rw-r--r--resource-manager/spritesheet.cpp38
-rw-r--r--resource-manager/spritesheet.h27
-rw-r--r--resource-manager/spritesheet_test.pngbin0 -> 616 bytes
-rw-r--r--resource-manager/test.cpp0
11 files changed, 131 insertions, 50 deletions
diff --git a/resource-manager/CMakeLists.txt b/resource-manager/CMakeLists.txt
index 5c2c962..c25ae2b 100644
--- a/resource-manager/CMakeLists.txt
+++ b/resource-manager/CMakeLists.txt
@@ -23,6 +23,7 @@ add_executable(main
Image_asset.cpp
resource_fabricator.cpp
resource_manager.cpp
+ spritesheet.cpp
constants.cpp
)
diff --git a/resource-manager/Image_asset.cpp b/resource-manager/Image_asset.cpp
index 77eee2f..10b35f8 100644
--- a/resource-manager/Image_asset.cpp
+++ b/resource-manager/Image_asset.cpp
@@ -1,12 +1,21 @@
#include "Image_asset.h"
+#include "spritesheet.h"
#include <SDL2/SDL_surface.h>
+#include <SDL_image.h>
+#include <string>
+Image::Image(const std::string& path){
+ surface = IMG_Load(path.c_str());
+}
+
Image::~Image(){
- if (surface) {
- SDL_FreeSurface(surface);
- }
+ SDL_FreeSurface(surface);
+}
+
+SDL_Surface* Image::getSurface() const {
+ return surface;
}
diff --git a/resource-manager/Image_asset.h b/resource-manager/Image_asset.h
index e7d13e9..8563352 100644
--- a/resource-manager/Image_asset.h
+++ b/resource-manager/Image_asset.h
@@ -3,7 +3,9 @@
#include "resource.h"
+#include "spritesheet.h"
#include <SDL_surface.h>
+#include <string>
@@ -12,8 +14,12 @@
class Image : public Resource {
public:
- SDL_Surface* surface = nullptr;
-
+ Image(const std::string& path);
~Image();
+ SDL_Surface* getSurface() const;
+
+private:
+ SDL_Surface* surface;
};
+
diff --git a/resource-manager/main.cpp b/resource-manager/main.cpp
index 62426d1..6dc362b 100644
--- a/resource-manager/main.cpp
+++ b/resource-manager/main.cpp
@@ -2,56 +2,61 @@
#include "Image_asset.h"
#include "resource_manager.h"
+#include "spritesheet.h"
#include <SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL_events.h>
#include <SDL_render.h>
+#include <SDL_stdinc.h>
#include <SDL_surface.h>
#include <SDL_timer.h>
#include <SDL_video.h>
#include <cstddef>
-
-int main(){
+int main() {
SDL_Init(SDL_INIT_VIDEO);
- ResourceManager* rm = new ResourceManager;
-
+ ResourceManager * rm = new ResourceManager;
- Image* img = static_cast<Image*>(rm->Load("../img.png"));
+ //Image* img = rm->Load<Image>("../spritesheet_test.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_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);
+ SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0);
+ SpriteSheet spritesheet("../spritesheet_test.png", *renderer, 1, 4);
+ SDL_SetRenderDrawColor(renderer, 168, 230, 255, 255);
+ SDL_RenderClear(renderer);
while (!quit) {
- SDL_WaitEvent(&event);
-
- switch (event.type) {
- case SDL_QUIT:
- quit = true;
- break;
+ 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) {
+ case SDL_QUIT:
+ quit = true;
+ break;
+ }
}
- SDL_RenderCopy(renderer, texture, NULL, NULL);
+ SDL_RenderClear(renderer);
+ spritesheet.select_sprite(sprite, 0);
+ spritesheet.draw_selected_sprite(renderer, &dstrect);
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_fabricator.cpp b/resource-manager/resource_fabricator.cpp
index 03e3f74..2b1f5f7 100644
--- a/resource-manager/resource_fabricator.cpp
+++ b/resource-manager/resource_fabricator.cpp
@@ -15,12 +15,8 @@ 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;
+ return new Image(file_path);
}
else if ( extension == Constants::OGG_EXT ){
return new Audio;
diff --git a/resource-manager/resource_manager.cpp b/resource-manager/resource_manager.cpp
index ce4d5dc..e8828a4 100644
--- a/resource-manager/resource_manager.cpp
+++ b/resource-manager/resource_manager.cpp
@@ -24,21 +24,6 @@ ResourceManager::~ResourceManager(){
}
-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()){
diff --git a/resource-manager/resource_manager.h b/resource-manager/resource_manager.h
index 7a86360..fd6657c 100644
--- a/resource-manager/resource_manager.h
+++ b/resource-manager/resource_manager.h
@@ -2,12 +2,14 @@
+#include <algorithm>
#include <string>
#include <unordered_map>
#include "resource.h"
#include "constants.h"
+#include "resource_fabricator.h"
using namespace crepe;
@@ -20,9 +22,21 @@ public:
ResourceManager();
~ResourceManager();
- Resource* Load(const Constants::FILE_PATH& file_path);
- void Unload(const Constants::FILE_PATH& file_path);
+ template<typename T>
+ T* Load(const Constants::FILE_PATH& file_path){
+
+ if (m_resources.find(file_path) != m_resources.end()) {
+ return static_cast<T*>(m_resources[file_path]);
+ }
+
+ Resource* resource = ResourceFactory::create_resource(file_path);
+ if (resource) {
+ m_resources[file_path] = std::move(resource);
+ }
+ return static_cast<T*>(m_resources[file_path]);
+ }
+ void Unload(const Constants::FILE_PATH& file_path);
private:
std::unordered_map<Constants::FILE_PATH, Resource*> m_resources;
diff --git a/resource-manager/spritesheet.cpp b/resource-manager/spritesheet.cpp
new file mode 100644
index 0000000..c8e1b70
--- /dev/null
+++ b/resource-manager/spritesheet.cpp
@@ -0,0 +1,38 @@
+
+
+#include "spritesheet.h"
+
+#include <SDL2/SDL.h>
+#include <SDL_image.h>
+#include <SDL_render.h>
+#include <SDL_surface.h>
+#include <cstdlib>
+#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(){
+ SDL_DestroyTexture(texture);
+ SDL_FreeSurface(m_spritesheet_image);
+}
+
+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(SDL_Renderer* window_surface, SDL_Rect* position){
+ SDL_RenderCopy(window_surface, texture, &m_clip, position);
+}
+
+
+SDL_Surface* SpriteSheet::getSurface() const {
+ return m_spritesheet_image;
+}
diff --git a/resource-manager/spritesheet.h b/resource-manager/spritesheet.h
new file mode 100644
index 0000000..022b6cf
--- /dev/null
+++ b/resource-manager/spritesheet.h
@@ -0,0 +1,27 @@
+#pragma once
+
+
+
+
+#include <SDL_rect.h>
+#include <SDL_render.h>
+#include <SDL_surface.h>
+#include <string>
+class SpriteSheet{
+
+
+public:
+ SpriteSheet(const std::string& path, SDL_Renderer& renderer, const int row, const int column);
+ ~SpriteSheet();
+
+
+ void select_sprite(const int x, const int y);
+ void draw_selected_sprite(SDL_Renderer* window_surface, SDL_Rect* position);
+ SDL_Surface* getSurface() const;
+
+private:
+ SDL_Rect m_clip;
+ SDL_Surface* m_spritesheet_image;
+ SDL_Texture * texture;
+
+};
diff --git a/resource-manager/spritesheet_test.png b/resource-manager/spritesheet_test.png
new file mode 100644
index 0000000..d68a72a
--- /dev/null
+++ b/resource-manager/spritesheet_test.png
Binary files differ
diff --git a/resource-manager/test.cpp b/resource-manager/test.cpp
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/resource-manager/test.cpp