introduced game data dir and user data dir

- the game data dir contains system wide data such as levels, sounds, etc.
- user data dir stores higscore, configurations
This commit is contained in:
2010-12-03 00:15:26 +01:00
parent cfedddcae0
commit d01ab37554
12 changed files with 217 additions and 33 deletions
+1 -1
View File
@@ -33,7 +33,7 @@ bool Controller::OnReceiveEvent (const Engine::EventBasePtr &event) {
}
void Controller::ResetPlayerEntity () {
Engine::HaltSoundLoop("./data/sounds/thrust.wav");
Engine::HaltSoundLoop(Engine::GetResourceFullPath("/data/sounds/thrust.wav"));
Engine::EntityBase *player_entity = GetModel()->GetEntity(GetModel()->GetPlayerEntityId());
+10 -10
View File
@@ -65,7 +65,7 @@ int Model::OnInit (int argc, char* argv[]) {
mPlayerName = "Player";
Engine::PlayMusic ("./data/sounds/intro_music.ogg");
Engine::PlayMusic (Engine::GetResourceFullPath("/data/sounds/intro_music.ogg"));
Engine::RegisterListener (this, EventAccelerateStart);
Engine::RegisterListener (this, EventAccelerateStop);
@@ -76,10 +76,10 @@ int Model::OnInit (int argc, char* argv[]) {
bool Model::OnReceiveEvent (const Engine::EventBasePtr &event) {
switch (event->mEventType) {
case EventAccelerateStart:
Engine::PlaySoundLoop("./data/sounds/thrust.wav", -1);
Engine::PlaySoundLoop(Engine::GetResourceFullPath("/data/sounds/thrust.wav"), -1);
break;
case EventAccelerateStop:
Engine::HaltSoundLoop("./data/sounds/thrust.wav");
Engine::HaltSoundLoop(Engine::GetResourceFullPath("/data/sounds/thrust.wav"));
break;
case EventShipExplode:
OnShipExplode();
@@ -117,19 +117,19 @@ void Model::Process () {
}
unsigned int Model::InitLevelList () {
const char* level_dir_name = "./data/levels/";
Engine::LogDebug ("Searching for levels in %s", level_dir_name);
std::string level_dir_name = Engine::GetResourceFullPath("/data/levels/");
Engine::LogDebug ("Searching for levels in %s", level_dir_name.c_str());
mLevelList.clear();
boost::filesystem::path level_dir(level_dir_name);
if (!boost::filesystem::exists(level_dir)) {
Engine::LogError ("Could not init level list: %s does not exist!");
Engine::LogError ("Could not init level list: \todo %s does not exist!");
}
if (!boost::filesystem::is_directory(level_dir)) {
Engine::LogError ("Could not init level list: %s is not a directory!");
Engine::LogError ("Could not init level list: \todo %s is not a directory!");
}
boost::filesystem::directory_iterator end_iter;
@@ -151,7 +151,7 @@ unsigned int Model::InitLevelList () {
void Model::LoadHighscoreList () {
Engine::LogDebug ("Loading highscore file");
boost::filesystem::path highscore_file("./highscore.dat");
boost::filesystem::path highscore_file(Engine::GetUserDirFullPath("/highscore.dat"));
// if the file does not exist, we create it and write standard values into
// it.
@@ -185,7 +185,7 @@ void Model::LoadHighscoreList () {
}
void Model::SaveHighscoreList () {
std::ofstream highscore_file ("./highscore.dat");
std::ofstream highscore_file (Engine::GetUserDirFullPath("/highscore.dat").c_str());
std::list<HighscoreEntry>::iterator iter = mHighscoreList.begin();
@@ -425,7 +425,7 @@ void Model::OnKillEntity (const Engine::EntityBase *entity) {
GameEntityType entity_type = (GameEntityType) entity->mType;
if (entity_type == GameEntityTypeAsteroid) {
Engine::PlaySound("./data/sounds/rock_destroyed.wav");
Engine::PlaySound(Engine::GetResourceFullPath("/data/sounds/rock_destroyed.wav"));
unsigned int i;
const AsteroidEntity *asteroid = static_cast<const AsteroidEntity*>(entity);
+1 -1
View File
@@ -117,7 +117,7 @@ void ShipEntity::Attack () {
rocket_physics->mVelocity = attack_dir.normalize();
rocket_physics->mVelocity *= ShipEntity::VarMaxSpeed.GetFloatValue() + 0.5;
Engine::PlaySound ("./data/sounds/laser.wav");
Engine::PlaySound (Engine::GetResourceFullPath("/data/sounds/laser.wav"));
}
}
+5 -5
View File
@@ -56,16 +56,16 @@ int View::OnInit (int argc, char* argv[]) {
mBackgroundStars.push_back (star);
}
mGUIShipSprite.LoadFromPNG("./data/textures/ship.png");
mGUIShipSprite.LoadFromPNG(Engine::GetResourceFullPath("/data/textures/ship.png"));
mGUIShipSprite.SetScale (0.1);
mAsteroidSprite.LoadFromPNG ("./data/textures/asteroid.png");
mShipSprite.LoadFromPNG ("./data/textures/ship.png");
mAsteroidSprite.LoadFromPNG (Engine::GetResourceFullPath("/data/textures/asteroid.png"));
mShipSprite.LoadFromPNG (Engine::GetResourceFullPath("/data/textures/ship.png"));
mShipThrustSprite.LoadFromPNG ("./data/textures/ship_thrust.png");
mShipThrustSprite.LoadFromPNG (Engine::GetResourceFullPath("/data/textures/ship_thrust.png"));
mShipThrustSprite.SetAnimation (4, 8);
mShipPartsSprite.LoadFromPNG ("./data/textures/ship_parts.png");
mShipPartsSprite.LoadFromPNG (Engine::GetResourceFullPath("/data/textures/ship_parts.png"));
mShipPartsSprite.SetSubSpriteCount (10);
Engine::RegisterListener (this, EventAccelerateStart);
+96 -2
View File
@@ -9,12 +9,94 @@
#include "Physics.h"
#include "EntityFactory.h"
#include <boost/filesystem.hpp>
#ifdef WIN32
#include <Windows.h>
#endif
using namespace std;
/* Returns a path where files such as logs and config files can be
* written to
*/
std::string create_user_path () {
std::string result_dir = ".";
std::string test_file_path = result_dir;
// first we check in $HOME/.fysxasteroids
char* env_home_dir = getenv("HOME");
result_dir = env_home_dir;
result_dir += "/.fysxasteroids";
boost::filesystem::path result_dir_path(result_dir);
if(!boost::filesystem::is_directory (result_dir_path)) {
if (!boost::filesystem::create_directory(result_dir_path)) {
cerr << "Warning: could not create user data directory " << result_dir<< endl;
result_dir = "";
}
}
test_file_path = result_dir;
test_file_path += "/game.log";
ofstream test_file (test_file_path.c_str(), ios_base::app);
if (!test_file) {
test_file.close();
cerr << "Warning: user data directory not writable! " << result_dir << endl;
result_dir = "";
} else {
test_file.close();
return result_dir;
}
// then we check the local directory
result_dir = ".";
test_file_path = result_dir;
test_file_path += "/game.log";
test_file.open (test_file_path.c_str(), ios_base::out);
if (test_file) {
test_file.close();
return result_dir;
} else {
cerr << "Warning could not find suitable user data directory" << endl;
result_dir = "";
}
test_file.close();
return result_dir;
}
std::string find_game_data_dir () {
std::string result;
std::vector<std::string> paths;
paths.push_back(".");
paths.push_back("/usr/local/share/fysxasteroids");
paths.push_back("/usr/share/fysxasteroids");
std::vector<std::string>::iterator iter = paths.begin();
for (iter; iter != paths.end(); iter++) {
std::string test_path = *iter;
if (!boost::filesystem::is_directory(test_path + "/data/fonts"))
continue;
if (!boost::filesystem::is_directory(test_path + "/data/levels"))
continue;
if (!boost::filesystem::is_directory(test_path + "/data/sounds"))
continue;
if (!boost::filesystem::is_directory(test_path + "/data/textures"))
continue;
break;
}
if (iter != paths.end())
return *iter;
cerr << "Could not find game data" << endl;
return result;
}
int main (int argc, char* argv[]) {
cout << "Game Start" << endl;
@@ -32,7 +114,19 @@ int main (int argc, char* argv[]) {
engine.SetView (new asteroids::View);
SetLogPrintLevel (Engine::LogLevelMessage);
Engine::SetLogFilename ("game.log");
// we assume the user path to be local folder
std::string user_path = create_user_path();
std::string log_file_path = user_path;
log_file_path += "/game.log";
cout << "User Data Dir = " << user_path << endl;
engine.SetUserDataPath (user_path);
Engine::SetLogFilename (log_file_path.c_str());
std::string game_data_path = find_game_data_dir();
engine.SetGameDataPath (game_data_path);
cout << "Game Data Dir = " << game_data_path << endl;
if (engine.Init (argc, argv) != 0) {
cout << "Could not start engine!" << endl;
@@ -60,7 +154,7 @@ int main (int argc, char* argv[]) {
engine.MainLoop ();
// save the configuration
std::ofstream config_file ("config.rc");
std::ofstream config_file (engine.GetUserDirFullPath("/config.rc").c_str());
config_file << "set effects_volume " << Engine::GetEffectsVolume() << std::endl;
config_file << "set music_volume " << Engine::GetMusicVolume() << std::endl;
config_file.close();