From e40d886f84974af424d806976d13270a83fe1a00 Mon Sep 17 00:00:00 2001 From: cinnaboot Date: Tue, 9 Mar 2021 18:11:50 -0500 Subject: [PATCH] setup imgui --- Makefile | 45 +++++++++++++++++++++++++++----------------- ext/tangerine | 2 +- src/gooey.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/gooey.h | 15 +++++++++++++++ src/main.cpp | 43 +++++++++++++++++++++++++++++++++++++++++- src/orbits.cpp | 2 -- 6 files changed, 137 insertions(+), 21 deletions(-) create mode 100644 src/gooey.cpp create mode 100644 src/gooey.h diff --git a/Makefile b/Makefile index cb50b4f..6a3f7f7 100644 --- a/Makefile +++ b/Makefile @@ -2,25 +2,32 @@ SHELL := /bin/sh OBJDIR := build SRCDIR := src -TESTDIR := tests BINDIR := bin LIBDIR := ext/tangerine LIB := $(LIBDIR)/build/libTangerine.a CXX := g++ -CXXFLAGS := -std=c++11 -g -ggdb3 -Wall -I$(LIBDIR)/include/ -LDFLAGS := -lSDL2 -lGLEW -lGL -lassimp +CXXFLAGS := -std=c++11 -g -ggdb3 -Wall -I$(LIBDIR)/include/ $(shell sdl2-config --cflags) +LDFLAGS := $(shell sdl2-config --libs) -lGLEW -lGL -lassimp COMMON_SOURCES := $(SRCDIR)/orbits.cpp COMMON_OBJECTS := $(patsubst $(SRCDIR)/%.cpp, $(OBJDIR)/%.o, $(COMMON_SOURCES)) -TEST_SOURCES := $(TESTDIR)/orbit_test.cpp +TESTDIR := tests +TEST_SOURCES := $(wildcard $(TESTDIR)/*.cpp) TEST_OBJECTS := $(patsubst $(TESTDIR)/%.cpp, $(OBJDIR)/%.o, $(TEST_SOURCES)) -TEST_MAIN_SOURCE := $(TESTDIR)/tests_main.cpp -TEST_MAIN_OBJ := $(OBJDIR)/tests_main.o TEST_BIN := $(BINDIR)/test_orbit -SOURCES := $(SRCDIR)/main.cpp +# imgui setup +IMGUI_DIR := ext/imgui +CXXFLAGS += -I$(IMGUI_DIR) +IMGUI_IMPL_DIR := $(IMGUI_DIR)/backends +IMGUI_IMPL_SOURCES := $(IMGUI_IMPL_DIR)/imgui_impl_opengl3.cpp $(IMGUI_IMPL_DIR)/imgui_impl_sdl.cpp +IMGUI_SOURCES := $(filter-out $(IMGUI_DIR)/imgui_demo.cpp, $(wildcard $(IMGUI_DIR)/*.cpp)) +IMGUI_OBJECTS := $(patsubst $(IMGUI_DIR)/%.cpp, $(OBJDIR)/%.o, $(IMGUI_SOURCES)) +IMGUI_IMP_OBJECTS += $(patsubst $(IMGUI_IMPL_DIR)/%.cpp, $(OBJDIR)/%.o, $(IMGUI_IMPL_SOURCES)) + +SOURCES := $(SRCDIR)/main.cpp $(SRCDIR)/gooey.cpp OBJECTS := $(patsubst $(SRCDIR)/%.cpp, $(OBJDIR)/%.o, $(SOURCES)) BIN := $(BINDIR)/orbital_shipping @@ -44,23 +51,27 @@ mkdirs: @-mkdir -p ./bin ./build .PHONY: mkdirs -$(BIN): $(COMMON_OBJECTS) $(OBJECTS) $(LIB) - $(CXX) -o $(BIN) $(LDFLAGS) $(COMMON_OBJECTS) $(OBJECTS) $(LIB) $(ASSIMP_LIB) +$(BIN): $(COMMON_OBJECTS) $(OBJECTS) $(IMGUI_OBJECTS) $(IMGUI_IMP_OBJECTS) $(LIB) + $(CXX) -o $@ $(LDFLAGS) $(COMMON_OBJECTS) $(OBJECTS) $(LIB) $(ASSIMP_LIB) \ + $(IMGUI_OBJECTS) $(IMGUI_IMP_OBJECTS) -$(TEST_BIN): $(TEST_OBJECTS) $(TEST_MAIN_OBJ) $(LIB) - $(CXX) -o $(TEST_BIN) $(LDFLAGS) $(TEST_OBJECTS) $(TEST_MAIN_OBJ) $(LIB) +$(TEST_BIN): $(TEST_OBJECTS) $(LIB) + $(CXX) -o $@ $(LDFLAGS) $(TEST_OBJECTS) $(LIB) $(COMMON_OBJECTS): $(COMMON_SOURCES) $(CXX) $(CXXFLAGS) -c -MMD $(SRCDIR)/orbits.cpp -o $@ -$(OBJECTS): $(SOURCES) - $(CXX) $(CXXFLAGS) -c -MMD $(SOURCES) -o $(OBJECTS) +$(OBJECTS): $(OBJDIR)/%.o : $(SRCDIR)/%.cpp + $(CXX) $(CXXFLAGS) -c -MMD $< -o $@ + +$(TEST_OBJECTS): $(OBJDIR)/%.o : $(TESTDIR)/%.cpp + $(CXX) $(CXXFLAGS) -c -MMD $< -o $@ -$(TEST_OBJECTS): $(TEST_SOURCES) - $(CXX) $(CXXFLAGS) -c -MMD $(TESTDIR)/orbit_test.cpp -o $(OBJDIR)/orbit_test.o +$(IMGUI_OBJECTS): $(OBJDIR)/%.o : $(IMGUI_DIR)/%.cpp + $(CXX) $(CXXFLAGS) -c -MMD $< -o $@ -$(TEST_MAIN_OBJ): $(TEST_MAIN_SOURCE) - $(CXX) $(CXXFLAGS) -c -MMD $(TESTDIR)/tests_main.cpp -o $(OBJDIR)/tests_main.o +$(IMGUI_IMP_OBJECTS): $(OBJDIR)/%.o : $(IMGUI_IMPL_DIR)/%.cpp + $(CXX) $(CXXFLAGS) -c -MMD $< -o $@ $(LIB): USE_INTERNAL_ASSIMP=$(USE_INTERNAL_ASSIMP) $(MAKE) -C $(LIBDIR) diff --git a/ext/tangerine b/ext/tangerine index dd0659c..6be56bc 160000 --- a/ext/tangerine +++ b/ext/tangerine @@ -1 +1 @@ -Subproject commit dd0659c0298a8ee3925f8f6ca01ae37091d2ba32 +Subproject commit 6be56bc532c12fe02c8de9218c97c2d354f64a2e diff --git a/src/gooey.cpp b/src/gooey.cpp new file mode 100644 index 0000000..6857901 --- /dev/null +++ b/src/gooey.cpp @@ -0,0 +1,51 @@ + +#include + +#include +#include +#include + +#include "gooey.h" + + +bool +gooInit(SDL_Window* window, SDL_GLContext gl_context) +{ + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImGuiIO& io = ImGui::GetIO(); + io.IniFilename = NULL; // don't save window state to imgui.ini + ImGui::StyleColorsDark(); + + bool ret = true; + ret = ret && ImGui_ImplSDL2_InitForOpenGL(window, gl_context); + ret = ret && ImGui_ImplOpenGL3_Init(); + + return ret; +} + +void +gooFree() +{ + ImGui_ImplOpenGL3_Shutdown(); + ImGui_ImplSDL2_Shutdown(); + ImGui::DestroyContext(); +} + +void +gooDraw(SDL_Window* window) +{ + // 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::End(); + + ImGui::Render(); + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); +} + diff --git a/src/gooey.h b/src/gooey.h new file mode 100644 index 0000000..6d26467 --- /dev/null +++ b/src/gooey.h @@ -0,0 +1,15 @@ + +#pragma once + +#include + + +bool +gooInit(SDL_Window* window, SDL_GLContext gl_context); + +void +gooFree(); + +void +gooDraw(SDL_Window* window); + diff --git a/src/main.cpp b/src/main.cpp index 006b6c0..43d8008 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,6 +10,7 @@ #include "renderer.h" #include "util.h" +#include "gooey.h" #include "orbits.h" @@ -92,6 +93,39 @@ 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) +{ + 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); + } +} + int main() { @@ -104,6 +138,11 @@ main() return 1; } + if (!gooInit(rs->handles->window, rs->handles->glContext)) { + LOG(Error) << "Error Initialzing gooey\n"; + return 1; + } + cameraInitPerspective( rs->cam, glm::vec3(0, -75 / SCALING, 0), @@ -136,10 +175,12 @@ main() entRotate(satellite_entity, (float) M_PI_2, glm::vec3(1, 0, 0)); entSetWorldPosition(satellite_entity, g_ellipse.vertices[0]); - renDoRenderLoop(rs, 60, doFrameCallback); + //renDoRenderLoop(rs, 60, doFrameCallback); + doCustomRenderLoop(rs, 60, doFrameCallback); // TODO: clean up mesh pointers? don't remember if renderer does that // automatically + gooFree(); renShutdown(rs); return 0; } diff --git a/src/orbits.cpp b/src/orbits.cpp index 6b428c3..1fbf802 100644 --- a/src/orbits.cpp +++ b/src/orbits.cpp @@ -1,6 +1,4 @@ -#include "dumbLog.h" - #include "orbits.h"