#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 ); }