src/proto2/Graphics.cc
author terom
Thu, 20 Nov 2008 23:45:33 +0000
branchno-netsession
changeset 41 ca80cd67785d
parent 35 e21cfda0edde
permissions -rw-r--r--
merge r64 through r88 from trunk

#include "Graphics.hh"
#include "Physics.hh"
#include "GameState.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()) {

    Vector tmp;
    CL_Color color;
    CL_PixelBuffer terr(MAP_WIDTH, MAP_HEIGHT, 3*MAP_WIDTH, CL_PixelFormat::rgb888);
    
    
    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(80, 35, 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
            }
            terr.draw_pixel(tmp.x, tmp.y, color);
            }
    }
    terrain = CL_Surface(terr);
 
    Engine::log(DEBUG, "Graphics") << "Taalla ollaan.";

    // 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 terrain
    terrain.draw(0,0, gc);

    // 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();
}