src/proto2/Graphics.cc
author saiam
Sun, 30 Nov 2008 00:23:44 +0000
changeset 138 cc326b64ae20
parent 125 8e15c7db2333
child 153 73402d5b778e
permissions -rw-r--r--
Nyt kaivaminen visualisoidaan oikein, mutta fysiikkakoodi on kyll? edelleen ihan kauheata, pit?isi tuo Terrain v??nt??
kuntoon nyt.

#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;
        
        Vector loc = p->getPosition();
        bool right = p->getFacing();
        float aim = p->getAim();
        std::vector<Vector> &shape = p->getShape();
        // draw quad
        gc->fill_quad(
                      CL_Quad(
                              (loc.x+shape[0].x) * factorX, (loc.y+shape[0].y) * factorY,
                              (loc.x+shape[1].x) * factorX, (loc.y+shape[1].y) * factorY,
                              (loc.x+shape[2].x) * factorX, (loc.y+shape[2].y) * factorY,
                              (loc.x+shape[3].x) * factorX, (loc.y+shape[3].y) * factorY
                              ), CL_Color::green
                      );

        // Draw crosshair
        if (right) {
            gc->draw_line(loc.x, loc.y, loc.x + std::cos(aim)*5, loc.y - std::sin(aim)*5, CL_Color::black);
        } else {
            gc->draw_line(loc.x, loc.y, loc.x - std::cos(aim)*5, loc.y - std::sin(aim)*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();
}