82 lines
1.6 KiB
C++
82 lines
1.6 KiB
C++
#include"texture.h"
|
|
|
|
Texture::Texture(){
|
|
//Initialize variables
|
|
szW = 0;
|
|
szH = 0;
|
|
|
|
//Set the SDL_Texture to null
|
|
texture = NULL;
|
|
|
|
//Initialize the SDL_Image library
|
|
IMG_Init(IMG_INIT_PNG);
|
|
};
|
|
|
|
void Texture::setRenderer(SDL_Renderer* render){
|
|
//Set renderer
|
|
renderer = render;
|
|
};
|
|
|
|
Texture::~Texture(){
|
|
//Deallocate the texture when the class closes
|
|
free();
|
|
};
|
|
|
|
void Texture::loadTexture(std::string path){
|
|
//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 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);
|
|
}
|
|
};
|
|
|
|
void Texture::free(){
|
|
//Free texture if it exists
|
|
if(texture != NULL){
|
|
SDL_DestroyTexture(texture);
|
|
texture = NULL;
|
|
szW = 0;
|
|
szH = 0;
|
|
}
|
|
};
|
|
|
|
void Texture::render(SDL_Rect* quad,SDL_Rect* frame){
|
|
//Render the texture
|
|
SDL_RenderCopy(renderer,texture,frame,quad);
|
|
};
|
|
|
|
void Texture::render(SDL_Rect* quad){
|
|
//Make a frame of the size of the quad
|
|
SDL_Rect frame = {0,0,quad->w,quad->h};
|
|
//Render the texture
|
|
SDL_RenderCopy(renderer,texture,&frame,quad);
|
|
};
|
|
|
|
int Texture::getWidth(){
|
|
return szW;
|
|
};
|
|
|
|
int Texture::getHeight(){
|
|
return szH;
|
|
};
|