NESemu/src/Application.cpp

145 lines
2.6 KiB
C++
Raw Permalink Normal View History

2022-02-28 15:04:25 +00:00
#include "Application.hpp"
#include <stdexcept>
#include <glad/glad.h>
2022-02-28 17:02:24 +00:00
#include <GLFW/glfw3.h>
2022-02-28 15:04:25 +00:00
#include <imgui/backends/imgui_impl_glfw.h>
#include <imgui/backends/imgui_impl_opengl3.h>
#include <imgui/imgui.h>
#include "Log.hpp"
#include "Bus.hpp"
2022-03-03 01:33:08 +00:00
#include "Screen.hpp"
2022-02-28 15:04:25 +00:00
#include "Debugger.hpp"
#include "gfx/Window.hpp"
void Application::Launch()
{
glfwInit();
Application* app = nullptr;
try
{
app = new Application;
}
catch (const std::runtime_error& err)
{
LOG_CORE_FATAL(err.what());
delete app;
glfwTerminate();
return;
}
if (app == nullptr)
{
LOG_CORE_ERROR("Application object is nullptr");
glfwTerminate();
return;
}
while (app->Update());
delete app;
glfwTerminate();
}
Application::Application() :
bus(nullptr), window(nullptr)
{
LOG_CORE_INFO("Creating window");
try
{
2022-03-03 01:33:08 +00:00
window = new Window(256, 240, "NES Emulator");
2022-02-28 15:04:25 +00:00
}
catch (const std::runtime_error& err)
{
LOG_CORE_ERROR("Window creation failed");
throw err;
}
LOG_CORE_INFO("Loading OpenGL API");
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
throw std::runtime_error("Failed to set up OpenGL loader");
2022-03-03 01:33:08 +00:00
window->SetScale(scale);
2022-02-28 15:04:25 +00:00
LOG_CORE_INFO("Setting up ImGui");
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;
ImGui_ImplGlfw_InitForOpenGL(window->GetNativeWindow(), true);
ImGui_ImplOpenGL3_Init("#version 460 core");
ImGui::StyleColorsDark();
ImGuiStyle& style = ImGui::GetStyle();
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
style.WindowRounding = 0.0f;
style.Colors[ImGuiCol_WindowBg].w = 1.0f;
}
2022-03-03 01:33:08 +00:00
try
{
screen = new Screen;
}
catch(const std::runtime_error& err)
{
LOG_CORE_ERROR("Screen creation failed");
throw err;
}
bus = new Bus(screen);
2022-02-28 15:04:25 +00:00
debugger = new Debugger(bus);
}
Application::~Application()
{
delete debugger;
if(bus)
delete bus;
delete window;
}
bool Application::Update()
{
2023-01-28 00:21:51 +00:00
window->SetScale(scale);
2022-03-03 01:33:08 +00:00
2022-02-28 15:04:25 +00:00
glfwPollEvents();
if (!debugger->Update())
return false;
window->Begin();
2022-03-03 01:33:08 +00:00
screen->Render();
if (ImGui::BeginMainMenuBar())
{
if (ImGui::BeginMenu("Screen"))
{
if (ImGui::BeginMenu("Scale"))
{
ImGui::RadioButton("x1", &scale, 1);
ImGui::RadioButton("x2", &scale, 2);
ImGui::RadioButton("x3", &scale, 3);
ImGui::RadioButton("x4", &scale, 4);
ImGui::EndMenu();
}
ImGui::EndMenu();
}
ImGui::EndMainMenuBar();
}
2022-02-28 15:04:25 +00:00
debugger->Render();
window->End();
return !window->ShouldClose();
}