File: Utils.cpp

package info (click to toggle)
blockout2 2.4%2Bdfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,268 kB
  • sloc: cpp: 8,725; ansic: 143; makefile: 105
file content (392 lines) | stat: -rw-r--r-- 9,343 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
 	File:        Utils.cpp
  Description: Various util functions
  Program:     BlockOut
  Author:      Jean-Luc PONS

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
*/

#include "Types.h"
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <CImage.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>

static char bl2Home[512];
static char usrHome[512];

//-----------------------------------------------------------------------------
// Name: v()
// Desc: Construct a VERTEX.
//-----------------------------------------------------------------------------
VERTEX v(float x,float y,float z) {

  static VERTEX ret;
  ret.x = x;
  ret.y = y;
  ret.z = z;
  return ret;

}

//-----------------------------------------------------------------------------
// Nomalize a 3D vector
//-----------------------------------------------------------------------------
void Normalize(VERTEX *v) {

	float n = sqrtf( v->x * v->x + v->y * v->y + v->z * v->z );
	v->x = v->x / n;
	v->y = v->y / n;
	v->z = v->z / n;

}

//-----------------------------------------------------------------------------
// Name: fround()
// Desc: Round to the nearest integer
//-----------------------------------------------------------------------------
int fround(float x) {

  int i;
  if( x<0.0f ) {
    i = (int)( (-x) + 0.5f );
    i = -i;
  } else {
    i = (int)( x + 0.5f );
  }

  return i;

}

//-----------------------------------------------------------------------------
// Name: FormatTime(float seconds)
// Desc: Format time as XXmin YYsec
//-----------------------------------------------------------------------------
char *FormatTime(float seconds) {

  static char ret[32];
  int min = (int)seconds / 60;
  int sec = (int)seconds % 60;
  sprintf(ret,"%dmin %02dsec",min,sec);

  return ret;

}

//-----------------------------------------------------------------------------
// Name: FormatDate(int time)
// Desc: Format date as DD-MM-YYYY HH:MM:SS
//-----------------------------------------------------------------------------
char *FormatDate(int time) {

  static char ret[32];
  if( time>0 ) {
#ifdef LOCALTIME32
    struct tm *ts = _localtime32((__time32_t *)&time);
#else
    struct tm *ts = localtime((time_t *)&time);
#endif
    sprintf(ret,"%02d-%02d-%04d %02d:%02d:%02d",ts->tm_mday,ts->tm_mon+1,ts->tm_year+1900,
                                                ts->tm_hour,ts->tm_min,ts->tm_sec);
  } else {
    strcpy(ret,"");
  }

  return ret;

}

//-----------------------------------------------------------------------------
// Name: FormatDate(int time)
// Desc: Format date as DD-MM-YYYY
//-----------------------------------------------------------------------------
char *FormatDateShort(int time) {

  static char ret[32];
  if( time>0 ) {
#ifdef LOCALTIME32
    struct tm *ts = _localtime32((__time32_t *)&time);
#else
    struct tm *ts = localtime((time_t *)&time);
#endif
    sprintf(ret,"%02d-%02d-%04d",ts->tm_mday,ts->tm_mon+1,ts->tm_year+1900);
  } else {
    strcpy(ret,"..........");
  }

  return ret;

}

//-----------------------------------------------------------------------------
// Name: DirExists()
// Desc: Return TRUE if the specified directory exists
//-----------------------------------------------------------------------------

BOOL DirExists(char *dirname) {

  DIR    *dp;

  if((dp  = opendir(dirname)) == NULL) {
    return FALSE;
  }
  closedir(dp);
	
  return TRUE;

}

//-----------------------------------------------------------------------------
// Name: CheckEnv()
// Desc: Check environemt
//-----------------------------------------------------------------------------
BOOL CheckEnv() {

	char *homePath = getenv("HOME");
	if( homePath==NULL ) {
	  printf("HOME environement variable if not defined !\n");
	  printf("Please set the HOME variable to your home directory (ex: HOME=/users/jeanluc)\n");
		return FALSE;
	}
		
	char *blockoutHome = getenv("BL2_HOME");
	if( blockoutHome==NULL ) {
	  printf("BL2_HOME environement variable if not defined !\n");
	  printf("Please set the BL2_HOME to the BlockOut II installation directory (ex: BL2_HOME=/usr/local/bl2).\n");
		return FALSE;
	}
	strcpy( bl2Home , blockoutHome );
		
	char bl2Dir[512];
	sprintf(bl2Dir,"%s/.bl2",homePath);
	if( !DirExists(bl2Dir) ) {
	  // Create it
	  if( mkdir(bl2Dir,S_IRWXU) < 0 ) {
	    printf("Directory %s cannot be created or accessed !\n",bl2Dir);
		  return FALSE;
		}
		// Set rigth
     chmod(bl2Dir,S_IRWXU);  
	}
	strcpy( usrHome , bl2Dir );
	
	return TRUE;

}

//-----------------------------------------------------------------------------
// Name: LID()
// Desc: Locate file in the installation directory
//-----------------------------------------------------------------------------
char *LID(char *fileName) {

  static char ret[512];
	sprintf(ret,"%s/%s",bl2Home,fileName);
	return ret;

}

//-----------------------------------------------------------------------------
// Name: LHD()
// Desc: Locate file in the home directory
//-----------------------------------------------------------------------------
char *LHD(char *fileName) {

  static char ret[512];
	sprintf(ret,"%s/%s",usrHome,fileName);
	return ret;

}

//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Name: CreateTexture()
// Desc: Create a texture (no alpha)
//-----------------------------------------------------------------------------
int CreateTexture(int width,int height,char *imgName,GLuint *hmap) {

  *hmap = 0;
  CImage img;

  if( !img.LoadImage(LID(imgName)) ) {

    printf( "CreateTexture(): %s\nFile: %s\n",img.GetErrorMessage(),imgName );
    return GL_FAIL;

  }

  if( img.Width() != width || img.Height() != height ) {

    printf( "CreateTexture(): Wrong image dimension (%dx%d required)\nFile: %s\n",width,height,LID(imgName) );
    return GL_FAIL;

  }

	BYTE *buff32 = (BYTE *)malloc(width*height*3);
	BYTE *data = img.GetData();
  for(int j=0;j<height;j++)
		for(int i=0;i<width;i++ ) {
			buff32[i*3+0 + j*width*3] = data[i*3+2 + j*width*3];
			buff32[i*3+1 + j*width*3] = data[i*3+1 + j*width*3];
			buff32[i*3+2 + j*width*3] = data[i*3+0 + j*width*3];
		}

  glGenTextures(1,hmap);
  glBindTexture(GL_TEXTURE_2D,*hmap);

  glTexImage2D (
	  GL_TEXTURE_2D, 	    // Type
    0, 	                // No Mipmap
    3,                  // Format RGB
    width, 	            // Width
    height,             // Height
    0, 	                // Border
    GL_RGB, 	          // Format RGB
    GL_UNSIGNED_BYTE, 	// 8 Bit/color
    buff32              // Data
  ); 	

  img.Release();
	free(buff32);

	if( glGetError() != GL_NO_ERROR ) {
    printf( "CreateTexture(): OpenGL error (code=%d)\n",glGetError() );
  	return GL_FAIL;
	}

  return GL_OK;
}

//-----------------------------------------------------------------------------
// Reset memory to 0
//-----------------------------------------------------------------------------
void ZeroMemory(void *buff,int size) {
  memset(buff,0,size);
}

//-----------------------------------------------------------------------------
// Name: GetChar()
// Desc: Return char according to pressed key
//-----------------------------------------------------------------------------
extern char GetChar(BYTE *keys) {

  char retChar = 0;

  // Check letters
  for(BYTE i='A';i<='Z';i++) 
    if( keys[i] ) retChar = i;

	for(BYTE i='a';i<='z';i++) 
    if( keys[i] ) retChar = i;

  // Check numbers
  for(BYTE i=0;i<=9;i++)
    if( keys[i+'0'] ) retChar = (char)i+'0';

  // Space
  if( keys[SDLK_SPACE] ) {
    retChar = ' ';
  }

  // Dot
  if( keys['.'] ) retChar = '.';

  // Comma
  if( keys[','] ) retChar = ',';
  
  // Minus
  if( keys['-'] ) retChar = '-';

  // Plus
  if( keys['+'] ) retChar = '+';

  // Divide
  if( keys['/'] ) retChar = '/';

  // ':'
	if( keys[':'] ) retChar = ':';

  // '@'
	if( keys['@'] ) retChar = '@';

  // '''
	if( keys['\''] ) retChar = '\'';

  // '!'
	if( keys['!'] ) retChar = '!';

  // '$'
	if( keys['$'] ) retChar = '$';

  // '%'
	if( keys['%'] ) retChar = '%';

  // '&'
	if( keys['&'] ) retChar = '&';

  // '*'
	if( keys['*'] ) retChar = '*';

  // '('
	if( keys['('] ) retChar = '(';

  // ')'
	if( keys[')'] ) retChar = ')';

  // '_'
	if( keys['_'] ) retChar = '_';

  // '?'
	if( keys['?'] ) retChar = '?';

  // ';'
	if( keys[';'] ) retChar = ';';

  // Reset keys
  for(BYTE i='A';i<='Z';i++)
    keys[i]=0;

	for(BYTE i='a';i<='z';i++)
    keys[i]=0;

  for(BYTE i=0;i<=9;i++)
    keys[i+'0']=0;

  keys[SDLK_SPACE]=0;
  keys['.']=0;
  keys['/']=0;
  keys['-']=0;
  keys['+']=0;
	keys[':']=0;
	keys[',']=0;
  keys['.']=0;
  keys['@']=0;
  keys['\'']=0;
  keys['!']=0;
  keys['$']=0;
  keys['%']=0;
  keys['&']=0;
  keys['*']=0;
  keys['(']=0;
  keys[')']=0;
  keys['_']=0;
  keys['?']=0;
  keys[';']=0;

  return retChar;

}