#include "Graphics.hh"
Graphics::Graphics (Engine &engine, GameState &state) :
engine(engine),
state(state),
update_timer(GRAPHICS_UPDATE_INTERVAL_MS),
win(GRAPHICS_WINDOW_TITLE, MAP_DIM_W, MAP_DIM_H),
keyboard(win.get_ic()->get_keyboard()) {
// connect timer signal
slots.connect(update_timer.sig_timer(), this, &Graphics::on_update);
// enable
update_timer.enable();
}
void Graphics::check_input (void) {
LocalPlayer *player;
int dx = 0, dy = 0;
// stop on escape
if (keyboard.get_keycode(CL_KEY_ESCAPE)) {
engine.stop();
return;
}
// ignore if we don't have a local player
if ((player = state.getLocalPlayer()) == NULL)
return;
// handle up/down/left/right
if (keyboard.get_keycode(CL_KEY_UP))
dy -= 3;
if (keyboard.get_keycode(CL_KEY_DOWN))
dy += 3;
if (keyboard.get_keycode(CL_KEY_LEFT))
dx -= 3;
if (keyboard.get_keycode(CL_KEY_RIGHT))
dx += 3;
// apply movement if applicable
if (dx || dy)
player->move(PositionDelta(dx, dy));
}
void Graphics::do_redraw (void) {
CL_GraphicContext *gc = win.get_gc();
// white background
gc->clear(CL_Color::white);
// draw players
for (std::list<Player*>::iterator it = state.player_list.begin(); it != state.player_list.end(); it++) {
Player *p = *it;
// draw square
gc->fill_rect(
CL_Rect(
p->getPosition().x - 5, p->getPosition().y - 5,
p->getPosition().x + 5, p->getPosition().y + 5
), CL_Color::black
);
}
// flip window buffer, LIEK NAO
win.flip(0);
}
void Graphics::on_update (void) {
// check keyboard input
check_input();
// redraw display
do_redraw();
}