src/Graphics/GameView.cc
author Tero Marttila <terom@fixme.fi>
Wed, 21 Jan 2009 03:33:35 +0200
branchnew_graphics
changeset 411 106aaf6eadfe
parent 410 41fd46cffc52
child 412 721c60072091
permissions -rw-r--r--
there's a grain of truth in the new graphics code now...

#include "GameView.hh"
#include "Graphics.hh"

#include <cassert>

namespace graphics
{

GameView::GameView (GameState &state, LocalPlayer *player) :
    View(PixelArea(0, 0, graphics->display.get_width, graphics->display.get_height)),
    state(state), player(NULL), info_view(NULL), message_view(getMessageViewArea())
{
    // have player?
    if (player)
        setPlayer(player);

    // insert message
    message_view.add_message("Hello World!");
}

void GameView::setPlayer (LocalPlayer *player) {
    assert(!this->player && player);
    
    // remember it
    this->player = player;

    // build the info_view as well
    info_view = new PlayerInfoView(getInfoViewArea(), player);
}


void GameView::draw (Display *display) {
    CL_GraphicContext *gc = display->get_gc();

    // calculate camera
    PixelCoordinate camera(0, 0);
    
    // ...to track our local player
    if (player != NULL) {
        // display resolution
        PixelCoordinate resolution = display->getResolution();

        // try and center the screen on the player
        PixelCoordinate target = player->getCoordinate() - PixelCoordinate(resolution.x / 2, (resolution.y - 100) / 2);

        // ...but keep the world in view
        PixelCoordinate max = state.terrain.getDimensions() - resolution + PixelCoordinate(0, 100);
        
        // ...by limiting the value to 0...max
        camera = PixelCoordinate(
            value_between(0, target.x, max.x),
            value_between(0, target.y, max.y)
        );
    }
    
    // Black background
    gc->clear(CL_Color::black);

    // Draw the game
    state.draw(this, camera, flags & GUI_INPUT_DISPLAY_WEAPON);
    
    // draw info view?
    if (info_view)
        info_view->draw(display);

    // draw messages
    message_view.draw(this);
}

void GameView::resize (const PixelArea &new_area) {
    View::resize(new_area);
    
    // resize subcomponents
    if (info_view)
        info_view->resize(getInfoViewArea());
    
    message_view.resize(getMessageViewArea());

    // log message
    message_view.add_message(CL_Color::yellow, CL_String::format("[ Resized window to %1 x %2 ]", getWidth(), getHeight()));
}


}