/
BaseObject.cpp
62 lines (46 loc) · 1.22 KB
/
BaseObject.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
#include "BaseObject.h"
BaseObject::BaseObject()
{
p_object_ = NULL;
rect_.x = 0;
rect_.y = 0;
rect_.w = 0;
rect_.h = 0;
}
BaseObject::~BaseObject()
{
Free();
}
bool BaseObject::LoadImg(std::string path, SDL_Renderer*screen)
{
SDL_Texture* new_texture = NULL;
SDL_Surface* load_surface = IMG_Load(path.c_str());
if(load_surface != NULL)
{
SDL_SetColorKey(load_surface, SDL_TRUE, SDL_MapRGB(load_surface ->format, COLOR_KEY_R, COLOR_KEY_G,COLOR_KEY_B));
new_texture = SDL_CreateTextureFromSurface(screen, load_surface);
if(new_texture != NULL)
{
rect_.w = load_surface->w;
rect_.h = load_surface->h;
}
SDL_FreeSurface(load_surface);
}
p_object_ = new_texture;
return p_object_ != NULL;
}
void BaseObject::Render(SDL_Renderer*des, const SDL_Rect*clip)
{
SDL_Rect renderquad = {rect_.x, rect_.y, rect_.w, rect_.h};
SDL_RenderCopy(des,p_object_,clip, &renderquad);
}
void BaseObject::Free()
{
if(p_object_ != NULL);
{
SDL_DestroyTexture(p_object_);
p_object_ = NULL;
rect_.w = 0;
rect_.h = 0;
}
}