76 lines
1.4 KiB
C++
76 lines
1.4 KiB
C++
|
#include"texture.h"
|
||
|
|
||
|
PosuTexture::PosuTexture(){
|
||
|
//Initialize variables
|
||
|
texture = NULL;
|
||
|
szW = 0;
|
||
|
szH = 0;
|
||
|
|
||
|
IMG_Init(IMG_INIT_JPG);
|
||
|
//Set renderer
|
||
|
}
|
||
|
|
||
|
PosuTexture::~PosuTexture(){
|
||
|
//Deallocate
|
||
|
free();
|
||
|
}
|
||
|
|
||
|
bool PosuTexture::loadTexture(std::string path,SDL_Renderer** renderer){
|
||
|
//Get rid of preexisting texture
|
||
|
free();
|
||
|
|
||
|
//Load image at specified path
|
||
|
SDL_Surface* loadedSurface = IMG_Load( path.c_str() );
|
||
|
|
||
|
if(loadedSurface == NULL)
|
||
|
std::cout << "Couldn't load " << path.c_str() << std::endl;
|
||
|
else{
|
||
|
//Create texture from surface pixels
|
||
|
texture = SDL_CreateTextureFromSurface
|
||
|
(*renderer,loadedSurface);
|
||
|
|
||
|
if(texture == NULL){
|
||
|
std::cout << "Couldn't to create texture from "
|
||
|
<< path.c_str() << std::endl;
|
||
|
}
|
||
|
else{
|
||
|
//Get image dimensions
|
||
|
szW = loadedSurface->w;
|
||
|
szH = loadedSurface->h;
|
||
|
}
|
||
|
//Get rid of old loaded surface
|
||
|
SDL_FreeSurface(loadedSurface);
|
||
|
}
|
||
|
|
||
|
return texture != NULL;
|
||
|
}
|
||
|
|
||
|
void PosuTexture::free(){
|
||
|
//Free texture if it exists
|
||
|
if(texture != NULL){
|
||
|
SDL_DestroyTexture(texture);
|
||
|
texture = NULL;
|
||
|
szW = 0;
|
||
|
szH = 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void PosuTexture::render(SDL_Rect* quad,SDL_Rect* frame,SDL_Renderer** renderer){
|
||
|
//Set clip rendering dimensions
|
||
|
/*if(frame != NULL){
|
||
|
quad.w = frame->w;
|
||
|
quad.h = frame->h;
|
||
|
}*/
|
||
|
|
||
|
//Render to screen
|
||
|
SDL_RenderCopy(*renderer,texture,frame,quad);
|
||
|
}
|
||
|
|
||
|
int PosuTexture::getWidth(){
|
||
|
return szW;
|
||
|
}
|
||
|
|
||
|
int PosuTexture::getHeight(){
|
||
|
return szH;
|
||
|
}
|