source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/common.cpp @ 3019

Revision 3019, 4.5 KB checked in by mattausch, 16 years ago (diff)

detected memory leaks mainly in shadowmapping!!
strange problems with deferred rendering, seems to be uninitialized sometimes (solved?)

Line 
1#include <math.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <time.h>
5
6#include "common.h"
7
8#ifdef _X_WINDOW_
9#include "xwindow.h"
10#endif
11
12#ifndef _MSC_VER
13#include <unistd.h>
14#include <sys/types.h>
15#include <sys/time.h>
16#include <sys/times.h>
17#include <sys/resource.h>
18#else
19#include <windows.h>
20#include <sys/types.h>
21#include <sys/timeb.h>
22#endif
23
24
25using namespace std;
26
27
28namespace CHCDemoEngine
29{
30
31
32Real Limits::Threshold = 1e-6f;
33Real Limits::Small = 0.01f;
34Real Limits::Infinity = 1e20f;
35
36
37
38Real Random(Real max)
39{
40#ifdef __UNIX__
41  return (random()*max/0x7FFFFFFF);
42#else
43  return (((Real)rand()) / RAND_MAX) * max;
44#endif
45}
46
47
48int Random(int max)
49{
50        return rand() % max;
51}
52
53
54void Randomize()
55{
56        time_t t;
57        srand((unsigned) time(&t));
58}
59
60
61void Randomize(unsigned int seed)
62{
63        srand(seed);
64}
65
66
67string
68ReplaceSuffix(const string &filename, const string &a, const string &b)
69{
70        string result = filename;
71
72        int pos = (int)filename.rfind(a, (int)filename.size() - 1);
73        if (pos == filename.size() - a.size())
74        {
75                result.replace(pos, a.size(), b);
76        }
77
78        return result;
79}
80
81
82int
83SplitFilenames(const string &str, vector<string> &filenames)
84{
85        int pos = 0;
86
87        while(1) {
88                int npos = (int)str.find(';', pos);
89               
90                if (npos < 0 || npos - pos < 1)
91                        break;
92                filenames.push_back(string(str, pos, npos - pos));
93                pos = npos + 1;
94        }
95       
96        filenames.push_back(string(str, pos, str.size() - pos));
97        return (int)filenames.size();
98}
99
100void GetKey(char *s)
101{
102#ifdef _X_WINDOW_
103XWaitForButton(s);
104#else
105getch();
106#endif
107}
108
109#ifdef  _MSC_VER
110static bool hasHRTimer = false;
111static LARGE_INTEGER hrFreq;
112#endif
113
114
115void InitTiming()
116{
117#ifdef  _MSC_VER
118        hasHRTimer = (bool) QueryPerformanceFrequency(&hrFreq);
119#endif
120}
121
122long GetTime()
123{
124#ifndef  _MSC_VER
125        static struct rusage r;
126        getrusage(RUSAGE_SELF,&r);
127        return r.ru_utime.tv_usec+1000000*r.ru_utime.tv_sec;
128#else
129        if (hasHRTimer) {
130                LARGE_INTEGER counter;
131                QueryPerformanceCounter(&counter);
132                // return in usec
133                //return (long) (1000000*counter.QuadPart/(hrFreq.QuadPart));
134                // $$
135                // tmp store time in ms
136                return (long) (1000*counter.QuadPart/(hrFreq.QuadPart));
137        } else {
138                static struct _timeb mtime;
139                _ftime(&mtime);
140
141                return 1000*(1000*(long)mtime.time + (long)mtime.millitm);
142        }
143#endif
144}
145
146
147long
148GetRealTime()
149{
150#ifndef  _MSC_VER
151        static struct timeval _tstart;
152        static struct timezone tz;
153        gettimeofday(&_tstart,&tz);
154        return (long)(1000000*_tstart.tv_sec + _tstart.tv_usec);
155#else
156        if (hasHRTimer) {
157                LARGE_INTEGER counter;
158                QueryPerformanceCounter(&counter);
159                // return in usec
160                return (long) (1000000*counter.QuadPart/(hrFreq.QuadPart));
161        } else {
162                static struct _timeb mtime;
163                _ftime(&mtime);
164                return 1000*(1000*(long)mtime.time + (long)mtime.millitm);
165        }
166#endif
167}
168
169// return time diff. in ms
170Real TimeDiff(long time1, long time2) // in ms
171{
172        //const Real clk=1.0e-3f; // ticks per second
173        // $$ tmp store time in ms
174        const Real clk=1.0f; // ticks per second
175        long t=time2-time1;
176
177        return ((t<0)?-t:t)*clk;
178}
179
180char *TimeString()
181{
182        time_t t;
183        time(&t);
184
185        return ctime(&t);
186}
187
188
189char *GetAbsPath(char *name)
190{
191        //  char *url = new char[strlen(name)+1];
192        //  strcpy(url,name);
193
194        // get the absolute path
195        size_t c = strlen(name);
196        size_t i = c - 1;
197        bool wasDot = false;
198
199        // search for double dot
200        for (; i >= 0; -- i)
201        {
202                if (name[i]=='.')
203                {
204                        if (wasDot)
205                                break;
206                        wasDot = true;
207                }
208                else
209                        wasDot = false;
210        }
211
212        if (i>0)
213                i+=3;
214        if (i<0)
215                i=0;
216
217        char *url = new char[c-i+1];
218
219        int j=0;
220
221        for (;i<c;i++,j++)
222                url[j] = name[i];
223
224        url[j]=0;
225
226        return url;
227}
228
229
230void indent(ostream &app, int ind)
231{
232        int i;
233       
234        for (i = 0; i < ind; i++)
235                app << ' ';
236}
237
238
239char *GetPath(const char *s)
240{
241        size_t i = strlen(s);
242        for (; i > 0; -- i)
243        {
244                if (s[i]=='/' || s[i]=='\\')
245                        break;
246        }
247
248        char *path = new char[i+1];
249        int j=0;
250        for (; j<i; j++)
251                path[j] = s[j];
252        path[j] = 0;
253        return path;
254}
255
256
257char *
258StripPath(const char *s)
259{
260        const size_t len = strlen(s);
261
262        size_t i = len;
263        for (; i>0; i--) {
264                if (s[i]=='/' || s[i]=='\\')
265                        break;
266        }
267
268        char *filename = new char[len - i];
269        int j = 0;
270
271        for (; j < len - i; ++ j)
272                filename[j] = s[i + j + 1];
273        //filename[j] = 0;
274
275        return filename;
276}
277
278
279bool
280FileExists(char *filename)
281{
282        FILE *f;
283
284        f = fopen(filename,"r");
285        if (f) {
286                fclose(f);
287                return true;
288        }
289        return false;
290}
291
292}
Note: See TracBrowser for help on using the repository browser.