-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTexture.cpp
More file actions
83 lines (74 loc) · 1.46 KB
/
Texture.cpp
File metadata and controls
83 lines (74 loc) · 1.46 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
#include "Texture.h"
#include <SDL.h>
#include <SDL_image.h>
#include <iostream>
#include "Vector2.h"
Texture::Texture()
{
intTexture = NULL;
height = 0;
width = 0;
}
Texture::~Texture()
{
free();
}
bool Texture::loadFromFile(char* path, SDL_Renderer* renderer)
{
free();
SDL_Surface* rawSurface = IMG_Load(path);
if (rawSurface == NULL)
{
std::cout << "loadFail" << SDL_GetError();
std::cin.get();
}
else
{
SDL_SetColorKey(rawSurface, SDL_TRUE, SDL_MapRGB(rawSurface->format, 0, 0, 0));
intTexture = SDL_CreateTextureFromSurface(renderer, rawSurface);
if (intTexture == NULL)
{
std::cout << "convertFail" << SDL_GetError();
std::cin.get();
}
else
{
height = rawSurface->h;
width = rawSurface->w;
}
SDL_FreeSurface(rawSurface);
}
return intTexture != NULL;
}
void Texture::free()
{
if (intTexture != NULL)
{
SDL_DestroyTexture(intTexture);
intTexture = NULL;
height = 0;
width = 0;
}
}
void Texture::setColor(Uint8 r, Uint8 g, Uint8 b)
{
SDL_SetTextureColorMod(intTexture, r, g, b);
}
void Texture::render(Vector2 pos, SDL_Renderer* renderer)
{
SDL_Rect target = { pos.x,pos.y,width,height };
SDL_RenderCopy(renderer, intTexture, NULL, &target);
}
void Texture::centerRender(Vector2 pos, SDL_Renderer* renderer)
{
SDL_Rect target = { pos.x-width/2,pos.y-height/2,width,height };
SDL_RenderCopy(renderer, intTexture, NULL, &target);
}
int Texture::getHeight()
{
return height;
}
int Texture::getWidth()
{
return width;
}