src/proto2/Graphics.cc
author terom
Mon, 10 Nov 2008 16:49:09 +0000
branchno-netsession
changeset 31 d0d7489d4e8b
parent 25 af75a1894a32
child 35 e21cfda0edde
child 50 9e1a6506f5a1
permissions -rw-r--r--
add initial code written so far
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
     1
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
     2
#include "Graphics.hh"
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
     3
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
     4
Graphics::Graphics (Engine &engine, GameState &state) :
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
     5
    engine(engine), 
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
     6
    state(state), 
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
     7
    update_timer(GRAPHICS_UPDATE_INTERVAL_MS),
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
     8
    win(GRAPHICS_WINDOW_TITLE, MAP_DIM_W, MAP_DIM_H), 
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
     9
    keyboard(win.get_ic()->get_keyboard()) {
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    10
    
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    11
    // connect timer signal
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    12
    slots.connect(update_timer.sig_timer(), this, &Graphics::on_update);
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    13
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    14
    // enable
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    15
    update_timer.enable();
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    16
}
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    17
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    18
void Graphics::check_input (void) {
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    19
    LocalPlayer *player;
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    20
    int dx = 0, dy = 0;
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    21
    
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    22
    // stop on escape
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    23
    if (keyboard.get_keycode(CL_KEY_ESCAPE)) {
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    24
            engine.stop();
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    26
            return;
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    27
    }
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    28
     
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    29
    // ignore if we don't have a local player
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    30
    if ((player = state.getLocalPlayer()) == NULL)
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    31
        return;
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    32
    
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    33
    // handle up/down/left/right
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    34
    if (keyboard.get_keycode(CL_KEY_UP))
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    35
            dy -= 3;
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    36
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    37
    if (keyboard.get_keycode(CL_KEY_DOWN))
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    38
            dy += 3;
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    39
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    40
    if (keyboard.get_keycode(CL_KEY_LEFT))
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    41
            dx -= 3;
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    42
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    43
    if (keyboard.get_keycode(CL_KEY_RIGHT))
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    44
            dx += 3;
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    45
    
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    46
    // apply movement if applicable
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    47
    if (dx || dy)
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    48
        player->move(PositionDelta(dx, dy));
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    49
}
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    50
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    51
void Graphics::do_redraw (void) {
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    52
    CL_GraphicContext *gc = win.get_gc();
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    53
    
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    54
    // white background
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    55
    gc->clear(CL_Color::white);
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    56
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    57
    // draw players
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    58
    for (std::list<Player*>::iterator it = state.player_list.begin(); it != state.player_list.end(); it++) {
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    59
        Player *p = *it;
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    60
        
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    61
        // draw square
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    62
        gc->fill_rect(
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    63
            CL_Rect(
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    64
                p->getPosition().x - 5, p->getPosition().y - 5,
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    65
                p->getPosition().x + 5, p->getPosition().y + 5
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    66
            ), CL_Color::black
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    67
        );
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    68
    }
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    69
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    70
    // flip window buffer, LIEK NAO
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    71
    win.flip(0);
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    72
}
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    73
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    74
void Graphics::on_update (void) {
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    75
    // check keyboard input
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    76
    check_input();
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    77
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    78
    // redraw display
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    79
    do_redraw();
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    80
}