author | ekku |
Sat, 06 Dec 2008 17:51:19 +0000 | |
changeset 222 | 293ddf4c067d |
parent 221 | fbc5db6fce45 |
child 230 | 78cf0cd69af4 |
permissions | -rw-r--r-- |
25 | 1 |
|
2 |
#include "Graphics.hh" |
|
86 | 3 |
#include "GameState.hh" |
108 | 4 |
#include <cmath> |
25 | 5 |
|
6 |
Graphics::Graphics (Engine &engine, GameState &state) : |
|
7 |
engine(engine), |
|
8 |
state(state), |
|
9 |
update_timer(GRAPHICS_UPDATE_INTERVAL_MS), |
|
60 | 10 |
win(GRAPHICS_WINDOW_TITLE, GRAPHICS_RESOLUTION_WIDTH, GRAPHICS_RESOLUTION_HEIGHT), |
25 | 11 |
keyboard(win.get_ic()->get_keyboard()) { |
86 | 12 |
|
25 | 13 |
// connect timer signal |
205 | 14 |
slots.connect(update_timer.sig_tick(), this, &Graphics::on_update); |
25 | 15 |
|
16 |
// enable |
|
205 | 17 |
update_timer.start(); |
25 | 18 |
} |
19 |
||
20 |
void Graphics::check_input (void) { |
|
21 |
LocalPlayer *player; |
|
221 | 22 |
PlayerInput input_mask = 0; |
25 | 23 |
|
24 |
// stop on escape |
|
25 |
if (keyboard.get_keycode(CL_KEY_ESCAPE)) { |
|
108 | 26 |
engine.stop(); |
25 | 27 |
|
108 | 28 |
return; |
25 | 29 |
} |
30 |
||
31 |
// ignore if we don't have a local player |
|
32 |
if ((player = state.getLocalPlayer()) == NULL) |
|
33 |
return; |
|
34 |
||
221 | 35 |
// dump debug info on stderr |
108 | 36 |
if (keyboard.get_keycode(CL_KEY_I)) |
221 | 37 |
player->printDebugInfo(); |
180 | 38 |
|
221 | 39 |
// check our keymap |
40 |
for (InputKeymapEntry *e = getGlobalInputKeymap(); e->keycode && e->input; e++) { |
|
41 |
if (keyboard.get_keycode(e->keycode)) |
|
42 |
input_mask |= e->input; |
|
180 | 43 |
} |
221 | 44 |
|
45 |
// apply input if applicable |
|
46 |
if (input_mask) |
|
47 |
player->handleInput(input_mask); |
|
25 | 48 |
} |
49 |
||
50 |
void Graphics::do_redraw (void) { |
|
51 |
CL_GraphicContext *gc = win.get_gc(); |
|
52 |
||
162
f760591b7481
Removed unnecessary variables (factoFoo) from do_redraw. Renamed
saiam
parents:
161
diff
changeset
|
53 |
// White background |
25 | 54 |
gc->clear(CL_Color::white); |
55 |
||
162
f760591b7481
Removed unnecessary variables (factoFoo) from do_redraw. Renamed
saiam
parents:
161
diff
changeset
|
56 |
// Draw terrain |
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
57 |
state.draw(gc); |
180 | 58 |
|
184 | 59 |
// Flip window buffer, sync |
60 |
win.flip(1); |
|
25 | 61 |
} |
62 |
||
205 | 63 |
void Graphics::on_update (TimeMS tick_length) { |
25 | 64 |
// check keyboard input |
65 |
check_input(); |
|
66 |
||
67 |
// redraw display |
|
68 |
do_redraw(); |
|
69 |
} |