Filter:   InfoImg
download message.cc
Language: C++
License: GPL
LOC: 71
Project Info
Ultima Online Project(uop)
Server: SourceForge
Type: cvs
SourceForge\u\uop\uop\uop\
   acinclude.m4
   aclocal.m4
   action.cc
   action.h
   animation.cc
   animation.h
   artexport.cc
   artfactory.cc
   artfactory.h
   audio.cc
   audio.h
   btree.cc
   btree.h
   cache.h
   cliloc.cc
   cliloc.h
   config.h.in
   configure.in
   cvs2cl.pl
   entity.cc
   entity.h
   gumps.cc
   gumps.h
   interface.cc
   interface.h
   interfacegumps.cc
   interfacegumps.h
   interfacehandler.cc
   interfacehandler.h
   intrface.def
   Makefile.am
   Makefile.in
   message.cc
   message.h
   musicmap.def
   network.cc
   network.h
   patchhandler.cc
   patchhandler.h
   readable.pl
   serverhandler.cc
   serverhandler.h
   sprite.cc
   sprite.h
   stamp-h.in
   texthandler.cc
   texthandler.h
   tiledata.cc
   tiledata.h
   unreadable.pl
   uop.cc
   uop.cfg
   uop.dox
   uop.h
   uopconfig.cc
   uopconfig.h
   uosprite.h
   uostub.pl
   world.cc
   world.h

/**
 *  @class Message
 *  @brief Represents temporary text messages.
 *  @author Samuel Kaufman
 *  @date 2002
 */
/*****
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *****/

#include "uop.h"
#include <SDL/SDL.h>
#include <iostream>
#include <string>
#include <list>
#include "interfacehandler.h"
#include "texthandler.h"
#include "uopconfig.h"
#include "message.h"

#define LEFTRULE 15
#define LIFETIME 50000
#define STACKSIZE 12

#define DEFAULTMESSAGEFONT 0
#define DEFAULTMESSAGEHUE 0xaabbccdd

extern SDL_Surface *screen;
extern UOPConfig *config;
extern TextHandler *textHandler;
extern InterfaceHandler *interfaceHandler;

list<Message*> Message::messageStack;

/**
 * Creates a new message, and possibly
 * adds it to the stack.
 * @param text String that the message will
 * draw to the screen.
 * @param stack Wether or not to add this
 * message to the message stack.
 */
Message::Message(string text, bool unicode, bool stack)
{
  this->text = text;
  this->birth = SDL_GetTicks();
  this->font = DEFAULTMESSAGEFONT;
  this->hue = (Uint16)DEFAULTMESSAGEHUE;
  this->unicode=unicode;
  if (stack)
    messageStack.push_back(this);

  // do some stack message throw-out checking
  while (messageStack.size() > STACKSIZE)
    {
      delete messageStack.front();
      messageStack.pop_front();
    }
}

/**
 * Returns the height, in pixels, of the
 * message string as it would be when drawn
 * to the screen.
 */
unsigned int Message::getHeight()
{
	if (!unicode)
		return textHandler->getHeight(font);
	else
		return textHandler->unicodeHeight(font);
}

/**
 * Draw the message string to the screen at
 * the specified coordinates.
 * @param x X offset (in pixels)
 * @param y Y offset (in pixels)
 */
void Message::draw(unsigned int x, unsigned int y)
{
	if (!unicode)
		textHandler->drawString(text,font,x,y,hue);
	else
		textHandler->drawUnicodeString(text, font, x, y, hue);
}

/**
 * Draws all Messages in the message stack
 * to the screen in their correct positions.
 */
void Message::drawMessageStack()
{
  if (messageStack.empty())
    return;

  list<Message*>::iterator cur;
  // 30 pixels for input line
  unsigned int height = 30;

  // do some expired stack message checking
  Message *fElement = messageStack.front();
  while (fElement->birth+LIFETIME < SDL_GetTicks())
    {
      delete fElement;
      messageStack.pop_front();
      if (messageStack.empty())
	return;
      fElement = messageStack.front();
    }

  for (cur = messageStack.begin(); cur != messageStack.end(); cur++)
    height += (unsigned int)((*cur)->getHeight()) + 5;
  // TODO: Add a little space for the insertion field

  for (cur = messageStack.begin(); cur != messageStack.end(); cur++)
    {
      (*cur)->draw(LEFTRULE, interfaceHandler->getScreenHeight()-height);
      height -= (unsigned int)((*cur)->getHeight()) + 5;
    }
}