src/messageq.cpp

Go to the documentation of this file.
00001 #include "messageq.h"
00002 
00003 MESSAGEQ::MESSAGEQ()
00004 {
00005         Clear();
00006         posx = 0;
00007         posy = 0;
00008         size = 6;
00009         fset = 1;
00010         qpersist = 1.0f;
00011         printtime = false;
00012         buildup = true;
00013 }
00014 
00015 extern bool verbose_output;
00016 MESSAGEQ::~MESSAGEQ()
00017 {
00018         if (verbose_output)
00019                 cout << "message queue deinit" << endl;
00020         
00021         Clear();
00022 }
00023 
00024 void MESSAGEQ::Clear()
00025 {
00026         for (int i = 0; i < MAX_Q_DEPTH; i++)
00027         {
00028                 valid[i] = false;
00029                 qlen[i] = -1;
00030         }
00031 }
00032 
00033 void MESSAGEQ::SetDepth(int newqdepth)
00034 {
00035         qdepth = newqdepth;
00036         Clear();
00037 }
00038 
00039 void MESSAGEQ::SetPersist(float newqpersist)
00040 {
00041         qpersist = newqpersist;
00042 }
00043 
00044 void MESSAGEQ::SetBuildUp(bool newbuildup)
00045 {
00046         buildup = newbuildup;
00047 }
00048 
00049 void MESSAGEQ::SetPos(float newx, float newy, int newsize, int newfset)
00050 {
00051         posx = newx;
00052         posy = newy;
00053         size = newsize;
00054         fset = newfset;
00055 }
00056 
00057 void MESSAGEQ::AddMessage(string newq)
00058 {
00059         int foundopen = -1;
00060         int i;
00061         
00062         int enforcer = 0;
00063         
00064         //first look for an open slot
00065         for (i = 0; i < MAX_Q_DEPTH; i++)
00066         {
00067                 if (valid[i])
00068                         enforcer++;
00069                 
00070                 if (!valid[i])
00071                         foundopen = i;
00072         }
00073         
00074         //okay, now look for the least recent slot
00075         if (foundopen < 0 || enforcer + 1 > qdepth)
00076         {
00077                 float minlen = 10000.0;
00078                 foundopen = 0;
00079                 
00080                 for (i = 0; i < MAX_Q_DEPTH; i++)
00081                 {
00082                         if (valid[i] && qlen[i] < minlen)
00083                         {
00084                                 foundopen = i;
00085                                 minlen = qlen[i];
00086                         }
00087                 }
00088         }
00089 
00090         //so, now foundopen is garunteed to have a reasonable value
00091 
00092         q[foundopen] = newq;
00093         qlen[foundopen] = qpersist;     
00094         valid[foundopen] = true;
00095 }
00096 
00097 void MESSAGEQ::Draw(float timefactor, float fps, FONT & font)
00098 {
00099         int i;
00100 
00101         
00102         //***update queue
00103         
00104         for (i = 0; i < MAX_Q_DEPTH; i++)
00105         {
00106                 //subtract frame length
00107                 if (valid[i])
00108                         qlen[i] -= timefactor/fps;
00109                 
00110                 //remove expired messages
00111                 if (qlen[i] < 0)
00112                         valid[i] = false;
00113         }
00114         
00115         
00116         //***sort the queue
00117         
00118         int sortnum = 0;
00119         string sortq[MAX_Q_DEPTH];
00120         float sortlen[MAX_Q_DEPTH];
00121         
00122         bool tempvalid[MAX_Q_DEPTH];
00123         
00124         //populate temp vars
00125         for (i = 0; i < MAX_Q_DEPTH; i++)
00126         {
00127                 tempvalid[i] = valid[i];
00128         }
00129         
00130         //do the sorting with my kewl VENZALGORITHM
00131         if (buildup)
00132         {
00133                 int m;
00134                 for (i = 0; i < MAX_Q_DEPTH; i++)
00135                 {
00136                         float maxval = 0.0f;
00137                         int maxindex = -1;
00138                         
00139                         for (m = 0; m < MAX_Q_DEPTH; m++)
00140                         {
00141                                 if (tempvalid[m] && qlen[m] > maxval)
00142                                 {
00143                                         maxval = qlen[m];
00144                                         //tempvalid[m] = false;
00145                                         maxindex = m;
00146                                 }
00147                         }
00148                         
00149                         if (maxindex < 0)
00150                                 break;
00151                         
00152                         sortlen[sortnum] = qlen[maxindex];
00153                         
00154                         if (printtime)
00155                         {
00156                                 char tempchar[1024];
00157                                 sprintf(tempchar, "%.1f) %s", sortlen[sortnum], q[maxindex].c_str());
00158                                 sortq[sortnum] = tempchar;
00159                         }
00160                         else
00161                         {
00162                                 sortq[sortnum] = q[maxindex];
00163                         }
00164                         tempvalid[maxindex] = false;
00165                         sortnum++;
00166                 }
00167         }
00168         else
00169         {
00170                 int m;
00171                 for (i = 0; i < MAX_Q_DEPTH; i++)
00172                 {
00173                         float minval = 1000.0f;
00174                         int minindex = -1;
00175                         
00176                         for (m = 0; m < MAX_Q_DEPTH; m++)
00177                         {
00178                                 if (tempvalid[m] && qlen[m] < minval)
00179                                 {
00180                                         minval = qlen[m];
00181                                         //tempvalid[m] = false;
00182                                         minindex = m;
00183                                 }
00184                         }
00185                         
00186                         if (minindex < 0)
00187                                 break;
00188                         
00189                         sortlen[sortnum] = qlen[minindex];
00190                         
00191                         if (printtime)
00192                         {
00193                                 char tempchar[1024];
00194                                 sprintf(tempchar, "%.1f) %s", sortlen[sortnum], q[minindex].c_str());
00195                                 sortq[sortnum] = tempchar;
00196                         }
00197                         else
00198                         {
00199                                 sortq[sortnum] = q[minindex];
00200                         }
00201                         tempvalid[minindex] = false;
00202                         sortnum++;
00203                 }
00204         }
00205         
00206         
00207         
00208         
00209         
00210         //***print sorted queue
00211         
00212         string strout;
00213         string finalstr;
00214         strout = "";
00215         
00216         for (i = 0; i < sortnum; i++)
00217         {
00218                 finalstr = strout;
00219                 
00220                 float trans;
00221                 if (sortlen[i] < qpersist/4.0)
00222                         trans = sortlen[i]/(qpersist/4.0);
00223                 else
00224                         trans = 1.0;
00225                 
00226                 finalstr.append(sortq[i]);
00227                 font.Print(posx,posy,finalstr.c_str(),fset,size,trans);
00228                 strout.append("\n");
00229         }
00230 }
00231 
00232 void MESSAGEQ::SetTimePrint(bool newprinttime)
00233 {
00234         printtime = newprinttime;
00235 }

Generated on Thu Oct 19 04:05:54 2006 by  doxygen 1.4.6