#include "Graphics.hh"
#include "Physics.hh"
#include "GameState.hh"
#include <cmath>
Graphics::Graphics (Engine &engine, GameState &state) :
engine(engine),
state(state),
update_timer(GRAPHICS_UPDATE_INTERVAL_MS),
win(GRAPHICS_WINDOW_TITLE, GRAPHICS_RESOLUTION_WIDTH, GRAPHICS_RESOLUTION_HEIGHT),
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;
PlayerInput_Move input_move = 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 movement
if (keyboard.get_keycode(CL_KEY_LEFT))
input_move |= INPUT_MOVE_LEFT;
if (keyboard.get_keycode(CL_KEY_RIGHT))
input_move |= INPUT_MOVE_RIGHT;
if (keyboard.get_keycode(CL_KEY_UP))
input_move |= INPUT_MOVE_UP;
if (keyboard.get_keycode(CL_KEY_DOWN))
input_move |= INPUT_MOVE_DOWN;
if (keyboard.get_keycode(CL_KEY_RSHIFT))
input_move |= INPUT_MOVE_JUMP;
if (keyboard.get_keycode(CL_KEY_I))
player->debugInfo();
if (keyboard.get_keycode(CL_KEY_M))
input_move |= INPUT_MOVE_DIG;
// apply movement if applicable
if (input_move)
player->handleMove(input_move);
}
void Graphics::do_redraw (void) {
CL_GraphicContext *gc = win.get_gc();
// white background
gc->clear(CL_Color::white);
const float factorX = GRAPHICS_RESOLUTION_WIDTH / MAP_WIDTH;
const float factorY = GRAPHICS_RESOLUTION_HEIGHT / MAP_HEIGHT;
// draw terrain
state.drawTerrain(gc);
//terrain.draw(gc);
/*
// Demonstrates digging, but is very slow
Vector tmp(0, 0);
CL_Color color;
CL_PixelBuffer pix(1, 1, 4, CL_PixelFormat::rgba8888);
CL_Surface surf(pix);
for (tmp.x = 0; tmp.x < MAP_WIDTH; tmp.x++) {
for (tmp.y = 0; tmp.y < MAP_HEIGHT; tmp.y++) {
if (state.getType(tmp) == EMPTY) {
color = CL_Color(86, 41, 0);
} else if (state.getType(tmp) == DIRT) {
color = CL_Color(144, 82, 23);
} else if (state.getType(tmp) == ROCK) {
color = CL_Color(132, 136, 135);
} else {
// Fale
}
surf.set_color(color);
surf.draw(tmp.x, tmp.y, gc);
}
}*/
// draw players
for (std::list<Player*>::iterator it = state.player_list.begin(); it != state.player_list.end(); it++) {
Player *p = *it;
p->draw(gc);
}
// flip window buffer, LIEK NAO
win.flip(0);
}
void Graphics::on_update (void) {
// check keyboard input
check_input();
// redraw display
do_redraw();
}