src/Graphics.cc
author ekku
Thu, 04 Dec 2008 19:53:59 +0000
changeset 197 d9ac888de778
parent 196 e2d32c4601ce
child 199 f5c86420facd
permissions -rw-r--r--
Hajotetaan lis??
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 "GameState.hh"
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
     4
#include <cmath>
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),
60
26571fd9a8d1 physics is starting to work
terom
parents: 58
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()) {
86
ed31ece6f340 Segfault <3 (mik? oli odotettavaa)
saiam
parents: 60
diff changeset
    12
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    13
    // connect timer signal
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    14
    slots.connect(update_timer.sig_timer(), this, &Graphics::on_update);
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    15
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    16
    // enable
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    17
    update_timer.enable();
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    18
}
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    19
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    20
void Graphics::check_input (void) {
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    21
    LocalPlayer *player;
58
a53f5ad69500 "working" singleplayer
terom
parents: 54
diff changeset
    22
    PlayerInput_Move input_move = 0;
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    23
    
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    24
    // stop on escape
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    25
    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
    26
        engine.stop();
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    27
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    28
        return;
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    29
    }
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    30
     
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    31
    // ignore if we don't have a local player
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    32
    if ((player = state.getLocalPlayer()) == NULL)
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    33
        return;
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    34
    
96
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 94
diff changeset
    35
    // handle movement
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    36
    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
    37
        input_move |= INPUT_MOVE_LEFT;
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    38
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    39
    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
    40
        input_move |= INPUT_MOVE_RIGHT;
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    41
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    42
    if (keyboard.get_keycode(CL_KEY_UP))
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    43
        input_move |= INPUT_MOVE_UP;
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    44
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    45
    if (keyboard.get_keycode(CL_KEY_DOWN))
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    46
        input_move |= INPUT_MOVE_DOWN;
94
08bebac3d0c2 Maalla liikkumista
ekku
parents: 91
diff changeset
    47
96
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 94
diff changeset
    48
    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
    49
        input_move |= INPUT_MOVE_JUMP;
96
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 94
diff changeset
    50
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    51
    if (keyboard.get_keycode(CL_KEY_I))
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    52
        player->debugInfo();
180
bfe1077edab3 Ammuksia f:lla
ekku
parents: 162
diff changeset
    53
    
bfe1077edab3 Ammuksia f:lla
ekku
parents: 162
diff changeset
    54
    if (keyboard.get_keycode(CL_KEY_F)) {
bfe1077edab3 Ammuksia f:lla
ekku
parents: 162
diff changeset
    55
        Engine::log(DEBUG, "Graphics.check_input") << "Fire!";
182
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
    56
        input_move |= INPUT_SHOOT;
180
bfe1077edab3 Ammuksia f:lla
ekku
parents: 162
diff changeset
    57
    }
116
0d36aade845e some stuff, don't remember what
nireco
parents: 108
diff changeset
    58
   
0d36aade845e some stuff, don't remember what
nireco
parents: 108
diff changeset
    59
    if (keyboard.get_keycode(CL_KEY_M))
0d36aade845e some stuff, don't remember what
nireco
parents: 108
diff changeset
    60
        input_move |= INPUT_MOVE_DIG;
0d36aade845e some stuff, don't remember what
nireco
parents: 108
diff changeset
    61
 
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    62
    // apply movement if applicable
50
9e1a6506f5a1 some rough-handed code modifications towards a newer, better, working Physics
terom
parents: 25
diff changeset
    63
    if (input_move)
9e1a6506f5a1 some rough-handed code modifications towards a newer, better, working Physics
terom
parents: 25
diff changeset
    64
        player->handleMove(input_move);
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    65
}
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    66
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    67
void Graphics::do_redraw (void) {
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    68
    CL_GraphicContext *gc = win.get_gc();
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    69
    
162
f760591b7481 Removed unnecessary variables (factoFoo) from do_redraw. Renamed
saiam
parents: 161
diff changeset
    70
    // White background
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    71
    gc->clear(CL_Color::white);
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    72
162
f760591b7481 Removed unnecessary variables (factoFoo) from do_redraw. Renamed
saiam
parents: 161
diff changeset
    73
    // Draw terrain
182
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
    74
    state.draw(gc);
180
bfe1077edab3 Ammuksia f:lla
ekku
parents: 162
diff changeset
    75
184
561892e2a30e try and sync graphics flip
terom
parents: 182
diff changeset
    76
    // Flip window buffer, sync
561892e2a30e try and sync graphics flip
terom
parents: 182
diff changeset
    77
    win.flip(1);
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    78
}
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    79
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    80
void Graphics::on_update (void) {
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    81
    // check keyboard input
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    82
    check_input();
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    83
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    84
    // redraw display
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    85
    do_redraw();
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    86
}