Planeshift

psutil.h

Go to the documentation of this file.
00001 /*
00002  * Author: Andrew Robberts
00003  *
00004  * Copyright (C) 2004 Atomic Blue ([email protected], http://www.atomicblue.org) 
00005  *
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation (version 2 of the License)
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017  *
00018  */
00019 
00020 #ifndef PSUTIL_H
00021 #define PSUTIL_H
00022 
00023 #include <cstypes.h>
00024 #include <csutil/csstring.h>
00025 
00030 // Colour support. Only supported by pawsMessageTextBox for now.
00031 // 1 is a control character which should be filtered out of user text input.
00032 // The code is a fixed-size of 8 bytes.
00033 // byte 1 is 033 which is the control character
00034 // 2-3 are the R value in hex
00035 // 4-5 are the G value in hex
00036 // 6-7 are the B value in hex
00037 // 8 is the text size in decimal
00038 
00039 // Shortcut color/size codes
00040 // Safe for use with tolower, toupper, UTF-8
00041 #define REDCODE "\033ff00000" 
00042 #define GREENCODE "\03300ff000"
00043 #define BLUECODE "\0330000ff0" 
00044 #define WHITECODE "\033ffffff0"
00045 #define DEFAULTCODE "\0330000000" 
00046 #define ESCAPECODE '\033'
00047 #define LENGTHCODE 8
00048 
00049 class psColours
00050 {
00051 public:
00052         static bool ParseColour(const char* str, int& r, int& g, int& b, int& size)
00053         {
00054                 if(strlen(str) < LENGTHCODE || str[0] != '\033')
00055                         return false;
00056                 str++;
00057                 r = DecodeHex(str, 2);
00058                 if(r == -1)
00059                         return false;
00060                 str += 2;
00061                 g = DecodeHex(str, 2);
00062                 if(g == -1)
00063                         return false;
00064                 str += 2;
00065                 b = DecodeHex(str, 2);
00066                 if(b == -1)
00067                         return false;
00068                 str += 2;
00069                 size = DecodeHex(str, 1);
00070                 if(size == -1)
00071                         return false;
00072                 str++;
00073                 return true;
00074         }
00075         
00076         // Decode a hex string, can accept both uppercase and lowercase.
00077         // -1 indicates an error occurred.
00078         static int DecodeHex(const char* str, int len)
00079         {
00080                 int count = 0;
00081                 for(int i = 0; i < len; i++)
00082                 {
00083                         if(str[i] >= 'a' && str[i] <= 'f')
00084                                 count += (str[i] - 'a') + 10;
00085                         else if(str[i] >= '0' && str[i] <= '9')
00086                                 count += (str[i] - '0');
00087                         else if(str[i] >= 'A' && str[i] <= 'F')
00088                                 count += (str[i] - 'A') + 10;
00089                         else return -1;
00090                         count *= 16;
00091                 }
00092                 return count;
00093         }
00094 };
00095 
00099 void GetTimeOfDay(csString& string);
00100 
00101 struct psPoint
00102 {
00103     int x;
00104     int y;
00105 
00106     psPoint ():x(0), y(0) { }
00107 
00109     psPoint (int iX, int iY):x(iX), y(iY) { }
00110 };
00111 
00112 class ScopedTimer; // Forward declaration
00113 
00121 class ScopedTimerCB
00122 {
00123 public:
00124     virtual void ScopedTimerCallback(const ScopedTimer* timer) = 0;
00125     virtual ~ScopedTimerCB() {}
00126 };
00127 
00128 
00136 class ScopedTimer
00137 {
00138     csTicks         start;    
00139     csTicks         timeUsed; 
00140     csTicks         limit;    
00141     ScopedTimerCB*  callback; 
00142     csString        comment;  
00143          
00144 public:
00145 
00151     ScopedTimer(csTicks limit, const char * format, ... );
00152 
00158     ScopedTimer(csTicks limit, ScopedTimerCB* callback );
00159     
00163     ~ScopedTimer();
00164 
00168     csTicks TimeUsed() const;
00169 };
00170 
00175 float psGetRandom();
00176 
00181 uint32 psGetRandom(uint32 limit);
00182 
00183 
00184 
00187 #endif