EventThread: Properly allocate SDL user event codes
This commit is contained in:
		
							parent
							
								
									0a336ea315
								
							
						
					
					
						commit
						5f16eef273
					
				
					 3 changed files with 84 additions and 54 deletions
				
			
		| 
						 | 
				
			
			@ -45,19 +45,35 @@ EventThread::MouseState EventThread::mouseState =
 | 
			
		|||
	0, 0, { false }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/* User event codes */
 | 
			
		||||
enum
 | 
			
		||||
{
 | 
			
		||||
	REQUEST_FIRST = SDL_USEREVENT,
 | 
			
		||||
 | 
			
		||||
	REQUEST_TERMINATION,
 | 
			
		||||
	REQUEST_SETFULLSCREEN,
 | 
			
		||||
	REQUEST_WINRESIZE,
 | 
			
		||||
	REQUEST_MESSAGEBOX,
 | 
			
		||||
	REQUEST_SETCURSORVISIBLE,
 | 
			
		||||
 | 
			
		||||
	UPDATE_FPS
 | 
			
		||||
	UPDATE_FPS,
 | 
			
		||||
 | 
			
		||||
	EVENT_COUNT
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static uint32_t usrId[EVENT_COUNT];
 | 
			
		||||
 | 
			
		||||
bool EventThread::allocUserEvents()
 | 
			
		||||
{
 | 
			
		||||
	uint32_t first = SDL_RegisterEvents(EVENT_COUNT);
 | 
			
		||||
 | 
			
		||||
	if (first == (uint32_t) -1)
 | 
			
		||||
		return false;
 | 
			
		||||
 | 
			
		||||
	for (size_t i = 0; i < EVENT_COUNT; ++i)
 | 
			
		||||
		usrId[i] = first + i;
 | 
			
		||||
 | 
			
		||||
	return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
EventThread::EventThread()
 | 
			
		||||
    : fullscreen(false),
 | 
			
		||||
      showCursor(false)
 | 
			
		||||
| 
						 | 
				
			
			@ -144,7 +160,6 @@ void EventThread::process(RGSSThreadData &rtData)
 | 
			
		|||
			break;
 | 
			
		||||
 | 
			
		||||
		case SDL_QUIT :
 | 
			
		||||
		case REQUEST_TERMINATION :
 | 
			
		||||
			terminate = true;
 | 
			
		||||
			Debug() << "EventThread termination requested";
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -198,49 +213,6 @@ void EventThread::process(RGSSThreadData &rtData)
 | 
			
		|||
			keyStates[event.key.keysym.scancode] = true;
 | 
			
		||||
			break;
 | 
			
		||||
 | 
			
		||||
		case REQUEST_SETFULLSCREEN :
 | 
			
		||||
			setFullscreen(win, static_cast<bool>(event.user.code));
 | 
			
		||||
			break;
 | 
			
		||||
 | 
			
		||||
		case REQUEST_WINRESIZE :
 | 
			
		||||
			SDL_SetWindowSize(win, event.window.data1, event.window.data2);
 | 
			
		||||
			break;
 | 
			
		||||
 | 
			
		||||
		case REQUEST_MESSAGEBOX :
 | 
			
		||||
			SDL_ShowSimpleMessageBox(event.user.code,
 | 
			
		||||
			                         rtData.config.game.title.c_str(),
 | 
			
		||||
			                         (const char*) event.user.data1, win);
 | 
			
		||||
			free(event.user.data1);
 | 
			
		||||
			msgBoxDone = true;
 | 
			
		||||
 | 
			
		||||
			break;
 | 
			
		||||
 | 
			
		||||
		case REQUEST_SETCURSORVISIBLE :
 | 
			
		||||
			showCursor = event.user.code;
 | 
			
		||||
			updateCursorState(cursorInWindow);
 | 
			
		||||
 | 
			
		||||
			break;
 | 
			
		||||
 | 
			
		||||
		case UPDATE_FPS :
 | 
			
		||||
			if (!fps.displaying)
 | 
			
		||||
				break;
 | 
			
		||||
 | 
			
		||||
			snprintf(buffer, sizeof(buffer), "%s - %d FPS",
 | 
			
		||||
			         rtData.config.game.title.c_str(), event.user.code);
 | 
			
		||||
 | 
			
		||||
			/* Updating the window title in fullscreen
 | 
			
		||||
			 * mode seems to cause flickering */
 | 
			
		||||
			if (fullscreen)
 | 
			
		||||
			{
 | 
			
		||||
				strncpy(pendingTitle, buffer, sizeof(pendingTitle));
 | 
			
		||||
				havePendingTitle = true;
 | 
			
		||||
 | 
			
		||||
				break;
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			SDL_SetWindowTitle(win, buffer);
 | 
			
		||||
			break;
 | 
			
		||||
 | 
			
		||||
		case SDL_KEYUP :
 | 
			
		||||
			keyStates[event.key.keysym.scancode] = false;
 | 
			
		||||
			break;
 | 
			
		||||
| 
						 | 
				
			
			@ -285,6 +257,55 @@ void EventThread::process(RGSSThreadData &rtData)
 | 
			
		|||
			mouseState.y = event.motion.y;
 | 
			
		||||
 | 
			
		||||
			break;
 | 
			
		||||
 | 
			
		||||
		default :
 | 
			
		||||
			/* Handle user events */
 | 
			
		||||
			if (event.type == usrId[REQUEST_TERMINATION])
 | 
			
		||||
			{
 | 
			
		||||
				terminate = true;
 | 
			
		||||
				Debug() << "EventThread termination requested";
 | 
			
		||||
			}
 | 
			
		||||
			else if (event.type == usrId[REQUEST_SETFULLSCREEN])
 | 
			
		||||
			{
 | 
			
		||||
				setFullscreen(win, static_cast<bool>(event.user.code));
 | 
			
		||||
			}
 | 
			
		||||
			else if (event.type == usrId[REQUEST_WINRESIZE])
 | 
			
		||||
			{
 | 
			
		||||
				SDL_SetWindowSize(win, event.window.data1, event.window.data2);
 | 
			
		||||
			}
 | 
			
		||||
			else if (event.type == usrId[REQUEST_MESSAGEBOX])
 | 
			
		||||
			{
 | 
			
		||||
				SDL_ShowSimpleMessageBox(event.user.code,
 | 
			
		||||
				                         rtData.config.game.title.c_str(),
 | 
			
		||||
				                         (const char*) event.user.data1, win);
 | 
			
		||||
				free(event.user.data1);
 | 
			
		||||
				msgBoxDone = true;
 | 
			
		||||
			}
 | 
			
		||||
			else if (event.type == usrId[REQUEST_SETCURSORVISIBLE])
 | 
			
		||||
			{
 | 
			
		||||
				showCursor = event.user.code;
 | 
			
		||||
				updateCursorState(cursorInWindow);
 | 
			
		||||
			}
 | 
			
		||||
			else if (event.type == usrId[UPDATE_FPS])
 | 
			
		||||
			{
 | 
			
		||||
				if (!fps.displaying)
 | 
			
		||||
					break;
 | 
			
		||||
 | 
			
		||||
				snprintf(buffer, sizeof(buffer), "%s - %d FPS",
 | 
			
		||||
				         rtData.config.game.title.c_str(), event.user.code);
 | 
			
		||||
 | 
			
		||||
				/* Updating the window title in fullscreen
 | 
			
		||||
				 * mode seems to cause flickering */
 | 
			
		||||
				if (fullscreen)
 | 
			
		||||
				{
 | 
			
		||||
					strncpy(pendingTitle, buffer, sizeof(pendingTitle));
 | 
			
		||||
					havePendingTitle = true;
 | 
			
		||||
 | 
			
		||||
					break;
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				SDL_SetWindowTitle(win, buffer);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if (terminate)
 | 
			
		||||
| 
						 | 
				
			
			@ -300,7 +321,7 @@ void EventThread::cleanup()
 | 
			
		|||
	SDL_Event event;
 | 
			
		||||
	while (SDL_PollEvent(&event))
 | 
			
		||||
	{
 | 
			
		||||
		if (event.type == REQUEST_MESSAGEBOX)
 | 
			
		||||
		if (event.type == usrId[REQUEST_MESSAGEBOX])
 | 
			
		||||
		{
 | 
			
		||||
			free(event.user.data1);
 | 
			
		||||
		}
 | 
			
		||||
| 
						 | 
				
			
			@ -332,7 +353,7 @@ void EventThread::updateCursorState(bool inWindow)
 | 
			
		|||
void EventThread::requestTerminate()
 | 
			
		||||
{
 | 
			
		||||
	SDL_Event event;
 | 
			
		||||
	event.type = REQUEST_TERMINATION;
 | 
			
		||||
	event.type = usrId[REQUEST_TERMINATION];
 | 
			
		||||
	SDL_PushEvent(&event);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -342,7 +363,7 @@ void EventThread::requestFullscreenMode(bool mode)
 | 
			
		|||
		return;
 | 
			
		||||
 | 
			
		||||
	SDL_Event event;
 | 
			
		||||
	event.type = REQUEST_SETFULLSCREEN;
 | 
			
		||||
	event.type = usrId[REQUEST_SETFULLSCREEN];
 | 
			
		||||
	event.user.code = static_cast<Sint32>(mode);
 | 
			
		||||
	SDL_PushEvent(&event);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -350,7 +371,7 @@ void EventThread::requestFullscreenMode(bool mode)
 | 
			
		|||
void EventThread::requestWindowResize(int width, int height)
 | 
			
		||||
{
 | 
			
		||||
	SDL_Event event;
 | 
			
		||||
	event.type = REQUEST_WINRESIZE;
 | 
			
		||||
	event.type = usrId[REQUEST_WINRESIZE];
 | 
			
		||||
	event.window.data1 = width;
 | 
			
		||||
	event.window.data2 = height;
 | 
			
		||||
	SDL_PushEvent(&event);
 | 
			
		||||
| 
						 | 
				
			
			@ -359,7 +380,7 @@ void EventThread::requestWindowResize(int width, int height)
 | 
			
		|||
void EventThread::requestShowCursor(bool mode)
 | 
			
		||||
{
 | 
			
		||||
	SDL_Event event;
 | 
			
		||||
	event.type = REQUEST_SETCURSORVISIBLE;
 | 
			
		||||
	event.type = usrId[REQUEST_SETCURSORVISIBLE];
 | 
			
		||||
	event.user.code = mode;
 | 
			
		||||
	SDL_PushEvent(&event);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -371,7 +392,7 @@ void EventThread::showMessageBox(const char *body, int flags)
 | 
			
		|||
	SDL_Event event;
 | 
			
		||||
	event.user.code = flags;
 | 
			
		||||
	event.user.data1 = strdup(body);
 | 
			
		||||
	event.type = REQUEST_MESSAGEBOX;
 | 
			
		||||
	event.type = usrId[REQUEST_MESSAGEBOX];
 | 
			
		||||
	SDL_PushEvent(&event);
 | 
			
		||||
 | 
			
		||||
	/* Keep repainting screen while box is open */
 | 
			
		||||
| 
						 | 
				
			
			@ -424,6 +445,6 @@ void EventThread::notifyFrame()
 | 
			
		|||
 | 
			
		||||
	SDL_Event event;
 | 
			
		||||
	event.user.code = avgFPS;
 | 
			
		||||
	event.user.type = UPDATE_FPS;
 | 
			
		||||
	event.user.type = usrId[UPDATE_FPS];
 | 
			
		||||
	SDL_PushEvent(&event);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -61,6 +61,8 @@ public:
 | 
			
		|||
 | 
			
		||||
	static MouseState mouseState;
 | 
			
		||||
 | 
			
		||||
	static bool allocUserEvents();
 | 
			
		||||
 | 
			
		||||
	EventThread();
 | 
			
		||||
 | 
			
		||||
	void process(RGSSThreadData &rtData);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -234,6 +234,13 @@ int main(int, char *argv[])
 | 
			
		|||
		return 0;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (!EventThread::allocUserEvents())
 | 
			
		||||
	{
 | 
			
		||||
		Debug() << "Error allocating SDL user events";
 | 
			
		||||
 | 
			
		||||
		return 0;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/* set working directory */
 | 
			
		||||
	char *dataDir = SDL_GetBasePath();
 | 
			
		||||
	if (dataDir)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue