int main(int argc, char *argv[]) { __android_log_write(ANDROID_LOG_ERROR, "GD", "Started game"); std::cout.rdbuf(new androidbuf); sf::RenderWindow window(sf::VideoMode::getDesktopMode(), ""); //TODO: extension CppPlatform::Get().AddExtension(std::shared_ptr<ExtensionBase>(CreateGDCppTextObjectExtension())); std::cout.flush(); CppPlatform::Get().AddExtension(std::shared_ptr<ExtensionBase>(CreateGDCppTopDownMovementBehaviorExtension())); std::cout.flush(); CppPlatform::Get().AddExtension(std::shared_ptr<ExtensionBase>(CreateGDCppDestroyOutsideBehaviorExtension())); std::cout.flush(); CppPlatform::Get().AddExtension(std::shared_ptr<ExtensionBase>(CreateGDCppPhysicsBehaviorExtension())); std::cout.flush(); CppPlatform::Get().AddExtension(std::shared_ptr<ExtensionBase>(CreateGDCppTileMapObjectExtension())); std::cout.flush(); CppPlatform::Get().AddExtension(std::shared_ptr<ExtensionBase>(CreateGDCppTiledSpriteObjectExtension())); std::cout.flush(); CppPlatform::Get().AddExtension(std::shared_ptr<ExtensionBase>(CreateGDCppPlatformBehaviorExtension())); std::cout.flush(); CppPlatform::Get().AddExtension(std::shared_ptr<ExtensionBase>(CreateGDCppPanelSpriteObjectExtension())); std::cout.flush(); GDLogBanner(); gd::Project game; gd::String json = gd::ResourcesLoader::Get()->LoadPlainText("gd-project.json"); SerializerElement rootElement = Serializer::FromJSON(json); game.UnserializeFrom(rootElement); RuntimeGame runtimeGame; runtimeGame.LoadFromProject(game); bool abort = false; SceneStack sceneStack(runtimeGame, &window); sceneStack.OnError([&abort](gd::String error) { std::cout << error << std::endl; abort = true; }); sceneStack.OnLoadScene([](std::shared_ptr<RuntimeScene> scene) { int (*function)(RuntimeContext*) = nullptr; /* GDCPP_EVENTS_ASSIGNMENTS */ if (function) { scene->GetCodeExecutionEngine()->LoadFunction(function); return true; } return false; }); sceneStack.Push(game.GetLayout(0).GetName()); while (sceneStack.Step() && !abort) ; std::cout << "Exiting game" << std::endl; }
void TileSet::LoadResources(RuntimeGame &game) { m_tilesetTexture = game.GetImageManager()->GetSFMLTexture(textureName); }
int main( int argc, char *p_argv[] ) { GDLogBanner(); //Get executable location gd::String fullExecutablePath; if ( *p_argv[0] != '/' ) { fullExecutablePath += GetCurrentWorkingDirectory(); fullExecutablePath += "/"; } #ifndef WINDOWS fullExecutablePath += p_argv[0]; #endif gd::String executablePath = fullExecutablePath.substr( 0, fullExecutablePath.find_last_of( "/" ) ); gd::String executableFilename = fullExecutablePath.find_last_of( "/" ) < fullExecutablePath.length() ? fullExecutablePath.substr( fullExecutablePath.find_last_of( "/" ), fullExecutablePath.length() ) : ""; gd::String executableNameOnly = executableFilename.substr(0, executableFilename.length()-4); #ifdef WINDOWS gd::String codeFileExtension = "dll"; #elif defined(LINUX) gd::String codeFileExtension = "so"; chdir( executablePath.c_str() ); //For linux, make the executable dir the current working directory #elif defined(MACOS) gd::String codeFileExtension = "dylib"; #else #error Please update this part to support your target system. #endif //Check GDCpp version CompilationChecker::EnsureCorrectGDVersion(); //Load extensions gd::ExtensionsLoader::LoadAllExtensions(".", CppPlatform::Get()); gd::ExtensionsLoader::ExtensionsLoadingDone("."); //Load resource file gd::ResourcesLoader * resLoader = gd::ResourcesLoader::Get(); if (!resLoader->SetResourceFile( executablePath+"/"+executableNameOnly+".egd" ) && !resLoader->SetResourceFile( executableNameOnly+".egd" ) && !resLoader->SetResourceFile( executablePath+"/gam.egd" ) && !resLoader->SetResourceFile( "gam.egd" ) ) { return DisplayMessage("Unable to load resources. Aborting."); } gd::Project game; //Load game data { cout << "Getting src file size..." << endl; int fsize = resLoader->GetBinaryFileSize( "src" ); // round up (ignore pad for here) int size = (fsize+15)&(~15); cout << "Getting src raw data..." << endl; char * ibuffer = resLoader->LoadBinaryFile( "src" ); char * obuffer = new char[size]; unsigned char key[] = "-P:j$4t&OHIUVM/Z+u4DeDP."; const unsigned char iv[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }; aes_ks_t keySetting; aes_setks_decrypt(key, 192, &keySetting); aes_cbc_decrypt(reinterpret_cast<const unsigned char*>(ibuffer), reinterpret_cast<unsigned char*>(obuffer), (uint8_t*)iv, size/AES_BLOCK_SIZE, &keySetting); std::string uncryptedSrc = std::string(obuffer, size); delete [] obuffer; cout << "Loading game data..." << endl; TiXmlDocument doc; if ( !doc.Parse(uncryptedSrc.c_str()) ) { return DisplayMessage("Unable to parse game data. Aborting."); } TiXmlHandle hdl(&doc); gd::SerializerElement rootElement; gd::Serializer::FromXML(rootElement, hdl.FirstChildElement().Element()); game.UnserializeFrom(rootElement); } if ( game.GetLayoutsCount() == 0 ) return DisplayMessage("No scene to be loaded. Aborting."); //Loading the code Handle codeLibrary = NULL; gd::String codeLibraryName; auto loadLibrary = [&codeLibraryName, &codeLibrary](gd::String path) { codeLibraryName = path; codeLibrary = gd::OpenLibrary(codeLibraryName.ToLocale().c_str()); return codeLibrary != NULL; }; if (!loadLibrary(executablePath+"/"+executableNameOnly+"."+codeFileExtension) && !loadLibrary(executableNameOnly+"."+codeFileExtension) && !loadLibrary(executablePath+"/Code."+codeFileExtension) && !loadLibrary("Code."+codeFileExtension)) { return DisplayMessage("Unable to load the execution engine for game. Aborting."); } #if defined(WINDOWS) //Handle special argument to change working directory if ( argc >= 2 && gd::String(p_argv[1]).size() > 5 && gd::String(p_argv[1]).substr(0, 5) == "-cwd=" ) { gd::String newWorkingDir = gd::String(p_argv[1]).substr(5, gd::String::npos); cout << "Changing working directory to " << newWorkingDir << endl; chdir(newWorkingDir.ToLocale().c_str()); } #endif //Initialize image manager and load always loaded images game.GetImageManager()->LoadPermanentImages(); //Create main window sf::RenderWindow window; RuntimeGame runtimeGame; runtimeGame.LoadFromProject(game); window.create(sf::VideoMode(game.GetMainWindowDefaultWidth(), game.GetMainWindowDefaultHeight(), 32), "", sf::Style::Close); window.setActive(true); //Game main loop bool abort = false; SceneStack sceneStack(runtimeGame, &window, codeLibraryName); sceneStack.OnError([&abort](gd::String error) { DisplayMessage(error); abort = true; }); sceneStack.Push(game.GetLayout(0).GetName()); while (sceneStack.Step() && !abort) ; runtimeGame.GetSoundManager().ClearAllSoundsAndMusics(); FontManager::Get()->DestroySingleton(); gd::CloseLibrary(codeLibrary); return EXIT_SUCCESS; }
int main( int argc, char *p_argv[] ) { GDLogBanner(); //Get executable location string fullExecutablePath; if ( *p_argv[0] != '/' ) { fullExecutablePath += GetCurrentWorkingDirectory(); fullExecutablePath += "/"; } #ifndef WINDOWS fullExecutablePath += p_argv[0]; #endif std::string executablePath = fullExecutablePath.substr( 0, fullExecutablePath.find_last_of( "/" ) ); std::string executableFilename = fullExecutablePath.find_last_of( "/" ) < fullExecutablePath.length() ? fullExecutablePath.substr( fullExecutablePath.find_last_of( "/" ), fullExecutablePath.length() ) : ""; std::string executableNameOnly = executableFilename.substr(0, executableFilename.length()-4); #ifdef WINDOWS std::string codeFileExtension = "dll"; #elif defined(LINUX) std::string codeFileExtension = "so"; chdir( executablePath.c_str() ); //For linux, make the executable dir the current working directory #elif defined(MAC) std::string codeFileExtension = "dylib"; #else #error Please update this part to support your target system. #endif //Check GDCpp version CompilationChecker::EnsureCorrectGDVersion(); //Load extensions gd::ExtensionsLoader::LoadAllExtensions(".", CppPlatform::Get()); gd::ExtensionsLoader::ExtensionsLoadingDone("."); //Load resource file gd::RessourcesLoader * resLoader = gd::RessourcesLoader::Get(); if (!resLoader->SetResourceFile( executablePath+"/"+executableNameOnly+".egd" ) && !resLoader->SetResourceFile( executableNameOnly+".egd" ) && !resLoader->SetResourceFile( executablePath+"/gam.egd" ) && !resLoader->SetResourceFile( "gam.egd" ) ) { return AbortWithMessage("Unable to load resources. Aborting."); } gd::Project game; //Load game data { cout << "Getting src file size..." << endl; int fsize = resLoader->GetBinaryFileSize( "src" ); // round up (ignore pad for here) int size = (fsize+15)&(~15); cout << "Getting src raw data..." << endl; char * ibuffer = resLoader->LoadBinaryFile( "src" ); char * obuffer = new char[size]; unsigned char key[] = "-P:j$4t&OHIUVM/Z+u4DeDP."; const unsigned char iv[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }; aes_ks_t keySetting; aes_setks_decrypt(key, 192, &keySetting); aes_cbc_decrypt(reinterpret_cast<const unsigned char*>(ibuffer), reinterpret_cast<unsigned char*>(obuffer), (uint8_t*)iv, size/AES_BLOCK_SIZE, &keySetting); string uncryptedSrc = obuffer; delete [] obuffer; cout << "Loading game data..." << endl; TiXmlDocument doc; if ( !doc.Parse(uncryptedSrc.c_str()) ) { return AbortWithMessage("Unable to parse game data. Aborting."); } TiXmlHandle hdl(&doc); gd::SerializerElement rootElement; gd::Serializer::FromXML(rootElement, hdl.FirstChildElement().Element()); game.UnserializeFrom(rootElement); } if ( game.GetLayoutsCount() == 0 ) return AbortWithMessage("No scene to be loaded. Aborting."); //Loading the code std::string codeLibraryName = executablePath+"/"+executableNameOnly+"."+codeFileExtension; Handle codeLibrary = gd::OpenLibrary(codeLibraryName.c_str()); if ( codeLibrary == NULL ) { codeLibraryName = executablePath+"/Code."+codeFileExtension; Handle codeLibrary = gd::OpenLibrary(codeLibraryName.c_str()); if ( codeLibrary == NULL ) { return AbortWithMessage("Unable to load the execution engine for game. Aborting."); } } #if defined(WINDOWS) //Handle special argument to change working directory if ( argc >= 2 && std::string(p_argv[1]).size() > 5 && std::string(p_argv[1]).substr(0, 5) == "-cwd=" ) { std::string newWorkingDir = std::string(p_argv[1]).substr(5, std::string::npos); cout << "Changing working directory to " << newWorkingDir << endl; chdir(newWorkingDir.c_str()); } #endif //Initialize image manager and load always loaded images game.GetImageManager()->LoadPermanentImages(); //Create main window sf::RenderWindow window; RuntimeGame runtimeGame; runtimeGame.LoadFromProject(game); RuntimeScene scenePlayed(&window, &runtimeGame); if ( !scenePlayed.LoadFromScene( game.GetLayout(0) ) ) return AbortWithMessage("Unable to load the first scene \"" + game.GetLayout(0).GetName() + "\". Aborting."); if (scenePlayed.GetCodeExecutionEngine() == boost::shared_ptr<CodeExecutionEngine>() || !scenePlayed.GetCodeExecutionEngine()->LoadFromDynamicLibrary(codeLibraryName, "GDSceneEvents"+gd::SceneNameMangler::GetMangledSceneName(scenePlayed.GetName())) ) { return AbortWithMessage("Unable to setup execution engine for scene \"" + game.GetLayout(0).GetName() + "\". Aborting."); } window.create( sf::VideoMode( game.GetMainWindowDefaultWidth(), game.GetMainWindowDefaultHeight(), 32 ), scenePlayed.GetWindowDefaultTitle(), sf::Style::Close ); window.setActive(true); window.setFramerateLimit( game.GetMaximumFPS() ); window.setVerticalSyncEnabled( game.IsVerticalSynchronizationEnabledByDefault() ); scenePlayed.ChangeRenderWindow(&window); //Game main loop while ( scenePlayed.running ) { int returnCode = scenePlayed.RenderAndStep(); if ( returnCode == -2 ) //Quit the game scenePlayed.running = false; else if ( returnCode != -1 && returnCode < game.GetLayoutsCount()) //Change the scene being played { RuntimeScene emptyScene(&window, &runtimeGame); scenePlayed = emptyScene; //Clear the scene if ( !scenePlayed.LoadFromScene( game.GetLayout(returnCode) ) ) return AbortWithMessage("Unable to load scene \"" + game.GetLayout(returnCode).GetName() + "\". Aborting."); if (!scenePlayed.GetCodeExecutionEngine()->LoadFromDynamicLibrary(codeLibraryName, "GDSceneEvents"+gd::SceneNameMangler::GetMangledSceneName(scenePlayed.GetName()))) { return AbortWithMessage("Unable to setup execution engine for scene \"" + scenePlayed.GetName() + "\". Aborting."); } } } SoundManager::Get()->DestroySingleton(); FontManager::Get()->DestroySingleton(); gd::CloseLibrary(codeLibrary); return EXIT_SUCCESS; }