From d2f42abd5529cdfda316a2e217995ad87188602e Mon Sep 17 00:00:00 2001 From: cinnaboot Date: Sun, 14 Mar 2021 03:20:14 -0400 Subject: [PATCH] update libTangerine add note about SDL input loop --- ext/tangerine | 2 +- src/gooey.cpp | 24 ++++++++++++++++++++---- src/gooey.h | 7 ++++++- src/main.cpp | 48 ++++++++++++++++-------------------------------- 4 files changed, 43 insertions(+), 38 deletions(-) diff --git a/ext/tangerine b/ext/tangerine index 6be56bc..e412f21 160000 --- a/ext/tangerine +++ b/ext/tangerine @@ -1 +1 @@ -Subproject commit 6be56bc532c12fe02c8de9218c97c2d354f64a2e +Subproject commit e412f21c7e3ca25283cdd8fe7db7d093bc63bc92 diff --git a/src/gooey.cpp b/src/gooey.cpp index 6857901..456b88c 100644 --- a/src/gooey.cpp +++ b/src/gooey.cpp @@ -32,17 +32,33 @@ gooFree() ImGui::DestroyContext(); } +bool +gooProcessEvent(SDL_Event& e) +{ + ImGui_ImplSDL2_ProcessEvent(&e); + return (ImGui::GetIO().WantCaptureMouse + || ImGui::GetIO().WantCaptureKeyboard); +} + void -gooDraw(SDL_Window* window) +gooDraw(SDL_Window* window, orbital_elements& orbit) { // Start the Dear ImGui frame ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplSDL2_NewFrame(window); ImGui::NewFrame(); - ImGui::Begin("Hello, world!"); - ImGui::Text("This is some useful text."); - ImGui::Button("Hey"); + ImGui::SetNextWindowPos(ImVec2(0,0)); + ImGui::SetNextWindowSize(ImVec2(300,400)); + ImGui::Begin("Orbital Elements"); + + ImGui::Text("a: %f km", orbit.ep.a); + ImGui::Text("e: %f", orbit.ep.e); + ImGui::Text("mu: %f km^3/s^2", orbit.mu); + + static char str0[128] = "Hello"; + ImGui::InputText("testing", str0, IM_ARRAYSIZE(str0)); + ImGui::End(); ImGui::Render(); diff --git a/src/gooey.h b/src/gooey.h index 6d26467..15cdbb5 100644 --- a/src/gooey.h +++ b/src/gooey.h @@ -3,6 +3,8 @@ #include +#include "orbits.h" + bool gooInit(SDL_Window* window, SDL_GLContext gl_context); @@ -10,6 +12,9 @@ gooInit(SDL_Window* window, SDL_GLContext gl_context); void gooFree(); +bool +gooProcessEvent(SDL_Event &e); + void -gooDraw(SDL_Window* window); +gooDraw(SDL_Window* window, orbital_elements& orbit); diff --git a/src/main.cpp b/src/main.cpp index 43d8008..3f40934 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,6 +6,7 @@ #include #include "dumbLog.h" +#include "input.h" #include "mesh.h" #include "renderer.h" #include "util.h" @@ -77,8 +78,19 @@ initOrbit() } void -doFrameCallback(render_state* rs) +doFrameCallbackPre(render_state* rs) { + // handle input + static input_state is = {}; + // TODO: probably need to break the while loop out of inputProcessEvents + // so we can send individual sdl events to ImGui_ImplSDL2_ProcessEvent() + // via gooProcessEvent() + inputProcessEvents(&is); + + if (is.window_closed || is.escape) + rs->running = false; + + // update satellite position const static glm::mat4 xform = glm::rotate(glm::mat4(1.0), (float) M_PI_2, glm::vec3(1, 0, 0)); entity& satellite = rs->render_groups[0].entities[2]; @@ -93,37 +105,10 @@ doFrameCallback(render_state* rs) entSetWorldPosition(satellite, xform * glm::vec4(v.x, v.y, v.z, 1)); } -// TODO: needs cleaning -// could maybe split up renderer callbacks into pre and post 'renderFrame' -// which is what ogre3d used IIRC -// TODO: (renderer) need to re-add input handling to libTangerine void -doCustomRenderLoop(render_state* rs, uint framerate, frame_callback_fn cb_func) +doFrameCallbackPost(render_state* rs) { - uint delay = (framerate > 0) ? 1 / framerate : 0; - uint frameStart, frameTime; - bool running = true; - SDL_Event e; - - while (running) { - frameStart = SDL_GetTicks(); - - while (SDL_PollEvent(&e)) { - if ((e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_ESCAPE) - || e.type == SDL_QUIT) { - running = false; - } - } - - if (cb_func != nullptr) cb_func(rs); - renRenderFrame(rs); - gooDraw(rs->handles->window); - SDL_GL_SwapWindow(rs->handles->window); - frameTime = SDL_GetTicks() - frameStart; - - if (delay > frameTime) - SDL_Delay(delay - frameTime); - } + gooDraw(rs->handles->window, g_orbit); } int @@ -175,8 +160,7 @@ main() entRotate(satellite_entity, (float) M_PI_2, glm::vec3(1, 0, 0)); entSetWorldPosition(satellite_entity, g_ellipse.vertices[0]); - //renDoRenderLoop(rs, 60, doFrameCallback); - doCustomRenderLoop(rs, 60, doFrameCallback); + renDoRenderLoop(rs, 60 , doFrameCallbackPre, doFrameCallbackPost); // TODO: clean up mesh pointers? don't remember if renderer does that // automatically