[nostalgia/core/glfw] Put wait for event in main loop to prevent processor waste
This commit is contained in:
		| @@ -30,19 +30,25 @@ ox::Result<ox::UniquePtr<Context>> init(ox::UniquePtr<ox::FileSystem> fs, const | ||||
|  | ||||
| ox::Error run(Context *ctx) noexcept { | ||||
| 	const auto id = ctx->windowerData<GlfwImplData>(); | ||||
| 	int sleepTime = 0; | ||||
| 	while (!glfwWindowShouldClose(id->window)) { | ||||
| 		glfwPollEvents(); | ||||
| 		const auto ticks = ticksMs(ctx); | ||||
| 		if (id->wakeupTime <= ticks && id->eventHandler) { | ||||
| 			auto sleepTime = id->eventHandler(ctx); | ||||
| 		if (id->eventHandler) { | ||||
| 			if (id->wakeupTime <= ticks) { | ||||
| 				 sleepTime = id->eventHandler(ctx); | ||||
| 				 if (sleepTime >= 0) { | ||||
| 					  id->wakeupTime = ticks + static_cast<unsigned>(sleepTime); | ||||
| 				 } else { | ||||
| 					  id->wakeupTime = ~uint64_t(0); | ||||
| 				 } | ||||
| 			} | ||||
| 		} else { | ||||
| 			sleepTime = 2; | ||||
| 		} | ||||
| 		draw(ctx); | ||||
| 		glfwSwapBuffers(id->window); | ||||
| 		glfwWaitEventsTimeout(sleepTime); | ||||
| 	} | ||||
| 	// destroy GLFW window | ||||
| 	renderer::shutdown(ctx, ctx->rendererData<void>()); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user