openttd.c
changeset 2380 392bba57462d
parent 2360 4e4ebe18e448
child 2383 7589e75b873a
equal deleted inserted replaced
2379:3ef366cf3493 2380:392bba57462d
   540 	UnInitializeGame();
   540 	UnInitializeGame();
   541 
   541 
   542 	return 0;
   542 	return 0;
   543 }
   543 }
   544 
   544 
       
   545 /** Mutex so that only one thread can communicate with the main program
       
   546  * at any given time */
       
   547 static ThreadMsg _message = 0;
       
   548 
       
   549 static inline void OTTD_ReleaseMutex(void) {_message = 0;}
       
   550 static inline ThreadMsg OTTD_PollThreadEvent(void) {return _message;}
       
   551 
       
   552 /** Called by running thread to execute some action in the main game.
       
   553  * It will stall as long as the mutex is not freed (handled) by the game */
       
   554 void OTTD_SendThreadMessage(ThreadMsg msg)
       
   555 {
       
   556 	while (_message != 0) CSleep(10);
       
   557 
       
   558 	_message = msg;
       
   559 }
       
   560 
       
   561 
       
   562 /** Handle the user-messages sent to us
       
   563  * @param message message sent
       
   564  */
       
   565 void ProcessSentMessage(ThreadMsg message)
       
   566 {
       
   567 	switch (message) {
       
   568 		case MSG_OTTD_SAVETHREAD_START: SaveFileStart(); break;
       
   569 		case MSG_OTTD_SAVETHREAD_DONE:  SaveFileDone(); break;
       
   570 		case MSG_OTTD_SAVETHREAD_ERROR: SaveFileError(); break;
       
   571 		default: NOT_REACHED();
       
   572 	}
       
   573 
       
   574 	OTTD_ReleaseMutex(); // release mutex so that other threads, messages can be handled
       
   575 }
       
   576 
   545 static void ShowScreenshotResult(bool b)
   577 static void ShowScreenshotResult(bool b)
   546 {
   578 {
   547 	if (b) {
   579 	if (b) {
   548 		SetDParamStr(0, _screenshot_name);
   580 		SetDParamStr(0, _screenshot_name);
   549 		ShowErrorMessage(INVALID_STRING_ID, STR_031B_SCREENSHOT_SUCCESSFULLY, 0, 0);
   581 		ShowErrorMessage(INVALID_STRING_ID, STR_031B_SCREENSHOT_SUCCESSFULLY, 0, 0);
   912 }
   944 }
   913 
   945 
   914 void GameLoop(void)
   946 void GameLoop(void)
   915 {
   947 {
   916 	int m;
   948 	int m;
       
   949 	ThreadMsg message;
       
   950 
       
   951 
       
   952 	if ((message = OTTD_PollThreadEvent()) != 0) ProcessSentMessage(message);
   917 
   953 
   918 	// autosave game?
   954 	// autosave game?
   919 	if (_do_autosave) {
   955 	if (_do_autosave) {
   920 		_do_autosave = false;
   956 		_do_autosave = false;
   921 		DoAutosave();
   957 		DoAutosave();