src/Graphics.cc
author ekku
Sat, 06 Dec 2008 17:51:19 +0000
changeset 222 293ddf4c067d
parent 221 fbc5db6fce45
child 230 78cf0cd69af4
permissions -rw-r--r--
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
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
205
905028e58ed1 implement a new tick-timer that doesn't suck
terom
parents: 199
diff changeset
    14
    slots.connect(update_timer.sig_tick(), this, &Graphics::on_update);
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    15
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    16
    // enable
205
905028e58ed1 implement a new tick-timer that doesn't suck
terom
parents: 199
diff changeset
    17
    update_timer.start();
25
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;
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 212
diff changeset
    22
    PlayerInput input_mask = 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
    
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 212
diff changeset
    35
    // dump debug info on stderr
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    36
    if (keyboard.get_keycode(CL_KEY_I))
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 212
diff changeset
    37
        player->printDebugInfo();
180
bfe1077edab3 Ammuksia f:lla
ekku
parents: 162
diff changeset
    38
    
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 212
diff changeset
    39
    // check our keymap
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 212
diff changeset
    40
    for (InputKeymapEntry *e = getGlobalInputKeymap(); e->keycode && e->input; e++) {
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 212
diff changeset
    41
        if (keyboard.get_keycode(e->keycode))
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 212
diff changeset
    42
            input_mask |= e->input;
180
bfe1077edab3 Ammuksia f:lla
ekku
parents: 162
diff changeset
    43
    }
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 212
diff changeset
    44
    
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 212
diff changeset
    45
    // apply input if applicable
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 212
diff changeset
    46
    if (input_mask)
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 212
diff changeset
    47
        player->handleInput(input_mask);
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    48
}
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    49
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    50
void Graphics::do_redraw (void) {
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    51
    CL_GraphicContext *gc = win.get_gc();
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    52
    
162
f760591b7481 Removed unnecessary variables (factoFoo) from do_redraw. Renamed
saiam
parents: 161
diff changeset
    53
    // White background
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    54
    gc->clear(CL_Color::white);
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    55
162
f760591b7481 Removed unnecessary variables (factoFoo) from do_redraw. Renamed
saiam
parents: 161
diff changeset
    56
    // Draw terrain
222
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
    57
    state.draw(gc);
180
bfe1077edab3 Ammuksia f:lla
ekku
parents: 162
diff changeset
    58
184
561892e2a30e try and sync graphics flip
terom
parents: 182
diff changeset
    59
    // Flip window buffer, sync
561892e2a30e try and sync graphics flip
terom
parents: 182
diff changeset
    60
    win.flip(1);
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    61
}
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    62
205
905028e58ed1 implement a new tick-timer that doesn't suck
terom
parents: 199
diff changeset
    63
void Graphics::on_update (TimeMS tick_length) {
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    64
    // check keyboard input
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    65
    check_input();
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    66
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    67
    // redraw display
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    68
    do_redraw();
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    69
}