src/proto2/Graphics.cc
author saiam
Mon, 01 Dec 2008 21:49:35 +0000
changeset 161 ea2f295c279f
parent 153 73402d5b778e
child 162 f760591b7481
permissions -rw-r--r--
Made PhysicsWorld to inherit Terrain instead of having it as an attribute.
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
     1
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
     2
#include "Graphics.hh"
86
ed31ece6f340 Segfault <3 (mik? oli odotettavaa)
saiam
parents: 60
diff changeset
     3
#include "Physics.hh"
ed31ece6f340 Segfault <3 (mik? oli odotettavaa)
saiam
parents: 60
diff changeset
     4
#include "GameState.hh"
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
     5
#include <cmath>
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
     6
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
     7
Graphics::Graphics (Engine &engine, GameState &state) :
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
     8
    engine(engine), 
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
     9
    state(state), 
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    10
    update_timer(GRAPHICS_UPDATE_INTERVAL_MS),
60
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
    11
    win(GRAPHICS_WINDOW_TITLE, GRAPHICS_RESOLUTION_WIDTH, GRAPHICS_RESOLUTION_HEIGHT),
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    12
    keyboard(win.get_ic()->get_keyboard()) {
86
ed31ece6f340 Segfault <3 (mik? oli odotettavaa)
saiam
parents: 60
diff changeset
    13
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    14
    // connect timer signal
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    15
    slots.connect(update_timer.sig_timer(), this, &Graphics::on_update);
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    16
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    17
    // enable
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    18
    update_timer.enable();
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    19
}
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    20
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    21
void Graphics::check_input (void) {
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    22
    LocalPlayer *player;
58
a53f5ad69500 "working" singleplayer
terom
parents: 54
diff changeset
    23
    PlayerInput_Move input_move = 0;
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    24
    
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    25
    // stop on escape
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    26
    if (keyboard.get_keycode(CL_KEY_ESCAPE)) {
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    27
        engine.stop();
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    28
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    29
        return;
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    30
    }
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    31
     
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    32
    // ignore if we don't have a local player
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    33
    if ((player = state.getLocalPlayer()) == NULL)
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    34
        return;
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    35
    
96
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 94
diff changeset
    36
    // handle movement
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    37
    if (keyboard.get_keycode(CL_KEY_LEFT))
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    38
        input_move |= INPUT_MOVE_LEFT;
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_RIGHT))
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    41
        input_move |= INPUT_MOVE_RIGHT;
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    42
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    43
    if (keyboard.get_keycode(CL_KEY_UP))
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    44
        input_move |= INPUT_MOVE_UP;
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    45
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    46
    if (keyboard.get_keycode(CL_KEY_DOWN))
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    47
        input_move |= INPUT_MOVE_DOWN;
94
08bebac3d0c2 Maalla liikkumista
ekku
parents: 91
diff changeset
    48
96
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 94
diff changeset
    49
    if (keyboard.get_keycode(CL_KEY_RSHIFT))
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    50
        input_move |= INPUT_MOVE_JUMP;
96
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 94
diff changeset
    51
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    52
    if (keyboard.get_keycode(CL_KEY_I))
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    53
        player->debugInfo();
116
0d36aade845e some stuff, don't remember what
nireco
parents: 108
diff changeset
    54
   
0d36aade845e some stuff, don't remember what
nireco
parents: 108
diff changeset
    55
    if (keyboard.get_keycode(CL_KEY_M))
0d36aade845e some stuff, don't remember what
nireco
parents: 108
diff changeset
    56
        input_move |= INPUT_MOVE_DIG;
0d36aade845e some stuff, don't remember what
nireco
parents: 108
diff changeset
    57
 
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    58
    // apply movement if applicable
50
9e1a6506f5a1 some rough-handed code modifications towards a newer, better, working Physics
terom
parents: 25
diff changeset
    59
    if (input_move)
9e1a6506f5a1 some rough-handed code modifications towards a newer, better, working Physics
terom
parents: 25
diff changeset
    60
        player->handleMove(input_move);
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    61
}
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    62
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    63
void Graphics::do_redraw (void) {
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    64
    CL_GraphicContext *gc = win.get_gc();
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    65
    
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    66
    // white background
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    67
    gc->clear(CL_Color::white);
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    68
60
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
    69
    const float factorX = GRAPHICS_RESOLUTION_WIDTH / MAP_WIDTH;
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
    70
    const float factorY = GRAPHICS_RESOLUTION_HEIGHT / MAP_HEIGHT;
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
    71
86
ed31ece6f340 Segfault <3 (mik? oli odotettavaa)
saiam
parents: 60
diff changeset
    72
    // draw terrain
161
ea2f295c279f Made PhysicsWorld to inherit Terrain instead of having it as an attribute.
saiam
parents: 153
diff changeset
    73
    state.draw(gc);
138
cc326b64ae20 Nyt kaivaminen visualisoidaan oikein, mutta fysiikkakoodi on kyll? edelleen ihan kauheata, pit?isi tuo Terrain v??nt??
saiam
parents: 125
diff changeset
    74
    //terrain.draw(gc);
cc326b64ae20 Nyt kaivaminen visualisoidaan oikein, mutta fysiikkakoodi on kyll? edelleen ihan kauheata, pit?isi tuo Terrain v??nt??
saiam
parents: 125
diff changeset
    75
    /*
125
8e15c7db2333 added outcommented digging demonstration
nireco
parents: 116
diff changeset
    76
    // Demonstrates digging, but is very slow
138
cc326b64ae20 Nyt kaivaminen visualisoidaan oikein, mutta fysiikkakoodi on kyll? edelleen ihan kauheata, pit?isi tuo Terrain v??nt??
saiam
parents: 125
diff changeset
    77
    Vector tmp(0, 0);
125
8e15c7db2333 added outcommented digging demonstration
nireco
parents: 116
diff changeset
    78
    CL_Color color;
8e15c7db2333 added outcommented digging demonstration
nireco
parents: 116
diff changeset
    79
    CL_PixelBuffer pix(1, 1, 4, CL_PixelFormat::rgba8888);
8e15c7db2333 added outcommented digging demonstration
nireco
parents: 116
diff changeset
    80
    CL_Surface surf(pix);
138
cc326b64ae20 Nyt kaivaminen visualisoidaan oikein, mutta fysiikkakoodi on kyll? edelleen ihan kauheata, pit?isi tuo Terrain v??nt??
saiam
parents: 125
diff changeset
    81
    for (tmp.x = 0; tmp.x < MAP_WIDTH; tmp.x++) {
cc326b64ae20 Nyt kaivaminen visualisoidaan oikein, mutta fysiikkakoodi on kyll? edelleen ihan kauheata, pit?isi tuo Terrain v??nt??
saiam
parents: 125
diff changeset
    82
        for (tmp.y = 0; tmp.y < MAP_HEIGHT; tmp.y++) {
125
8e15c7db2333 added outcommented digging demonstration
nireco
parents: 116
diff changeset
    83
            if (state.getType(tmp) == EMPTY) {
8e15c7db2333 added outcommented digging demonstration
nireco
parents: 116
diff changeset
    84
                color = CL_Color(86, 41, 0);
8e15c7db2333 added outcommented digging demonstration
nireco
parents: 116
diff changeset
    85
            } else if (state.getType(tmp) == DIRT) {
8e15c7db2333 added outcommented digging demonstration
nireco
parents: 116
diff changeset
    86
                color = CL_Color(144, 82, 23);
8e15c7db2333 added outcommented digging demonstration
nireco
parents: 116
diff changeset
    87
            } else if (state.getType(tmp) == ROCK) {
8e15c7db2333 added outcommented digging demonstration
nireco
parents: 116
diff changeset
    88
                color = CL_Color(132, 136, 135);
8e15c7db2333 added outcommented digging demonstration
nireco
parents: 116
diff changeset
    89
            } else {
8e15c7db2333 added outcommented digging demonstration
nireco
parents: 116
diff changeset
    90
                // Fale
8e15c7db2333 added outcommented digging demonstration
nireco
parents: 116
diff changeset
    91
            }
8e15c7db2333 added outcommented digging demonstration
nireco
parents: 116
diff changeset
    92
            surf.set_color(color);
8e15c7db2333 added outcommented digging demonstration
nireco
parents: 116
diff changeset
    93
            surf.draw(tmp.x, tmp.y, gc);
8e15c7db2333 added outcommented digging demonstration
nireco
parents: 116
diff changeset
    94
        }
138
cc326b64ae20 Nyt kaivaminen visualisoidaan oikein, mutta fysiikkakoodi on kyll? edelleen ihan kauheata, pit?isi tuo Terrain v??nt??
saiam
parents: 125
diff changeset
    95
        }*/
125
8e15c7db2333 added outcommented digging demonstration
nireco
parents: 116
diff changeset
    96
  
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    97
    // draw players
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    98
    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
    99
        Player *p = *it;
153
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 138
diff changeset
   100
        p->draw(gc);
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 138
diff changeset
   101
    }        
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   102
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   103
    // flip window buffer, LIEK NAO
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   104
    win.flip(0);
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   105
}
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   106
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   107
void Graphics::on_update (void) {
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   108
    // check keyboard input
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   109
    check_input();
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   110
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   111
    // redraw display
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   112
    do_redraw();
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   113
}