SDRPlusPlus/src/main.cpp

252 lines
8.6 KiB
C++
Raw Normal View History

2020-06-10 04:13:56 +02:00
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include <stdio.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <main_window.h>
2020-08-16 03:39:05 +02:00
#include <style.h>
2020-07-19 15:59:44 +02:00
#include <icons.h>
2020-07-19 18:09:59 +02:00
#include <version.h>
2020-08-04 21:34:56 +02:00
#include <spdlog/spdlog.h>
#include <bandplan.h>
2020-08-07 14:29:06 +02:00
#include <module.h>
2020-08-16 03:39:05 +02:00
#include <stb_image.h>
#include <config.h>
2020-09-18 00:23:03 +02:00
#include <dsp/block.h>
2020-08-16 03:39:05 +02:00
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#include <stb_image_resize.h>
2020-06-22 16:45:57 +02:00
2020-06-10 04:13:56 +02:00
#ifdef _WIN32
#include <Windows.h>
#endif
2020-09-06 16:31:50 +02:00
// Comment to build a normal release
2020-09-18 00:23:03 +02:00
#define DEV_BUILD
2020-09-06 16:31:50 +02:00
2020-08-20 18:29:23 +02:00
bool maximized = false;
2020-08-21 15:34:50 +02:00
bool fullScreen = false;
2020-08-20 18:29:23 +02:00
2020-06-10 04:13:56 +02:00
static void glfw_error_callback(int error, const char* description) {
2020-08-04 21:34:56 +02:00
spdlog::error("Glfw Error {0}: {1}", error, description);
2020-06-10 04:13:56 +02:00
}
2020-08-20 18:29:23 +02:00
static void maximized_callback(GLFWwindow* window, int n) {
if (n == GLFW_TRUE) {
maximized = true;
}
else {
maximized = false;
}
}
2020-09-18 00:23:03 +02:00
// main
2020-06-10 04:13:56 +02:00
int main() {
#ifdef _WIN32
//FreeConsole();
#endif
2020-08-04 21:34:56 +02:00
spdlog::info("SDR++ v" VERSION_STR);
2020-09-06 16:31:50 +02:00
#ifdef DEV_BUILD
2020-09-18 00:23:03 +02:00
config::setRootDirectory("../root_dev");
2020-09-06 16:31:50 +02:00
#elif _WIN32
2020-09-06 15:39:09 +02:00
config::setRootDirectory(".");
#else
config::setRootDirectory("/etc/sdrpp");
#endif
2020-08-20 18:29:23 +02:00
// Load config
spdlog::info("Loading config");
2020-09-06 15:39:09 +02:00
config::load(config::getRootDirectory() + "/config.json");
2020-08-20 18:29:23 +02:00
config::startAutoSave();
2020-06-10 04:13:56 +02:00
// Setup window
glfwSetErrorCallback(glfw_error_callback);
if (!glfwInit()) {
return 1;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Required on Mac
2020-08-16 03:39:05 +02:00
2020-08-20 18:29:23 +02:00
int winWidth = config::config["windowSize"]["w"];
int winHeight = config::config["windowSize"]["h"];
maximized = config::config["maximized"];
2020-06-10 04:13:56 +02:00
// Create window with graphics context
2020-08-21 15:34:50 +02:00
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
2020-08-20 18:29:23 +02:00
GLFWwindow* window = glfwCreateWindow(winWidth, winHeight, "SDR++ v" VERSION_STR " (Built at " __TIME__ ", " __DATE__ ")", NULL, NULL);
2020-06-10 04:13:56 +02:00
if (window == NULL)
return 1;
glfwMakeContextCurrent(window);
glfwSwapInterval(1); // Enable vsync
2020-08-20 18:29:23 +02:00
if (maximized) {
glfwMaximizeWindow(window);
}
glfwSetWindowMaximizeCallback(window, maximized_callback);
2020-08-16 03:39:05 +02:00
// Load app icon
GLFWimage icons[10];
2020-09-06 15:39:09 +02:00
icons[0].pixels = stbi_load((config::getRootDirectory() + "/res/icons/sdrpp.png").c_str(), &icons[0].width, &icons[0].height, 0, 4);
2020-08-16 03:39:05 +02:00
icons[1].pixels = (unsigned char*)malloc(16 * 16 * 4); icons[1].width = icons[1].height = 16;
icons[2].pixels = (unsigned char*)malloc(24 * 24 * 4); icons[2].width = icons[2].height = 24;
icons[3].pixels = (unsigned char*)malloc(32 * 32 * 4); icons[3].width = icons[3].height = 32;
icons[4].pixels = (unsigned char*)malloc(48 * 48 * 4); icons[4].width = icons[4].height = 48;
icons[5].pixels = (unsigned char*)malloc(64 * 64 * 4); icons[5].width = icons[5].height = 64;
icons[6].pixels = (unsigned char*)malloc(96 * 96 * 4); icons[6].width = icons[6].height = 96;
icons[7].pixels = (unsigned char*)malloc(128 * 128 * 4); icons[7].width = icons[7].height = 128;
icons[8].pixels = (unsigned char*)malloc(196 * 196 * 4); icons[8].width = icons[8].height = 196;
icons[9].pixels = (unsigned char*)malloc(256 * 256 * 4); icons[9].width = icons[9].height = 256;
stbir_resize_uint8(icons[0].pixels, icons[0].width, icons[0].height, icons[0].width * 4, icons[1].pixels, 16, 16, 16 * 4, 4);
stbir_resize_uint8(icons[0].pixels, icons[0].width, icons[0].height, icons[0].width * 4, icons[2].pixels, 24, 24, 24 * 4, 4);
stbir_resize_uint8(icons[0].pixels, icons[0].width, icons[0].height, icons[0].width * 4, icons[3].pixels, 32, 32, 32 * 4, 4);
stbir_resize_uint8(icons[0].pixels, icons[0].width, icons[0].height, icons[0].width * 4, icons[4].pixels, 48, 48, 48 * 4, 4);
stbir_resize_uint8(icons[0].pixels, icons[0].width, icons[0].height, icons[0].width * 4, icons[5].pixels, 64, 64, 64 * 4, 4);
stbir_resize_uint8(icons[0].pixels, icons[0].width, icons[0].height, icons[0].width * 4, icons[6].pixels, 96, 96, 96 * 4, 4);
stbir_resize_uint8(icons[0].pixels, icons[0].width, icons[0].height, icons[0].width * 4, icons[7].pixels, 128, 128, 128 * 4, 4);
stbir_resize_uint8(icons[0].pixels, icons[0].width, icons[0].height, icons[0].width * 4, icons[8].pixels, 196, 196, 196 * 4, 4);
stbir_resize_uint8(icons[0].pixels, icons[0].width, icons[0].height, icons[0].width * 4, icons[9].pixels, 256, 256, 256 * 4, 4);
glfwSetWindowIcon(window, 10, icons);
stbi_image_free(icons[0].pixels);
for (int i = 1; i < 10; i++) {
free(icons[i].pixels);
}
2020-06-10 04:13:56 +02:00
if (glewInit() != GLEW_OK) {
2020-08-04 21:34:56 +02:00
spdlog::error("Failed to initialize OpenGL loader!");
2020-06-10 04:13:56 +02:00
return 1;
}
2020-08-17 02:39:56 +02:00
2020-06-10 04:13:56 +02:00
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.IniFilename = NULL;
// Setup Platform/Renderer bindings
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 150");
2020-08-17 02:39:56 +02:00
style::setDarkStyle();
2020-06-10 04:13:56 +02:00
2020-09-18 00:23:03 +02:00
// ====================================================
// glfwPollEvents();
// ImGui_ImplOpenGL3_NewFrame();
// ImGui_ImplGlfw_NewFrame();
// ImGui::NewFrame();
// ImGui::ShowDemoWindow();
// ImGui::Render();
// int display_w, display_h;
// glfwGetFramebufferSize(window, &display_w, &display_h);
// glViewport(0, 0, display_w, display_h);
// glClearColor(0.0666f, 0.0666f, 0.0666f, 1.0f);
// //glClearColor(0.9f, 0.9f, 0.9f, 1.0f);
// glClear(GL_COLOR_BUFFER_BIT);
// ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
// glfwSwapBuffers(window);
// ====================================================
2020-08-04 21:34:56 +02:00
spdlog::info("Loading icons");
2020-07-19 15:59:44 +02:00
icons::load();
2020-08-05 11:23:13 +02:00
spdlog::info("Loading band plans");
2020-09-06 15:39:09 +02:00
bandplan::loadFromDir(config::getRootDirectory() + "/bandplans");
2020-08-04 21:34:56 +02:00
2020-08-05 21:13:53 +02:00
spdlog::info("Loading band plans color table");
2020-09-06 15:39:09 +02:00
bandplan::loadColorTable(config::getRootDirectory() + "/band_colors.json");
2020-08-05 21:13:53 +02:00
2020-08-16 03:39:05 +02:00
windowInit();
2020-08-04 21:34:56 +02:00
spdlog::info("Ready.");
2020-08-20 18:29:23 +02:00
bool _maximized = maximized;
2020-08-21 15:34:50 +02:00
int fsWidth, fsHeight, fsPosX, fsPosY;
2020-08-20 18:29:23 +02:00
2020-06-10 04:13:56 +02:00
// Main loop
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
2020-08-20 18:29:23 +02:00
if (_maximized != maximized) {
_maximized = maximized;
config::config["maximized"]= _maximized;
config::configModified = true;
if (!maximized) {
glfwSetWindowSize(window, config::config["windowSize"]["w"], config::config["windowSize"]["h"]);
}
}
2020-08-21 15:34:50 +02:00
2020-08-20 18:29:23 +02:00
int _winWidth, _winHeight;
glfwGetWindowSize(window, &_winWidth, &_winHeight);
2020-08-21 15:34:50 +02:00
if (ImGui::IsKeyPressed(GLFW_KEY_F11)) {
fullScreen = !fullScreen;
if (fullScreen) {
spdlog::info("Fullscreen: ON");
fsWidth = _winWidth;
fsHeight = _winHeight;
glfwGetWindowPos(window, &fsPosX, &fsPosY);
const GLFWvidmode * mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, 0);
}
else {
spdlog::info("Fullscreen: OFF");
glfwSetWindowMonitor(window, nullptr, fsPosX, fsPosY, fsWidth, fsHeight, 0);
}
}
2020-08-20 18:29:23 +02:00
if ((_winWidth != winWidth || _winHeight != winHeight) && !maximized && _winWidth > 0 && _winHeight > 0) {
winWidth = _winWidth;
winHeight = _winHeight;
config::config["windowSize"]["w"] = winWidth;
config::config["windowSize"]["h"] = winHeight;
config::configModified = true;
}
if (winWidth > 0 && winHeight > 0) {
ImGui::SetNextWindowPos(ImVec2(0, 0));
ImGui::SetNextWindowSize(ImVec2(_winWidth, _winHeight));
drawWindow();
}
2020-06-10 04:13:56 +02:00
// Rendering
ImGui::Render();
int display_w, display_h;
glfwGetFramebufferSize(window, &display_w, &display_h);
glViewport(0, 0, display_w, display_h);
glClearColor(0.0666f, 0.0666f, 0.0666f, 1.0f);
2020-08-17 02:39:56 +02:00
//glClearColor(0.9f, 0.9f, 0.9f, 1.0f);
2020-06-10 04:13:56 +02:00
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
}
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}