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 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
|
/*
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>
#undef LoadImage
#include <CImage.h>
#ifdef WINDOWS
#include <windows.h>
#include <shlobj.h>
#include <shlwapi.h>
#else
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#endif
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(uint32 time)
// Desc: Format date as DD-MM-YYYY HH:MM:SS
//-----------------------------------------------------------------------------
char *FormatDate(uint32 time) {
static char ret[32];
if( time>0 ) {
#ifdef LOCALTIME64
time_t d = (time_t)time;
struct tm *ts = localtime(&d);
#else
time_t innerTm = (time_t)time;
struct tm *ts = localtime((time_t *)&innerTm);
#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(uint32 time)
// Desc: Format date as DD-MM-YYYY
//-----------------------------------------------------------------------------
char *FormatDateShort(uint32 time) {
static char ret[32];
if( time>0 ) {
#ifdef LOCALTIME64
time_t d = (time_t)time;
struct tm *ts = localtime(&d);
#else
time_t innerTm = (time_t)time;
struct tm *ts = localtime((time_t *)&innerTm);
#endif
sprintf(ret,"%02d-%02d-%04d",ts->tm_mday,ts->tm_mon+1,ts->tm_year+1900);
} else {
strcpy(ret,"..........");
}
return ret;
}
#ifndef WINDOWS
//-----------------------------------------------------------------------------
// 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;
}
#endif
//-----------------------------------------------------------------------------
// Name: CheckEnv()
// Desc: Check environemt
//-----------------------------------------------------------------------------
BOOL CheckEnv() {
#ifdef WINDOWS
strcpy(usrHome,"");
TCHAR strPath[MAX_PATH];
if( SUCCEEDED(SHGetFolderPath( NULL, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, strPath ) ) )
{
PathAppend( strPath, TEXT( "Blockout" ) );
if( !PathFileExists( strPath ) )
{
if( ERROR_SUCCESS != SHCreateDirectoryEx( NULL, strPath, NULL ) ) {
char message[256];
sprintf(message,"SHCreateDirectoryEx() failed\nCannot create directory\n%s",strPath);
MessageBox(NULL,message,"Warning",MB_OK|MB_ICONWARNING);
return TRUE;
}
}
strcpy(usrHome,strPath);
} else {
MessageBox(NULL,"SHGetFolderPath(CSIDL_LOCAL_APPDATA) failed\nCannot retreive directory","Warning",MB_OK|MB_ICONWARNING);
}
return TRUE;
#else
char *homePath = getenv("HOME");
if( homePath==NULL ) {
printf("HOME environment variable if not defined !\n");
printf("Please set the HOME variable to your home directory (ex: HOME=/users/jeanluc)\n");
return FALSE;
}
const char *blockoutHome = "/usr/share/games/blockout2";
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;
#endif
}
//-----------------------------------------------------------------------------
// Name: LID()
// Desc: Locate file in the installation directory
//-----------------------------------------------------------------------------
const char *LID(const char *fileName) {
#ifdef WINDOWS
return fileName;
#endif
static char ret[512];
sprintf(ret,"%s/%s",bl2Home,fileName);
return ret;
}
//-----------------------------------------------------------------------------
// Name: LHD()
// Desc: Locate file in the home directory
//-----------------------------------------------------------------------------
const char *LHD(const char *fileName) {
static char ret[512];
#ifdef WINDOWS
if( strlen(usrHome)>0 ) {
sprintf(ret,"%s\\%s",usrHome,fileName);
return ret;
} else {
return fileName;
}
#else
sprintf(ret,"%s/%s",usrHome,fileName);
return ret;
#endif
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Name: CreateTexture()
// Desc: Create a texture (no alpha)
//-----------------------------------------------------------------------------
int CreateTexture(int width,int height,const char *imgName,GLuint *hmap) {
*hmap = 0;
CImage img;
if( !img.LoadImage(LID(imgName)) ) {
#ifdef WINDOWS
char message[256];
sprintf(message,"CreateTexture(): %s\nFile: %s\n",img.GetErrorMessage(),imgName);
MessageBox(NULL,message,"ERROR",MB_OK|MB_ICONERROR);
#else
printf( "CreateTexture(): %s\nFile: %s\n",img.GetErrorMessage(),imgName );
#endif
return GL_FAIL;
}
if( img.Width() != width || img.Height() != height ) {
#ifdef WINDOWS
char message[256];
sprintf(message,"CreateTexture(): Wrong image dimension (%dx%d required)\nFile: %s\n",width,height,LID(imgName));
MessageBox(NULL,message,"ERROR",MB_OK|MB_ICONERROR);
#else
printf( "CreateTexture(): Wrong image dimension (%dx%d required)\nFile: %s\n",width,height,LID(imgName) );
#endif
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 ) {
#ifdef WINDOWS
char message[256];
sprintf(message,"CreateTexture(): OpenGL error (code=%d)\n",glGetError());
MessageBox(NULL,message,"ERROR",MB_OK|MB_ICONERROR);
#else
printf( "CreateTexture(): OpenGL error (code=%d)\n",glGetError() );
#endif
return GL_FAIL;
}
return GL_OK;
}
#ifndef WINDOWS
//-----------------------------------------------------------------------------
// Reset memory to 0
//-----------------------------------------------------------------------------
void ZeroMemory(void *buff,int size) {
memset(buff,0,size);
}
#endif
//-----------------------------------------------------------------------------
// 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;
}
|