Browse Source

scaffold GUI architecture

master
Evan Burkey 6 months ago
parent
commit
591d3da6b7
  1. 5
      .idea/codeStyles/codeStyleConfig.xml
  2. 10
      include/gui.hpp
  3. 36
      src/gui.cpp

5
.idea/codeStyles/codeStyleConfig.xml

@ -0,0 +1,5 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</state>
</component>

10
include/gui.hpp

@ -1,13 +1,21 @@
#pragma once
#include <imgui.h>
#include <SDL2/SDL.h>
#include <vector>
#include <memory>
#include "imgui.h"
#include "vm.hpp"
struct Window {
virtual void draw(Vm &vm) = 0;
};
struct GUI {
SDL_Window* window;
SDL_GLContext gl_context;
ImGuiIO io;
std::vector<std::unique_ptr<Window>> windows;
bool show_demo_window;
bool quit_flag;

36
src/gui.cpp

@ -3,8 +3,19 @@
#include <SDL_opengl.h>
#include "gui.hpp"
GUI::GUI() : quit_flag(false), show_demo_window(true) {
struct MemoryWindow : public Window {
MemoryWindow() = default;
void draw(Vm &vm) override;
};
void MemoryWindow::draw(Vm &vm) {
ImGui::Begin("Memory");
ImGui::Text("Sup");
ImGui::End();
}
GUI::GUI() : quit_flag(false), show_demo_window(false) {
windows.push_back(std::make_unique<MemoryWindow>());
}
GUI::~GUI() {
@ -16,23 +27,20 @@ GUI::~GUI() {
SDL_DestroyWindow(window);
}
void GUI::frame(Vm& vm) {
// Poll and handle events (inputs, window resize, etc.)
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
void GUI::frame(Vm &vm) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
ImGui_ImplSDL2_ProcessEvent(&event);
if (event.type == SDL_QUIT)
if (event.type == SDL_QUIT) {
quit_flag = true;
}
// Main window is closed
if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE &&
event.window.windowID == SDL_GetWindowID(window))
event.window.windowID == SDL_GetWindowID(window)) {
quit_flag = true;
}
}
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplSDL2_NewFrame(window);
ImGui::NewFrame();
@ -41,14 +49,16 @@ void GUI::frame(Vm& vm) {
if (show_demo_window) {
ImGui::ShowDemoWindow(&show_demo_window);
} else {
for (const auto& w : windows) {
w->draw(vm);
}
}
// Rendering
ImGui::Render();
glViewport(0, 0, (int) io.DisplaySize.x, (int) io.DisplaySize.y);
glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w,
clear_color.w);
glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w,
clear_color.z * clear_color.w, clear_color.w);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
SDL_GL_SwapWindow(window);
Loading…
Cancel
Save