-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextureWrapper.cpp
More file actions
94 lines (79 loc) · 2.22 KB
/
TextureWrapper.cpp
File metadata and controls
94 lines (79 loc) · 2.22 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
#include "TextureWrapper.h"
#include <SDL2/SDL_image.h>
#include <stdio.h>
TextureWrapper::TextureWrapper()
{
texture = NULL;
textureHeight = 0;
textureWidth = 0;
}
TextureWrapper::~TextureWrapper()
{
destroyTexture();
}
bool TextureWrapper::loadFromFile(SDL_Renderer* renderer, std::string path)
{
//remove existing texture
destroyTexture();
SDL_Texture* newTexture = NULL;
//load image to surface
SDL_Surface* loadedImage = IMG_Load(path.c_str());
if(loadedImage == NULL)
{
printf("Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError());
return false;
}
//Color key image (make a color transparent. currently using cyan 0, 255, 255)
//SDL_SetColorKey(loadedImage, SDL_TRUE, SDL_MapRGB(loadedImage->format, 0, 0xFF, 0xFF));
//Create texture from the loaded surface
newTexture = SDL_CreateTextureFromSurface(renderer, loadedImage);
if(newTexture == NULL)
{
printf( "Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError() );
return false;
}
// Get size of texture
textureWidth = loadedImage->w;
textureHeight = loadedImage->h;
// Deallocate surface used to load image
SDL_FreeSurface(loadedImage);
texture = newTexture;
return texture != NULL;
}
void TextureWrapper::destroyTexture()
{
if (texture != NULL)
{
SDL_DestroyTexture(texture);
texture = NULL;
}
}
void TextureWrapper::setBlendMode(SDL_BlendMode blendMode)
{
SDL_SetTextureBlendMode(texture, blendMode);
}
void TextureWrapper::setTransparency(Uint8 alpha)
{
SDL_SetTextureAlphaMod(texture, alpha);
}
void TextureWrapper::render(SDL_Renderer* renderer, int x, int y, SDL_Rect* crop, SDL_Rect* stretch)
{
SDL_Rect renderQuad = {x, y, textureWidth, textureHeight};
if(crop != NULL) {
if (stretch == NULL)
{
renderQuad.w = crop->w;
renderQuad.h = crop->h;
}
else
{
renderQuad.w = stretch->w;
renderQuad.h = stretch->h;
}
}
SDL_RenderCopy(renderer, texture, crop, &renderQuad);
}
void TextureWrapper::render(SDL_Renderer* renderer, SDL_Rect* crop, SDL_Rect* dst)
{
SDL_RenderCopy(renderer, texture, crop, dst);
}