From 35465f27ed87646a18cd9298eae861f14385300e Mon Sep 17 00:00:00 2001 From: Ekaitz Zarraga Date: Thu, 23 Dec 2021 20:52:49 +0100 Subject: First commit: moves a simple thing on the screen --- src/texture.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/texture.cpp (limited to 'src/texture.cpp') 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 +#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 ); +} -- cgit v1.2.3