src/Graphics.cc
author saiam
Thu, 04 Dec 2008 21:10:41 +0000
changeset 199 f5c86420facd
parent 196 e2d32c4601ce
child 205 905028e58ed1
permissions -rw-r--r--
Jeejee, hirvee hinaus ohi toistaseks.

#include "Graphics.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_F)) {
        Engine::log(DEBUG, "Graphics.check_input") << "Fire!";
        input_move |= INPUT_SHOOT;
    }
   
    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);

    // Draw terrain
    state.world.draw(gc);

    // Flip window buffer, sync
    win.flip(1);
}

void Graphics::on_update (void) {
    // check keyboard input
    check_input();

    // redraw display
    do_redraw();
}