summaryrefslogtreecommitdiff
path: root/src/texture.cpp
diff options
context:
space:
mode:
authorEkaitz Zarraga <ekaitz@elenq.tech>2021-12-23 20:52:49 +0100
committerEkaitz Zarraga <ekaitz@elenq.tech>2021-12-23 20:52:49 +0100
commit35465f27ed87646a18cd9298eae861f14385300e (patch)
tree7f0256d28fa6fd5368b7a39500d080456e70be8e /src/texture.cpp
First commit: moves a simple thing on the screen
Diffstat (limited to 'src/texture.cpp')
-rw-r--r--src/texture.cpp46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/texture.cpp b/src/texture.cpp
new file mode 100644
index 0000000..6657a4a
--- /dev/null
+++ b/src/texture.cpp
@@ -0,0 +1,46 @@
+#include <SDL2/SDL_image.h>
+#include "texture.h"
+
+Texture::Texture(SDL_Renderer* renderer):
+ texture_(nullptr),
+ w_(0),
+ h_(0),
+ renderer_ (renderer){
+}
+
+Texture::~Texture(){
+ if(texture_){
+ SDL_DestroyTexture(texture_);
+ }
+}
+
+int Texture::loadFromFile(std::string path){
+ SDL_Surface* Loading_Surf = IMG_Load(path.c_str());
+ if(Loading_Surf == NULL){
+ printf("Failed to load surface: %s\n", SDL_GetError());
+ return -1;
+ }
+ texture_ = SDL_CreateTextureFromSurface(renderer_, Loading_Surf);
+ if(texture_ == NULL){
+ printf("Failed to create texture: %s\n", SDL_GetError());
+ return -1;
+ }
+ SDL_FreeSurface(Loading_Surf);
+ w_ = Loading_Surf->w;
+ h_ = Loading_Surf->h;
+ return 0;
+}
+
+void Texture::render(int x, int y, SDL_Rect* clip){
+ SDL_Rect renderQuad = { x, y, w_, h_ };
+
+ //Set clip rendering dimensions
+ if( clip )
+ {
+ renderQuad.w = clip->w;
+ renderQuad.h = clip->h;
+ }
+
+ //Render to screen
+ SDL_RenderCopy( renderer_, texture_, clip, &renderQuad );
+}