summaryrefslogtreecommitdiff
path: root/frontend/shared_cpp
diff options
context:
space:
mode:
authorMatthew Kosarek <mattkae@protonmail.com>2021-03-27 11:49:56 -0400
committerMatthew Kosarek <mattkae@protonmail.com>2021-03-27 11:49:56 -0400
commit58488f8eabcc61089e0ae4297f38f10cc28d78c7 (patch)
tree81ac504e61c57c7228024ad40710f782a3d8c884 /frontend/shared_cpp
parent26ea81c5893778a7fa4ecbb2e8f561c9edabd0b3 (diff)
Working reusable simulation loop
Diffstat (limited to 'frontend/shared_cpp')
-rw-r--r--frontend/shared_cpp/MainLoop.cpp30
-rw-r--r--frontend/shared_cpp/MainLoop.h29
-rw-r--r--frontend/shared_cpp/OrthographicRenderer.cpp2
-rw-r--r--frontend/shared_cpp/WebglContext.h10
4 files changed, 68 insertions, 3 deletions
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 <cstdio>
+#include <cstdlib>
+
+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<float>(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 <emscripten.h>
+#include <emscripten/html5.h>
+#include <GLES2/gl2.h>
+#include <EGL/egl.h>
+
+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 <emscripten.h>
#include <emscripten/html5.h>
#include <GLES2/gl2.h>
#include <EGL/egl.h>
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