#ifndef TQUEUE_H_
# define TQUEUE_H_
template<class T> struct Node {
T* data;
Node<T>* next;
};
template<class T> class TQueue{
private:
Node<T>* mpFront;
Node<T>* mpCur;
int mSize;
public:
TQueue(){
this->mpFront = NULL;
this->mpCur = NULL;
this->mSize = 0;
}
~TQueue(){
if(this->mpFront!=NULL){
delete [] this->mpFront;
this->mpFront = NULL;
}
if(this->mpCur != NULL){
delete [] this->mpCur;
this->mpCur = NULL;
}
}
bool hasNext(){
if(this->isEmpty())return false;
return true;
}
T* next(){
T empty
if(this->isEmpty()||this->mpCur==NULL)return ∅
this->mpCur = this->mpCur->next;
return this->mpCur->data;
}
void addNode(T* node){
Node<T> tmp3;
tmp3.data = node;
tmp3.next = NULL;
Node<T>* tmp2 = &tmp3;
if(this->isEmpty()){
this->mpFront = tmp2;
}else{
Node<T>* tmp = this->mpFront;
while(tmp->next!=NULL){
tmp = tmp->next;
}
tmp->next = tmp2;
}
this->mSize++;
this->reset();
}
void addNodeAtFront(T* node){
Node<T>* tmp;
tmp->data = node;
tmp->next = this->mpFront;
this->mpFront = tmp;
this->mSize++;
this->reset();
}
T pop(){
T empty;
if(this->isEmpty())return empty;
Node<T>* tmp = this->mpFront;
Node<T> tmp2 = (*tmp);
this->mpFront = this->mpFront->next;
T* tDat = tmp2.data;
this->mSize--;
return (*tDat);
}
T getLast(){
if(this->isEmpty())return NULL;
Node<T>* tmp = this->mpFront;
while(tmp->next!=NULL){
tmp = tmp->next;
}
Node<T> tmp2 = (*tmp);
T* tDat = tmp2.data;
return (*tDat);
}
void removeFirst(){
if(this->isEmpty())return;
this->mpFront = this->mpFront->next;
this->reset();
this->mSize--;
}
void removeLast(){
if(this->isEmpty())return;
Node<T>* tmp = this->mpFront;
while(tmp->next->next!=NULL){
tmp = tmp->next;
}
tmp->next = NULL;
this->reset();
this->mSize--;
}
void removeAll(){
this->mpFront = NULL;
this->mpCur = NULL;
this->mSize = 0;
}
void reset(){
this->mpCur = this->mpFront;
}
bool isEmpty(){
return (this->mpFront == NULL);
}
int size(){
return this->mSize;
}
};
#endif
here is my code for MessageTracker.h:
#ifndef MESSAGE_TRACKER_H_
# define MESSAGE_TRACKER_H_
# include <irrlicht.h>
# include <string>
# include "GameManager.h"
# include "TQueue.h"
struct MessageNode {
irr::u32 mStartTime;
const wchar_t* mMsg;
MessageNode():mStartTime(0),mMsg(L"empty_stringw_"){}
};
class MessageTracker {
private:
static irr::u32 sMaxDisplayTime;
static irr::u32 sNumMsgDisplay;
static irr::video::SColor sColor;
irr::u32 mFontHeight;
irr::core::dimension2d<irr::u32> mScreenSize;
static irr::gui::IGUIFont* spFont;
static MessageTracker* spMsgTrackInstance;
static TQueue<MessageNode> mQueue;
private:
MessageTracker();
MessageTracker(MessageTracker const&);
~MessageTracker();
private:
void RemoveOldMessages(irr::u32);
public:
static MessageTracker* Instance();
void SetFont(irr::gui::IGUIFont*);
void SetColor(irr::video::SColor);
void AddMessage(irr::core::stringw);
void DrawMessages();
};
#endif
and here is my code for MessageTracker.cc:
#include <irrlicht.h>
#include "MessageTracker.h"
#include "GameManager.h"
#include "TQueue.h"
MessageTracker* MessageTracker::spMsgTrackInstance = NULL;
irr::gui::IGUIFont* MessageTracker::spFont = NULL;
irr::video::SColor MessageTracker::sColor = irr::video::SColor(255,255,255,255);
irr::u32 MessageTracker::sMaxDisplayTime = 3000;
irr::u32 MessageTracker::sNumMsgDisplay = 5;
TQueue<MessageNode> MessageTracker::mQueue = TQueue<MessageNode>();
MessageTracker* MessageTracker::Instance(){
if(!spMsgTrackInstance){
spMsgTrackInstance = new MessageTracker;
}
return spMsgTrackInstance;
}
MessageTracker::MessageTracker(){}
MessageTracker::MessageTracker(MessageTracker const&){}
MessageTracker::~MessageTracker(){}
void MessageTracker::RemoveOldMessages(irr::u32 curTimeMils){
if(mQueue.isEmpty())return;
mQueue.reset();
do{
MessageNode* tmp;
tmp = mQueue.next();
if(tmp->mStartTime==0)return; // this hasn't been initialized yet
if(tmp->mStartTime+sMaxDisplayTime<curTimeMils){
mQueue.pop();
}
if(mQueue.isEmpty())return;
}while(mQueue.hasNext());
}
void MessageTracker::SetFont(irr::gui::IGUIFont* font){
spFont = font;
if(!spFont)return;
this->mFontHeight = spFont->getDimension(L"the quick brown fox jumps over the lazy dog. THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG?").Height;
}
void MessageTracker::SetColor(irr::video::SColor mColor){
sColor = mColor;
}
void MessageTracker::AddMessage(irr::core::stringw msg){
MessageNode temp;
temp.mMsg = msg.c_str();
temp.mStartTime = 0;
mQueue.addNode(&temp);
}
void MessageTracker::DrawMessages(){
GameManager* mGMgr = GameManager::Instance();
if(!mGMgr)return;
irr::IrrlichtDevice* dev = mGMgr->pGetDevice();
if(!dev)return;
irr::u32 curTime = dev->getTimer()->getTime();
this->RemoveOldMessages(curTime);
mQueue.reset();
if(mQueue.isEmpty()||!spFont)return;
irr::u32 pos = 0;
this->mScreenSize = dev->getVideoDriver()->getScreenSize();
irr::f32 msgX = 0;
for(;pos<sNumMsgDisplay&&mQueue.hasNext();pos++){
const wchar_t* mMsg = mQueue.next()->mMsg;
irr::f32 msgWidth = spFont->getDimension(mMsg).Width;
msgX = (this->mScreenSize.Width / 2) - (msgWidth / 2);
spFont->draw(mMsg, irr::core::rect<irr::s32>(msgX, pos*this->mFontHeight, msgX + msgWidth, (pos*this->mFontHeight)+this->mFontHeight), sColor);
mGMgr->Sleep(500);
}
}
this code references another class, GameManager, however, all of the functions in that class work properly and do as they're supposed to.

New Topic/Question
Reply



MultiQuote




|