src/proto2/Graphics.cc
author saiam
Fri, 28 Nov 2008 15:27:51 +0000
changeset 122 16a73ebca810
parent 116 0d36aade845e
child 125 8e15c7db2333
permissions -rw-r--r--
No warnings anymore, but well have to think about that applyForce
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
ed31ece6f340 Segfault <3 (mik? oli odotettavaa)
saiam
parents: 60
diff changeset
    14
    Vector tmp;
ed31ece6f340 Segfault <3 (mik? oli odotettavaa)
saiam
parents: 60
diff changeset
    15
    CL_Color color;
90
ekku
parents: 88
diff changeset
    16
    CL_PixelBuffer terr(MAP_WIDTH, MAP_HEIGHT, 4*MAP_WIDTH, CL_PixelFormat::rgba8888);
87
fa9512ace722 Mmm, hyvi? sieni?
saiam
parents: 86
diff changeset
    17
    
fa9512ace722 Mmm, hyvi? sieni?
saiam
parents: 86
diff changeset
    18
    
86
ed31ece6f340 Segfault <3 (mik? oli odotettavaa)
saiam
parents: 60
diff changeset
    19
    for (tmp.x = 0; tmp.x < MAP_WIDTH; tmp.x++) {
ed31ece6f340 Segfault <3 (mik? oli odotettavaa)
saiam
parents: 60
diff changeset
    20
        for (tmp.y = 0; tmp.y < MAP_HEIGHT; tmp.y++) {
ed31ece6f340 Segfault <3 (mik? oli odotettavaa)
saiam
parents: 60
diff changeset
    21
            if (state.getType(tmp) == EMPTY) {
90
ekku
parents: 88
diff changeset
    22
                color = CL_Color(86, 41, 0);
86
ed31ece6f340 Segfault <3 (mik? oli odotettavaa)
saiam
parents: 60
diff changeset
    23
            } else if (state.getType(tmp) == DIRT) {
88
7431cd0cf900 Kuu n?kyy!
ekku
parents: 87
diff changeset
    24
                color = CL_Color(144, 82, 23);
86
ed31ece6f340 Segfault <3 (mik? oli odotettavaa)
saiam
parents: 60
diff changeset
    25
            } else if (state.getType(tmp) == ROCK) {
88
7431cd0cf900 Kuu n?kyy!
ekku
parents: 87
diff changeset
    26
                color = CL_Color(132, 136, 135);
86
ed31ece6f340 Segfault <3 (mik? oli odotettavaa)
saiam
parents: 60
diff changeset
    27
            } else {
ed31ece6f340 Segfault <3 (mik? oli odotettavaa)
saiam
parents: 60
diff changeset
    28
                // Fale
ed31ece6f340 Segfault <3 (mik? oli odotettavaa)
saiam
parents: 60
diff changeset
    29
            }
ed31ece6f340 Segfault <3 (mik? oli odotettavaa)
saiam
parents: 60
diff changeset
    30
            terr.draw_pixel(tmp.x, tmp.y, color);
90
ekku
parents: 88
diff changeset
    31
     	}
86
ed31ece6f340 Segfault <3 (mik? oli odotettavaa)
saiam
parents: 60
diff changeset
    32
    }
87
fa9512ace722 Mmm, hyvi? sieni?
saiam
parents: 86
diff changeset
    33
    terrain = CL_Surface(terr);
fa9512ace722 Mmm, hyvi? sieni?
saiam
parents: 86
diff changeset
    34
 
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    35
    // connect timer signal
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    36
    slots.connect(update_timer.sig_timer(), this, &Graphics::on_update);
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    37
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    38
    // enable
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    39
    update_timer.enable();
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    40
}
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    41
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    42
void Graphics::check_input (void) {
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    43
    LocalPlayer *player;
58
a53f5ad69500 "working" singleplayer
terom
parents: 54
diff changeset
    44
    PlayerInput_Move input_move = 0;
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    45
    
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    46
    // stop on escape
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    47
    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
    48
        engine.stop();
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    49
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    50
        return;
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    51
    }
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    52
     
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    53
    // ignore if we don't have a local player
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    54
    if ((player = state.getLocalPlayer()) == NULL)
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    55
        return;
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    56
    
96
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 94
diff changeset
    57
    // handle movement
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    58
    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
    59
        input_move |= INPUT_MOVE_LEFT;
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    60
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    61
    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
    62
        input_move |= INPUT_MOVE_RIGHT;
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    63
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    64
    if (keyboard.get_keycode(CL_KEY_UP))
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    65
        input_move |= INPUT_MOVE_UP;
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    66
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    67
    if (keyboard.get_keycode(CL_KEY_DOWN))
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    68
        input_move |= INPUT_MOVE_DOWN;
94
08bebac3d0c2 Maalla liikkumista
ekku
parents: 91
diff changeset
    69
96
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 94
diff changeset
    70
    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
    71
        input_move |= INPUT_MOVE_JUMP;
96
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 94
diff changeset
    72
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    73
    if (keyboard.get_keycode(CL_KEY_I))
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    74
        player->debugInfo();
116
0d36aade845e some stuff, don't remember what
nireco
parents: 108
diff changeset
    75
   
0d36aade845e some stuff, don't remember what
nireco
parents: 108
diff changeset
    76
    if (keyboard.get_keycode(CL_KEY_M))
0d36aade845e some stuff, don't remember what
nireco
parents: 108
diff changeset
    77
        input_move |= INPUT_MOVE_DIG;
0d36aade845e some stuff, don't remember what
nireco
parents: 108
diff changeset
    78
 
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    79
    // apply movement if applicable
50
9e1a6506f5a1 some rough-handed code modifications towards a newer, better, working Physics
terom
parents: 25
diff changeset
    80
    if (input_move)
9e1a6506f5a1 some rough-handed code modifications towards a newer, better, working Physics
terom
parents: 25
diff changeset
    81
        player->handleMove(input_move);
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    82
}
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    83
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    84
void Graphics::do_redraw (void) {
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    85
    CL_GraphicContext *gc = win.get_gc();
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    86
    
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    87
    // white background
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    88
    gc->clear(CL_Color::white);
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    89
60
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
    90
    const float factorX = GRAPHICS_RESOLUTION_WIDTH / MAP_WIDTH;
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
    91
    const float factorY = GRAPHICS_RESOLUTION_HEIGHT / MAP_HEIGHT;
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
    92
86
ed31ece6f340 Segfault <3 (mik? oli odotettavaa)
saiam
parents: 60
diff changeset
    93
    // draw terrain
ed31ece6f340 Segfault <3 (mik? oli odotettavaa)
saiam
parents: 60
diff changeset
    94
    terrain.draw(0,0, gc);
ed31ece6f340 Segfault <3 (mik? oli odotettavaa)
saiam
parents: 60
diff changeset
    95
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    96
    // draw players
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    97
    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
    98
        Player *p = *it;
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    99
        
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
   100
        Vector loc = p->getPosition();
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
   101
        bool right = p->getFacing();
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
   102
        float aim = p->getAim();
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
   103
        std::vector<Vector> &shape = p->getShape();
91
0a6d675099dc Mato salmiakin muotoiseksi
ekku
parents: 90
diff changeset
   104
        // draw quad
0a6d675099dc Mato salmiakin muotoiseksi
ekku
parents: 90
diff changeset
   105
        gc->fill_quad(
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
   106
                      CL_Quad(
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
   107
                              (loc.x+shape[0].x) * factorX, (loc.y+shape[0].y) * factorY,
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
   108
                              (loc.x+shape[1].x) * factorX, (loc.y+shape[1].y) * factorY,
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
   109
                              (loc.x+shape[2].x) * factorX, (loc.y+shape[2].y) * factorY,
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
   110
                              (loc.x+shape[3].x) * factorX, (loc.y+shape[3].y) * factorY
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
   111
                              ), CL_Color::green
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
   112
                      );
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
   113
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
   114
        // Draw crosshair
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
   115
        if (right) {
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
   116
            gc->draw_line(loc.x, loc.y, loc.x + std::cos(aim)*5, loc.y - std::sin(aim)*5, CL_Color::black);
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
   117
        } else {
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
   118
            gc->draw_line(loc.x, loc.y, loc.x - std::cos(aim)*5, loc.y - std::sin(aim)*5, CL_Color::black);
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
   119
        }
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   120
    }
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   121
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   122
    // flip window buffer, LIEK NAO
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   123
    win.flip(0);
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   124
}
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   125
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   126
void Graphics::on_update (void) {
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   127
    // check keyboard input
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   128
    check_input();
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   129
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   130
    // redraw display
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   131
    do_redraw();
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   132
}