src/GameMessageView.cc
author Tero Marttila <terom@fixme.fi>
Fri, 16 Jan 2009 22:06:51 +0200
changeset 401 6cd204d05600
parent 393 5dd4d782cf3a
permissions -rw-r--r--
fix mkcmake.sh

#include "GameMessageView.hh"
#include "Graphics.hh"
#include "Engine.hh"

GameMessageView::GameMessageView (PixelArea area) :
    area(area), messages()
{
    
}

void GameMessageView::add_message (CL_Color color, std::string message) {
    GameMessage msg (color, message);

    messages.push_back(msg);
}

void GameMessageView::draw (Graphics *g) {
    // get font
    CL_Font &font = g->getSimpleFont(); 

    // remember color
    CL_Color font_color = font.get_color();
    
    // maximum width
    CL_Size max_size = CL_Size(area.right - area.left, 0);

    // starting point for drawing
    PixelDimension offset_prev = area.bottom;

    // render messages, bottom up
    for (std::vector<GameMessage>::reverse_iterator it = messages.rbegin(); it != messages.rend(); it++) {
        // set message color
        font.set_color(it->color);

        // calculate height
        PixelDimension height = font.get_height(it->message, max_size) + GRAPHICS_INFO_TEXT_LINE_OFFSET;
       
        // new draw_point
        PixelDimension offset_this = offset_prev - height;

        // break if it doesn't fit anymore
        if (offset_this < area.top)
            break;
 
        // draw text
        font.draw(CL_Rect(area.left, offset_this, area.right, offset_prev), it->message, g->get_gc());
        
        // advance offset
        offset_prev = offset_this;
    }

    // restore font color
    font.set_color(font_color);
}