[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 { | ox::Error run(Context *ctx) noexcept { | ||||||
| 	const auto id = ctx->windowerData<GlfwImplData>(); | 	const auto id = ctx->windowerData<GlfwImplData>(); | ||||||
|  | 	int sleepTime = 0; | ||||||
| 	while (!glfwWindowShouldClose(id->window)) { | 	while (!glfwWindowShouldClose(id->window)) { | ||||||
| 		glfwPollEvents(); | 		glfwPollEvents(); | ||||||
| 		const auto ticks = ticksMs(ctx); | 		const auto ticks = ticksMs(ctx); | ||||||
| 		if (id->wakeupTime <= ticks && id->eventHandler) { | 		if (id->eventHandler) { | ||||||
| 			auto sleepTime = id->eventHandler(ctx); | 			if (id->wakeupTime <= ticks) { | ||||||
|  | 				 sleepTime = id->eventHandler(ctx); | ||||||
| 				 if (sleepTime >= 0) { | 				 if (sleepTime >= 0) { | ||||||
| 					  id->wakeupTime = ticks + static_cast<unsigned>(sleepTime); | 					  id->wakeupTime = ticks + static_cast<unsigned>(sleepTime); | ||||||
| 				 } else { | 				 } else { | ||||||
| 					  id->wakeupTime = ~uint64_t(0); | 					  id->wakeupTime = ~uint64_t(0); | ||||||
| 				 } | 				 } | ||||||
| 			} | 			} | ||||||
|  | 		} else { | ||||||
|  | 			sleepTime = 2; | ||||||
|  | 		} | ||||||
| 		draw(ctx); | 		draw(ctx); | ||||||
| 		glfwSwapBuffers(id->window); | 		glfwSwapBuffers(id->window); | ||||||
|  | 		glfwWaitEventsTimeout(sleepTime); | ||||||
| 	} | 	} | ||||||
| 	// destroy GLFW window | 	// destroy GLFW window | ||||||
| 	renderer::shutdown(ctx, ctx->rendererData<void>()); | 	renderer::shutdown(ctx, ctx->rendererData<void>()); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user