#include"PSX_Texture.h" PSX_Texture::PSX_Texture(){ w = 0; h = 0; texture = NULL; imgPath = ""; TTF_Init(); } PSX_Texture::PSX_Texture(SDL_Renderer* render){ //Initialize variables w = 0; h = 0; texture = NULL; imgPath = ""; renderer = render; TTF_Init(); } PSX_Texture::~PSX_Texture(){ free(); IMG_Quit(); } void PSX_Texture::free(){ if(texture != NULL){ SDL_DestroyTexture(texture); texture = NULL; w = 0; h = 0; } } void PSX_Texture::loadTexture(std::string path){ imgPath = path; //In case of a preexisting texture free(); //In SDL to load a texture you first have to create a surface //(not loaded on VRAM bu RAM) convert it to texture (pass it to VRAM) //and then get rid of the temporal surface SDL_Surface* loadedSurface = IMG_Load( path.c_str() ); if(loadedSurface == NULL){ std::cout << "PSX_Texture: Couldn't load " << path.c_str() << std::endl; } else loadFromSurface(loadedSurface); } void PSX_Texture::loadText(std::string text, SDL_Color color, std::string path, int size){ free(); SDL_Surface* textSurface = TTF_RenderText_Solid(TTF_OpenFont( path.c_str(), size),text.c_str(),color); if(textSurface == NULL){ std::cout << "Unable to render text surface! SDL_ttf Error: " << TTF_GetError() << std::endl; } else loadFromSurface(textSurface); } void PSX_Texture::setRenderer(SDL_Renderer* render){ renderer = render; } void PSX_Texture::render(SDL_Rect* quad, SDL_Rect* frame){ SDL_RenderCopy(renderer, texture, frame, quad); } void PSX_Texture::render(SDL_Rect* quad){ SDL_Rect frame = {0,0,w,h}; render(quad, &frame); } int PSX_Texture::getW(){ return w; } int PSX_Texture::getH(){ return h; } std::string PSX_Texture::getPath(){ return imgPath; } void PSX_Texture::loadFromSurface(SDL_Surface* surface){ texture = SDL_CreateTextureFromSurface (renderer, surface); if(texture == NULL){ std::cout << "PSX_Texture: Couldn't create texture from" /*<< path.c_str()*/ << std::endl; } else{ w = surface->w; h = surface->h; } SDL_FreeSurface(surface); }