-
Notifications
You must be signed in to change notification settings - Fork 0
/
LTexture.c
391 lines (309 loc) · 9.09 KB
/
LTexture.c
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#ifndef __txt_
#define __txt_
#include <SDL.h>
#include <SDL_image.h>
#include <string>
//Texture wrapper class
class LTexture
{
public:
//Initializes variables
LTexture();
//Deallocates memory
~LTexture();
//Loads image at specified path
bool loadFromFile( std::string path );
#ifdef _SDL_TTF_H
//Creates image from font string
bool loadFromRenderedText( std::string textureText, SDL_Color textColor );
#endif
//Creates blank texture
bool createBlank( int width, int height, SDL_TextureAccess = SDL_TEXTUREACCESS_STREAMING );
//Deallocates texture
void free();
//Set color modulation
void setColor( Uint8 red, Uint8 green, Uint8 blue );
//Set blending
void setBlendMode( SDL_BlendMode blending );
//Set alpha modulation
void setAlpha( Uint8 alpha );
//Renders texture at given point
void render( int x, int y, SDL_Rect* clip = NULL, double angle = 0.0, SDL_Point* center = NULL,
SDL_RendererFlip flip = SDL_FLIP_NONE );
//Set self as render target
void setAsRenderTarget();
//Gets image dimensions
int getWidth();
int getHeight();
void setWindows(SDL_Window*, SDL_Renderer*);
//Pixel manipulators
bool lockTexture();
bool unlockTexture();
void* getPixels();
void copyPixels( void* pixels );
int getPitch();
Uint32 getPixel32( unsigned int x, unsigned int y );
//The actual hardware texture
SDL_Texture* mTexture;
void* mPixels;
int mPitch;
//Image dimensions
int mWidth;
int mHeight;
TTF_Font *font = NULL;
SDL_Window* window;
//The window renderer
SDL_Renderer* renderer;
};
/***********************************************************************/
/***********************************************************************/
LTexture::LTexture()
{
//Initialize
mTexture = NULL;
mWidth = 0;
mHeight = 0;
mPixels = NULL;
mPitch = 0;
}
LTexture::~LTexture()
{
//Deallocate
free();
}
bool LTexture::loadFromFile( std::string path )
{
//Get rid of preexisting texture
free();
//The final texture
SDL_Texture* newTexture = NULL;
//Load image at specified path
SDL_Surface* loadedSurface = IMG_Load( path.c_str() );
if( loadedSurface == NULL )
{
SDL_Log( "Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError() );
}
else
{
//Convert surface to display format
SDL_Surface* formattedSurface = SDL_ConvertSurfaceFormat( loadedSurface, SDL_PIXELFORMAT_RGBA8888, 0 );
if( formattedSurface == NULL )
{
SDL_Log( "Unable to convert loaded surface to display format! %s\n", SDL_GetError() );
}
else
{
//Create blank streamable texture
newTexture = SDL_CreateTexture( renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, formattedSurface->w, formattedSurface->h );
if( newTexture == NULL )
{
SDL_Log( "Unable to create blank texture! SDL Error: %s\n", SDL_GetError() );
}
else
{
//Enable blending on texture
SDL_SetTextureBlendMode( newTexture, SDL_BLENDMODE_BLEND );
//Lock texture for manipulation
SDL_LockTexture( newTexture, &formattedSurface->clip_rect, &mPixels, &mPitch );
//Copy loaded/formatted surface pixels
memcpy( mPixels, formattedSurface->pixels, formattedSurface->pitch * formattedSurface->h );
//Get image dimensions
mWidth = formattedSurface->w;
mHeight = formattedSurface->h;
//Get pixel data in editable format
Uint32* pixels = (Uint32*)mPixels;
int pixelCount = ( mPitch / 4 ) * mHeight;
//Map colors
Uint32 colorKey = SDL_MapRGB( formattedSurface->format, 0, 0xFF, 0xFF );
Uint32 transparent = SDL_MapRGBA( formattedSurface->format, 0x00, 0xFF, 0xFF, 0x00 );
//Color key pixels
for( int i = 0; i < pixelCount; ++i )
{
if( pixels[ i ] == colorKey )
{
pixels[ i ] = transparent;
}
}
//Unlock texture to update
SDL_UnlockTexture( newTexture );
mPixels = NULL;
}
//Get rid of old formatted surface
SDL_FreeSurface( formattedSurface );
}
//Get rid of old loaded surface
SDL_FreeSurface( loadedSurface );
}
//Return success
mTexture = newTexture;
return mTexture != NULL;
}
#ifdef _SDL_TTF_H
bool LTexture::loadFromRenderedText( std::string textureText, SDL_Color textColor )
{
//Get rid of preexisting texture
free();
//Render text surface
SDL_Surface* textSurface = TTF_RenderText_Solid( font, textureText.c_str(), textColor );
if( textSurface != NULL )
{
//Create texture from surface pixels
mTexture = SDL_CreateTextureFromSurface( renderer, textSurface );
if( mTexture == NULL )
{
SDL_Log( "Unable to create texture from rendered text! SDL Error: %s\n", SDL_GetError() );
}
else
{
//Get image dimensions
mWidth = textSurface->w;
mHeight = textSurface->h;
}
//Get rid of old surface
SDL_FreeSurface( textSurface );
}
else
{
SDL_Log( "Unable to render text surface! SDL_ttf Error: %s\n", TTF_GetError() );
}
//Return success
return mTexture != NULL;
}
#endif
bool LTexture::createBlank( int width, int height, SDL_TextureAccess access )
{
//Create uninitialized texture
mTexture = SDL_CreateTexture( renderer, SDL_PIXELFORMAT_RGBA8888, access, width, height );
if( mTexture == NULL )
{
SDL_Log( "Unable to create blank texture! SDL Error: %s\n", SDL_GetError() );
}
else
{
mWidth = width;
mHeight = height;
}
return mTexture != NULL;
}
void LTexture::free()
{
//Free texture if it exists
if( mTexture != NULL )
{
SDL_DestroyTexture( mTexture );
mTexture = NULL;
mWidth = 0;
mHeight = 0;
mPixels = NULL;
mPitch = 0;
}
}
void LTexture::setColor( Uint8 red, Uint8 green, Uint8 blue )
{
//Modulate texture rgb
SDL_SetTextureColorMod( mTexture, red, green, blue );
}
void LTexture::setBlendMode( SDL_BlendMode blending )
{
//Set blending function
SDL_SetTextureBlendMode( mTexture, blending );
}
void LTexture::setAlpha( Uint8 alpha )
{
//Modulate texture alpha
SDL_SetTextureAlphaMod( mTexture, alpha );
}
void LTexture::render( int x, int y, SDL_Rect* clip, double angle, SDL_Point* center, SDL_RendererFlip flip )
{
//Set rendering space and render to screen
SDL_Rect renderQuad = { x, y, mWidth, mHeight };
//Set clip rendering dimensions
if( clip != NULL )
{
renderQuad.w = clip->w;
renderQuad.h = clip->h;
}
//Render to screen
SDL_RenderCopyEx( renderer, mTexture, clip, &renderQuad, angle, center, flip );
}
void LTexture::setAsRenderTarget()
{
//Make self render target
SDL_SetRenderTarget( renderer, mTexture );
}
int LTexture::getWidth()
{
return mWidth;
}
int LTexture::getHeight()
{
return mHeight;
}
bool LTexture::lockTexture()
{
bool success = true;
//Texture is already locked
if( mPixels != NULL )
{
SDL_Log( "Texture is already locked!\n" );
success = false;
}
//Lock texture
else
{
if( SDL_LockTexture( mTexture, NULL, &mPixels, &mPitch ) != 0 )
{
SDL_Log( "Unable to lock texture! %s\n", SDL_GetError() );
success = false;
}
}
return success;
}
bool LTexture::unlockTexture()
{
bool success = true;
//Texture is not locked
if( mPixels == NULL )
{
SDL_Log( "Texture is not locked!\n" );
success = false;
}
//Unlock texture
else
{
SDL_UnlockTexture( mTexture );
mPixels = NULL;
mPitch = 0;
}
return success;
}
void* LTexture::getPixels()
{
return mPixels;
}
void LTexture::copyPixels( void* pixels )
{
//Texture is locked
if( mPixels != NULL )
{
//Copy to locked pixels
memcpy( mPixels, pixels, mPitch * mHeight );
}
}
int LTexture::getPitch()
{
return mPitch;
}
Uint32 LTexture::getPixel32( unsigned int x, unsigned int y )
{
//Convert the pixels to 32 bit
Uint32 *pixels = (Uint32*)mPixels;
//Get the pixel requested
return pixels[ ( y * ( mPitch / 4 ) ) + x ];
}
void LTexture::setWindows(SDL_Window* w, SDL_Renderer* r)
{
window = w;
renderer = r;
}
#endif