src/Graphics.cc
author nireco
Mon, 08 Dec 2008 21:54:04 +0000
changeset 316 d909a7e36f8d
parent 315 fe9da2d5355e
child 318 dca3c836edb2
permissions -rw-r--r--
value_between now puts small map in the center, or at least should
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) :
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
     7
    CL_DisplayWindow(GRAPHICS_WINDOW_TITLE, GRAPHICS_RESOLUTION_WIDTH, GRAPHICS_RESOLUTION_HEIGHT),
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
     8
    engine(engine), 
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
     9
    state(state), 
282
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 275
diff changeset
    10
    resolution(GRAPHICS_RESOLUTION_WIDTH, GRAPHICS_RESOLUTION_HEIGHT),
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    11
    update_timer(GRAPHICS_UPDATE_INTERVAL_MS),
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    12
    input(get_ic()->get_keyboard()),
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    13
    simple_font("Font2", engine.getResourceManager()) 
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    14
{
86
ed31ece6f340 Segfault <3 (mik? oli odotettavaa)
saiam
parents: 60
diff changeset
    15
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    16
    // connect timer signal
205
905028e58ed1 implement a new tick-timer that doesn't suck
terom
parents: 199
diff changeset
    17
    slots.connect(update_timer.sig_tick(), this, &Graphics::on_update);
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    18
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    19
    // enable
205
905028e58ed1 implement a new tick-timer that doesn't suck
terom
parents: 199
diff changeset
    20
    update_timer.start();
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    21
}
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    22
311
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 282
diff changeset
    23
void Graphics::check_input (void) {
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    24
    LocalPlayer *player;
311
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 282
diff changeset
    25
    PlayerInput input_mask;
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 282
diff changeset
    26
    TimeMS input_dt;
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    27
    
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    28
    // update gui flags
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    29
    this->flags = input.readGuiInput();
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    30
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    31
    // quit?
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    32
    if (flags & GUI_INPUT_QUIT) {
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    33
        engine.stop();
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    34
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    35
        return;
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    36
    }
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    37
     
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    38
    // stop here if we don't have a local player
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    39
    if ((player = state.getLocalPlayer()) == NULL)
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    40
        return;
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    41
    
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 212
diff changeset
    42
    // dump debug info on stderr
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    43
    if (flags & GUI_INPUT_DEBUG_PLAYER)
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 212
diff changeset
    44
        player->printDebugInfo();
180
bfe1077edab3 Ammuksia f:lla
ekku
parents: 162
diff changeset
    45
    
230
78cf0cd69af4 better input handling
terom
parents: 222
diff changeset
    46
    // build input_mask
311
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 282
diff changeset
    47
    input.readPlayerInput(input_mask, input_dt);
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 212
diff changeset
    48
    
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    49
    // apply input if there was any
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 212
diff changeset
    50
    if (input_mask)
311
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 282
diff changeset
    51
        player->handleInput(input_mask, input_dt);
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    52
}
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    53
266
ad72d0a0cc02 increase the map size and control the camera to always have the screen full of terrain
terom
parents: 248
diff changeset
    54
static PixelDimension value_between (PixelDimension low, PixelDimension value, PixelDimension high) {
316
d909a7e36f8d value_between now puts small map in the center, or at least should
nireco
parents: 315
diff changeset
    55
    if (high < low)
d909a7e36f8d value_between now puts small map in the center, or at least should
nireco
parents: 315
diff changeset
    56
        return (high + low) / 2;
d909a7e36f8d value_between now puts small map in the center, or at least should
nireco
parents: 315
diff changeset
    57
d909a7e36f8d value_between now puts small map in the center, or at least should
nireco
parents: 315
diff changeset
    58
    else if (value < low)
266
ad72d0a0cc02 increase the map size and control the camera to always have the screen full of terrain
terom
parents: 248
diff changeset
    59
        return low;
ad72d0a0cc02 increase the map size and control the camera to always have the screen full of terrain
terom
parents: 248
diff changeset
    60
ad72d0a0cc02 increase the map size and control the camera to always have the screen full of terrain
terom
parents: 248
diff changeset
    61
    else if (value > high)
ad72d0a0cc02 increase the map size and control the camera to always have the screen full of terrain
terom
parents: 248
diff changeset
    62
        return high;
ad72d0a0cc02 increase the map size and control the camera to always have the screen full of terrain
terom
parents: 248
diff changeset
    63
ad72d0a0cc02 increase the map size and control the camera to always have the screen full of terrain
terom
parents: 248
diff changeset
    64
    else
ad72d0a0cc02 increase the map size and control the camera to always have the screen full of terrain
terom
parents: 248
diff changeset
    65
        return value;
ad72d0a0cc02 increase the map size and control the camera to always have the screen full of terrain
terom
parents: 248
diff changeset
    66
}
ad72d0a0cc02 increase the map size and control the camera to always have the screen full of terrain
terom
parents: 248
diff changeset
    67
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    68
void Graphics::do_redraw (void) {
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    69
    CL_GraphicContext *gc = get_gc();
266
ad72d0a0cc02 increase the map size and control the camera to always have the screen full of terrain
terom
parents: 248
diff changeset
    70
    LocalPlayer *player;
ad72d0a0cc02 increase the map size and control the camera to always have the screen full of terrain
terom
parents: 248
diff changeset
    71
ad72d0a0cc02 increase the map size and control the camera to always have the screen full of terrain
terom
parents: 248
diff changeset
    72
    // calculate camera
ad72d0a0cc02 increase the map size and control the camera to always have the screen full of terrain
terom
parents: 248
diff changeset
    73
    PixelCoordinate camera(0, 0);
ad72d0a0cc02 increase the map size and control the camera to always have the screen full of terrain
terom
parents: 248
diff changeset
    74
    
ad72d0a0cc02 increase the map size and control the camera to always have the screen full of terrain
terom
parents: 248
diff changeset
    75
    // ...to track our local player
ad72d0a0cc02 increase the map size and control the camera to always have the screen full of terrain
terom
parents: 248
diff changeset
    76
    if ((player = state.getLocalPlayer()) != NULL) {
ad72d0a0cc02 increase the map size and control the camera to always have the screen full of terrain
terom
parents: 248
diff changeset
    77
        PixelCoordinate target = player->getCoordinate() - resolution / 2;
ad72d0a0cc02 increase the map size and control the camera to always have the screen full of terrain
terom
parents: 248
diff changeset
    78
        PixelCoordinate max = state.world.getDimensions() - resolution;
ad72d0a0cc02 increase the map size and control the camera to always have the screen full of terrain
terom
parents: 248
diff changeset
    79
        
ad72d0a0cc02 increase the map size and control the camera to always have the screen full of terrain
terom
parents: 248
diff changeset
    80
        // keep the terrain in view
ad72d0a0cc02 increase the map size and control the camera to always have the screen full of terrain
terom
parents: 248
diff changeset
    81
        camera = PixelCoordinate(
ad72d0a0cc02 increase the map size and control the camera to always have the screen full of terrain
terom
parents: 248
diff changeset
    82
            value_between(0, target.x, max.x),
ad72d0a0cc02 increase the map size and control the camera to always have the screen full of terrain
terom
parents: 248
diff changeset
    83
            value_between(0, target.y, max.y)
ad72d0a0cc02 increase the map size and control the camera to always have the screen full of terrain
terom
parents: 248
diff changeset
    84
        );
ad72d0a0cc02 increase the map size and control the camera to always have the screen full of terrain
terom
parents: 248
diff changeset
    85
    }
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    86
    
312
10743f190aab info box
nireco
parents: 311
diff changeset
    87
    // Black background
248
e40ef56dc62c scrolling looks like it works
nireco
parents: 235
diff changeset
    88
    gc->clear(CL_Color::black);
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
    89
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    90
    // Draw the game
266
ad72d0a0cc02 increase the map size and control the camera to always have the screen full of terrain
terom
parents: 248
diff changeset
    91
    state.draw(this, camera, flags & GUI_INPUT_DISPLAY_WEAPON);
180
bfe1077edab3 Ammuksia f:lla
ekku
parents: 162
diff changeset
    92
312
10743f190aab info box
nireco
parents: 311
diff changeset
    93
    // Draw box for player information
10743f190aab info box
nireco
parents: 311
diff changeset
    94
    if (player != NULL) {
315
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
    95
        draw_player_info(gc, (Player*)player);
312
10743f190aab info box
nireco
parents: 311
diff changeset
    96
    }
10743f190aab info box
nireco
parents: 311
diff changeset
    97
184
561892e2a30e try and sync graphics flip
terom
parents: 182
diff changeset
    98
    // Flip window buffer, sync
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    99
    flip(1);
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   100
}
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   101
205
905028e58ed1 implement a new tick-timer that doesn't suck
terom
parents: 199
diff changeset
   102
void Graphics::on_update (TimeMS tick_length) {
311
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 282
diff changeset
   103
    (void) tick_length;
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 282
diff changeset
   104
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   105
    // check keyboard input
311
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 282
diff changeset
   106
    check_input();
25
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   107
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   108
    // redraw display
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   109
    do_redraw();
af75a1894a32 simple proto *almost* works
terom
parents:
diff changeset
   110
}
315
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   111
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   112
void Graphics::draw_player_info(CL_GraphicContext *gc, Player *p) {
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   113
    int box_top = GRAPHICS_RESOLUTION_HEIGHT - 100;
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   114
    int box_left = 0;
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   115
    int box_right = GRAPHICS_RESOLUTION_WIDTH;
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   116
    int box_bottom = GRAPHICS_RESOLUTION_HEIGHT;
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   117
    int life_bar_length = 3; // *100
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   118
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   119
    // draw status info at bottom of display
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   120
    gc->fill_rect(
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   121
        CL_Rect(
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   122
            box_left,
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   123
            box_top,
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   124
            box_right,
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   125
            box_bottom
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   126
        ),
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   127
        CL_Gradient(
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   128
            CL_Color(0, 0, 0, 100),
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   129
            CL_Color(50, 50, 50, 150),
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   130
            CL_Color(50, 50, 50, 150),
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   131
            CL_Color(100, 100, 100, 200)
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   132
        )
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   133
    );
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   134
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   135
    gc->draw_rect(
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   136
        CL_Rect(
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   137
            box_left + 9,
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   138
            box_top + 9,
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   139
            box_left + 11 + 100 * life_bar_length,
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   140
            box_top + 31
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   141
        ),
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   142
        CL_Color(150, 150, 150)
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   143
    );
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   144
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   145
    gc->fill_rect(
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   146
        CL_Rect(
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   147
            box_left + 10,
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   148
            box_top + 10,
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   149
            box_left + 10 + (int) (p->getHealthPercent() * life_bar_length),
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   150
            box_top + 30
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   151
        ),
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   152
        CL_Gradient(
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   153
            CL_Color(200, 0, 0),
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   154
            CL_Color(200 - (int)(p->getHealthPercent() * 2), (int)(p->getHealthPercent() * 2), 0),
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   155
            CL_Color(200, 0, 0),
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   156
            CL_Color(200 - (int)(p->getHealthPercent() * 2), (int)(p->getHealthPercent() * 2), 0)
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   157
        )
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   158
    );
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   159
}
fe9da2d5355e better colors on downbar
nireco
parents: 312
diff changeset
   160