-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTexture.cpp
94 lines (83 loc) · 1.96 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "Texture.h"
Texture::Texture(SDL_Renderer* mRenderer)
{
this->mRenderer = mRenderer;
mTexture = NULL;
mFont = NULL;
mWidth = 0;
mHeight = 0;
}
Texture::~Texture()
{
free();
}
bool Texture::loadFromFile(string path)
{
free();
SDL_Texture* tex = NULL;
SDL_Surface* loaded = IMG_Load(path.c_str());
if(loaded==NULL) cerr << "Texture error: " << IMG_GetError() << endl;
else {
SDL_SetColorKey(loaded, SDL_TRUE, SDL_MapRGB(loaded->format, 255, 0, 255));
tex = SDL_CreateTextureFromSurface(mRenderer, loaded);
if(tex==NULL) cerr << "Texture error: " << SDL_GetError() << endl;
else{
mWidth = loaded->w;
mHeight = loaded->h;
}
SDL_FreeSurface(loaded);
}
mTexture = tex;
return mTexture!=NULL;
}
bool Texture::loadFromRenderedText(string text, SDL_Color color){
free();
SDL_Surface* textSurface = TTF_RenderText_Solid(mFont, text.c_str(), color);
mTexture = SDL_CreateTextureFromSurface(mRenderer, textSurface);
if(mTexture==NULL){
cerr << "Font texture error: " << TTF_GetError() << endl;
} else {
mWidth = textSurface->w;
mHeight = textSurface->h;
}
SDL_FreeSurface(textSurface);
return mTexture!=NULL;
}
void Texture::free()
{
if(mTexture!=NULL){
SDL_DestroyTexture(mTexture);
mTexture = NULL;
mWidth = 0;
mHeight = 0;
}
}
void Texture::setColor(Uint8 r, Uint8 g, Uint8 b)
{
SDL_SetTextureColorMod(mTexture, r, g, b);
}
void Texture::setBlendMode(SDL_BlendMode blending)
{
SDL_SetTextureBlendMode(mTexture, blending);
}
void Texture::setAlpha(Uint8 alpha)
{
SDL_SetTextureAlphaMod(mTexture, alpha);
}
void Texture::render(int x, int y, SDL_Rect * clip, double angle, SDL_Point* center, SDL_RendererFlip flip)
{
SDL_Rect renderQuad = {x, y, mWidth, mHeight};
if(clip!=NULL){
renderQuad.w = clip->w;
renderQuad.h = clip->h;
}
SDL_RenderCopyEx(mRenderer, mTexture, clip, &renderQuad, angle, center, flip);
}
int Texture::getWidth() const
{
return mWidth;
}
int Texture::getHeight() const
{
return mHeight;
}