|
25
|
1 |
|
|
|
2 |
#include "Graphics.hh"
|
|
|
3 |
|
|
|
4 |
Graphics::Graphics (Engine &engine, GameState &state) :
|
|
|
5 |
engine(engine),
|
|
|
6 |
state(state),
|
|
|
7 |
update_timer(GRAPHICS_UPDATE_INTERVAL_MS),
|
|
|
8 |
win(GRAPHICS_WINDOW_TITLE, MAP_DIM_W, MAP_DIM_H),
|
|
|
9 |
keyboard(win.get_ic()->get_keyboard()) {
|
|
|
10 |
|
|
|
11 |
// connect timer signal
|
|
|
12 |
slots.connect(update_timer.sig_timer(), this, &Graphics::on_update);
|
|
|
13 |
|
|
|
14 |
// enable
|
|
|
15 |
update_timer.enable();
|
|
|
16 |
}
|
|
|
17 |
|
|
|
18 |
void Graphics::check_input (void) {
|
|
|
19 |
LocalPlayer *player;
|
|
|
20 |
int dx = 0, dy = 0;
|
|
|
21 |
|
|
|
22 |
// stop on escape
|
|
|
23 |
if (keyboard.get_keycode(CL_KEY_ESCAPE)) {
|
|
|
24 |
engine.stop();
|
|
|
25 |
|
|
|
26 |
return;
|
|
|
27 |
}
|
|
|
28 |
|
|
|
29 |
// ignore if we don't have a local player
|
|
|
30 |
if ((player = state.getLocalPlayer()) == NULL)
|
|
|
31 |
return;
|
|
|
32 |
|
|
|
33 |
// handle up/down/left/right
|
|
|
34 |
if (keyboard.get_keycode(CL_KEY_UP))
|
|
|
35 |
dy -= 3;
|
|
|
36 |
|
|
|
37 |
if (keyboard.get_keycode(CL_KEY_DOWN))
|
|
|
38 |
dy += 3;
|
|
|
39 |
|
|
|
40 |
if (keyboard.get_keycode(CL_KEY_LEFT))
|
|
|
41 |
dx -= 3;
|
|
|
42 |
|
|
|
43 |
if (keyboard.get_keycode(CL_KEY_RIGHT))
|
|
|
44 |
dx += 3;
|
|
|
45 |
|
|
|
46 |
// apply movement if applicable
|
|
|
47 |
if (dx || dy)
|
|
|
48 |
player->move(PositionDelta(dx, dy));
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
void Graphics::do_redraw (void) {
|
|
|
52 |
CL_GraphicContext *gc = win.get_gc();
|
|
|
53 |
|
|
|
54 |
// white background
|
|
|
55 |
gc->clear(CL_Color::white);
|
|
|
56 |
|
|
|
57 |
// draw players
|
|
|
58 |
for (std::list<Player*>::iterator it = state.player_list.begin(); it != state.player_list.end(); it++) {
|
|
|
59 |
Player *p = *it;
|
|
|
60 |
|
|
|
61 |
// draw square
|
|
|
62 |
gc->fill_rect(
|
|
|
63 |
CL_Rect(
|
|
|
64 |
p->getPosition().x - 5, p->getPosition().y - 5,
|
|
|
65 |
p->getPosition().x + 5, p->getPosition().y + 5
|
|
|
66 |
), CL_Color::black
|
|
|
67 |
);
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
// flip window buffer, LIEK NAO
|
|
|
71 |
win.flip(0);
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
void Graphics::on_update (void) {
|
|
|
75 |
// check keyboard input
|
|
|
76 |
check_input();
|
|
|
77 |
|
|
|
78 |
// redraw display
|
|
|
79 |
do_redraw();
|
|
|
80 |
}
|