platform-test/source/texture.cpp

82 lines
1.6 KiB
C++
Raw Normal View History

2017-05-01 17:40:03 +00:00
#include"texture.h"
Texture::Texture(){
2017-05-01 17:40:03 +00:00
//Initialize variables
szW = 0;
szH = 0;
2017-06-17 17:12:59 +00:00
//Set the SDL_Texture to null
texture = NULL;
//Initialize the SDL_Image library
2017-05-06 07:52:25 +00:00
IMG_Init(IMG_INIT_PNG);
};
2017-06-17 17:12:59 +00:00
void Texture::setRenderer(SDL_Renderer* render){
//Set renderer
2017-05-06 07:52:25 +00:00
renderer = render;
};
2017-05-01 17:40:03 +00:00
Texture::~Texture(){
2017-06-17 17:12:59 +00:00
//Deallocate the texture when the class closes
free();
2017-05-06 07:52:25 +00:00
};
2017-05-01 17:40:03 +00:00
void Texture::loadTexture(std::string path){
2017-05-01 17:40:03 +00:00
//Get rid of preexisting texture
free();
2017-05-01 17:40:03 +00:00
//Load image at specified path
SDL_Surface* loadedSurface = IMG_Load( path.c_str() );
2017-05-06 07:52:25 +00:00
if(loadedSurface == NULL){
2017-05-01 17:40:03 +00:00
std::cout << "Couldn't load " << path.c_str() << std::endl;
2017-05-06 07:52:25 +00:00
}
2017-05-01 17:40:03 +00:00
else{
//Create texture from surface pixels
texture = SDL_CreateTextureFromSurface
2017-06-17 17:12:59 +00:00
(renderer,loadedSurface);
2017-05-01 17:40:03 +00:00
if(texture == NULL){
2017-06-17 17:12:59 +00:00
std::cout << "Couldn't create texture from " << path.c_str() << std::endl;
2017-05-01 17:40:03 +00:00
}
else{
//Get image dimensions
szW = loadedSurface->w;
szH = loadedSurface->h;
}
//Get rid of old loaded surface
SDL_FreeSurface(loadedSurface);
}
2017-05-06 07:52:25 +00:00
};
2017-05-01 17:40:03 +00:00
void Texture::free(){
2017-05-01 17:40:03 +00:00
//Free texture if it exists
if(texture != NULL){
SDL_DestroyTexture(texture);
texture = NULL;
szW = 0;
szH = 0;
}
2017-05-06 07:52:25 +00:00
};
2017-05-01 17:40:03 +00:00
void Texture::render(SDL_Rect* quad,SDL_Rect* frame){
2017-06-17 17:12:59 +00:00
//Render the texture
SDL_RenderCopy(renderer,texture,frame,quad);
2017-05-06 07:52:25 +00:00
};
2017-05-01 17:40:03 +00:00
void Texture::render(SDL_Rect* quad){
2017-06-17 17:12:59 +00:00
//Make a frame of the size of the quad
SDL_Rect frame = {0,0,quad->w,quad->h};
2017-06-17 17:12:59 +00:00
//Render the texture
SDL_RenderCopy(renderer,texture,&frame,quad);
};
int Texture::getWidth(){
2017-05-01 17:40:03 +00:00
return szW;
2017-05-06 07:52:25 +00:00
};
2017-05-01 17:40:03 +00:00
int Texture::getHeight(){
2017-05-01 17:40:03 +00:00
return szH;
2017-05-06 07:52:25 +00:00
};