Repository navigation
Expand file tree
/
Copy pathBaseObject.cpp
More file actions
146 lines (122 loc) · 3.5 KB
/
Copy pathBaseObject.cpp
File metadata and controls
146 lines (122 loc) · 3.5 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
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
#include "BaseObject.h"
BaseObject::BaseObject()
{
p_object = NULL;
p_rect.x = 0;
p_rect.y = 0;
p_rect.h = 0;
p_rect.w = 0;
}
BaseObject::~BaseObject()
{
}
bool BaseObject::LoadImg(std::string path, SDL_Renderer *screen)
{ //dung de load backround
SDL_DestroyTexture(p_object);
SDL_Texture *new_texture = NULL;
SDL_Surface *load_surface = IMG_Load(path.c_str());
if (load_surface != NULL)
{
//set color key
SDL_SetColorKey(load_surface, SDL_TRUE, SDL_MapRGB(load_surface->format, pngCOLOR_KEY_R, pngCOLOR_KEY_G, pngCOLOR_KEY_B));
new_texture = SDL_CreateTextureFromSurface(screen, load_surface);
if (new_texture != NULL)
{
p_rect.w = load_surface->w;
p_rect.h = load_surface->h;
}
SDL_FreeSurface(load_surface);
}
p_object = new_texture;
return p_object != NULL;
}
SDL_Texture *BaseObject::LoadPNG(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, imgCOLOR_KEY_R, imgCOLOR_KEY_G, imgCOLOR_KEY_B));
new_texture = SDL_CreateTextureFromSurface(screen, load_surface);
if (new_texture != NULL)
{
p_rect.w = load_surface->w;
p_rect.h = load_surface->h;
}
SDL_FreeSurface(load_surface);
}
p_object = new_texture;
return p_object;
}
bool BaseObject::BGLoadImg(std::string path, SDL_Renderer *screen)// dành riêng load back
{ //dung de load backround
SDL_DestroyTexture(p_object);
SDL_Texture *new_texture = NULL;
SDL_Surface *load_surface = IMG_Load(path.c_str());
if (load_surface != NULL)
{
//set color key
SDL_SetColorKey(load_surface, SDL_TRUE, SDL_MapRGB(load_surface->format, imgCOLOR_KEY_R, imgCOLOR_KEY_G, imgCOLOR_KEY_B));
new_texture = SDL_CreateTextureFromSurface(screen, load_surface);
if (new_texture != NULL)
{
p_rect.w = load_surface->w;
p_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 /* = NULL */)
{
SDL_Rect renderquad = {p_rect.x, p_rect.y, p_rect.w, p_rect.h};
SDL_RenderCopy(des, p_object, clip, &renderquad);
}
void BaseObject::renderTexture(SDL_Texture *tex, SDL_Renderer *ren, int x, int y)
{
SDL_Rect rect;
rect.x = x;
rect.y = y;
SDL_QueryTexture(tex, NULL, NULL, &rect.w, &rect.h);
SDL_RenderCopy(ren, tex, NULL, &rect);
}
void BaseObject::renderTexture(SDL_Texture *tex, SDL_Renderer *ren, int x, int y, int w, int h)
{
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.w = w;
rect.h = h;
SDL_RenderCopy(ren, tex, p_clip, &rect);
}
void BaseObject::Free()
{
if (p_object != NULL)
{
SDL_DestroyTexture(p_object);
p_object = NULL;
p_rect.w = 0;
p_rect.h = 0;
}
}
void BaseObject::SetRect(const int &x, const int &y)
{
p_rect.x = x, p_rect.y = y;
}
SDL_Rect BaseObject::GetRect() const
{
return p_rect;
}
SDL_Texture *BaseObject::GetObject() const
{
return p_object;
}
void BaseObject::Destroy(SDL_Texture *texture, SDL_Window *window, SDL_Renderer *ren)
{
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(window);
IMG_Quit();
SDL_Quit();
}