Пример #1
0
/**
 * Deallocates and shuts down game
 */
void SpringApp::Shutdown()
{
	if (gu) gu->globalQuit = true;

#define DeleteAndNull(x) delete x; x = NULL;

	GML::Exit();
	DeleteAndNull(pregame);
	DeleteAndNull(game);
	DeleteAndNull(gameServer);
	DeleteAndNull(gameSetup);
	CLoadScreen::DeleteInstance();
	ISound::Shutdown();
	DeleteAndNull(font);
	DeleteAndNull(smallFont);
	CNamedTextures::Kill();
	GLContext::Free();
	GlobalConfig::Deallocate();
	ConfigHandler::Deallocate();
	UnloadExtensions();

	IMouseInput::FreeInstance(mouseInput);
	KeyInput::FreeInstance(keyInput);

	SDL_WM_GrabInput(SDL_GRAB_OFF);
#if !defined(HEADLESS)
	SDL_QuitSubSystem(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_JOYSTICK);
#endif
	SDL_Quit();

	DeleteAndNull(gs);
	DeleteAndNull(gu);
	DeleteAndNull(globalRendering);
	DeleteAndNull(startsetup);

	FileSystemInitializer::Cleanup();

	Watchdog::Uninstall();

#ifdef USE_MMGR
	m_dumpMemoryReport();
#endif
}
Пример #2
0
/**
 * Deallocates and shuts down game
 */
void SpringApp::Shutdown()
{
    if (pregame)
        delete pregame;			//in case we exit during init
    if (game)
        delete game;
    if (gameSetup)
        delete gameSetup;
    delete font;
    ConfigHandler::Deallocate();
    UnloadExtensions();
    SDL_WM_GrabInput(SDL_GRAB_OFF);
    SDL_Quit();
    delete gs;
    delete gu;
    END_SYNCIFY;
#ifdef USE_MMGR
    m_dumpMemoryReport();
#endif
}
Пример #3
0
int main( int argc, char *argv[ ], char *envp[ ] )
#endif
{
#ifndef _WIN32
	chdir(SPRING_DATADIR);
#endif
	INIT_SYNCIFY;
	bool	done=false;
	BaseCmd *cmdline = BaseCmd::initialize(argc,argv);
	cmdline->addoption('f',"fullscreen",OPTPARM_NONE,"","Run in fullscreen mode");
	cmdline->addoption('w',"window",OPTPARM_NONE,"","Run in windowed mode");
	cmdline->addoption('s',"server",OPTPARM_NONE,"","Run as a server");
	cmdline->addoption('c',"client",OPTPARM_NONE,"","Run as a client");
//	cmdline->addoption('g',"runscript",OPTPARM_STRING,"script.txt", "Run with a game setup script");
	cmdline->parse();
#ifdef _DEBUG
	fullscreen = false;
#else
	fullscreen = configHandler.GetInt("Fullscreen",1)!=0;
#endif
	Uint8 scrollWheelSpeed = configHandler.GetInt("ScrollWheelSpeed",25);

	if (cmdline->result("help")) {
		cmdline->usage("TA:Spring",VERSION_STRING);
		delete cmdline;
		return 0;
	} else if (cmdline->result("version")) {
		std::cout << "TA:Spring " << VERSION_STRING << std::endl;
		delete cmdline;
		return 0;
	} else if (cmdline->result("window"))
		fullscreen = false;
	else if (cmdline->result("fullscreen"))
		fullscreen = true;
#ifdef _WIN32
	// Initialize crash reporting
	Install( (LPGETLOGFILE) crashCallback, "*****@*****.**", "TA Spring Crashreport");
#endif
	// Initialize class system
	creg::ClassBinder::InitializeClasses ();

	// Global structures
	ENTER_SYNCED;
	gs=new CGlobalSyncedStuff();
	ENTER_UNSYNCED;
	gu=new CGlobalUnsyncedStuff();


#ifndef NO_LUA
	// Initialize lua bindings
	CLuaBinder lua;
	if (!lua.LoadScript("testscript.lua")) 
		handleerror(NULL, lua.lastError.c_str(), "lua",MBF_OK|MBF_EXCL);
#endif

	// Check if the commandline parameter is specifying a demo file
	bool playDemo = false;
	string demofile, startscript;

#ifdef _WIN32
	string command(argv[0]);
	int idx = command.rfind("spring");
	string path = command.substr(0,idx);
	if (path.at(0) == '"')
		path.append(1,'"');
	if (path != "")
		_chdir(path.c_str());
#endif

	for (int i = 1; i < argc; i++) {
		if (argv[i][0] != '-') {
			string command(argv[i]);
			int idx = command.rfind("sdf");
			if (idx == command.size()-3) {
				playDemo = true;
				demofile = command;
			} else {
				playDemo = false;
				startscript = command;
			}
		}
	}

	// Create the archive scanner and vfs handler
	archiveScanner = new CArchiveScanner();
	archiveScanner->ReadCacheData();
	archiveScanner->Scan("./maps");
	archiveScanner->Scan("./base");
	archiveScanner->Scan("./mods");
	archiveScanner->WriteCacheData();
	hpiHandler = new CVFSHandler();

	palette.Init();

	ENTER_SYNCED;
	if (!playDemo) {
		gameSetup=new CGameSetup();
		if(!gameSetup->Init(startscript)){
			delete gameSetup;
			gameSetup=0;
		}
	}

	ENTER_MIXED;

	bool server = true;
	if (playDemo)
		server = false;
	else if(gameSetup)
		server=gameSetup->myPlayer-gameSetup->numDemoPlayers == 0;
	else
		server=!cmdline->result("client") || cmdline->result("server");
	
	if (
		!glwindow (
			"RtsSpring",
			configHandler.GetInt("XResolution",XRES_DEFAULT),
			configHandler.GetInt("YResolution",YRES_DEFAULT),
			0,
			fullscreen,
			configHandler.GetInt("DisplayFrequency",0)
		)
	) {
		SDL_Quit();
		return 0;
	}

	SDL_EnableKeyRepeat (SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);

	font = new CglFont(32,223);
	LoadExtensions();
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	SDL_GL_SwapBuffers();
	
	if (playDemo)
		pregame = new CPreGame(false, demofile);
	else
		pregame = new CPreGame(server, "");

#ifdef NEW_GUI
	guicontroller = new GUIcontroller();
#endif

#ifndef NO_LUA
	lua.CreateLateBindings();
#endif

	keys = new Uint8[SDLK_LAST];

	SDL_Event event;
	while (!done) {
		ENTER_UNSYNCED;
		while (SDL_PollEvent(&event)) {
			switch (event.type) {
				case SDL_VIDEORESIZE:
					screen = SDL_SetVideoMode(event.resize.w,event.resize.h,0,SDL_OPENGL|SDL_RESIZABLE|SDL_HWSURFACE|SDL_DOUBLEBUF);
					if (screen)
						resizescene(screen->w,screen->h);
					break;
				case SDL_QUIT:
					done = true;
					break;
				case SDL_MOUSEMOTION:
					if(mouse)
						mouse->MouseMove(event.motion.x,event.motion.y);
					break;
				case SDL_MOUSEBUTTONDOWN:
					if (mouse) {
						if (event.button.button == SDL_BUTTON_WHEELUP)
							mouse->currentCamController->MouseWheelMove(scrollWheelSpeed);
						else if (event.button.button == SDL_BUTTON_WHEELDOWN)
							mouse->currentCamController->MouseWheelMove(-scrollWheelSpeed);
						mouse->MousePress(event.button.x,event.button.y,event.button.button);
					}
					break;
				case SDL_MOUSEBUTTONUP:
					if (mouse)
						mouse->MouseRelease(event.button.x,event.button.y,event.button.button);
					break;
				case SDL_KEYDOWN:
				{
					int i = event.key.keysym.sym;
				
					update_sdlkeys(keys);
					update_sdlmods(keys);
					if (i == SDLK_RETURN)
						keys[i] = 1;
					
					if(activeController) {
						activeController->KeyPressed(i,1);
#ifndef NEW_GUI
						if(activeController->userWriting){ 
							i = event.key.keysym.unicode;
							if (i >= SDLK_SPACE && i <= SDLK_DELETE)
								if(activeController->ignoreNextChar || activeController->ignoreChar==char(i))
									activeController->ignoreNextChar=false;
								else
									activeController->userInput+=char(i);
						}
#endif
					}
#ifdef NEW_GUI
					i = event.key.keysym.unicode;
					if (i > SDLK_FIRST && i <= SDLK_DELETE) /* HACK */
						GUIcontroller::Character(char(i));
						
#endif
					break;
				}
				case SDL_KEYUP:
				{
					int i = event.key.keysym.sym;

					update_sdlkeys(keys);
					update_sdlmods(keys);
					if (i == SDLK_RETURN)
						keys[i] = 0;
					
					if (activeController)
						activeController->KeyReleased(i);
					
					break;
				}
			}
		}
		if (FSAA)
			glEnable(GL_MULTISAMPLE_ARB);
		int ret = drawscene();
		SDL_GL_SwapBuffers();
		if (FSAA)
			glDisable(GL_MULTISAMPLE_ARB);
		if (globalQuit || (active && !ret))
			done=true;
	}
	ENTER_MIXED;

	delete[] keys;

	// Shutdown
	if (pregame)
		delete pregame;			//in case we exit during init
	if (game)
		delete game;
	if (gameSetup)
		delete gameSetup;
	delete font;
	ConfigHandler::Deallocate();
	UnloadExtensions();
#ifndef DEBUG
	SDL_WM_GrabInput(SDL_GRAB_OFF);
#endif
	SDL_Quit();
	delete gs;
	delete gu;
	END_SYNCIFY;
#ifdef USE_MMGR
	m_dumpMemoryReport();
#endif
	delete cmdline;
	return 0;
}
Пример #4
0
/**
 * Deallocates and shuts down game
 */
void SpringApp::ShutDown()
{
	// if a thread crashes *during* first SA::Run-->SA::ShutDown
	// then main::Run will call us again via ErrorMessageBox (not
	// a good idea)
	static unsigned int numCalls = 0;

	if ((numCalls++) != 0)
		return;
	if (gu != NULL)
		gu->globalQuit = true;

	LOG("[SpringApp::%s][1]", __FUNCTION__);
	ThreadPool::SetThreadCount(0);
	LOG("[SpringApp::%s][2]", __FUNCTION__);

	game->KillLua(); // must be called before `game` var gets nulled, else stuff in LuaSyncedRead.cpp will fail
	SafeDelete(game);
	SafeDelete(pregame);

	agui::FreeGui();

	LOG("[SpringApp::%s][3]", __FUNCTION__);
	SafeDelete(clientNet);
	SafeDelete(gameServer);
	SafeDelete(gameSetup);

	LOG("[SpringApp::%s][4]", __FUNCTION__);
	CLoadScreen::DeleteInstance();
	ISound::Shutdown();
	FreeJoystick();

	LOG("[SpringApp::%s][5]", __FUNCTION__);
	SafeDelete(font);
	SafeDelete(smallFont);
	CNamedTextures::Kill();
	GLContext::Free();
	GlobalConfig::Deallocate();
	UnloadExtensions();

	IMouseInput::FreeInstance(mouseInput);

	LOG("[SpringApp::%s][6]", __FUNCTION__);
	SDL_SetWindowGrab(window, SDL_FALSE);
	WindowManagerHelper::FreeIcon();
#if !defined(HEADLESS)
	SDL_GL_DeleteContext(sdlGlCtx);
	SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
#endif
	SDL_Quit();

	LOG("[SpringApp::%s][7]", __FUNCTION__);
	SafeDelete(gs);
	SafeDelete(gu);
	SafeDelete(globalRendering);
	SafeDelete(luaSocketRestrictions);

	FileSystemInitializer::Cleanup();

	LOG("[SpringApp::%s][8]", __FUNCTION__);
	Watchdog::Uninstall();
	LOG("[SpringApp::%s][9]", __FUNCTION__);
}
Пример #5
0
int WINAPI WinMain(	HINSTANCE	hInstanceIn,			// Instance
									 HINSTANCE	hPrevInstance,		// Previous Instance
									 LPSTR		lpCmdLine,			// Command Line Parameters
									 int			nCmdShow)			// Window Show State
{
	INIT_SYNCIFY;

	MSG		msg;									// Windows Message Structure
	BOOL	done=FALSE;								// Bool Variable To Exit Loop
	hInstance=hInstanceIn;

	for(int b=0;b<256;b++)
		keys[b]=false;

	// Initialize crash reporting
	Install(crashCallback, "*****@*****.**", "TA Spring Crashreport");

	ENTER_SYNCED;
	gs=new CGlobalSyncedStuff();
	ENTER_UNSYNCED;
	gu=new CGlobalUnsyncedStuff();

	// Initialize lua bindings
	CLuaBinder lua;
	if (!lua.LoadScript("testscript.lua")) 
		MessageBox(NULL, lua.lastError.c_str(), "lua",MB_YESNO|MB_ICONQUESTION);

	// Check if the commandline parameter is specifying a demo file
	bool playDemo = false;
	string cmdline(lpCmdLine);
	for (string::size_type pos = cmdline.find("\""); pos != string::npos; pos = cmdline.find("\"")) 
		cmdline.erase(pos, 1);
	string cmdext = cmdline.substr(cmdline.find_last_of('.') + 1);
	transform(cmdext.begin(), cmdext.end(), cmdext.begin(), (int (*)(int))tolower);
	if (cmdext == "sdf") {
		playDemo = true;

		// Launching a demo through a file association will not start spring with a correct working directory
		// So we need to determine this from the commandline and change manually
		// It should look like "x:\path\path\spring.exe" demo.sdf

		string fullcmd(GetCommandLine());
		string executable;
		if (fullcmd[0] == '"')
			executable = fullcmd.substr(1, fullcmd.find('"', 1));
		else 
			executable = fullcmd.substr(0, fullcmd.find(' '));

		string path = executable.substr(0, executable.find_last_of('\\'));
		if (path != "")
			_chdir(path.c_str()); 

		//MessageBox(NULL,path.c_str(), "Path",MB_YESNO|MB_ICONQUESTION);
	}

	// Create the archive scanner and vfs handler
	archiveScanner = new CArchiveScanner();
	archiveScanner->ReadCacheData();
	archiveScanner->Scan("./maps");
	archiveScanner->Scan("./base");
	archiveScanner->Scan("./mods");
	archiveScanner->WriteCacheData();
	hpiHandler = new CVFSHandler();

	ENTER_SYNCED;
	if (!playDemo) {
		gameSetup=new CGameSetup();
		if(!gameSetup->Init(cmdline)){
			delete gameSetup;
			gameSetup=0;
		}
	}
	ENTER_MIXED;

	bool server;
	if (playDemo)
		server = false;
	else if(gameSetup)
		server=gameSetup->myPlayer-gameSetup->numDemoPlayers == 0;
	else
		server=MessageBox(NULL,"Do you want to be server?", "Be server?",MB_YESNO|MB_ICONQUESTION)==IDYES;

	/*	// Ask The User Which Screen Mode They Prefer
	if (MessageBox(NULL,"Would You Like To Run In Fullscreen Mode?", "Start FullScreen?",MB_YESNO|MB_ICONQUESTION)==IDNO)
	{
	fullscreen=FALSE;							// Windowed Mode
	}*/
	fullscreen=regHandler.GetInt("Fullscreen",1)!=0;
	
#ifdef _DEBUG
	fullscreen=false;
#endif;

	int xres=1024;
	int yres=768;
	xres=regHandler.GetInt("XResolution",xres);
	yres=regHandler.GetInt("YResolution",yres);

	int frequency=regHandler.GetInt("DisplayFrequency",0);
	// Create Our OpenGL Window
	if (!CreateGLWindow("RtsSpring",xres,yres,32,fullscreen,frequency))
	{
		return 0;									// Quit If Window Was Not Created
	}

	font=new CglFont(hDC,32,223);
	LoadExtensions();
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear Screen And Depth Buffer

	SwapBuffers(hDC);					// Swap Buffers (Double Buffering)
	if (playDemo)
		pregame = new CPreGame(false, cmdline);
	else
		pregame=new CPreGame(server, "");

	#ifdef NEW_GUI
	guicontroller = new GUIcontroller();
	#endif

	lua.CreateLateBindings();

	while(!done)									// Loop That Runs While done=FALSE
	{
		ENTER_UNSYNCED;
		if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))	// Is There A Message Waiting?
		{
			if (msg.message==WM_QUIT)				// Have We Received A Quit Message?
			{
				done=TRUE;							// If So done=TRUE
			}
			else									// If Not, Deal With Window Messages
			{
				TranslateMessage(&msg);				// Translate The Message
				DispatchMessage(&msg);				// Dispatch The Message
			}
		}
		else										// If There Are No Messages
		{
			// Draw The Scene.  Watch For ESC Key And Quit Messages From DrawGLScene()
			if ((active && !DrawGLScene()) || globalQuit)	// Active?  Was There A Quit Received?
			{
				done=TRUE;							// ESC or DrawGLScene Signalled A Quit
			}
			else									// Not Time To Quit, Update Screen
			{
				SwapBuffers(hDC);					// Swap Buffers (Double Buffering)
				SleepEx(0,true);
			}
		}
	}

	ENTER_MIXED;

	// Shutdown
	delete gameSetup;
	delete pregame;								//in case we exit during init
	delete game;
	delete font;
	RegHandler::Deallocate();
	UnloadExtensions();
	KillGLWindow();									// Kill The Window
	delete gs;
	delete gu;
	END_SYNCIFY;
	//m_dumpMemoryReport();
	return (msg.wParam);							// Exit The Program
}