コード例 #1
0
ファイル: Game.cpp プロジェクト: cwriter/SFMLGame
int Game::load(sf::String path, SFG::SplashScreen* ss)
{
	if(m_game_console.init() != 0)
		SFG::Util::printLog(SFG::Util::Error, __FILE__, __LINE__,
			"Failed to init console"
		);
	
	if(!m_timing_font.loadFromFile("Fonts/arial.ttf"))
		SFG::Util::printLog(SFG::Util::Error, __FILE__, __LINE__,
			"Failed to load font"
		);
	
	printf("Setting font... ");
	m_timing_display.setFont(m_timing_font);
	printf("Done.\n");
	m_timing_display.setString("Asd");
	
    XMLReader reader;
    sf::String out;
    if (basicLoadFile(path.toWideString(), out) != 0)
    {
        printf("[Error] Failed to load file %s in %s:%d\n", path.toAnsiString().c_str(), __FILE__, __LINE__);
        return -1;
    }


    reader.setSource(out);

    //Start parsing
    if (reader.parse() != 0)
    {
        //Errors happening
    }
    //For now, we only need to load the gamestates up...

    //Get the loading gamestate (with fancy animations and stuff)
    int ret = 0;
    auto sugs = SFG::Pointer<GameState>(new StartupGameState());
    if ((ret = loadGamestate(reader, L"Startup", sugs)) < 0)
    {
        printf("[Error] Failed to load Startup Gamestate in %s:%d from file \"%s\" with Code %d.\n", __FILE__, __LINE__, path.toAnsiString().c_str(), ret);
    }
    //The first gamestate always needs to be inited
    this->g_gamestates[0]->init(this->window);
    //Get the menu gamestate
    SFG::Pointer<GameState> menu(new MenuGameState());
    if ((ret = loadGamestate(reader, L"Menu", menu)) < 0)
    {
        printf("[Error] Failed to load Menu Gamestate in %s:%d from file \"%s\" with Code %d.\n", __FILE__, __LINE__, path.toAnsiString().c_str(), ret);
    }


    //Get all other gamestates
    for (size_t i = 2; ; i++)
    {
        sf::String path = L"xml/gamestate#" + std::to_wstring(i) + L"/";
        sf::String ret = reader.getValue(path);
        if (ret == L"__xml_failure")
        {
            break;
        }
        else
        {
            //#TODO: Add custom gamestate
            //STILL_TO_DO(__FILE__, __LINE__);

            int res = loadGamestate(reader, reader.getValue(path + "name."), SFG::Pointer<GameState>(nullptr));
            if (res != 0)
            {
                std::string tmp = reader.getValue(path + "name.").toAnsiString();
                SFG::Util::printLog(SFG::Util::LogMessageType::Error, __FILE__, __LINE__, "Failed to load custom gamestate named \"%s\"", tmp.c_str());
            }

        }

    }



    //Reading done
    if (ss != nullptr)
    {
        //Tell the SplashScreen to disappear
        ss->notify(SFG::SplashScreen::SplashScreenMessages::LoadingDone);
    }
    return 0;
}
コード例 #2
0
ファイル: SSG_Planet.cpp プロジェクト: cwriter/SFMLGame
int SSG_Planet::load(const XMLReader& reader)
{
	//SFG::Util::printLog(SFG::Util::Development, __FILE__, __LINE__, "Still to do");
	//Load values accordingly	
	this->m_name = reader.getValue("name.");
	bool real = false;
	this->setMass(PE::Mass(reader.asDouble("mass/", real)));
	if(!real)
	{
		//Failed to load mass
		SFG::Util::printLog(SFG::Util::Error, __FILE__, __LINE__, "Failed to load Mass");
	}
	sf::FloatRect r = reader.asFloatRect("PosAndVel/", real);
	if(!real)
	{
		//Failed to load pos and Velocity
		SFG::Util::printLog(SFG::Util::Error, __FILE__, __LINE__, "Failed to load PosAndVel");
	}
	//this->setPosition(r.left, r.top);
	this->setPosition(mpf_class(r.left), mpf_class(r.top));
	this->setVelocity(PE::Velocity(r.width, r.height));
	
	auto radius = reader.asDouble("radius/", real);
	if(!real)
	{
		SFG::Util::printLog(SFG::Util::Error, __FILE__, __LINE__,
							"Failed to get radius for planet %s", this->m_name.toAnsiString().c_str());
	}
	this->getShape().setRadius(radius);
	
	SFG::FloatRect rect(this->getShape().getLocalBounds());
	this->getShape().setOrigin(rect.center());
	
	m_planet_surface.reset(new SSG_PlanetSurface(radius));
	m_planet_surface->load();
	
	//Get Moons
	reader.for_all("Moon", [=](const XMLGroup* g){
		SFG::Pointer<SSG_Planet> ptr(new SSG_Planet());
		ptr->m_parentSys = this->m_parentSys;
		ptr->setGuiDesktop(this->desktop());
		int ret = ptr->load(XMLReader(*g));
		if(ret != 0)
		{
			SFG::Util::printLog(SFG::Util::Error, __FILE__, __LINE__,
								"Failed to load moon");
			return;
		}
		printf("Position: %f + %f | %f + %f\n", getShape().getPosition().x , ptr->getShape().getPosition().x,
			    getShape().getPosition().y, ptr->getShape().getPosition().y);
		//Correct relative values
		//ptr->setPosition(getShape().getPosition().x + ptr->getShape().getPosition().x, 
		//				 getShape().getPosition().y + ptr->getShape().getPosition().y);
		ptr->setPosition(x() + ptr->x(), 
						 y() + ptr->y());
		ptr->setVelocity(getVelocity() + ptr->getVelocity());
		
		
		//Add to system
		this->m_parentSys->addSpecificToSystem(ptr);
		
		SFG::Util::printLog(SFG::Util::Information, __FILE__, __LINE__,	"Moon \"%s\" has been added", ptr->getName().toAnsiString().c_str());
		
	});
	
	//TESTING
	this->getShape().setFillColor(sf::Color(255, 255, 255));
	//!TESTING
	return 0;
}
コード例 #3
0
ファイル: Game.cpp プロジェクト: cwriter/SFMLGame
int Game::loadGamestate(const XMLReader& reader, const sf::String& GSname, const SFG::Pointer<GameState>& included)
{
    SFG::Pointer<GameState> GS_ptr;

    sf::String GSxmlpath = L"xml/gamestate[" + GSname + L"]";
    auto str = reader.getValue(GSxmlpath, 0);

    if (str == L"__xml_failure")
    {
        printf("[Error] Unspecified startup gamestate in %s:%d.\n", __FILE__, __LINE__);
        return -2;
    }
    else
    {

        if (str == L"#included")
        {
            //Use default
            GS_ptr.reset(included);
            if (!GS_ptr.isValid() && included.getElement() != nullptr)
            {
                printf("[Critical] Failed to allocate memory for \"%s\" in %s:%d\n",GSname.toAnsiString().c_str(), __FILE__, __LINE__);
                return -1;
            }

        }
        else
        {
            //Use specified
            //#TODO
            STILL_TO_DO(__FILE__, __LINE__);
            //We first need an instance of a DLLLoadedGameState
            GS_ptr.reset(new DLLLoadedGameState());
            //Set the name
            GS_ptr->setName(GSname);
            if (GS_ptr->getName() == "__xml_failure")
            {
                SFG::Util::printLog(SFG::Util::Error, __FILE__, __LINE__, "Failed to get Gamestate name, aborting.");
                return -1;
            }
            //Then, load the specified file
            if (GS_ptr.cast<DLLLoadedGameState>()->loadFromDll(str.toAnsiString().c_str()) != 0)
            {
                SFG::Util::printLog(SFG::Util::Error, __FILE__, __LINE__,
									"Failed to load gamestate \"%s\" from DLL", str.toAnsiString().c_str());
                return -2;
            }

            //We actually should be done


        }

        //Get all modules
        sf::String uses = reader.getValue(GSxmlpath + L"uses.");
        if (uses != L"__xml_failure")
        {
            //We have specifiers
            size_t begin = 0;
            size_t last = 0;
            //Begin needs to be checked as the last module wouldn't be counted otherwise
            while ((last = uses.find(L',', last)) != sf::String::InvalidPos || begin != sf::String::InvalidPos)
            {
                sf::String string;
                if (last == sf::String::InvalidPos)
                {
                    //If the end would otherwise exceed the string length, set it to the maximum (sf::String::end()).
                    //string = sf::String(uses.begin() + begin, uses.end());
                    string = uses.substring(begin, uses.getSize() - begin);
                }
                else
                {
                    //If a ',' was found, use its position
                    //string = sf::String(uses.begin() + begin, uses.begin() + last);
                    string = uses.substring(begin, last - begin);
                }
                //Check for module names
                if (string == L"G2D")
                {
                    SFG::Pointer<ModuleG2D> ptr(new ModuleG2D());
                    if (!ptr.isValid())
                    {
                        char buf[512];
#ifdef _WIN32
                        strerror_s(buf, errno);
#else
                        strerror_r(errno, buf, 512);
#endif // _WIN32
                        printf("[Error] Failed to create Module G2D in %s:%d: %s\n", __FILE__, __LINE__, buf);
                        return -3;
                    }
                    GS_ptr->addModule(ptr);
                }
                else
                {
                    //#TODO: Add more modules (sound etc.)
                    STILL_TO_DO(__FILE__, __LINE__);
                }
                if(last != sf::String::InvalidPos) begin = last + 1; //if a ',' was found, go search on for the next one
                else begin = last; //Meaning begin is sf::String::npos
            }
        }

        //Check for external asset list
        auto ext_assets = reader.getValue(GSxmlpath + "assets.");
        if (ext_assets != "__xml_failure")
        {
            //We found external assets, load them
        }
        auto assets = reader.getXMLGroupHandle(L"xml/assetlist[" + GSname + L"]");
        if (assets == nullptr)
        {
            printf("[Warning] No assets for Gamestate \"%s\" found. This is not encouraged and might be an error. Please check the game.xml file. In: %s:%d\n", GSname.toAnsiString().c_str(), __FILE__, __LINE__);
            //return 1;
        }
        else
        {
            auto ret = GS_ptr->loadAssets(XMLReader(*assets));
            if (ret != 0)
            {
                printf("[Error] Failed to load assets for Gamestate \"%s\" in %s:%d.\n", GSname.toAnsiString().c_str(), __FILE__, __LINE__);
            }
        }

        //Empty path to tell the gamestate to just update what it has (or do nothing if nothing has to be done)
        GS_ptr->load(L"");

        //this->g_gamestates.push_back(GS_ptr);
        this->addGamestate(GS_ptr);
    }
    return 0;
}