src/proto2/Graphics.cc
author saiam
Thu, 20 Nov 2008 21:25:09 +0000
changeset 83 cbba9729e92b
parent 60 26571fd9a8d1
child 86 ed31ece6f340
permissions -rw-r--r--
Integrointia fyssaan, jotain pikkubugausta havaittavissa.

#include "Graphics.hh"

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 up/down/left/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_LEFT))
            input_move |= INPUT_MOVE_LEFT;

    if (keyboard.get_keycode(CL_KEY_RIGHT))
            input_move |= INPUT_MOVE_RIGHT;
    
    // 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 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 * factorX - 5, p->getPosition().y * factorY - 5,
                p->getPosition().x * factorX + 5, p->getPosition().y * factorY + 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();
}