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
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
     1
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
     2
#include "Graphics.hh"
41
ca80cd67785d merge r64 through r88 from trunk
terom
parents: 35
diff changeset
     3
#include "Physics.hh"
ca80cd67785d merge r64 through r88 from trunk
terom
parents: 35
diff changeset
     4
#include "GameState.hh"
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
     5
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
     6
Graphics::Graphics (Engine &engine, GameState &state) :
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
     7
    engine(engine), 
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
     8
    state(state), 
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
     9
    update_timer(GRAPHICS_UPDATE_INTERVAL_MS),
35
e21cfda0edde Merge from at r31:36
terom
parents: 25
diff changeset
    10
    win(GRAPHICS_WINDOW_TITLE, GRAPHICS_RESOLUTION_WIDTH, GRAPHICS_RESOLUTION_HEIGHT),
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    11
    keyboard(win.get_ic()->get_keyboard()) {
41
ca80cd67785d merge r64 through r88 from trunk
terom
parents: 35
diff changeset
    12
ca80cd67785d merge r64 through r88 from trunk
terom
parents: 35
diff changeset
    13
    Vector tmp;
ca80cd67785d merge r64 through r88 from trunk
terom
parents: 35
diff changeset
    14
    CL_Color color;
ca80cd67785d merge r64 through r88 from trunk
terom
parents: 35
diff changeset
    15
    CL_PixelBuffer terr(MAP_WIDTH, MAP_HEIGHT, 3*MAP_WIDTH, CL_PixelFormat::rgb888);
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    16
    
41
ca80cd67785d merge r64 through r88 from trunk
terom
parents: 35
diff changeset
    17
    
ca80cd67785d merge r64 through r88 from trunk
terom
parents: 35
diff changeset
    18
    for (tmp.x = 0; tmp.x < MAP_WIDTH; tmp.x++) {
ca80cd67785d merge r64 through r88 from trunk
terom
parents: 35
diff changeset
    19
        for (tmp.y = 0; tmp.y < MAP_HEIGHT; tmp.y++) {
ca80cd67785d merge r64 through r88 from trunk
terom
parents: 35
diff changeset
    20
            if (state.getType(tmp) == EMPTY) {
ca80cd67785d merge r64 through r88 from trunk
terom
parents: 35
diff changeset
    21
                color = CL_Color(80, 35, 0);
ca80cd67785d merge r64 through r88 from trunk
terom
parents: 35
diff changeset
    22
            } else if (state.getType(tmp) == DIRT) {
ca80cd67785d merge r64 through r88 from trunk
terom
parents: 35
diff changeset
    23
                color = CL_Color(144, 82, 23);
ca80cd67785d merge r64 through r88 from trunk
terom
parents: 35
diff changeset
    24
            } else if (state.getType(tmp) == ROCK) {
ca80cd67785d merge r64 through r88 from trunk
terom
parents: 35
diff changeset
    25
                color = CL_Color(132, 136, 135);
ca80cd67785d merge r64 through r88 from trunk
terom
parents: 35
diff changeset
    26
            } else {
ca80cd67785d merge r64 through r88 from trunk
terom
parents: 35
diff changeset
    27
                // Fale
ca80cd67785d merge r64 through r88 from trunk
terom
parents: 35
diff changeset
    28
            }
ca80cd67785d merge r64 through r88 from trunk
terom
parents: 35
diff changeset
    29
            terr.draw_pixel(tmp.x, tmp.y, color);
ca80cd67785d merge r64 through r88 from trunk
terom
parents: 35
diff changeset
    30
            }
ca80cd67785d merge r64 through r88 from trunk
terom
parents: 35
diff changeset
    31
    }
ca80cd67785d merge r64 through r88 from trunk
terom
parents: 35
diff changeset
    32
    terrain = CL_Surface(terr);
ca80cd67785d merge r64 through r88 from trunk
terom
parents: 35
diff changeset
    33
 
ca80cd67785d merge r64 through r88 from trunk
terom
parents: 35
diff changeset
    34
    Engine::log(DEBUG, "Graphics") << "Taalla ollaan.";
ca80cd67785d merge r64 through r88 from trunk
terom
parents: 35
diff changeset
    35
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    36
    // connect timer signal
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    37
    slots.connect(update_timer.sig_timer(), this, &Graphics::on_update);
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    38
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    39
    // enable
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    40
    update_timer.enable();
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    41
}
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    42
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    43
void Graphics::check_input (void) {
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    44
    LocalPlayer *player;
35
e21cfda0edde Merge from at r31:36
terom
parents: 25
diff changeset
    45
    PlayerInput_Move input_move = 0;
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    46
    
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    47
    // stop on escape
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    48
    if (keyboard.get_keycode(CL_KEY_ESCAPE)) {
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    49
            engine.stop();
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    50
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    51
            return;
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    52
    }
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    53
     
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    54
    // ignore if we don't have a local player
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    55
    if ((player = state.getLocalPlayer()) == NULL)
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    56
        return;
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    57
    
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    58
    // handle up/down/left/right
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    59
    if (keyboard.get_keycode(CL_KEY_UP))
35
e21cfda0edde Merge from at r31:36
terom
parents: 25
diff changeset
    60
            input_move |= INPUT_MOVE_UP;
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    61
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    62
    if (keyboard.get_keycode(CL_KEY_DOWN))
35
e21cfda0edde Merge from at r31:36
terom
parents: 25
diff changeset
    63
            input_move |= INPUT_MOVE_DOWN;
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    64
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    65
    if (keyboard.get_keycode(CL_KEY_LEFT))
35
e21cfda0edde Merge from at r31:36
terom
parents: 25
diff changeset
    66
            input_move |= INPUT_MOVE_LEFT;
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    67
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    68
    if (keyboard.get_keycode(CL_KEY_RIGHT))
35
e21cfda0edde Merge from at r31:36
terom
parents: 25
diff changeset
    69
            input_move |= INPUT_MOVE_RIGHT;
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    70
    
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    71
    // apply movement if applicable
35
e21cfda0edde Merge from at r31:36
terom
parents: 25
diff changeset
    72
    if (input_move)
e21cfda0edde Merge from at r31:36
terom
parents: 25
diff changeset
    73
        player->handleMove(input_move);
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    74
}
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    75
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    76
void Graphics::do_redraw (void) {
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    77
    CL_GraphicContext *gc = win.get_gc();
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    78
    
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    79
    // white background
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    80
    gc->clear(CL_Color::white);
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    81
35
e21cfda0edde Merge from at r31:36
terom
parents: 25
diff changeset
    82
    const float factorX = GRAPHICS_RESOLUTION_WIDTH / MAP_WIDTH;
e21cfda0edde Merge from at r31:36
terom
parents: 25
diff changeset
    83
    const float factorY = GRAPHICS_RESOLUTION_HEIGHT / MAP_HEIGHT;
e21cfda0edde Merge from at r31:36
terom
parents: 25
diff changeset
    84
41
ca80cd67785d merge r64 through r88 from trunk
terom
parents: 35
diff changeset
    85
    // draw terrain
ca80cd67785d merge r64 through r88 from trunk
terom
parents: 35
diff changeset
    86
    terrain.draw(0,0, gc);
ca80cd67785d merge r64 through r88 from trunk
terom
parents: 35
diff changeset
    87
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    88
    // draw players
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    89
    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
    90
        Player *p = *it;
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    91
        
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    92
        // draw square
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    93
        gc->fill_rect(
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    94
            CL_Rect(
35
e21cfda0edde Merge from at r31:36
terom
parents: 25
diff changeset
    95
                p->getPosition().x * factorX - 5, p->getPosition().y * factorY - 5,
e21cfda0edde Merge from at r31:36
terom
parents: 25
diff changeset
    96
                p->getPosition().x * factorX + 5, p->getPosition().y * factorY + 5
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    97
            ), CL_Color::black
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    98
        );
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    99
    }
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   100
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   101
    // flip window buffer, LIEK NAO
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   102
    win.flip(0);
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   103
}
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   104
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   105
void Graphics::on_update (void) {
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   106
    // check keyboard input
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   107
    check_input();
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   108
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   109
    // redraw display
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   110
    do_redraw();
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   111
}