summaryrefslogtreecommitdiff
path: root/src/texture.cpp
blob: 6657a4a7f1de01750266ab81f92905f1f6db9b97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <SDL2/SDL_image.h>
#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 );
}