forked from MetaCipher/the-sdl-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Texture.cpp
73 lines (56 loc) · 2.24 KB
/
Texture.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//==============================================================================
#include "Texture.h"
#include "Log.h"
//==============================================================================
Texture::Texture() {
}
//------------------------------------------------------------------------------
Texture::~Texture() {
if(SDLTexture) {
SDL_DestroyTexture(SDLTexture);
SDLTexture = NULL;
}
}
//==============================================================================
bool Texture::Load(SDL_Renderer* Renderer, std::string Filename) {
if(Renderer == NULL) {
Log("Bad SDL renderer passed");
return false;
}
this->Renderer = Renderer;
this->Filename = Filename;
SDL_Surface* TempSurface = IMG_Load(Filename.c_str());
if(TempSurface == NULL) {
Log("Unable to load image : %s : %s", Filename.c_str(), IMG_GetError());
return false;
}
// Convert SDL surface to a texture
if((SDLTexture = SDL_CreateTextureFromSurface(Renderer, TempSurface)) == NULL) {
Log("Unable to create SDL Texture : %s : %s", Filename.c_str(), IMG_GetError());
return false;
}
// Grab dimensions
SDL_QueryTexture(SDLTexture, NULL, NULL, &Width, &Height);
//Log("Texture Dimensions: %s : %d %d", Filename.c_str(), Width, Height);
SDL_FreeSurface(TempSurface);
return true;
}
//------------------------------------------------------------------------------
void Texture::Render(int X, int Y) {
Render(X, Y, Width, Height);
}
//------------------------------------------------------------------------------
void Texture::Render(int X, int Y, int Width, int Height) {
SDL_Rect Destination = {X, Y, Width, Height};
SDL_RenderCopy(Renderer, SDLTexture, NULL, &Destination);
}
//------------------------------------------------------------------------------
void Texture::Render(int X, int Y, int Width, int Height, int SX, int SY, int SWidth, int SHeight) {
SDL_Rect Source = {SX, SY, SWidth, SHeight};
SDL_Rect Destination = {X, Y, Width, Height};
SDL_RenderCopy(Renderer, SDLTexture, &Source, &Destination);
}
//------------------------------------------------------------------------------
int Texture::GetWidth() { return Width; }
int Texture::GetHeight() { return Height; }
//==============================================================================