Changeset - r24939:f00f6879a003
[Not reviewed]
master
0 12 0
Patric Stout - 4 years ago 2021-02-24 13:45:10
truebrain@openttd.org
Codechange: [Video] make the prototype of PollEvent() the same for all drivers

Additionally, call it from the draw-tick.
12 files changed with 36 insertions and 23 deletions:
0 comments (0 inline, 0 general)
src/video/allegro_v.cpp
Show inline comments
 
@@ -326,13 +326,13 @@ static uint32 ConvertAllegroKeyIntoMy(WC
 
	return key;
 
}
 

	
 
static const uint LEFT_BUTTON  = 0;
 
static const uint RIGHT_BUTTON = 1;
 

	
 
static void PollEvent()
 
bool VideoDriver_Allegro::PollEvent()
 
{
 
	poll_mouse();
 

	
 
	bool mouse_action = false;
 

	
 
	/* Mouse buttons */
 
@@ -400,12 +400,14 @@ static void PollEvent()
 
		ToggleFullScreen(!_fullscreen);
 
	} else if (keypressed()) {
 
		WChar character;
 
		uint keycode = ConvertAllegroKeyIntoMy(&character);
 
		HandleKeypress(keycode, character);
 
	}
 

	
 
	return false;
 
}
 

	
 
/**
 
 * There are multiple modules that might be using Allegro and
 
 * Allegro can only be initiated once.
 
 */
 
@@ -479,13 +481,12 @@ void VideoDriver_Allegro::InputLoop()
 

	
 
void VideoDriver_Allegro::MainLoop()
 
{
 
	for (;;) {
 
		InteractiveRandom(); // randomness
 

	
 
		PollEvent();
 
		if (_exit_game) return;
 

	
 
		if (this->Tick()) {
 
			this->Paint();
 
		}
 
		this->SleepTillNextTick();
src/video/allegro_v.h
Show inline comments
 
@@ -34,12 +34,13 @@ public:
 
	const char *GetName() const override { return "allegro"; }
 

	
 
protected:
 
	void InputLoop() override;
 
	void Paint() override;
 
	void CheckPaletteAnim() override;
 
	bool PollEvent() override;
 
};
 

	
 
/** Factory for the allegro video driver. */
 
class FVideoDriver_Allegro : public DriverFactoryBase {
 
public:
 
	FVideoDriver_Allegro() : DriverFactoryBase(Driver::DT_VIDEO, 4, "allegro", "Allegro Video Driver") {}
src/video/cocoa/cocoa_v.h
Show inline comments
 
@@ -59,12 +59,13 @@ protected:
 

	
 
	Dimension GetScreenSize() const override;
 
	float GetDPIScale() override;
 
	void InputLoop() override;
 
	bool LockVideoBuffer() override;
 
	void UnlockVideoBuffer() override;
 
	bool PollEvent() override;
 

	
 
	void GameSizeChanged();
 

	
 
	const char *Initialize();
 

	
 
	void UpdateVideoModes();
 
@@ -76,14 +77,12 @@ protected:
 
	/** Get a pointer to the video buffer. */
 
	virtual void *GetVideoPointer() = 0;
 
	/** Hand video buffer back to the drawing backend. */
 
	virtual void ReleaseVideoPointer() {}
 

	
 
private:
 
	bool PollEvent();
 

	
 
	bool IsFullscreen();
 
};
 

	
 
class VideoDriver_CocoaQuartz : public VideoDriver_Cocoa {
 
private:
 
	int buffer_depth;     ///< Colour depth of used frame buffer
src/video/cocoa/cocoa_v.mm
Show inline comments
 
@@ -437,14 +437,12 @@ void VideoDriver_Cocoa::GameLoop()
 
{
 
	for (;;) {
 
		@autoreleasepool {
 

	
 
			InteractiveRandom(); // randomness
 

	
 
			while (this->PollEvent()) {}
 

	
 
			if (_exit_game) {
 
				/* Restore saved resolution if in fullscreen mode. */
 
				if (this->IsFullscreen()) _cur_resolution = this->orig_res;
 
				break;
 
			}
 

	
src/video/sdl2_v.cpp
Show inline comments
 
@@ -367,17 +367,17 @@ static uint ConvertSdlKeycodeIntoMy(SDL_
 
	SDL_Scancode sc = SDL_GetScancodeFromKey(kc);
 
	if (sc == SDL_SCANCODE_GRAVE) key = WKC_BACKQUOTE;
 

	
 
	return key;
 
}
 

	
 
int VideoDriver_SDL_Base::PollEvent()
 
bool VideoDriver_SDL_Base::PollEvent()
 
{
 
	SDL_Event ev;
 

	
 
	if (!SDL_PollEvent(&ev)) return -2;
 
	if (!SDL_PollEvent(&ev)) return false;
 

	
 
	switch (ev.type) {
 
		case SDL_MOUSEMOTION:
 
#ifdef __EMSCRIPTEN__
 
			if (_cursor_new_in_window) {
 
				/* The cursor just moved into the window; this means we don't
 
@@ -513,13 +513,14 @@ int VideoDriver_SDL_Base::PollEvent()
 
				UndrawMouseCursor();
 
				_cursor.in_window = false;
 
			}
 
			break;
 
		}
 
	}
 
	return -1;
 

	
 
	return true;
 
}
 

	
 
static const char *InitializeSDL()
 
{
 
	/* Explicitly disable hardware acceleration. Enabling this causes
 
	 * UpdateWindowSurface() to update the window's texture instead of
 
@@ -626,13 +627,12 @@ void VideoDriver_SDL_Base::InputLoop()
 
}
 

	
 
void VideoDriver_SDL_Base::LoopOnce()
 
{
 
	InteractiveRandom(); // randomness
 

	
 
	while (PollEvent() == -1) {}
 
	if (_exit_game) {
 
#ifdef __EMSCRIPTEN__
 
		/* Emscripten is event-driven, and as such the main loop is inside
 
		 * the browser. So if _exit_game goes true, the main loop ends (the
 
		 * cancel call), but we still have to call the cleanup that is
 
		 * normally done at the end of the main loop for non-Emscripten.
src/video/sdl2_v.h
Show inline comments
 
@@ -57,12 +57,13 @@ protected:
 

	
 
	Dimension GetScreenSize() const override;
 
	void InputLoop() override;
 
	bool LockVideoBuffer() override;
 
	void UnlockVideoBuffer() override;
 
	void CheckPaletteAnim() override;
 
	bool PollEvent() override;
 

	
 
	/** Indicate to the driver the client-side might have changed. */
 
	void ClientSizeChanged(int w, int h, bool force);
 

	
 
	/** (Re-)create the backing store. */
 
	virtual bool AllocateBackingStore(int w, int h, bool force = false) = 0;
 
@@ -71,13 +72,12 @@ protected:
 
	/** Hand video buffer back to the painting backend. */
 
	virtual void ReleaseVideoPointer() = 0;
 
	/** Create the main window. */
 
	virtual bool CreateMainWindow(uint w, uint h, uint flags = 0);
 

	
 
private:
 
	int PollEvent();
 
	void LoopOnce();
 
	void MainLoopCleanup();
 
	bool CreateMainSurface(uint w, uint h, bool resize);
 
	const char *Initialize();
 

	
 
#ifdef __EMSCRIPTEN__
src/video/sdl_v.cpp
Show inline comments
 
@@ -504,17 +504,17 @@ static uint ConvertSdlKeyIntoMy(SDL_keys
 
	if (sym->mod & KMOD_ALT)   key |= WKC_ALT;
 

	
 
	*character = sym->unicode;
 
	return key;
 
}
 

	
 
int VideoDriver_SDL::PollEvent()
 
bool VideoDriver_SDL::PollEvent()
 
{
 
	SDL_Event ev;
 

	
 
	if (!SDL_PollEvent(&ev)) return -2;
 
	if (!SDL_PollEvent(&ev)) return false;
 

	
 
	switch (ev.type) {
 
		case SDL_MOUSEMOTION:
 
			if (_cursor.UpdateCursorPosition(ev.motion.x, ev.motion.y, true)) {
 
				SDL_WarpMouse(_cursor.pos.x, _cursor.pos.y);
 
			}
 
@@ -595,13 +595,14 @@ int VideoDriver_SDL::PollEvent()
 
			 * that SDL 1.2 seems to do this automatically
 
			 * in most cases, but 1.3 / 2.0 does not. */
 
		        _num_dirty_rects = MAX_DIRTY_RECTS + 1;
 
			break;
 
		}
 
	}
 
	return -1;
 

	
 
	return true;
 
}
 

	
 
const char *VideoDriver_SDL::Start(const StringList &parm)
 
{
 
	char buf[30];
 
	_use_hwpalette = GetDriverParamInt(parm, "hw_palette", 2);
 
@@ -716,13 +717,12 @@ void VideoDriver_SDL::MainLoop()
 

	
 
	DEBUG(driver, 1, "SDL: using %sthreads", _draw_threaded ? "" : "no ");
 

	
 
	for (;;) {
 
		InteractiveRandom(); // randomness
 

	
 
		while (PollEvent() == -1) {}
 
		if (_exit_game) break;
 

	
 
		if (this->Tick()) {
 
			if (_draw_mutex != nullptr && !HasModalProgress()) {
 
				_draw_signal->notify_one();
 
			} else {
src/video/sdl_v.h
Show inline comments
 
@@ -40,18 +40,18 @@ public:
 
protected:
 
	void InputLoop() override;
 
	bool LockVideoBuffer() override;
 
	void UnlockVideoBuffer() override;
 
	void Paint() override;
 
	void PaintThread() override;
 
	void CheckPaletteAnim();
 
	void CheckPaletteAnim() override;
 
	bool PollEvent() override;
 

	
 
private:
 
	std::unique_lock<std::recursive_mutex> draw_lock;
 

	
 
	int PollEvent();
 
	bool CreateMainSurface(uint w, uint h);
 
	void SetupKeyboard();
 

	
 
	static void PaintThreadThunk(VideoDriver_SDL *drv);
 
};
 

	
src/video/video_driver.cpp
Show inline comments
 
@@ -52,12 +52,13 @@ bool VideoDriver::Tick()
 
	/* Prevent drawing when switching mode, as windows can be removed when they should still appear. */
 
	if (this->HasGUI() && cur_ticks >= this->next_draw_tick && (_switch_mode == SM_NONE || HasModalProgress())) {
 
		this->next_draw_tick += this->GetDrawInterval();
 
		/* Avoid next_draw_tick getting behind more and more if it cannot keep up. */
 
		if (this->next_draw_tick < cur_ticks - ALLOWED_DRIFT * this->GetDrawInterval()) this->next_draw_tick = cur_ticks;
 

	
 
		while (this->PollEvent()) {}
 
		this->InputLoop();
 
		::InputLoop();
 
		UpdateWindows();
 
		this->CheckPaletteAnim();
 

	
 
		return true;
src/video/video_driver.hpp
Show inline comments
 
@@ -247,12 +247,18 @@ protected:
 
	/**
 
	 * Process any pending palette animation.
 
	 */
 
	virtual void CheckPaletteAnim() {}
 

	
 
	/**
 
	 * Process a single system event.
 
	 * @returns False if there are no more events to process.
 
	 */
 
	virtual bool PollEvent() { return false; };
 

	
 
	/**
 
	 * Run the game for a single tick, processing boththe game-tick and draw-tick.
 
	 * @returns True if the driver should redraw the screen.
 
	 */
 
	bool Tick();
 

	
 
	/**
src/video/win32_v.cpp
Show inline comments
 
@@ -854,16 +854,27 @@ void VideoDriver_Win32Base::InputLoop()
 
		_dirkeys = 0;
 
	}
 

	
 
	if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
 
}
 

	
 
void VideoDriver_Win32Base::MainLoop()
 
bool VideoDriver_Win32Base::PollEvent()
 
{
 
	MSG mesg;
 

	
 
	if (!PeekMessage(&mesg, nullptr, 0, 0, PM_REMOVE)) return false;
 

	
 
	/* Convert key messages to char messages if we want text input. */
 
	if (EditBoxInGlobalFocus()) TranslateMessage(&mesg);
 
	DispatchMessage(&mesg);
 

	
 
	return true;
 
}
 

	
 
void VideoDriver_Win32Base::MainLoop()
 
{
 
	std::thread draw_thread;
 

	
 
	if (this->draw_threaded) {
 
		/* Initialise the mutex first, because that's the thing we *need*
 
		 * directly in the newly created thread. */
 
		try {
 
@@ -895,17 +906,12 @@ void VideoDriver_Win32Base::MainLoop()
 
		}
 
	}
 

	
 
	for (;;) {
 
		InteractiveRandom(); // randomness
 

	
 
		while (PeekMessage(&mesg, nullptr, 0, 0, PM_REMOVE)) {
 
			/* Convert key messages to char messages if we want text input. */
 
			if (EditBoxInGlobalFocus()) TranslateMessage(&mesg);
 
			DispatchMessage(&mesg);
 
		}
 
		if (_exit_game) break;
 

	
 
		/* Flush GDI buffer to ensure we don't conflict with the drawing thread. */
 
		GdiFlush();
 

	
 
		if (this->Tick()) {
src/video/win32_v.h
Show inline comments
 
@@ -57,12 +57,13 @@ protected:
 
	Dimension GetScreenSize() const override;
 
	float GetDPIScale() override;
 
	void InputLoop() override;
 
	bool LockVideoBuffer() override;
 
	void UnlockVideoBuffer() override;
 
	void CheckPaletteAnim() override;
 
	bool PollEvent() override;
 

	
 
	void Initialize();
 
	bool MakeWindow(bool full_screen);
 
	void ClientSizeChanged(int w, int h, bool force = false);
 

	
 
	/** Get screen depth to use for fullscreen mode. */
0 comments (0 inline, 0 general)