| author | saiam |
| Thu, 20 Nov 2008 21:25:09 +0000 | |
| changeset 83 | cbba9729e92b |
| parent 60 | 26571fd9a8d1 |
| child 86 | ed31ece6f340 |
| permissions | -rw-r--r-- |
| 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), |
|
| 60 | 8 |
win(GRAPHICS_WINDOW_TITLE, GRAPHICS_RESOLUTION_WIDTH, GRAPHICS_RESOLUTION_HEIGHT), |
| 25 | 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; |
|
| 58 | 20 |
PlayerInput_Move input_move = 0; |
| 25 | 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)) |
|
| 54 | 35 |
input_move |= INPUT_MOVE_UP; |
| 25 | 36 |
|
37 |
if (keyboard.get_keycode(CL_KEY_DOWN)) |
|
| 54 | 38 |
input_move |= INPUT_MOVE_DOWN; |
| 25 | 39 |
|
40 |
if (keyboard.get_keycode(CL_KEY_LEFT)) |
|
| 54 | 41 |
input_move |= INPUT_MOVE_LEFT; |
| 25 | 42 |
|
43 |
if (keyboard.get_keycode(CL_KEY_RIGHT)) |
|
| 54 | 44 |
input_move |= INPUT_MOVE_RIGHT; |
| 25 | 45 |
|
46 |
// apply movement if applicable |
|
|
50
9e1a6506f5a1
some rough-handed code modifications towards a newer, better, working Physics
terom
parents:
25
diff
changeset
|
47 |
if (input_move) |
|
9e1a6506f5a1
some rough-handed code modifications towards a newer, better, working Physics
terom
parents:
25
diff
changeset
|
48 |
player->handleMove(input_move); |
| 25 | 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 |
||
| 60 | 57 |
const float factorX = GRAPHICS_RESOLUTION_WIDTH / MAP_WIDTH; |
58 |
const float factorY = GRAPHICS_RESOLUTION_HEIGHT / MAP_HEIGHT; |
|
59 |
||
| 25 | 60 |
// draw players |
61 |
for (std::list<Player*>::iterator it = state.player_list.begin(); it != state.player_list.end(); it++) {
|
|
62 |
Player *p = *it; |
|
63 |
||
64 |
// draw square |
|
65 |
gc->fill_rect( |
|
66 |
CL_Rect( |
|
| 60 | 67 |
p->getPosition().x * factorX - 5, p->getPosition().y * factorY - 5, |
68 |
p->getPosition().x * factorX + 5, p->getPosition().y * factorY + 5 |
|
| 25 | 69 |
), CL_Color::black |
70 |
); |
|
71 |
} |
|
72 |
||
73 |
// flip window buffer, LIEK NAO |
|
74 |
win.flip(0); |
|
75 |
} |
|
76 |
||
77 |
void Graphics::on_update (void) {
|
|
78 |
// check keyboard input |
|
79 |
check_input(); |
|
80 |
||
81 |
// redraw display |
|
82 |
do_redraw(); |
|
83 |
} |