aboutsummaryrefslogtreecommitdiffstats
path: root/source/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/main.cpp')
-rw-r--r--source/main.cpp184
1 files changed, 109 insertions, 75 deletions
diff --git a/source/main.cpp b/source/main.cpp
index ce1ea20..aa9f7c4 100644
--- a/source/main.cpp
+++ b/source/main.cpp
@@ -13,104 +13,138 @@
#include "backends/imgui_impl_sdl.h"
#include "backends/imgui_impl_opengl2.h"
-#include "config.h"
#include "logview.h"
#include "stmdsp.hpp"
+#include <chrono>
+#include <cmath>
+#include <iostream>
#include <string>
+#include <thread>
+
+void codeEditorInit();
+void codeRenderMenu();
+void codeRenderToolbar();
+void codeRenderWidgets(const ImVec2& size);
+void deviceRenderDraw();
+void deviceRenderMenu();
+void deviceRenderToolbar();
+void deviceRenderWidgets();
+void fileRenderMenu();
+void fileRenderDialog();
+void fileInit();
+bool guiInitialize();
+bool guiHandleEvents();
+void guiShutdown();
+void guiRender();
+void helpRenderMenu();
+void helpRenderDialog();
+
+void log(const std::string& str);
-// Externs
-extern ImFont *fontSans;
-extern ImFont *fontMono;
+static LogView logView;
+static ImFont *fontSans = nullptr;
+static ImFont *fontMono = nullptr;
-extern bool guiInitialize();
-extern void guiHandleEvents(bool& done);
-extern void guiShutdown();
-extern void guiRender(void (*func)());
+template<bool first = false>
+static void renderWindow();
-extern void fileRenderMenu();
-extern void fileRenderDialog();
-extern void fileScanTemplates();
+int main(int, char **)
+{
+ if (!guiInitialize())
+ return -1;
-extern void codeEditorInit();
-extern void codeRenderMenu();
-extern void codeRenderToolbar();
-extern void codeRenderWidgets();
+ auto& io = ImGui::GetIO();
+ fontSans = io.Fonts->AddFontFromFileTTF("fonts/Roboto-Regular.ttf", 20);
+ fontMono = io.Fonts->AddFontFromFileTTF("fonts/RobotoMono-Regular.ttf", 20);
+ if (fontSans == nullptr || fontMono == nullptr) {
+ std::cout << "Failed to load fonts!" << std::endl;
+ return -1;
+ }
-extern void deviceRenderDraw();
-extern void deviceRenderMenu();
-extern void deviceRenderToolbar();
-extern void deviceRenderWidgets();
+ codeEditorInit();
+ fileInit();
-// Globals that live here
-bool done = false;
-stmdsp::device *m_device = nullptr;
+ renderWindow<true>();
-static LogView logView;
+ while (1) {
+ constexpr std::chrono::duration<double> fpsDelay (1. / 60.);
+ const auto endTime = std::chrono::steady_clock::now() + fpsDelay;
+
+ const bool isDone = guiHandleEvents();
+ if (!isDone) {
+ renderWindow();
+ std::this_thread::sleep_until(endTime);
+ } else {
+ break;
+ }
+ }
+
+ guiShutdown();
+ return 0;
+}
void log(const std::string& str)
{
logView.AddLog(str);
}
-int main(int, char **)
+template<bool first>
+void renderWindow()
{
- if (!guiInitialize())
- return -1;
+ // Start the new window frame and render the menu bar.
+ ImGui_ImplOpenGL2_NewFrame();
+ ImGui_ImplSDL2_NewFrame();
+ ImGui::NewFrame();
+
+ if (ImGui::BeginMainMenuBar()) {
+ fileRenderMenu();
+ deviceRenderMenu();
+ codeRenderMenu();
+ helpRenderMenu();
+
+ ImGui::EndMainMenuBar();
+ }
- fileScanTemplates();
- codeEditorInit();
+ if constexpr (first) {
+ ImGui::SetNextWindowSize({550, 440});
+ }
- while (!done) {
- guiHandleEvents(done);
+ constexpr int MainTopMargin = 22;
+ const auto& displaySize = ImGui::GetIO().DisplaySize;
- // Start the new window frame and render the menu bar.
- ImGui_ImplOpenGL2_NewFrame();
- ImGui_ImplSDL2_NewFrame();
- ImGui::NewFrame();
+ ImGui::SetNextWindowPos({0, MainTopMargin});
+ ImGui::SetNextWindowSizeConstraints({displaySize.x, 150}, {displaySize.x, displaySize.y - 150});
+ ImGui::Begin("main", nullptr,
+ ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoTitleBar |
+ ImGuiWindowFlags_NoBringToFrontOnFocus);
- if (ImGui::BeginMainMenuBar()) {
- fileRenderMenu();
- deviceRenderMenu();
- codeRenderMenu();
- ImGui::EndMainMenuBar();
- }
+ const float mainWindowHeight = ImGui::GetWindowHeight();
- // Begin the main view which the controls will be drawn onto.
- ImGui::SetNextWindowPos({0, 22});
- ImGui::SetNextWindowSize({WINDOW_WIDTH, WINDOW_HEIGHT - 22 - 200});
- ImGui::Begin("main", nullptr,
- ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoDecoration |
- ImGuiWindowFlags_NoBringToFrontOnFocus);
-
- // Render main controls (order is important).
- ImGui::PushFont(fontSans);
- codeRenderToolbar();
- deviceRenderToolbar();
- fileRenderDialog();
- deviceRenderWidgets();
- ImGui::PopFont();
-
- ImGui::PushFont(fontMono);
- codeRenderWidgets();
- ImGui::SetNextWindowPos({0, WINDOW_HEIGHT - 200});
- ImGui::SetNextWindowSize({WINDOW_WIDTH, 200});
- logView.Draw("log", nullptr, ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoBringToFrontOnFocus);
- ImGui::PopFont();
-
- // Finish main view rendering.
- ImGui::End();
-
- deviceRenderDraw();
-
- // Draw everything to the screen.
- ImGui::Render();
- guiRender([] {
- ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData());
- });
- }
+ ImGui::PushFont(fontSans);
+ codeRenderToolbar();
+ deviceRenderToolbar();
+ fileRenderDialog();
+ helpRenderDialog();
+ deviceRenderWidgets();
+ ImGui::PopFont();
- guiShutdown();
- return 0;
+ ImGui::PushFont(fontMono);
+ codeRenderWidgets({displaySize.x - 16, mainWindowHeight - MainTopMargin - 24});
+ ImGui::PopFont();
+
+ ImGui::End();
+
+ // The log window is kept separate from "main" to support panel resizing.
+ ImGui::PushFont(fontMono);
+ ImGui::SetNextWindowPos({0, mainWindowHeight + MainTopMargin});
+ ImGui::SetNextWindowSize({displaySize.x, displaySize.y - mainWindowHeight - MainTopMargin});
+ logView.Draw("log", nullptr, ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoBringToFrontOnFocus);
+ ImGui::PopFont();
+
+ deviceRenderDraw();
+
+ // Draw everything to the screen.
+ guiRender();
}