From 58488f8eabcc61089e0ae4297f38f10cc28d78c7 Mon Sep 17 00:00:00 2001 From: Matthew Kosarek Date: Sat, 27 Mar 2021 11:49:56 -0400 Subject: Working reusable simulation loop --- frontend/shared_cpp/MainLoop.cpp | 30 ++++++++++++++++++++++++++++ frontend/shared_cpp/MainLoop.h | 29 +++++++++++++++++++++++++++ frontend/shared_cpp/OrthographicRenderer.cpp | 2 ++ frontend/shared_cpp/WebglContext.h | 10 +++++++--- 4 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 frontend/shared_cpp/MainLoop.cpp create mode 100644 frontend/shared_cpp/MainLoop.h (limited to 'frontend/shared_cpp') diff --git a/frontend/shared_cpp/MainLoop.cpp b/frontend/shared_cpp/MainLoop.cpp new file mode 100644 index 0000000..82a24b5 --- /dev/null +++ b/frontend/shared_cpp/MainLoop.cpp @@ -0,0 +1,30 @@ +#include "MainLoop.h" +#include +#include + +EM_BOOL loop(double time, void* loop) { + MainLoop* mainLoop = (MainLoop*) loop; + if (!mainLoop->isRunning) { + return false; + } + + if (mainLoop->lastTime == 0) { + mainLoop->lastTime = time; + return true; + } + + long deltaTime = time - mainLoop->lastTime; + mainLoop->lastTime = time; + mainLoop->elapsedTime += deltaTime; + mainLoop->numFrames++; + float deltaTimeSeconds = static_cast(deltaTime) / 1000.f; + + if (mainLoop->elapsedTime >= 1000.0) { + printf("FPS: %d\n", mainLoop->numFrames); + + mainLoop->elapsedTime = 0.0; + mainLoop->numFrames = 0; + } + + return mainLoop->updateFunc(deltaTimeSeconds, NULL); +} \ No newline at end of file diff --git a/frontend/shared_cpp/MainLoop.h b/frontend/shared_cpp/MainLoop.h new file mode 100644 index 0000000..7300f6b --- /dev/null +++ b/frontend/shared_cpp/MainLoop.h @@ -0,0 +1,29 @@ +#pragma once + +#include +#include +#include +#include + +EM_BOOL loop(double time, void* loop); + +struct MainLoop { + bool isRunning = false; + double lastTime = 0, elapsedTime = 0; + int numFrames = 0; + EM_BOOL (*updateFunc)(float dtSeconds, void *userData); + + void run(EM_BOOL (*cb)(float dtSeconds, void *userData)) { + isRunning = true; + lastTime = 0; + elapsedTime = 0; + numFrames = 0; + updateFunc = cb; + + emscripten_request_animation_frame_loop(loop, this); + } + + void stop() { + isRunning = false; + } +}; \ No newline at end of file diff --git a/frontend/shared_cpp/OrthographicRenderer.cpp b/frontend/shared_cpp/OrthographicRenderer.cpp index d160ecc..b6bd5b6 100644 --- a/frontend/shared_cpp/OrthographicRenderer.cpp +++ b/frontend/shared_cpp/OrthographicRenderer.cpp @@ -43,5 +43,7 @@ void OrthographicRenderer::render() { } void OrthographicRenderer::unload() { + glClearColor(0.2f, 0.3f, 0.3f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); glDeleteProgram(shader); } diff --git a/frontend/shared_cpp/WebglContext.h b/frontend/shared_cpp/WebglContext.h index c8a9480..1ea1c91 100644 --- a/frontend/shared_cpp/WebglContext.h +++ b/frontend/shared_cpp/WebglContext.h @@ -1,10 +1,13 @@ #pragma once +#include "types.h" #include #include #include #include struct WebglContext { + EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context; + void init(const char* query, int width = 640, int height = 480) { emscripten_set_canvas_element_size( query, width, height); @@ -17,6 +20,9 @@ struct WebglContext { context = emscripten_webgl_create_context(query, &attrs); makeCurrentContext(); + + glClearColor(0.2f, 0.3f, 0.3f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); }; void makeCurrentContext() { @@ -26,6 +32,4 @@ struct WebglContext { void destroy() { emscripten_webgl_destroy_context(context); } - - EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context; -}; +}; \ No newline at end of file -- cgit v1.2.1