177 lines
4.5 KiB
C++
177 lines
4.5 KiB
C++
|
#include <cstdlib>
|
||
|
#include <iostream>
|
||
|
|
||
|
#include "Engine.h"
|
||
|
|
||
|
#include "Controller.h"
|
||
|
#include "View.h"
|
||
|
#include "Model.h"
|
||
|
#include "Physics.h"
|
||
|
#include "EntityFactory.h"
|
||
|
|
||
|
#include "Game.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;
|
||
|
|
||
|
#ifdef WIN32
|
||
|
HWND hWnd = GetConsoleWindow();
|
||
|
ShowWindow( hWnd, SW_HIDE );
|
||
|
#endif
|
||
|
|
||
|
Engine::Engine engine;
|
||
|
|
||
|
engine.SetEntityFactory (new asteroids::EntityFactory);
|
||
|
engine.SetController (new asteroids::Controller);
|
||
|
engine.SetModel (new asteroids::Model);
|
||
|
engine.SetPhysics (new asteroids::Physics);
|
||
|
engine.SetView (new asteroids::View);
|
||
|
|
||
|
SetLogPrintLevel (Engine::LogLevelMessage);
|
||
|
|
||
|
// 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 += "/editor.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;
|
||
|
exit (-1);
|
||
|
}
|
||
|
|
||
|
Engine::SetMusicVolume (0.);
|
||
|
|
||
|
// Load the icon
|
||
|
Uint32 colorkey;
|
||
|
SDL_Surface *image = NULL;
|
||
|
image = SDL_LoadBMP("./data/textures/icon.bmp");
|
||
|
if (!image)
|
||
|
Engine::LogWarning("Could not load icon: ./data/textures/icon.bmp");
|
||
|
else
|
||
|
SDL_WM_SetIcon(image,NULL);
|
||
|
|
||
|
SDL_WM_SetCaption("Asteroids -BETA1-","Asteroids -BETA 1-");
|
||
|
|
||
|
engine.GetView()->SetGridSize (8,8);
|
||
|
|
||
|
/// \todo get rid of the world settings in asteroids
|
||
|
dynamic_cast<asteroids::Physics*>(engine.GetPhysics())->SetWorldSize (26, 20);
|
||
|
engine.GetPhysics()->SetWorldBounds (vector3d (-13, 0, -10), vector3d (13, 0, 10));
|
||
|
engine.GetPhysics()->EnableWorldWarp(Engine::PhysicsBase::WorldWarpModeX);
|
||
|
engine.GetPhysics()->EnableWorldWarp(Engine::PhysicsBase::WorldWarpModeZ);
|
||
|
|
||
|
// run the default commands and load the configuration
|
||
|
Engine::RunCommand ("exec asteroids.rc");
|
||
|
Engine::RunCommand ("exec config.rc");
|
||
|
|
||
|
asteroids::GetView()->PopViewState();
|
||
|
asteroids::GetView()->PushViewState(asteroids::ViewStateEditor);
|
||
|
|
||
|
engine.MainLoop ();
|
||
|
|
||
|
SDL_WM_SetIcon(NULL,NULL);
|
||
|
SDL_FreeSurface (image);
|
||
|
|
||
|
engine.Destroy ();
|
||
|
|
||
|
cout << "Game Quit" << endl;
|
||
|
|
||
|
return 0;
|
||
|
}
|