]> pilppa.org Git - familiar-h63xx-build.git/blob - org.handhelds.familiar/packages/powermanga/powermanga-0.79/linuxroutines.cpp
OE tree imported from monotone branch org.openembedded.oz354fam083 at revision 8b12e3...
[familiar-h63xx-build.git] / org.handhelds.familiar / packages / powermanga / powermanga-0.79 / linuxroutines.cpp
1 //*****************************************************************************
2 // copyright (c) 1998-2003 TLK Games all rights reserved
3 //-----------------------------------------------------------------------------
4 // file         : "linuxroutines.cpp"
5 // created      : 1999-08-19
6 // updates      : 2004-08-01 
7 //------------------------------------------------------------------------------
8 // functions    :  int              mem_initialise(unsigned int _iNombreZones)
9 //                 char*            reserveMemoire(unsigned int _iTailleMemoire)
10 //                 void             libereMemoire(char* _pMemoire)
11 //                 void             mem_libereTous()
12 //                 int              load_pcx(char*, char*) ;
13 //                 sDescriptionGfx* load_pcx(char*) ;
14 //                 unsigned int     littleWord(char* _pMem) ;
15 //                 short            litMot16bits(short* _pMem) ;
16 //                 int              litMot32bits(int* _pMem) ;
17 //                 int              chaine_cmp(char* _pChaine1, char* _pChaine2, unsigned int _iTaille) ;
18 //                 char*            chargeFichier(char* _pNomFichier)
19 //                 char*            chargeFichier(char* _pNomFichier, unsigned int* pTaille)
20 //                 int              chargeFichier(char* _pNomFichier, char* _pMemoire)
21 //                 void             fps_init()
22 //                 void             fps_affiche() 
23 //                 void             afficheErreur(char* _pErreur1, char* _pErreur2)
24 //                 int              synchro_processusPause(int _iTemps)
25 //                 int              synchro_CalculDifference()
26 //                 int              synchro_CalculDifference2()
27 //-----------------------------------------------------------------------------
28 // This program is free software; you can redistribute it and/or modify it under
29 // the terms of the GNU General Public License as published by the Free Software
30 // Foundation; either version 2 of the License, or (at your option) any later
31 // version.
32 // 
33 // This program is distributed in the hope that it will be useful, but WITHOUT
34 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
35 // FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
36 // details.
37 //
38 // You should have received a copy of the GNU General Public License along with
39 // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
40 // Place - Suite 330, Boston, MA  02111-1307, USA.
41 //
42 //*****************************************************************************
43
44 #include "powermanga.hpp"
45 #include <errno.h>
46
47 #ifndef SDL_TLK
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 #endif
51
52 //..............................................................................
53 extern unsigned int     iVerbeux;                              //1=affiche les arguments / 1=verbose mode
54
55 //gestion de la memoire .........................................................
56 typedef struct
57 { char *pMemoire;                                              //pointeur sur la zone memoire / pointer to memory zone
58   int   iTailleMemoire;                                         //taille de la zone en octets / size of memory zone in bytes
59 }
60 sListeMemoire;
61 char                   *pZoneListeMemoire;
62 sListeMemoire          *pZoneListeBase;
63 sListeMemoire          *pZoneListeLibre;
64 unsigned int            iTailleListe;                          //taille de la liste memoire / size of memory table             
65 unsigned int            iNombreZonesMaximum;                   //nombre de zones memoires maxi / number maximum of memory zone
66 unsigned int            iNombreZones;                          //nombre de zone actuelle       / number of memroy zone
67 unsigned int            iOctetsTotal;                          //taille prise en octets        / total memory size allocated           
68 unsigned int            iNombreZonesMax;                       //nombre maximum de zones reserves
69
70 //..............................................................................
71 unsigned int            iCompteurBoucle;
72 #ifdef SDL_TLK 
73 Uint32                  time_begin;
74 Uint32                  temps_actuel;
75 Uint32                  temps_sauve;
76 Uint32                  temps_actuel2;
77 Uint32                  temps_sauve2;
78 #else
79 static struct timeval   time_begin;
80 static struct timeval   temps_actuel;
81 static struct timeval   temps_sauve;
82 static struct timeval   temps_actuel2;
83 static struct timeval   temps_sauve2;
84 #endif
85
86 //..............................................................................
87 short                   litMot16bitsBig(short *_pMem);
88 unsigned short          litMot16bits(unsigned short *_pMem);
89 extern const char       nomprefix[];
90 extern unsigned int     iFrame3; 
91 //------------------------------------------------------------------------------
92 // initialise la liste memoire / initialise memory table
93 // input =>  _iNombreZones : nombre de zones memoire possibles au maximum / number maximum of memory zone
94 // ouput <= 0=erreur / 0=error
95 //------------------------------------------------------------------------------
96 int mem_initialise(unsigned int _iNombreZones)
97 {
98   unsigned int _iIndex;
99   iNombreZones = 0;                                            // nombre de zones reservees a zero
100   pZoneListeMemoire = 0;
101   iNombreZonesMaximum = _iNombreZones;                         // nombre maximum de zones pouvant etre reservees 
102   iNombreZonesMax = 0;
103   iTailleListe = iNombreZonesMaximum * sizeof(sListeMemoire);
104   iOctetsTotal = iTailleListe;
105   pZoneListeMemoire = (char *)malloc(iTailleListe);
106   if(!pZoneListeMemoire)
107   { afficheErreur("malloc() failed", "linuxroutines/mem_initialise()");
108     return 0;
109   }
110   pZoneListeBase = (sListeMemoire *) pZoneListeMemoire;
111   pZoneListeLibre = pZoneListeBase;
112   sListeMemoire          *_pMem = pZoneListeBase;
113
114   for(_iIndex = 0; _iIndex < iNombreZonesMaximum; _iIndex++, _pMem++)   //clean memory table
115   { _pMem->pMemoire = 0x0;
116     _pMem->iTailleMemoire = 0x0;
117   }
118   return 1;
119 }
120
121 //------------------------------------------------------------------------------
122 // reserve une zone memoire / allocate memory
123 // input  => _iTailleMemoire : taille memoire en octets souhaitee / size of bytes
124 // output <= _pMem           : adresse memoire (0=erreur) / pointer to memory
125 //------------------------------------------------------------------------------
126 char *reserveMemoire(unsigned int _iTailleMemoire)
127 {
128   char                   *_pMem = 0x0;
129   char                   *_pMemoire = 0x0;
130   char                    _cZero = 0;
131   unsigned int            _iIndex;
132   if(iNombreZones >= iNombreZonesMaximum)
133   { printf ("linuxroutines.cpp/reserveMemoire(unsigned int) : table overflow ; size request %i bytes ; total allocate : % i in %i zones\n", _iTailleMemoire, iOctetsTotal, iNombreZones);
134     return 0;
135   }
136   _pMem = (char *)malloc(_iTailleMemoire);
137   if(!_pMem)
138   { printf ("linuxroutines.cpp/reserveMemoire(unsigned int) : malloc(int) return 0 ; size request %i bytes ; total allocate : % i in %i zones\n", _iTailleMemoire, iOctetsTotal, iNombreZones);
139     return 0;
140   }
141   iOctetsTotal += _iTailleMemoire;
142   pZoneListeLibre->pMemoire = _pMem;                           //pointeur memoire      
143   pZoneListeLibre->iTailleMemoire = _iTailleMemoire;           //taille en octets      
144   pZoneListeLibre += 1;
145   iNombreZones++;
146   if(iNombreZones > iNombreZonesMax)
147       iNombreZonesMax = iNombreZones; 
148   _pMemoire = _pMem;
149   for(_iIndex = 0; _iIndex < _iTailleMemoire; _iIndex++)
150     *(_pMemoire++) = _cZero;                                   //efface memoire        
151   return (_pMem);
152 }
153
154 //------------------------------------------------------------------------------
155 // liberation d'une zone memoire / free memory
156 // input => _pMemoire : adresse de la zone a liberer / pointer to memory
157 //------------------------------------------------------------------------------
158 void libereMemoire(char *_pMemoire)
159 {
160   sListeMemoire          *_pListeMem;
161   sListeMemoire          *_pListeMemSource;
162   unsigned int            _iCompteur;
163   if(_pMemoire)
164   { _pListeMem = pZoneListeBase;
165     for(_iCompteur = 0; _iCompteur < iNombreZones; _iCompteur++, _pListeMem++)
166     { if(_pListeMem->pMemoire == _pMemoire)                    //on a trouve l'adresse dans la table ?
167       { free(_pMemoire);                                       //oui, libere la memoire                                
168         _pListeMemSource = _pListeMem + 1;
169         iOctetsTotal -= _pListeMem->iTailleMemoire;
170         iNombreZones--;
171         pZoneListeLibre--;
172         while (_iCompteur < iNombreZones)
173         { _pListeMem->pMemoire = _pListeMemSource->pMemoire;
174           _pListeMem->iTailleMemoire = _pListeMemSource->iTailleMemoire;
175           _iCompteur++;
176           _pListeMem++;
177           _pListeMemSource++;
178         }
179         _pListeMem->pMemoire = 0x0;
180         _pListeMem->iTailleMemoire = 0x0;
181         _pMemoire = 0;
182         break;
183       }
184     }
185     if(_pMemoire > 0)
186     { fprintf(stderr, "libereMemoire() : can't release the address %x\n", (int)_pMemoire);
187     }
188   }
189   else
190   { fprintf(stderr, "libereMemoire() : try to release a null address!\n");  
191   }
192 }
193
194 //------------------------------------------------------------------------------
195 // retourne la taille d'une adresse memoire reservee / get memory zone's size
196 // input => _pMem : pointeur sur une zone memoire / pointer to memory
197 //------------------------------------------------------------------------------
198 int mem_retourneTaille(char *_pMem)
199 {
200   unsigned int            _iCompteur;
201   sListeMemoire          *_pListeMem = pZoneListeBase;
202   if(_pMem)
203   { for(_iCompteur = 0; _iCompteur < iNombreZones; _iCompteur++, _pListeMem++)
204     { if(_pListeMem->pMemoire == _pMem)
205         return _pListeMem->iTailleMemoire;
206     }
207   }
208   return 0;
209 }
210
211 //------------------------------------------------------------------------------
212 // libere toutes les zones memoires / free all memory
213 //------------------------------------------------------------------------------
214 void mem_libereTous()
215 {
216   unsigned int _iCompteur;
217   char *_pMemoire;
218   sListeMemoire *_pListeMem = pZoneListeBase;
219 #ifdef _VERBEUX_
220   if(iVerbeux > 0)
221     fprintf(stdout, "> linuxroutines.cpp/mem_libereTous(): maximum of memory which were allocated during the game : %i\n", iNombreZonesMax);
222 #endif
223   if(iNombreZones >0)
224   { 
225 #ifdef _VERBEUX_
226     if(iVerbeux > 0)
227       fprintf(stdout, "> linuxroutines.cpp/mem_libereTous() : %i zones were not released.\n", iNombreZones);
228 #endif
229     for(_iCompteur = 0; _iCompteur < iNombreZones; _iCompteur++, _pListeMem++)
230     { _pMemoire = _pListeMem->pMemoire;
231       if(_pMemoire)
232       { /* fprintf(stderr, "mem_libereTous: adresseMem =%x ; taille_mem=%ld\n", 
233           (int)_pListeMem->pMemoire, _pListeMem->iTailleMemoire); */
234         free(_pMemoire); //libere la memoire                             
235         _pListeMem->pMemoire = 0;
236         _pListeMem->iTailleMemoire = 0;
237       }
238     }
239     if(pZoneListeMemoire)
240     { free(pZoneListeMemoire);
241       pZoneListeMemoire = 0x0;
242     }
243     iNombreZones = 0;
244   }
245 }
246
247 //------------------------------------------------------------------------------
248 // decompress pcx file 
249 // input  =>  _pFichier     : filename
250 // output <= _pStructureGfx : pointer to the "sDescriptionGfx" structure / 0=error 
251 //------------------------------------------------------------------------------
252 sDescriptionGfx *load_pcx(char *_pNomFichier)
253 {
254   unsigned int            iLongeur;
255   unsigned int            ptr = 128;
256   unsigned char           nbr_bytes;
257   unsigned char           val;
258   unsigned int            i1, i, total;
259   unsigned char          *_pFichier, *_pAdresseMem;
260   sDescriptionGfx        *_pDescripGfx = 0x0;
261   _pFichier = (unsigned char *)chargeFichier(_pNomFichier, &iLongeur);  //load file in memory
262   if(!_pFichier) return 0;
263   unsigned short         *_p16 = (unsigned short *)_pFichier;
264   unsigned int            _iLarge = (litMot16bits(_p16 + 4) - litMot16bits(_p16 + 2)) + 1;      //image width
265   unsigned int            _iHaut = (litMot16bits(_p16 + 5) - litMot16bits(_p16 + 3)) + 1;       //image height
266   unsigned int            _iProfondeur = _pFichier[3];         //bits per pixel
267
268   _pDescripGfx = (sDescriptionGfx *) reserveMemoire(sizeof(sDescriptionGfx));   //allocate structure memory
269   if(!_pDescripGfx)
270   { afficheErreur("_pDescripGfx out of memory", "linuxroutines.cpp/load_pcx()");
271     return 0;
272   }
273   _pDescripGfx->iLargeur = _iLarge;
274   _pDescripGfx->iHauteur = _iHaut;
275   _pDescripGfx->iProfond = _iProfondeur;
276   _pDescripGfx->iTaille = _iLarge * _iHaut * (_iProfondeur >> 3);
277   _pDescripGfx->pAdresse = reserveMemoire(_pDescripGfx->iTaille);       //allocate image memory
278   if(!_pDescripGfx->pAdresse)
279   { libereMemoire((char *)_pDescripGfx);
280     printf("linuxroutines.cpp/load_pcx() : height=%i / width=%i \n", _iLarge, _iHaut);
281     afficheErreur("_pDescripGfx->iTaille : out of memory\n", "linuxroutines.cpp/reserveMemoire()");
282     return 0;
283   }
284   //decompress rle
285   _pAdresseMem = (unsigned char *)_pDescripGfx->pAdresse;
286   total = 0;
287   i = iLongeur - 768;
288   while (ptr < i)
289   { if((_pFichier[ptr] & 0xC0) == 0xC0)
290     { nbr_bytes = _pFichier[ptr] & 0x3F;
291       ptr++;
292     }
293     else
294       nbr_bytes = 1;
295     val = _pFichier[ptr];
296     total += nbr_bytes;                  // Samuel Hocevar
297     if(total >= _pDescripGfx->iTaille)
298     break;
299     for(i1 = 0; i1 < nbr_bytes; i1++)
300     { *_pAdresseMem = val;
301       _pAdresseMem++;
302     }
303     ptr++;
304   }
305   libereMemoire((char *)_pFichier);
306 #ifdef _VERBEUX_
307   if(iVerbeux > 0)
308     fprintf (
309       stdout,
310       "> linuxroutines.cpp/load_pcx(): \"%s\"; height=%i; width=%i; size=%i\n",
311       _pNomFichier,
312       _iLarge,
313       _iHaut,
314       _pDescripGfx->iTaille);
315 #endif
316   return _pDescripGfx;
317 }
318
319 //------------------------------------------------------------------------------
320 // lecture d'un mot de 16 bits code en memoire en little-endian
321 // input  => _pMem    : pointeur sur le mot en memoire
322 // output <= _iValeur : valeur utilisale
323 //------------------------------------------------------------------------------
324 unsigned int littleWord(char *_pMem)
325 {
326   unsigned short *t = (unsigned short *)_pMem;
327   return *t;
328 }
329
330 //------------------------------------------------------------------------------
331 // lecture d'un long mot signe de 16 bits little-endian (compatible little-endian/big-endian)
332 // input  => _pMem    : pointeur sur le mot en memoire
333 // output <= _iValeur : valeur utilisale
334 //------------------------------------------------------------------------------
335 short litMot16bits(short *_pMem)
336 {
337   short  _iValeur = 0;
338   unsigned char  *_pMemoire = (unsigned char *)_pMem;
339   _iValeur = _pMemoire[1];                                     //lit du little endian pour un powerpc
340   _iValeur <<= 8;
341   _iValeur += _pMemoire[0];
342   return (_iValeur);
343 }
344
345 //------------------------------------------------------------------------------
346 // lecture d'un long mot signe de 16 bits little-endian (compatible little-endian/big-endian)
347 // input  => _pMem    : pointeur sur le mot en memoire
348 // output <= _iValeur : valeur utilisale
349 //------------------------------------------------------------------------------
350 unsigned short litMot16bits(unsigned short *_pMem)
351 {
352   unsigned short          _iValeur = 0;
353   unsigned char          *_pMemoire = (unsigned char *)_pMem;
354   _iValeur = _pMemoire[1];                                     //lit du little endian pour un powerpc
355   _iValeur <<= 8;
356   _iValeur += _pMemoire[0];
357   return (_iValeur);
358 }
359
360 //------------------------------------------------------------------------------
361 // lecture d'un long mot signe de 16 bits big-endian (compatible little-endian/big-endian)
362 // input => _pMem    : pointeur sur le mot en memoire
363 // output <= _iValeur : valeur utilisale
364 //------------------------------------------------------------------------------
365 short litMot16bitsBig(short *_pMem)
366 {
367   short                   _iValeur = 0;
368   unsigned char          *_pMemoire = (unsigned char *)_pMem;
369   _iValeur = _pMemoire[0];                                     //lit du big endian pour un 80x86
370   _iValeur <<= 8;
371   _iValeur += _pMemoire[1];
372   return (_iValeur);
373 }
374
375 //------------------------------------------------------------------------------
376 //lecture d'un long mot signe de 32 bits little-endian(compatible little-endian/big-endian)
377 // input  => _pMem    : pointeur sur le mot 32 bits en memoire
378 // output <= _iValeur : valeur utilisale
379 //------------------------------------------------------------------------------
380 int litMot32bits(int *_pMem)
381 {
382   int                     _iValeur = 0;
383   unsigned char          *_pMemoire = (unsigned char *)_pMem;
384   _iValeur = _pMemoire[3];
385   _iValeur <<= 8;
386   _iValeur += _pMemoire[2];
387   _iValeur <<= 8;
388   _iValeur = _pMemoire[1];
389   _iValeur <<= 8;
390   _iValeur += _pMemoire[0];
391   return (_iValeur);
392 }
393
394 //------------------------------------------------------------------------------
395 // convertit un long mot de 32 bits en memoire little-endian => big-endian
396 //------------------------------------------------------------------------------
397 void convert32bits_2bigendian (unsigned char* memory)
398 { unsigned char b0, b1, b2, b3;
399   b0 = memory[1];
400   b1 = memory[0];
401   b2 = memory[3];
402   b3 = memory[2];
403   memory[0] = b2;
404   memory[1] = b3;
405   memory[2] = b0;
406   memory[3] = b1;
407 }
408
409 //------------------------------------------------------------------------------
410 //comparaison de deux chaines de caracteres
411 // input  => _pChaine1 : pointeur sur la premiere chaine
412 //        => _pChaine1 : pointeur sur la deuxieme chaine
413 //        => _iTaille  : longeurs en octets de chaines
414 // output <= 0les chaines ont au moins une difference / 1=les chaines sont identiques
415 //------------------------------------------------------------------------------
416 int chaine_cmp(char *_pChaine1, char *_pChaine2, unsigned int _iTaille)
417 {
418   unsigned int            _iLongeur = _iTaille;
419   char                   *_pMem1 = _pChaine1;
420   char                   *_pMem2 = _pChaine2;
421   unsigned int            _iIndex;
422   char                    _cOctet1, _cOctet2;
423   for(_iIndex = 0; _iIndex < _iLongeur; _iIndex++)
424   { _cOctet1 = _pMem1[_iIndex];
425     _cOctet2 = _pMem2[_iIndex];
426     if(_cOctet2 != _cOctet1)
427       return (0);
428   }
429   return (1);
430 }
431
432 //------------------------------------------------------------------------------
433 // locate a file under one of the data directories (thanx to Andre Majorel :-)
434 // input  =>  name : name of file relative to data directory
435 // output <=  0    : could not locate file (not found, or not enough
436 //                   memory, or the name given was absolute)
437 // other           : pointer to a malloc'd buffer containing the name
438 //                   under which the file was found. free()-ing the
439 //                   buffer is the responsibility of the caller.
440 //------------------------------------------------------------------------------
441 static const char      *data_directories[]=
442 { ".",                                                         // Normally unused, except when running from the source directory...
443   "/opt/QtPalmtop/share/games/powermanga/",
444   0                                                            // Special value meaning "$(PREFIX)/share/games/powermanga/"
445                                                                // Also marks end of list
446 };
447 static char pathstring[256];
448
449 char *locate_data_file(const char *const name)
450 {
451   
452   for(int i = 0; i < 256; i++)
453         pathstring[i] = 0;
454         
455   if(name == 0) return 0;
456   if(*name == '/') return strdup(name);                        // Guillaume Cottenceau (2001-03-15) replace "return 0;" by "strdup(name);"
457                                                                // En effet, nous sommes dans le cas ou le programme recherche dans /usr/share/games/powermanga
458   for(const char **p = data_directories;; p++)
459   { char *pathname;
460     if(*p == 0)
461     { const char *subdir = "/share/games/powermanga/";
462       pathname = &pathstring[0];
463       /*pathname=(char *)malloc(strlen(nomprefix) + strlen(subdir) + strlen(name) + 1);
464       if(pathname == 0)
465       { fflush(stdout);
466         fprintf(stderr, "powermanga: not enough memory\n");
467         return 0;
468       }*/
469       strcpy(pathname, nomprefix);
470       strcat(pathname, subdir);
471       strcat(pathname, name);
472     }
473     else if(**p == '~')                                        // Not used anymore
474     { static const char       bogus = '\0';
475       static const char      *home_dir = &bogus;
476       if(home_dir == &bogus) home_dir = getenv("HOME");
477       if(home_dir == 0) continue;                             // $HOME not set. Skip this directory.
478       pathname = &pathstring[0];
479       
480       /*pathname = (char *)malloc(strlen(home_dir) + 1 + strlen(*p + 1) + 1 + strlen(name) + 1);
481       if(pathname == 0)
482       { fflush(stdout);
483         fprintf(stderr, "powermanga: not enough memory\n");
484         return 0;
485       }*/
486       strcpy(pathname, home_dir);
487       strcat(pathname, *p + 1);
488       strcat(pathname, "/");
489       strcat(pathname, name);
490     }
491     else
492     {
493       pathname = &pathstring[0];
494       /*pathname = (char *)malloc(strlen(*p) + 1 + strlen(name) + 1);
495       if(pathname == 0)
496       { fflush(stdout);
497         fprintf(stderr, "powermanga: not enough memory\n");
498         return 0;
499       }*/
500       strcpy(pathname, *p);
501       strcat(pathname, "/");
502       strcat(pathname, name);
503     }
504     //puts(pathname);  // DEBUG
505 #ifdef WIN32
506     struct _stat s;
507     if(_stat(pathname, &s) == 0 && !_S_ISDIR(s.st_mode))
508       return pathname;
509 #else
510     struct stat s;
511     if(stat(pathname, &s) == 0 && !S_ISDIR(s.st_mode))
512       return pathname;
513 #endif
514     //free(pathname);
515     if(*p == 0) break;
516   }
517   return 0;                                                    // Not found.
518 }
519
520 //------------------------------------------------------------------------------
521 // load a file in memory / charge un fichier en memoire
522 // input =>  _pNomFichier  : nom du fichier
523 //           _pAdresse     : adresse de chargemebt
524 //           _iTaille      : taille a lire
525 // output <= 1             : tout c'est bien passe
526 //           0             : erreur
527 //------------------------------------------------------------------------------
528 char *chargeFichier(char *_pNomFichier)
529 {
530   unsigned int            _pTaille;
531   return chargeFichier(_pNomFichier, &_pTaille);
532 }
533
534 //------------------------------------------------------------------------------
535 // load a file in memory 
536 // input => _pNomFichier : filename
537 // sortie <= _pMemoire   : ptr/integer 
538 //------------------------------------------------------------------------------
539 char *chargeFichier(char *_pNomFichier, unsigned int *pTaille)
540 {
541   int _iHandle;
542   char *_pMemoire;
543   struct stat sDescriptionFichier;
544   char *pathname = locate_data_file(_pNomFichier);
545   if(pathname == 0)
546   { printf("linuxroutines/chargeFichier() : can't locate file : %s\n\n", _pNomFichier);
547     return 0;
548   }
549 #ifdef WIN32
550   _iHandle = open(pathname, O_RDONLY | O_BINARY, 0); 
551 #else
552   _iHandle = open(pathname, O_RDONLY, 0); 
553 #endif
554   if(_iHandle == -1)
555   { printf("linuxroutines/chargeFichier() : can't open file : %s (%s)\n\n", pathname, strerror(errno));
556     //free(pathname);
557     return 0;
558   }
559   if(fstat(_iHandle, &sDescriptionFichier))
560   { printf("linuxroutines/chargeFichier() : can't stat file : %s (%s)\n\n", pathname, strerror(errno));
561     close(_iHandle);
562     //free(pathname);
563     return 0;
564   }
565   (*pTaille) = sDescriptionFichier.st_size;                    //sauve taille
566   _pMemoire = reserveMemoire(sDescriptionFichier.st_size);     //reserve la place en memoire pour lire le fichier
567   if(!_pMemoire)
568   { printf("linuxroutines/chargeFichier() %s : out of memory\n\n", pathname);
569     close(_iHandle);
570     //free(pathname);
571     return 0;
572   }
573   if(read(_iHandle, _pMemoire, sDescriptionFichier.st_size) != sDescriptionFichier.st_size)
574   { libereMemoire(_pMemoire);
575     printf("linuxroutines/chargeFichier() can't read file %s (%s)\n\n", pathname, strerror(errno));
576     close(_iHandle);
577     //free(pathname);
578     return 0;
579   }
580   close(_iHandle);
581 #ifdef _VERBEUX_
582   if(iVerbeux > 1)
583     printf("linuxroutines/chargeFichier : file %s was loaded in memory\n", pathname);
584 #endif
585   //free(pathname);
586   return _pMemoire;
587 }
588
589 //------------------------------------------------------------------------------
590 // load a file in memory / charge un fichier en memoire
591 // input   => _pNomFichier : filename / nom du fichier
592 //         => _pMemoire    : pointer to memory / pointeur sur la memoire   
593 // output <= 1             : 0 error/erreur
594 //------------------------------------------------------------------------------
595 int chargeFichier(char *_pNomFichier, char *_pMemoire)
596 {
597   int                     _iHandle;
598   struct stat             sDescriptionFichier;
599   char                   *pathname = locate_data_file(_pNomFichier);
600   if(pathname == 0)
601   { printf("linuxroutines/chargeFichier() : can't locate file : %s\n\n", _pNomFichier);
602     return 0;
603   }
604 #ifdef WIN32
605   _iHandle = open(pathname, O_RDONLY | O_BINARY, 0); 
606 #else
607   _iHandle = open(pathname, O_RDONLY, 0); 
608 #endif
609   if(_iHandle == -1)
610   { printf ("linuxroutines.cpp/chargeFichier() : can't open file : %s (%s)\n\n", pathname, strerror(errno));
611     //free(pathname);
612     return 0;
613   }
614   if(fstat(_iHandle, &sDescriptionFichier))
615   { printf ("linuxroutines.cpp/chargeFichier() : can't stat file : %s (%s)\n\n", pathname, strerror(errno));
616     close(_iHandle);
617     //free(pathname);
618     return 0;
619   }
620   if(read(_iHandle, _pMemoire, sDescriptionFichier.st_size) != sDescriptionFichier.st_size)
621   { printf("linuxroutines.cpp/chargeFichier() can't read file %s (%s)\n\n", pathname, strerror(errno));
622     close(_iHandle);
623     //free(pathname);
624     return 0;
625   }
626   close(_iHandle);
627 #ifdef _VERBEUX_
628   if(iVerbeux > 1) printf("linuxroutines/chargeFichier : file %s was loaded in memory\n", pathname);
629 #endif
630   //free(pathname);
631   return 1;
632 }
633
634 //------------------------------------------------------------------------------
635 // initialise le compteur de boucle et l'heure de depart du programme
636 //------------------------------------------------------------------------------
637 void fps_init()
638 {
639 #ifdef SDL_TLK 
640   time_begin = SDL_GetTicks();
641   temps_sauve = SDL_GetTicks();
642   temps_sauve2 = SDL_GetTicks();
643 #else
644   gettimeofday(&time_begin, NULL);
645   gettimeofday(&temps_sauve, NULL);
646   gettimeofday(&temps_sauve2, NULL);
647 #endif
648   iCompteurBoucle = 0;
649 }
650
651 //------------------------------------------------------------------------------
652 // affiche information sur le systeme
653 //------------------------------------------------------------------------------
654 void fps_affiche()
655 {
656   
657 #ifdef SDL_TLK
658   double fps;
659   unsigned long duration;
660   Sint32 time_end; 
661   time_end = SDL_GetTicks();
662   duration = time_end - time_begin;
663   fps = (1000.0 * iCompteurBoucle) / duration;
664   if(iVerbeux > 0)
665   { printf("> linuxroutines.cpp/number of loops : %i\n", iCompteurBoucle);
666     printf("> linuxroutines.cpp/running time    : %li\n", duration);
667     printf("> linuxroutines.cpp/fps_affiche()   : frames per seconde : %g \n", fps);
668   }
669 #else  
670   struct utsname          kernel;
671   struct stat             statmem;
672   unsigned long           duration;
673   double                  fps;
674   double                  mem;
675   char                    os_name[32];
676   char                    os_vers[32];
677   char                    cpu[64];
678   char                    freq[32];
679   FILE                   *cpuinfo;
680   char                    txt[256];
681   static struct timeval   time_end;
682
683   //calcul le temps total d'execution ............................................
684   gettimeofday(&time_end, NULL);
685   duration = (time_end.tv_sec - time_begin.tv_sec) * 1000 + (time_end.tv_usec - time_begin.tv_usec) / 1000;
686   fps = (1000.0 * iCompteurBoucle) / duration;
687
688   //version du systeme d'exploitation ............................................
689   if(uname(&kernel) < 0)
690   { strcpy(os_name, "?");
691     strcpy(os_vers, "");
692   }
693   else
694   { strncpy(os_name, kernel.sysname, 32);
695     strncpy(os_vers, kernel.release, 32);
696   }
697   //processeur & memoire physique du systeme .....................................
698   stat("/proc/kcore", &statmem);
699   mem = ((float)statmem.st_size) / 1024 / 1024;
700   strcpy(cpu, "Unknown");
701   strcpy(freq, "Unknown");
702   cpuinfo = fopen("/proc/cpuinfo", "r");
703   if(cpuinfo != NULL)
704   { while (fgets(txt, 255, cpuinfo))
705     { if(!strncmp(txt, "model", 5))
706       { int                     i = 0;
707         while (txt[i] != ':') i++;
708         i += 2;
709         for(int j = 0; j < 64;)
710         { if(txt[i++] != '\n') cpu[j++] = txt[i - 1];
711         }
712       }
713       if(!strncmp(txt, "cpu MHz", 7))
714       { int                     i = 0;
715         while (txt[i] != ':') i++;
716         i += 2;
717         sprintf(freq, "%d", atoi(txt + i));
718       }
719     }
720   }
721   //processeur & memoire physique du systeme .....................................
722   if(iVerbeux > 0)
723   { printf("operating system   : %s %s\n", os_name, os_vers);
724     printf("processor          : %s at %s Mhz with %.0f RAM\n", cpu, freq, mem);
725     printf("number of loops    : %i\n", iCompteurBoucle);
726     printf("running time       : %li\n", duration);
727     printf("frames per seconde : %g \n", fps);
728     //printf ( "Compilateur      : %s\n" , COMPILO );
729   }
730 #endif
731 }
732
733 //------------------------------------------------------------------------------
734 // display error message
735 // input => _Message :  message 
736 //------------------------------------------------------------------------------
737 void afficheMessage(char *_Message)
738
739   printf("%s\n", _Message);
740 }
741
742 //------------------------------------------------------------------------------
743 // display error message
744 // input => _Message1 :  message 1 
745 //       => _Message2 :  message 2 
746 //------------------------------------------------------------------------------
747 void afficheMessage(char *_Message1, char *_Message2)
748 {
749   printf("%s %s\n", _Message1, _Message2);
750 }
751
752 //------------------------------------------------------------------------------
753 // display error message
754 // input => _pErreur1 : error message 
755 //       => _pErreur2 : title error
756 //------------------------------------------------------------------------------
757 void afficheErreur(char *_pErreur1, char *_pErreur2)
758 {
759   printf("%s %s\n", _pErreur2, _pErreur1);
760 }
761
762 //------------------------------------------------------------------------------
763 // display error message
764 // input        => _pErreur1 : error message 
765 //              => _pErreur2 : title error
766 //              => _pErreur3 : error message
767 //------------------------------------------------------------------------------
768 void afficheErreur(char *_pErreur1, char *_pErreur2, char *_pErreur3)
769 {
770   printf("%s %s %s\n", _pErreur2, _pErreur1, _pErreur3);
771 }
772
773 //------------------------------------------------------------------------------
774 // display error message
775 // input        => _pErreur1 : error message 
776 //              => _pErreur2 : title error
777 //              => _iErreur : error number
778 //------------------------------------------------------------------------------
779 void afficheErreur(char *_pErreur1, char *_pErreur2, int _iErreur)
780 {
781   printf("%s %s error=%i\n", _pErreur2, _pErreur1, _iErreur);
782 }
783
784 //------------------------------------------------------------------------------
785 // sleep process
786 // input => i : time
787 //------------------------------------------------------------------------------
788 int synchro_processusPause(int _iTemps)
789 {
790 #ifdef SDL_TLK 
791   if(_iTemps > (int)iFrame3)
792     _iTemps = iFrame3;
793   if(_iTemps > 0)
794     SDL_Delay(_iTemps);
795   return (_iTemps > 0 ? _iTemps : 0);
796 #else
797   struct timeval temps;
798   if(_iTemps > (int)iFrame3)
799     _iTemps = iFrame3;
800   if(_iTemps > 0)
801   { temps.tv_usec = _iTemps % (unsigned long)1000000;
802     temps.tv_sec = _iTemps / (unsigned long)1000000;
803     //sleep for a time interval
804     select(0, NULL, NULL, NULL, &temps);
805   }
806   return (_iTemps > 0 ? _iTemps : 0);
807 #endif
808 }
809
810 #ifdef SDL_TLK
811 int synchro_CalculDifference()
812 {
813   int _iDifference;
814   temps_actuel = SDL_GetTicks();
815   _iDifference = temps_actuel - temps_sauve;
816   temps_sauve = temps_actuel;
817   return _iDifference;
818 }
819 #else
820 //------------------------------------------------------------------------------
821 // calcul la difference entre deux heure differentes
822 // sortie <= : _iDifference
823 //------------------------------------------------------------------------------
824 int synchro_CalculDifference()
825 {
826   int _iDifference;
827   gettimeofday(&temps_actuel, NULL);
828   _iDifference = (1000000 * (temps_actuel.tv_sec - temps_sauve.tv_sec)) + (temps_actuel.tv_usec - temps_sauve.tv_usec);
829   temps_sauve = temps_actuel;
830   return _iDifference;
831 }
832
833 //------------------------------------------------------------------------------
834 // calcul la difference entre deux heure differentes
835 // sortie <= : _iDifference
836 //------------------------------------------------------------------------------
837 int synchro_CalculDifference2()
838 {
839   int _iDifference;
840   gettimeofday(&temps_actuel2, NULL);
841   _iDifference = (1000000 * (temps_actuel2.tv_sec - temps_sauve2.tv_sec)) + (temps_actuel2.tv_usec - temps_sauve2.tv_usec);
842   temps_sauve2 = temps_actuel2;
843   return _iDifference;
844 }
845 #endif