27 |
#include <iostream> |
#include <iostream> |
28 |
#include <functional> |
#include <functional> |
29 |
#include <cassert> |
#include <cassert> |
30 |
|
#include <ctime> |
31 |
#include <sys/types.h> |
#include <sys/types.h> |
32 |
#include <dirent.h> |
#include <dirent.h> |
33 |
|
|
39 |
{ |
{ |
40 |
class FontAlloc { |
class FontAlloc { |
41 |
public: |
public: |
42 |
Font *acquire(const std::string &name) { |
Font *acquire (const std::string &name) { |
43 |
string fname = enigma::FindDataFile(string("fonts/")+name+".png"); |
string png, bmf; |
44 |
string dname = enigma::FindDataFile(string("fonts/")+name+".bmf"); |
using enigma::FindFile; |
45 |
return px::LoadBitmapFont(fname.c_str(), dname.c_str()); |
if (FindFile (string("fonts/")+name+".png", png) && |
46 |
|
FindFile (string("fonts/")+name+".bmf", bmf)) |
47 |
|
{ |
48 |
|
return px::LoadBitmapFont(png.c_str(), bmf.c_str()); |
49 |
|
} |
50 |
|
else |
51 |
|
return 0; |
52 |
} |
} |
53 |
void release(Font *f) { delete f; } |
void release(Font *f) { delete f; } |
54 |
}; |
}; |
86 |
} |
} |
87 |
|
|
88 |
Direction |
Direction |
89 |
direction_fromto(GridPos source, GridPos target) { |
direction_fromto(GridPos source, GridPos target) |
90 |
|
{ |
91 |
// source and target have to be adjacent |
// source and target have to be adjacent |
92 |
|
|
93 |
int dx = target.x-source.x; |
int dx = target.x-source.x; |
121 |
// ---------------------- |
// ---------------------- |
122 |
|
|
123 |
DirectionBits |
DirectionBits |
124 |
enigma::rotate(DirectionBits d, bool clockwise) { |
enigma::rotate(DirectionBits d, bool clockwise) |
125 |
|
{ |
126 |
if (clockwise) { |
if (clockwise) { |
127 |
d = DirectionBits(((d>>1) | (d<<3)) & ALL_DIRECTIONS); |
d = DirectionBits(((d>>1) | (d<<3)) & ALL_DIRECTIONS); |
128 |
} |
} else { |
|
else { |
|
129 |
d = DirectionBits(((d>>3) | (d<<1)) & ALL_DIRECTIONS); |
d = DirectionBits(((d>>3) | (d<<1)) & ALL_DIRECTIONS); |
130 |
} |
} |
|
|
|
131 |
return d; |
return d; |
132 |
} |
} |
133 |
|
|
428 |
{ |
{ |
429 |
class DataPathList { |
class DataPathList { |
430 |
public: |
public: |
431 |
DataPathList(const string &pathspec=DEFAULT_DATA_PATH) |
DataPathList(const string &pathspec=DEFAULT_DATA_PATH); |
|
{ |
|
|
set_path(pathspec); |
|
|
} |
|
432 |
|
|
433 |
void set_path(const string &pathspec) |
void set_path(const string &pathspec); |
434 |
{ |
const string &get_path() const; |
|
path=pathspec; |
|
|
datapaths.clear(); |
|
|
split_copy(pathspec, ':', back_inserter(datapaths)); |
|
435 |
|
|
436 |
for (unsigned i=0; i<datapaths.size(); ++i) |
bool find_file(const string &filename, string &dest) const; |
|
datapaths[i] = sysdep::expand_path(datapaths[i]); |
|
|
} |
|
|
const string &get_path() const { return path; } |
|
437 |
|
|
438 |
bool find_file(const string &filename, string &dest) const |
std::list <string> find_files (const string &folder, |
439 |
{ |
const string &filename) const; |
|
for (unsigned i=0; i<datapaths.size(); ++i) { |
|
|
string complete_name = datapaths[i] + sysdep::path_separator + filename; |
|
|
if (sysdep::FileExists(complete_name)) |
|
|
{ |
|
|
dest = complete_name; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
return false; |
|
|
} |
|
|
|
|
|
std::list <string> find_files(const string &folder, const string &filename) const |
|
|
{ |
|
|
std::list <string> matches; |
|
|
|
|
|
for (unsigned i=0; i<datapaths.size(); ++i) { |
|
|
string complete_name = datapaths[i] + sysdep::path_separator + folder; |
|
|
if (sysdep::FolderExists(complete_name)) |
|
|
{ |
|
|
DIR *dir = opendir( complete_name.c_str()); |
|
|
struct dirent *entry; |
|
|
while((entry = readdir(dir)) != NULL) |
|
|
{ |
|
|
if( strcmp( entry->d_name, ".") != 0 && strcmp( entry->d_name, "..") != 0) |
|
|
{ |
|
|
string tmp_name = complete_name + sysdep::path_separator + string(entry->d_name) + sysdep::path_separator + filename; |
|
|
if ( sysdep::FileExists( tmp_name)) |
|
|
{ |
|
|
matches.push_back( tmp_name); |
|
|
} |
|
|
} |
|
|
} |
|
|
closedir(dir); |
|
|
} |
|
|
} |
|
|
return matches; |
|
|
} |
|
440 |
|
|
441 |
private: |
private: |
442 |
|
// Variables |
443 |
string path; |
string path; |
444 |
vector<string> datapaths; |
vector<string> datapaths; |
445 |
}; |
}; |
447 |
DataPathList datapaths; |
DataPathList datapaths; |
448 |
} |
} |
449 |
|
|
450 |
string enigma::GetDataPath() { |
DataPathList::DataPathList(const string &pathspec) |
451 |
|
{ |
452 |
|
set_path(pathspec); |
453 |
|
} |
454 |
|
|
455 |
|
void |
456 |
|
DataPathList::set_path(const string &pathspec) |
457 |
|
{ |
458 |
|
path=pathspec; |
459 |
|
datapaths.clear(); |
460 |
|
split_copy(pathspec, ':', back_inserter(datapaths)); |
461 |
|
|
462 |
|
for (unsigned i=0; i<datapaths.size(); ++i) |
463 |
|
datapaths[i] = sysdep::expand_path(datapaths[i]); |
464 |
|
} |
465 |
|
|
466 |
|
const string & |
467 |
|
DataPathList::get_path() const |
468 |
|
{ |
469 |
|
return path; |
470 |
|
} |
471 |
|
|
472 |
|
bool |
473 |
|
DataPathList::find_file(const string &filename, string &dest) const |
474 |
|
{ |
475 |
|
for (unsigned i=0; i<datapaths.size(); ++i) { |
476 |
|
string complete_name = datapaths[i] + sysdep::path_separator + filename; |
477 |
|
if (sysdep::FileExists(complete_name)) |
478 |
|
{ |
479 |
|
dest = complete_name; |
480 |
|
return true; |
481 |
|
} |
482 |
|
} |
483 |
|
return false; |
484 |
|
} |
485 |
|
|
486 |
|
std::list <string> |
487 |
|
DataPathList::find_files(const string &folder, const string &filename) const |
488 |
|
{ |
489 |
|
std::list <string> matches; |
490 |
|
|
491 |
|
for (unsigned i=0; i<datapaths.size(); ++i) { |
492 |
|
string complete_name = datapaths[i] + sysdep::path_separator + folder; |
493 |
|
if (sysdep::FolderExists(complete_name)) |
494 |
|
{ |
495 |
|
DIR *dir = opendir( complete_name.c_str()); |
496 |
|
struct dirent *entry; |
497 |
|
while((entry = readdir(dir)) != NULL) |
498 |
|
{ |
499 |
|
if( strcmp( entry->d_name, ".") != 0 && strcmp( entry->d_name, "..") != 0) |
500 |
|
{ |
501 |
|
string tmp_name = complete_name + sysdep::path_separator |
502 |
|
+ string(entry->d_name) + sysdep::path_separator + filename; |
503 |
|
if (sysdep::FileExists (tmp_name)) |
504 |
|
matches.push_back (tmp_name); |
505 |
|
} |
506 |
|
} |
507 |
|
closedir(dir); |
508 |
|
} |
509 |
|
} |
510 |
|
return matches; |
511 |
|
} |
512 |
|
|
513 |
|
string enigma::GetDataPath() |
514 |
|
{ |
515 |
return datapaths.get_path(); |
return datapaths.get_path(); |
516 |
} |
} |
517 |
|
|
518 |
void enigma::SetDataPath(const string &p) { |
void enigma::SetDataPath(const string &p) |
519 |
|
{ |
520 |
datapaths.set_path(p); |
datapaths.set_path(p); |
521 |
} |
} |
522 |
|
|
592 |
//---------------------------------------- |
//---------------------------------------- |
593 |
// Random numbers |
// Random numbers |
594 |
//---------------------------------------- |
//---------------------------------------- |
595 |
|
void enigma::Randomize () |
596 |
|
{ |
597 |
|
srand (time(NULL)); |
598 |
|
} |
599 |
|
|
600 |
void enigma::Randomize (unsigned seed) |
void enigma::Randomize (unsigned seed) |
601 |
{ |
{ |
602 |
srand (seed); |
srand (seed); |