/* This file is part of Mjolnir.
*
* Mjolnir is a game engine.
* Copyright (C) 2005 Mjolnir Developers
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*
* mjHUD.cpp is the implementation file for mjHUD.h.
*
* Written by:
* Benny Sperisen
*****************************************************************************/
#include "mjHUD.h"
mjHUD::mjHUD()
{}
mjHUD::message::message(float x,float y,string text,string font_filename,
int font_ptsize,int lasts)
:_x(x),_y(y),_text(text),_lasts(lasts),shrink_ratio(0.00125)
{
SDL_Surface* temp;
SDL_Surface* convtemp;
Uint32 rmask,gmask,bmask,amask;
SDL_Rect src,dest;
SDL_Color color;
if(_lasts>=0)
last_tick=SDL_GetTicks();
_font=TTF_OpenFont(font_filename.c_str(),font_ptsize);
if(_font==NULL)
{
cerr<<"Error: mjHUD::message::message() failed to load font: "
<<font_filename<<endl;
exit(1);
}
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask=0xff000000;
gmask=0x00ff0000;
bmask=0x0000ff00;
amask=0x000000ff;
#else
rmask=0x000000ff;
gmask=0x0000ff00;
bmask=0x00ff0000;
amask=0xff000000;
#endif
_surface=SDL_CreateRGBSurface(SDL_SWSURFACE,2048,2048,24,rmask,
gmask,bmask,amask);
// _surface=IMG_Load("/home/lasindi/trans.png");
if(_surface==NULL)
{
cerr<<"Error: mjHUD::message::message() failed to initialize _surface.\n";
exit(1);
}
/* Uint32 icolor=SDL_MapRGBA(_surface->format,255,255,255,128);
SDL_FillRect(_surface,NULL,icolor);*/
color.r=color.g=0;
color.b=255;
convtemp=TTF_RenderText_Blended(_font,_text.c_str(),color);
temp=SDL_ConvertSurface(convtemp,_surface->format,SDL_SWSURFACE);
if(temp->format->Amask)
cout<<"font - yes\n";
else
cout<<"font - no\n";
SDL_FreeSurface(convtemp);
src.x=src.y=0;
src.w=temp->w;
src.h=temp->h;
dest.x=dest.y=0;
dest.w=src.w;
dest.h=src.h;
SDL_BlitSurface(temp,&src,_surface,&dest);
_w=temp->w;
_h=temp->h;
updateTexture();
SDL_FreeSurface(temp);
}
mjHUD::message::~message()
{
SDL_FreeSurface(_surface);
TTF_CloseFont(_font);
}
void mjHUD::message::move(float x,float y)
{
_x=x;
_y=y;
}
void mjHUD::message::setText(string text)
{
_text=text;
}
float mjHUD::message::x() const
{
return _x;
}
float mjHUD::message::y() const
{
return _y;
}
string mjHUD::message::text() const
{
return _text;
}
int mjHUD::message::lasts()
{
if(_lasts==-1)
return _lasts;
else
{
int current_time=SDL_GetTicks();
_lasts-=current_time-last_tick;
last_tick=current_time;
if(_lasts<0)
_lasts=-2;
return _lasts;
}
}
float mjHUD::message::w() const
{
return _w*shrink_ratio;
}
float mjHUD::message::h() const
{
return _h*shrink_ratio;
}
void mjHUD::message::draw()
{
const float z_value=-0.902;
glBindTexture(GL_TEXTURE_2D,_texture);
glBegin(GL_QUADS);
glTexCoord2f(0.0,_h/_surface->h);
glVertex3f(_x,_y,z_value);
glTexCoord2f(_w/_surface->w,_h/_surface->h);
glVertex3f(_x+w(),_y,z_value);
glTexCoord2f(_w/_surface->w,0.0);
glVertex3f(_x+w(),_y+h(),z_value);
glTexCoord2f(0.0,0.0);
glVertex3f(_x,_y+h(),z_value);
glEnd();
}
void mjHUD::message::updateTexture()
{
if(_surface->format->Amask)
cout<<"final - yes\n";
else
cout<<"final - no\n";
glGenTextures(1,&_texture);
glBindTexture(GL_TEXTURE_2D,_texture);
glTexImage2D(GL_TEXTURE_2D,0,4,_surface->w,_surface->h,0,GL_RGBA,
GL_UNSIGNED_BYTE,_surface->pixels);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
}
void mjHUD::add(message* a_message)
{
_messages.push_back(a_message);
}
void mjHUD::remove(message* a_message)
{
for(list<message*>::iterator it=_messages.begin();it!=_messages.end();it++)
if(*it==a_message)
{
eraseIterator(it);
return;
}
}
void mjHUD::draw()
{
glDisable(GL_DEPTH_TEST);
glPushMatrix();
glTranslatef(-0.499,-0.377,0.0);
for(list<message*>::iterator it=_messages.begin();it!=_messages.end();it++)
(*it)->draw();
glPopMatrix();
glEnable(GL_DEPTH_TEST);
}
float mjHUD::scaleY(float y)
{
return y*0.75;
}
void mjHUD::eraseIterator(list<message*>::iterator it)
{
delete *it;
_messages.erase(it);
}