aboutsummaryrefslogtreecommitdiffstats
path: root/source/main.cpp
blob: e8641bc77a38eacaf16ae4f537bc93a50008a607 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
 * @file main.cpp
 * @brief Program entry point and main loop.
 *
 * Copyright (C) 2021 Clyne Sullivan
 *
 * Distributed under the GNU GPL v3 or later. You should have received a copy of
 * the GNU General Public License along with this program.
 * If not, see <https://www.gnu.org/licenses/>.
 */

#include "imgui.h"
#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();
void deviceRenderDraw();
void deviceRenderMenu();
void deviceRenderToolbar();
void deviceRenderWidgets();
void fileRenderMenu();
void fileRenderDialog();
void fileInit();
bool guiInitialize();
bool guiHandleEvents();
void guiShutdown();
void guiRender();

void log(const std::string& str);

static LogView logView;
static ImFont *fontSans = nullptr;
static ImFont *fontMono = nullptr;

static void renderWindow();

int main(int, char **)
{
    if (!guiInitialize())
        return -1;

    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;
    }

    codeEditorInit();
    fileInit();

    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);
}

void renderWindow()
{
    // Start the new window frame and render the menu bar.
    ImGui_ImplOpenGL2_NewFrame();
    ImGui_ImplSDL2_NewFrame();
    ImGui::NewFrame();

    if (ImGui::BeginMainMenuBar()) {
        fileRenderMenu();
        deviceRenderMenu();
        codeRenderMenu();
        ImGui::EndMainMenuBar();
    }

    // Begin the main view which the controls will be drawn onto.
    constexpr float LOGVIEW_HEIGHT = 200;
    constexpr ImVec2 WINDOW_POS (0, 22);
    constexpr ImVec2 WINDOW_SIZE (WINDOW_WIDTH, WINDOW_HEIGHT - 22 - LOGVIEW_HEIGHT);
    ImGui::SetNextWindowPos(WINDOW_POS);
    ImGui::SetNextWindowSize(WINDOW_SIZE);
    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 - LOGVIEW_HEIGHT});
        ImGui::SetNextWindowSize({WINDOW_WIDTH, LOGVIEW_HEIGHT});
        logView.Draw("log", nullptr, ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoBringToFrontOnFocus);
        ImGui::PopFont();
    }

    ImGui::End();

    deviceRenderDraw();

    // Draw everything to the screen.
    guiRender();
}