-
Notifications
You must be signed in to change notification settings - Fork 0
/
Texture.cpp
88 lines (70 loc) · 2.49 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
#include "Texture.h"
#include <stdio.h>
#include <SDL2/SDL_image.h>
bool Texture::LoadFromFile( SDL_Renderer* renderer, std::string path, SDL_Color keyColor )
{
Cleanup();
SDL_Surface* loadedSurface = IMG_Load( path.c_str() );
if ( loadedSurface == NULL )
{
printf("Unable to load image at %s! SDL_image error: %s\n", path.c_str(), IMG_GetError() );
return false;
}
if ( keyColor.r != 0x00 || keyColor.g != 0x00 || keyColor.b != 0x00 || keyColor.a != 0x00 )
SDL_SetColorKey( loadedSurface, SDL_TRUE, SDL_MapRGB( loadedSurface->format, keyColor.r, keyColor.g, keyColor.b ) );
SDL_Texture* finalTexture = NULL;
finalTexture = SDL_CreateTextureFromSurface( renderer, loadedSurface );
if ( finalTexture == NULL )
printf("Unable to create texture from %s. SDL error: %s\n", path.c_str(), SDL_GetError() );
else
{
tWidth = loadedSurface->w;
tHeight = loadedSurface->h;
}
SDL_FreeSurface( loadedSurface );
actualTexture = finalTexture;
return actualTexture != NULL;
}
bool Texture::LoadFromRenderedText( SDL_Renderer* renderer, std::string textureText, SDL_Color textColor, TTF_Font* font )
{
Cleanup();
SDL_Surface* textSurface = TTF_RenderText_Solid( font, textureText.c_str(), textColor );
if( textSurface == NULL )
{
printf( "Unable to render text surface. SDL_ttf error: %s\n", TTF_GetError() );
printf( "Failed to render text texture for '%s'.\n", textureText.c_str() );
return false;
}
actualTexture = SDL_CreateTextureFromSurface( renderer, textSurface );
if( actualTexture == NULL )
{
printf( "Unable to create texture from rendered text. SDL error: %s\n", SDL_GetError() );
printf( "Failed to render text texture for '%s'.\n", textureText.c_str() );
SDL_FreeSurface( textSurface );
return false;
}
tWidth = textSurface->w;
tHeight = textSurface->h;
SDL_FreeSurface( textSurface );
return true;
}
void Texture::Cleanup()
{
if ( actualTexture != NULL )
{
SDL_DestroyTexture( actualTexture );
actualTexture = NULL;
tWidth = 0;
tHeight = 0;
}
}
void Texture::Render( SDL_Renderer* renderer, int x, int y, SDL_Rect* clip )
{
SDL_Rect renderQuad = { x, y, tWidth, tHeight };
if ( clip != NULL )
{
renderQuad.w = clip->w;
renderQuad.h = clip->h;
}
SDL_RenderCopy( renderer, actualTexture, clip, &renderQuad );
}