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.
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),
60
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
     8
    win(GRAPHICS_WINDOW_TITLE, GRAPHICS_RESOLUTION_WIDTH, GRAPHICS_RESOLUTION_HEIGHT),
25
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;
58
a53f5ad69500 "working" singleplayer
terom
parents: 54
diff changeset
    20
    PlayerInput_Move input_move = 0;
25
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))
54
b8b043ba0abd fix some more compiler errors...
terom
parents: 50
diff changeset
    35
            input_move |= INPUT_MOVE_UP;
25
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))
54
b8b043ba0abd fix some more compiler errors...
terom
parents: 50
diff changeset
    38
            input_move |= INPUT_MOVE_DOWN;
25
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))
54
b8b043ba0abd fix some more compiler errors...
terom
parents: 50
diff changeset
    41
            input_move |= INPUT_MOVE_LEFT;
25
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))
54
b8b043ba0abd fix some more compiler errors...
terom
parents: 50
diff changeset
    44
            input_move |= INPUT_MOVE_RIGHT;
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    45
    
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    46
    // apply movement if applicable
50
9e1a6506f5a1 some rough-handed code modifications towards a newer, better, working Physics
terom
parents: 25
diff changeset
    47
    if (input_move)
9e1a6506f5a1 some rough-handed code modifications towards a newer, better, working Physics
terom
parents: 25
diff changeset
    48
        player->handleMove(input_move);
25
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
60
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
    57
    const float factorX = GRAPHICS_RESOLUTION_WIDTH / MAP_WIDTH;
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
    58
    const float factorY = GRAPHICS_RESOLUTION_HEIGHT / MAP_HEIGHT;
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
    59
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    60
    // draw players
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    61
    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
    62
        Player *p = *it;
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    63
        
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    64
        // draw square
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    65
        gc->fill_rect(
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    66
            CL_Rect(
60
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
    67
                p->getPosition().x * factorX - 5, p->getPosition().y * factorY - 5,
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
    68
                p->getPosition().x * factorX + 5, p->getPosition().y * factorY + 5
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    69
            ), CL_Color::black
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    70
        );
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    71
    }
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    72
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    73
    // flip window buffer, LIEK NAO
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    74
    win.flip(0);
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    75
}
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    76
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    77
void Graphics::on_update (void) {
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    78
    // check keyboard input
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    79
    check_input();
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    80
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    81
    // redraw display
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    82
    do_redraw();
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    83
}