-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviewport.cpp
45 lines (40 loc) · 1.27 KB
/
viewport.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
#include <sstream>
#include "ioManager.h"
#include "viewport.h"
Viewport& Viewport::getInstance() {
static Viewport viewport;
return viewport;
}
Viewport::Viewport() :
gdata(Gamedata::getInstance()),
position(0, 0),
viewWidth(gdata.getXmlInt("view/width")),
viewHeight(gdata.getXmlInt("view/height")),
worldWidth( Gamedata::getInstance().getXmlInt("world/width")),
worldHeight( Gamedata::getInstance().getXmlInt("world/height")),
objWidth(0), objHeight(0),
objectToTrack(NULL)
{}
void Viewport::setObjectToTrack(const Drawable *obj) {
objectToTrack = obj;
objWidth = objectToTrack->getFrame()->getWidth();
objHeight = objectToTrack->getFrame()->getHeight();
}
void Viewport::draw() const {
IOManager::getInstance().
printMessageCenteredAt("Tracking "+objectToTrack->getName(), 30);
}
void Viewport::update() {
const float x = objectToTrack->X();
const float y = objectToTrack->Y();
position[0] = (x + objWidth/2) - viewWidth/2;
position[1] = (y + objHeight/2) - viewHeight/2;
if (position[0] < 0) position[0] = 0;
if (position[1] < 0) position[1] = 0;
if (position[0] > (worldWidth - viewWidth)) {
position[0] = worldWidth-viewWidth;
}
if (position[1] > (worldHeight - viewHeight)) {
position[1] = worldHeight-viewHeight;
}
}