src/Graphics/GameView.cc
author Tero Marttila <terom@fixme.fi>
Wed, 21 Jan 2009 23:07:22 +0200
branchnew_graphics
changeset 412 721c60072091
parent 411 106aaf6eadfe
child 414 cede5463b845
permissions -rw-r--r--
new graphics code compiles... no, it doesn't work yet
410
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
#include "GameView.hh"
411
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
     3
#include "Graphics.hh"
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
     4
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
     5
#include <cassert>
410
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
namespace graphics
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
{
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
411
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    10
GameView::GameView (GameState &state, LocalPlayer *player) :
412
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    11
    View(PixelArea(0, 0, graphics->display.get_width(), graphics->display.get_height())),
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    12
    state(state), player(NULL), info_view(NULL), message_view(getMessageViewArea()),
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    13
    flags(0)
411
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    14
{
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    15
    // have player?
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    16
    if (player)
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    17
        setPlayer(player);
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    18
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    19
    // insert message
412
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    20
    message_view.add_message(CL_Color::white, "Hello World!");
411
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    21
}
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    22
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    23
void GameView::setPlayer (LocalPlayer *player) {
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    24
    assert(!this->player && player);
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    25
    
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    26
    // remember it
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    27
    this->player = player;
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    28
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    29
    // build the info_view as well
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    30
    info_view = new PlayerInfoView(getInfoViewArea(), player);
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    31
}
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    32
412
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    33
/*
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    34
 * Helper function for Camera
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    35
 */
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    36
static PixelDimension value_between (PixelDimension low, PixelDimension value, PixelDimension high) {
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    37
        if (high < low)
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    38
            return (high + low) / 2;
410
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
412
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    40
        else if (value < low)
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    41
            return low;
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    42
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    43
        else if (value > high)
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    44
            return high;
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    45
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    46
        else
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    47
            return value;
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    48
}
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    49
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    50
void GameView::draw (Display &display) {
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    51
    CL_GraphicContext *gc = display.get_gc();
410
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
    // calculate camera
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
    PixelCoordinate camera(0, 0);
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
    
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
    // ...to track our local player
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
    if (player != NULL) {
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
        // display resolution
412
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    59
        PixelCoordinate resolution = display.getResolution();
410
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
        // try and center the screen on the player
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
        PixelCoordinate target = player->getCoordinate() - PixelCoordinate(resolution.x / 2, (resolution.y - 100) / 2);
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    64
        // ...but keep the world in view
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    65
        PixelCoordinate max = state.terrain.getDimensions() - resolution + PixelCoordinate(0, 100);
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
        
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
        // ...by limiting the value to 0...max
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
        camera = PixelCoordinate(
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
            value_between(0, target.x, max.x),
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    70
            value_between(0, target.y, max.y)
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
        );
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
    }
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
    
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
    // Black background
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    75
    gc->clear(CL_Color::black);
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    77
    // Draw the game
412
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    78
    state.draw(display, camera, flags & GUI_INPUT_DISPLAY_WEAPON);
410
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    79
    
411
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    80
    // draw info view?
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    81
    if (info_view)
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    82
        info_view->draw(display);
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    83
410
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    84
    // draw messages
412
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    85
    message_view.draw(display);
410
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    86
}
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    87
411
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    88
void GameView::resize (const PixelArea &new_area) {
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    89
    View::resize(new_area);
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    90
    
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    91
    // resize subcomponents
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    92
    if (info_view)
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    93
        info_view->resize(getInfoViewArea());
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    94
    
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    95
    message_view.resize(getMessageViewArea());
410
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    96
411
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    97
    // log message
412
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    98
    message_view.add_message(CL_Color::yellow, CL_String::format("[ Resized window to %1 x %2 ]", (int) getWidth(), (int) getHeight()));
411
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    99
}
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
   100
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
   101
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
   102
}