Changeset - r21263:87b7f54217b8
[Not reviewed]
master
0 1 0
frosch - 10 years ago 2014-02-16 21:57:22
frosch@openttd.org
(svn r26351) -Fix: Protect all VideoDriver_SDL methods with the _draw_mutex.
1 file changed with 13 insertions and 6 deletions:
0 comments (0 inline, 0 general)
src/video/sdl_v.cpp
Show inline comments
 
@@ -724,120 +724,127 @@ void VideoDriver_SDL::MainLoop()
 
#if SDL_VERSION_ATLEAST(1, 3, 0)
 
		keys = SDL_CALL SDL_GetKeyboardState(&numkeys);
 
#else
 
		keys = SDL_CALL SDL_GetKeyState(&numkeys);
 
#endif
 
#if defined(_DEBUG)
 
		if (_shift_pressed)
 
#else
 
		/* Speedup when pressing tab, except when using ALT+TAB
 
		 * to switch to another application */
 
#if SDL_VERSION_ATLEAST(1, 3, 0)
 
		if (keys[SDL_SCANCODE_TAB] && (mod & KMOD_ALT) == 0)
 
#else
 
		if (keys[SDLK_TAB] && (mod & KMOD_ALT) == 0)
 
#endif /* SDL_VERSION_ATLEAST(1, 3, 0) */
 
#endif /* defined(_DEBUG) */
 
		{
 
			if (!_networking && _game_mode != GM_MENU) _fast_forward |= 2;
 
		} else if (_fast_forward & 2) {
 
			_fast_forward = 0;
 
		}
 

	
 
		cur_ticks = SDL_CALL SDL_GetTicks();
 
		if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode) || cur_ticks < prev_cur_ticks) {
 
			_realtime_tick += cur_ticks - last_cur_ticks;
 
			last_cur_ticks = cur_ticks;
 
			next_tick = cur_ticks + MILLISECONDS_PER_TICK;
 

	
 
			bool old_ctrl_pressed = _ctrl_pressed;
 

	
 
			_ctrl_pressed  = !!(mod & KMOD_CTRL);
 
			_shift_pressed = !!(mod & KMOD_SHIFT);
 

	
 
			/* determine which directional keys are down */
 
			_dirkeys =
 
#if SDL_VERSION_ATLEAST(1, 3, 0)
 
				(keys[SDL_SCANCODE_LEFT]  ? 1 : 0) |
 
				(keys[SDL_SCANCODE_UP]    ? 2 : 0) |
 
				(keys[SDL_SCANCODE_RIGHT] ? 4 : 0) |
 
				(keys[SDL_SCANCODE_DOWN]  ? 8 : 0);
 
#else
 
				(keys[SDLK_LEFT]  ? 1 : 0) |
 
				(keys[SDLK_UP]    ? 2 : 0) |
 
				(keys[SDLK_RIGHT] ? 4 : 0) |
 
				(keys[SDLK_DOWN]  ? 8 : 0);
 
#endif
 
			if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
 

	
 
			/* The gameloop is the part that can run asynchronously. The rest
 
			 * except sleeping can't. */
 
			if (_draw_mutex != NULL) _draw_mutex->EndCritical();
 

	
 
			GameLoop();
 

	
 
			if (_draw_mutex != NULL) _draw_mutex->BeginCritical();
 

	
 
			UpdateWindows();
 
			_local_palette = _cur_palette;
 
		} else {
 
			/* Release the thread while sleeping */
 
			if (_draw_mutex != NULL) _draw_mutex->EndCritical();
 
			CSleep(1);
 
			if (_draw_mutex != NULL) _draw_mutex->BeginCritical();
 

	
 
			NetworkDrawChatMessage();
 
			DrawMouseCursor();
 
		}
 

	
 
		/* End of the critical part. */
 
		if (_draw_mutex != NULL && !HasModalProgress()) {
 
			_draw_mutex->SendSignal();
 
		} else {
 
			/* Oh, we didn't have threads, then just draw unthreaded */
 
			CheckPaletteAnim();
 
			DrawSurfaceToScreen();
 
		}
 
	}
 

	
 
	if (_draw_mutex != NULL) {
 
		_draw_continue = false;
 
		/* Sending signal if there is no thread blocked
 
		 * is very valid and results in noop */
 
		_draw_mutex->SendSignal();
 
		_draw_mutex->EndCritical();
 
		_draw_thread->Join();
 

	
 
		delete _draw_mutex;
 
		delete _draw_thread;
 

	
 
		_draw_mutex = NULL;
 
		_draw_thread = NULL;
 
	}
 
}
 

	
 
bool VideoDriver_SDL::ChangeResolution(int w, int h)
 
{
 
	if (_draw_mutex != NULL) _draw_mutex->BeginCritical();
 
	if (_draw_mutex != NULL) _draw_mutex->BeginCritical(true);
 
	bool ret = CreateMainSurface(w, h);
 
	if (_draw_mutex != NULL) _draw_mutex->EndCritical();
 
	if (_draw_mutex != NULL) _draw_mutex->EndCritical(true);
 
	return ret;
 
}
 

	
 
bool VideoDriver_SDL::ToggleFullscreen(bool fullscreen)
 
{
 
	if (_draw_mutex != NULL) _draw_mutex->BeginCritical(true);
 
	_fullscreen = fullscreen;
 
	GetVideoModes(); // get the list of available video modes
 
	if (_num_resolutions == 0 || !CreateMainSurface(_cur_resolution.width, _cur_resolution.height)) {
 
	bool ret = _num_resolutions != 0 && CreateMainSurface(_cur_resolution.width, _cur_resolution.height);
 

	
 
	if (!ret) {
 
		/* switching resolution failed, put back full_screen to original status */
 
		_fullscreen ^= true;
 
		return false;
 
	}
 
	return true;
 

	
 
	if (_draw_mutex != NULL) _draw_mutex->EndCritical(true);
 
	return ret;
 
}
 

	
 
bool VideoDriver_SDL::AfterBlitterChange()
 
{
 
	return CreateMainSurface(_screen.width, _screen.height);
 
	if (_draw_mutex != NULL) _draw_mutex->BeginCritical(true);
 
	bool ret = CreateMainSurface(_screen.width, _screen.height);
 
	if (_draw_mutex != NULL) _draw_mutex->EndCritical(true);
 
	return ret;
 
}
 

	
 
#endif /* WITH_SDL */
0 comments (0 inline, 0 general)