00001 #include <vector>
00002 #include <fstream>
00003 #include <string>
00004 #include <SDL/SDL.h>
00005
00006 #include "graphics.h"
00007 #include "sprite.h"
00008 #include "util.h"
00009
00010 nnctime Util_GetTime()
00011 {
00012 nnctime nt;
00013 Uint32 x = SDL_GetTicks();
00014
00015 nt.secs = x / 1000;
00016 nt.msecs = x % 1000;
00017
00018 return nt;
00019 }
00020
00021 nnctime Util_DiffTime(nnctime *old, nnctime *now)
00022 {
00023 nnctime diff;
00024 nnctime ntmp;
00025
00026 if(now == NULL) {
00027 ntmp = Util_GetTime();
00028 now = &ntmp;
00029 }
00030
00031 diff.secs = now->secs - old->secs;
00032 if(diff.secs < 0) diff.secs = -diff.secs;
00033 diff.msecs = now->msecs - old->msecs;
00034
00035 if(diff.msecs < 0) {
00036 diff.secs--;
00037 diff.msecs += 1000;
00038 }
00039
00040 return diff;
00041 }
00042
00043 nnctime operator-(nnctime &a, nnctime &b)
00044 {
00045 nnctime n;
00046
00047 n.secs = a.secs - b.secs;
00048 n.msecs = a.msecs - b.msecs;
00049 if(n.msecs < 0) {
00050 n.secs--;
00051 n.msecs += 1000;
00052 }
00053 return n;
00054 }
00055
00056
00057 int Util_TimePassed(nnctime *old, Uint32 ms)
00058 {
00059 nnctime now = Util_GetTime();
00060 nnctime diff = Util_DiffTime(old, &now);
00061 Uint32 tell = (diff.secs * 1000) + diff.msecs;
00062
00063 if(tell >= ms) {
00064 int count = tell / ms;
00065 int rem = tell - (count * ms);
00066 nnctime t;
00067 t.secs = rem / 1000;
00068 t.msecs = rem % 1000;
00069 *old = now - t;
00070 return count;
00071 }
00072
00073 return 0;
00074 }
00075
00076 Rect Util_BuildRect(int x, int y, int w, int h)
00077 {
00078 Rect r;
00079 r.x = x;
00080 r.y = y;
00081 r.w = w;
00082 r.h = h;
00083 return r;
00084 }
00085
00086 Velocity Util_BuildVelocity (double dx, double dy, nnctime *old, nnctime *now)
00087 {
00088 Velocity v;
00089 nnctime temp = Util_DiffTime(old, now);
00090 v.dx = dx;
00091 v.dy = dy;
00092 v.pertime.msecs = temp.msecs;
00093 v.pertime.secs = temp.secs;
00094 return v;
00095 }
00096
00097 int Util_Random(int max)
00098 {
00099 static bool init = false;
00100
00101 if(!init) {
00102 nnctime nt = Util_GetTime();
00103 srand(nt.secs);
00104 init = true;
00105 }
00106
00107 return (rand() % max);
00108 }
00109
00110
00111 bool Util_Intersect(Rect &R1, Rect &R2) {
00112
00113
00114
00115 if ( (R2.x >= R1.x) && (R2.x <= (R1.x + R1.w))
00116 && (R2.y >= R1.y) && (R2.y <= (R1.y + R1.h)) ) return true;
00117
00118 if ( ((R2.x + R2.w) >= R1.x) && ((R2.x + R2.w) <= (R1.x + R1.w))
00119 && (R2.y >= R1.y) && (R2.y <= (R1.y + R1.h)) ) return true;
00120
00121 if ( (R2.x >= R1.x) && (R2.x <= (R1.x + R1.w))
00122 && ((R2.y + R2.h) >= R1.y) && ((R2.y + R2.h) <= (R1.y + R1.h)) ) return true;
00123
00124 if ( ((R2.x + R2.w) >= R1.x) && ((R2.x + R2.w) <= (R1.x + R1.w))
00125 && ((R2.y + R2.h) >= R1.y) && ((R2.y + R2.h) <= (R1.y + R1.h)) ) return true;
00126
00127
00128
00129 if ( (R1.x >= R2.x) && (R1.x <= (R2.x + R2.w))
00130 && (R1.y >= R2.y) && (R1.y <= (R2.y + R2.h)) ) return true;
00131
00132 if ( ((R1.x + R1.w) >= R2.x) && ((R1.x + R1.w) <= (R2.x + R2.w))
00133 && (R1.y >= R2.y) && (R1.y <= (R2.y + R2.h)) ) return true;
00134
00135 if ( (R1.x >= R2.x) && (R1.x <= (R2.x + R2.w))
00136 && ((R1.y + R1.h) >= R2.y) && ((R1.y + R1.h) <= (R2.y + R2.h)) ) return true;
00137
00138 if ( ((R1.x + R1.w) >= R2.x) && ((R1.x + R1.w) <= (R2.x + R2.w))
00139 && ((R1.y + R1.h) >= R2.y) && ((R1.y + R1.h) <= (R2.y + R2.h)) ) return true;
00140
00141
00142 return false;
00143 }
00144
00145
00146 vector<Surface *> Util_parseImage(string filename)
00147 {
00148 string textfile = filename + ".txt";
00149 vector<Surface *> images;
00150
00151 ifstream input(textfile.c_str());
00152
00153 string spriteName;
00154 string spritePath;
00155
00156 while (true) {
00157 input >> spriteName;
00158 input >> spritePath;
00159 if (input.eof()) break;
00160 Surface *temp = Sprite::cache.Find(spritePath);
00161
00162
00163
00164 images.push_back(temp);
00165 }
00166
00167 return images;
00168 }
00169
00170 int Util_FindBestSurfaceSize(int w, int h)
00171 {
00172 if (w <= 128 && h <= 128)
00173 return 128;
00174 if (w <= 256 && h <= 256)
00175 return 256;
00176 if (w <= 512 && h <= 512)
00177 return 512;
00178 if (w <= 1024 && h <= 1024)
00179 return 1024;
00180 if (w <= 2048 && h <= 2048)
00181 return 2048;
00182 return 0;
00183 }
00184