-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLTexture.cpp
More file actions
101 lines (89 loc) · 2.12 KB
/
Copy pathLTexture.cpp
File metadata and controls
101 lines (89 loc) · 2.12 KB
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
95
96
97
98
99
100
101
#include "LTexture.h"
LTexture::LTexture()
{
mTexture = NULL;
mWidth = 0;
mHeight = 0;
}
LTexture::~LTexture()
{
free();
}
void LTexture::free()
{
if( mTexture != NULL )
{
SDL_DestroyTexture(mTexture);
mTexture = NULL;
mWidth = 0;
mHeight = 0;
}
}
bool LTexture::loadFromRenderedText(string textureText, TTF_Font* gFont, SDL_Color textColor, SDL_Renderer* gRenderer)
{
free();
SDL_Surface* textSurface = TTF_RenderText_Solid(gFont, textureText.c_str(), textColor);
if( textSurface == NULL )
{
cout<< "Unable to render text surface! SDL_ttf Error: " << TTF_GetError() << endl;
}
else{
mTexture = SDL_CreateTextureFromSurface( gRenderer, textSurface );
if( mTexture == NULL )
{
cout<< "Unable to create texture from rendered text! SDL Error: "<< SDL_GetError() << endl;
}
else
{
mWidth = textSurface->w;
mHeight = textSurface->h;
}
SDL_FreeSurface(textSurface);
}
return mTexture != NULL;
}
bool LTexture::loadFromFile(string path, SDL_Renderer* gRenderer)
{
free();
SDL_Texture* newTexture = NULL;
SDL_Surface* loadedSurface = IMG_Load(path.c_str());
if( loadedSurface == NULL )
{
cout<< "Unable to load image" << path.c_str() << "! SDL_image Error: "<< IMG_GetError()<< endl;
}
else
{
SDL_SetColorKey(loadedSurface, SDL_TRUE, SDL_MapRGB(loadedSurface->format, 0, 0xFF, 0xFF));
newTexture = SDL_CreateTextureFromSurface(gRenderer, loadedSurface);
if( newTexture == nullptr )
{
cout<< "Unable to create texture from "<< path.c_str() << "! SDL Error:" << SDL_GetError() << endl;
}
else
{
mWidth = loadedSurface->w;
mHeight = loadedSurface->h;
}
SDL_FreeSurface(loadedSurface);
}
mTexture = newTexture;
return mTexture != NULL;
}
void LTexture::render(int x, int y, SDL_Renderer* gRenderer, SDL_Rect* clip)
{
SDL_Rect renderQuad = { x, y, mWidth, mHeight };
if (clip != NULL)
{
renderQuad.w = clip->w;
renderQuad.h = clip->h;
}
SDL_RenderCopy(gRenderer, mTexture, clip, &renderQuad);
}
int LTexture::getWidth()
{
return mWidth;
}
int LTexture::getHeight()
{
return mHeight;
}