-
Notifications
You must be signed in to change notification settings - Fork 0
/
lTexture.cc
143 lines (134 loc) · 2.82 KB
/
lTexture.cc
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "lTexture.h"
void lTexture::free()
{
if (mTexture != NULL)
{
SDL_DestroyTexture(mTexture);
mTexture=NULL;
mHeight=0;
mWidth=0;
}
}
lTexture::lTexture(SDL_Renderer* Renderer)
{
mRenderer=Renderer;
mTexture = NULL;
mWidth = 0;
mHeight = 0;
}
lTexture::~lTexture()
{
free();
mRenderer=NULL;
}
bool lTexture::setFont(TTF_Font* Font)
{
mFont=NULL;
mFont=Font;
return (mFont != NULL);
}
bool lTexture::setRenderer(SDL_Renderer* Renderer)
{
mRenderer=NULL;
mRenderer=Renderer;
return (mRenderer != NULL);
}
bool lTexture::loadfromFile(std::string path)
{
SDL_Texture* newTexture = NULL;
SDL_Surface* loadedSurface = IMG_Load(path.c_str());
if (loadedSurface == NULL)
{
printf("Bild konnte nicht geladen werden. IMG_Error: %s\n",IMG_GetError());
}
else
{
newTexture = SDL_CreateTextureFromSurface(mRenderer,loadedSurface);
if (newTexture == NULL)
{
printf("Konnte keine Textur aus %s erstellen. SDL_Error: %s\n",path.c_str(),SDL_GetError());
}
else
{
mHeight = loadedSurface->h;
mWidth = loadedSurface->w;
}
}
SDL_FreeSurface(loadedSurface);
mTexture=newTexture;
return (mTexture != NULL);
}
// #ifdef _SLD_TTF_H
bool lTexture::loadfromText(std::string text,SDL_Color textcolor, TTF_Font* font)
{
free();
if (font != NULL)
{mFont=font;
}
if (mFont != NULL)
{
SDL_Surface* textSurface= TTF_RenderText_Solid(mFont,text.c_str(),textcolor);
if (textSurface==NULL)
{
printf("Text konnte nicht gerendert werden. TTF_Error= %s\n",TTF_GetError());
}
else
{
mTexture = SDL_CreateTextureFromSurface(mRenderer,textSurface);
if (mTexture==NULL)
{
printf("Konnte keine Textur aus gerendertem Text erstellen. SDL_Error: %s\n",SDL_GetError());
}
else
{
mHeight=textSurface->h;
mWidth=textSurface->w;
}
SDL_FreeSurface(textSurface);
}
}
else
{printf("Keine Schriftart angegeben!");
}
return (mTexture != NULL);
}
// #endif
lTexture::lTexture(std::string path,SDL_Renderer* Renderer)
{
mRenderer=Renderer;
loadfromFile(path);
}
int lTexture::getWidth()
{
return mWidth;
}
int lTexture::getHeight()
{
return mHeight;
}
void lTexture::render(SDL_Rect* space, SDL_Rect* clip,double angle,SDL_Point* center,SDL_RendererFlip flip)
{
SDL_RenderCopyEx(mRenderer,mTexture,clip,space,angle,center,flip);
}
void lTexture::render(int x, int y, SDL_Rect* clip,double angle,SDL_Point* center,SDL_RendererFlip flip)
{
SDL_Rect renderRect = {x,y,mWidth,mHeight};
if (clip != NULL)
{
renderRect.w=clip->w;
renderRect.h=clip->h;
}
render(&renderRect,clip,angle,center,flip);
}
void lTexture::setColor(Uint8 red,Uint8 green,Uint8 blue)
{
SDL_SetTextureColorMod(mTexture,red, green, blue);
}
void lTexture::setAlpha(Uint8 alpha)
{
SDL_SetTextureAlphaMod(mTexture,alpha);
}
void lTexture::setBlendMode(SDL_BlendMode blending)
{
SDL_SetTextureBlendMode(mTexture,blending);
}