100 lines
2.8 KiB
C++
100 lines
2.8 KiB
C++
#include "Graphics.h"
|
|
|
|
#include <cmath>
|
|
#include <format>
|
|
#include <iostream>
|
|
|
|
#include "imgui.h"
|
|
#include "imgui_impl_sdl3.h"
|
|
#include "imgui_impl_sdlrenderer3.h"
|
|
|
|
Graphics::Graphics() : window_width{1366}, window_height{768}, main_scale{1.0f}
|
|
{
|
|
create_sdl();
|
|
create_imgui();
|
|
}
|
|
|
|
void Graphics::create_sdl()
|
|
{
|
|
SDL_SetAppMetadata("CHIP-8 Emulator", "0.0.1", "fun.skrd.chip8");
|
|
|
|
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO))
|
|
{
|
|
throw std::runtime_error(std::format("Couldn't initialize SDL: {}", SDL_GetError()));
|
|
}
|
|
|
|
SDL_Window* raw_window = nullptr;
|
|
SDL_Renderer* raw_renderer = nullptr;
|
|
|
|
this->main_scale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay());
|
|
constexpr SDL_WindowFlags window_flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN | SDL_WINDOW_HIGH_PIXEL_DENSITY;
|
|
|
|
if (!SDL_CreateWindowAndRenderer(
|
|
"CHIP-8 Emulator",
|
|
std::floor(static_cast<float>(this->window_width) * main_scale),
|
|
std::floor(static_cast<float>(this->window_height) * main_scale),
|
|
window_flags,
|
|
&raw_window,
|
|
&raw_renderer
|
|
))
|
|
{
|
|
throw std::runtime_error(std::format("Couldn't create window/renderer: {}", SDL_GetError()));
|
|
}
|
|
|
|
this->window = std::shared_ptr<SDL_Window>(raw_window, SDL_DestroyWindow);
|
|
this->renderer = std::shared_ptr<SDL_Renderer>(raw_renderer, SDL_DestroyRenderer);
|
|
|
|
SDL_SetRenderVSync(this->renderer.get(), 1);
|
|
SDL_SetWindowPosition(this->window.get(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
|
|
SDL_ShowWindow(this->window.get());
|
|
}
|
|
|
|
void Graphics::create_imgui() const
|
|
{
|
|
IMGUI_CHECKVERSION();
|
|
ImGui::CreateContext();
|
|
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
|
|
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
|
|
|
|
ImGui::StyleColorsDark();
|
|
ImGuiStyle& style = ImGui::GetStyle();
|
|
style.ScaleAllSizes(this->main_scale);
|
|
style.FontScaleDpi = this->main_scale;
|
|
|
|
ImGui_ImplSDL3_InitForSDLRenderer(this->window.get(), this->renderer.get());
|
|
ImGui_ImplSDLRenderer3_Init(this->renderer.get());
|
|
}
|
|
|
|
std::shared_ptr<SDL_Renderer> Graphics::get_renderer()
|
|
{
|
|
return this->renderer;
|
|
}
|
|
|
|
std::shared_ptr<SDL_Window> Graphics::get_window()
|
|
{
|
|
return this->window;
|
|
}
|
|
|
|
void Graphics::start() const
|
|
{
|
|
ImGui_ImplSDLRenderer3_NewFrame();
|
|
ImGui_ImplSDL3_NewFrame();
|
|
ImGui::NewFrame();
|
|
ImGui::DockSpaceOverViewport(0, ImGui::GetMainViewport());
|
|
}
|
|
|
|
void Graphics::end() const
|
|
{
|
|
const ImGuiIO& io = ImGui::GetIO();
|
|
|
|
ImGui::Render();
|
|
SDL_SetRenderScale(this->renderer.get(), io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y);
|
|
SDL_SetRenderDrawColor(this->renderer.get(), 0, 0, 0, 0xFF);
|
|
SDL_RenderClear(this->renderer.get());
|
|
|
|
ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData(), this->renderer.get());
|
|
SDL_RenderPresent(this->renderer.get());
|
|
}
|