Jump to content

Input Events while map loading is not being cleared by Flush


Dreikblack
 Share

Go to solution Solved by Josh,

Recommended Posts

Not sure if it's a bug or i'm doing something wrong, but even 2nd Mouse Up event of double click made before loading in event queue after map loading.

Tried do FlushEvents, FlushMouse, FlushKeys before and after map load.

 

To reproduce:

1.  Debug mode to make loading longer, same issues in release mode persist.

2. Double click new game button to load map.

3. Press keys while loading.

4. Result after map load:

image.png.712e80354bfa5f1ac2ccc1c4de977276.png

If it's not possible to fix it then would be nice to have some event that will be up when map and gui are fully loaded so input events made while loading could be ignored in game code.

Start map is from 3rd person template

#include "UltraEngine.h"
#include "ComponentSystem.h"

using namespace UltraEngine;

shared_ptr<World> gameWorld;

shared_ptr<World> world;
shared_ptr<Interface> ui;

shared_ptr<Camera> uiCamera;
shared_ptr<Map> gameScene;

shared_ptr<Window> window;
shared_ptr<Framebuffer> framebuffer;

shared_ptr<World> loadingWorld;
shared_ptr<Interface> loadingUi;
shared_ptr<Camera> loadingCamera;
shared_ptr<Widget> loadingLabel;

shared_ptr<Widget> menuPanel;
bool isMainMenuOn = true;

bool newGameButtonCallback(const Event& ev, shared_ptr<Object> extra) {
	ui = nullptr;
	world = nullptr;
	loadingWorld->Render(framebuffer);
	WString mapName = "Maps/start.ultra";
	FlushEvents();
	window->FlushMouse();
	window->FlushKeys();
	gameScene = LoadMap(gameWorld, mapName);
	FlushEvents();
	window->FlushMouse();
	window->FlushKeys();
	Print("Map loaded");
	gameWorld->Render(framebuffer);
	isMainMenuOn = false;
	return true;
}

bool exitButtonCallback(const Event& ev, shared_ptr<Object> extra) {
	exit(0);
	return true;
}

void loadingInit() {
	loadingWorld = CreateWorld();
	//Loading UI
	auto font = LoadFont("Fonts/arial.ttf");
	loadingUi = CreateInterface(loadingWorld, font, framebuffer->GetSize());
	loadingUi->SetScale(1);
	loadingUi->SetRenderLayers(2);
	loadingUi->LoadColorScheme("Resources/configs/Style.json");
	//Label LOADING...
	int centerX = float(framebuffer->GetSize().x) * 0.5f;
	int centerY = float(framebuffer->GetSize().y) * 0.5f;
	float labelHeight = float(framebuffer->GetSize().y) * 0.2f;
	loadingLabel = CreateLabel("LOADING", float(framebuffer->GetSize().x) * 0.05f, centerY - labelHeight * 0.5f, float(framebuffer->GetSize().x) * 0.95f, labelHeight, loadingUi->root, LABEL_CENTER | LABEL_MIDDLE);
	loadingLabel->SetFontScale(20);
	//Create ui camera
	loadingCamera = CreateCamera(loadingWorld, PROJECTION_ORTHOGRAPHIC);
	loadingCamera->SetPosition(centerX, centerY, 0);
	loadingCamera->SetRenderLayers(2);
	loadingCamera->SetClearMode(CLEAR_DEPTH);
	loadingCamera->SetLighting(false);
	loadingWorld->Render(framebuffer);
}

int main(int argc, const char* argv[]) {
	RegisterComponents();
	//Get the displays
	auto displays = GetDisplays();
	//Create a window
	window = CreateWindow("Ultra Engine", 0, 0, 1280 * displays[0]->scale, 720 * displays[0]->scale, displays[0], WINDOW_CENTER | WINDOW_TITLEBAR);
	//Create a framebuffer
	framebuffer = CreateFramebuffer(window);
	//Create a world
	world = CreateWorld();

	//Load a font
	auto font = LoadFont("Fonts/arial.ttf");

	//Create user interface
	ui = CreateInterface(world, font, framebuffer->GetSize());
	ui->SetRenderLayers(2);
	ui->root->SetColor(0.2f, 0.2f, 0.2f, 1.0f);
	ui->LoadColorScheme("Resources/configs/Style.json");

	//Create ui camera
	auto uiCamera = CreateCamera(world, PROJECTION_ORTHOGRAPHIC);
	uiCamera->SetPosition(float(framebuffer->GetSize().x) * 0.5f, float(framebuffer->GetSize().y) * 0.5f, 0);
	uiCamera->SetRenderLayers(2);
	uiCamera->SetClearMode(CLEAR_DEPTH);

	auto newGameButton = CreateButton("New game", 200, 125, 200, 50, ui->root);
	ListenEvent(EVENT_DOUBLECLICK, newGameButton, newGameButtonCallback);

	auto exitButton = CreateButton("Exit", 200, 200, 200, 50, ui->root);
	ListenEvent(EVENT_WIDGETACTION, exitButton, exitButtonCallback);

	gameWorld = CreateWorld();
	loadingInit();

	shared_ptr<World> currentWorld = world;
	shared_ptr<Interface> currentUI = ui;

	while (window->Closed() == false || window->KeyHit(KEY_ESCAPE)) {
		if (!isMainMenuOn) currentWorld = gameWorld;
		while (PeekEvent()) {
			const Event ev = WaitEvent();
			switch (ev.id) {
			case EVENT_WINDOWCLOSE:
				if (ev.source == window) {
					exit(0);
					break;
				}
				break;
			case EVENT_KEYDOWN: {
				Print("Key Down");
				currentUI->ProcessEvent(ev);
				break;
			}
			case EVENT_MOUSEUP:
				Print("Mouse up");
			default:
				currentUI->ProcessEvent(ev);
				break;
			}
		}
		currentWorld->Update();
		currentWorld->Render(framebuffer);
	}
	return 0;
}

 

Link to comment
Share on other sites

  • Solution

This is occurring because at the point you are calling FlushEvents(), the windows events have not been received by the engine yet, and are still stuck in the OS event system. I am adding code that will check the OS and clear all waiting events from the system.

I am also adding an error that will occur if FlushEvents() is called inside an event callback, since this would modify the event loop as it is being iterated through.

Also note that you should check the event source on key and mouse events, since widgets will sometimes emit events like this:

			case EVENT_KEYDOWN:
				if (ev.source == window)
				{
					Print("Key Down: " + String(ev.data));
					currentUI->ProcessEvent(ev);
				}

 

  • Like 2

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...