-
Notifications
You must be signed in to change notification settings - Fork 0
/
obj.cpp
58 lines (47 loc) · 1.37 KB
/
obj.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
#include "obj.h"
#include "SDL_image.h"
#include <math.h>
Video *Object::video_ = NULL;
StaticObject::
StaticObject(const std::string &name) {
SDL_Surface *s = IMG_Load(name.c_str());
if (!s)
throw std::string("Unable to create object from skin " + name);
bitmap_ = SDL_DisplayFormatAlpha(s);
SDL_FreeSurface(s);
}
bool Object::
on_screen(const SDL_Rect &rect, int mapx, int mapy) const {
if (mapx < (x_ - rect.w - video_->width()) ||
mapy < (y_ - rect.h - video_->height()) ||
mapx > (x_ + rect.w) ||
mapy > (y_ + rect.h))
return false;
return true;
}
int Object::
distance(int x, int y) const {
return sqrt((x_ - x) * (x_ - x) + (y_ - y) * (y_ - y));
}
// low level blitting code used by every object
void Object::
lowblit(SDL_Surface *src, SDL_Rect rect, int mapx, int mapy) const {
int posx = x_ - mapx, posy = y_ - mapy;
// check if we render the whole object or only a piece of it
if (posx < 0) {
rect.x -= posx;
rect.w += posx;
posx = 0;
}
else if (posx + rect.w > video_->width())
rect.w = video_->width() - posx;
if (posy < 0) {
rect.y -= posy;
rect.h += posy;
posy = 0;
}
else if (posy + rect.h > video_->height())
rect.h = video_->height() - posy;
// do the render
video_->blit(src, rect, posx, posy);
}