Keishiki/Keishiki/Keishiki.cpp

163 lines
5.1 KiB
C++

#include "Keishiki.h"
#include "Graphics.h"
#include "UI.h"
#include <SDL.h>
#include <SDL_main.h>
#include <SDL_timer.h>
#include <SDL_syswm.h>
#include <bgfx/bgfx.h>
#include <bgfx/platform.h>
#include "backends/imgui_impl_sdl2.h"
#include <imgui.h>
namespace {
K::CompState state;
const K::Resource::Resource<K::Resource::Type::K_R_Still> *logo;
}
namespace K {
K::AppState app_state;
bool Init() {
if (SDL_Init(SDL_INIT_VIDEO)) {
LogError(SDL_GetError());
return false;
}
app_state.window = SDL_CreateWindow("Keishiki",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
app_state.window_width, app_state.window_height,
SDL_WINDOW_SHOWN);
if (app_state.window == nullptr) {
LogError(SDL_GetError());
return false;
}
bgfx::Init bgfxInit;
bgfxInit.type = bgfx::RendererType::Count; // Automatically choose a renderer.
bgfxInit.resolution.width = app_state.window_width;
bgfxInit.resolution.height = app_state.window_height;
bgfxInit.resolution.reset = BGFX_RESET_VSYNC;
#if !BX_PLATFORM_EMSCRIPTEN
SDL_SysWMinfo wmi;
SDL_VERSION(&wmi.version);
if (!SDL_GetWindowWMInfo(app_state.window, &wmi)) {
LogError(SDL_GetError());
}
#endif
#if BX_PLATFORM_WINDOWS
bgfxInit.platformData.nwh = wmi.info.win.window;
#elif BX_PLATFORM_OSX
bgfxInit.platformData.nwh = wmi.info.cocoa.window;
bgfx::renderFrame();
#elif BX_PLATFORM_LINUX
bgfxInit.platformData.ndt = wmi.info.x11.display;
bgfxInit.platformData.nwh = (void*)(uintptr_t)wmi.info.x11.window;
#elif BX_PLATFORM_EMSCRIPTEN
bgfxInit.platformData.nwh = (void*)"#canvas";
#endif
if (!bgfx::init(bgfxInit)) {
LogError("bgfx initialization failed");
return false;
}
bgfx::setDebug(BGFX_DEBUG_TEXT);
bgfx::setViewRect(K::Graphics::K_VIEW_LOGO, 0, 0, app_state.window_width, app_state.window_height);
float view[16];
bx::mtxTranslate(view, 0.f, 0.f, 1.0f);
float proj[16];
bx::mtxOrtho(proj, 0.0f, app_state.window_width, 0.0f, app_state.window_height, 0.1f, 100.0f, 0.f, bgfx::getCaps()->homogeneousDepth);
bgfx::setViewTransform(K::Graphics::K_VIEW_LOGO, view, proj);
K::Resource::Init();
K::UI::Init(app_state.window);
if (!K::Graphics::Init(app_state.window_width, app_state.window_height)) {
LogError("Graphics init failed");
return false;
}
logo = Resource::Load<Resource::K_R_Still>("Keishiki.png");
state.width = 1280;
state.height = 550;
state.current_frame = 0;
state.fps = 30.0f;
state.frame_max = 200;
// state.plugboard.in = Plugboard::K_P_CompositionIn{};
// state.plugboard.out = Plugboard::K_P_CompositionOut{};
K::UI::SetupViewport(state);
return true;
}
void Render() {
K::Graphics::DrawTextureImageAlpha(K::Graphics::K_VIEW_LOGO, logo, app_state.window_width - logo->w + 10, 0, .4f);
UI::Draw(app_state.bgfx_frame, state);
const bgfx::Stats *stat = bgfx::getStats();
const bgfx::Caps *caps = bgfx::getCaps();
bgfx::dbgTextClear();
bgfx::dbgTextPrintf(0, app_state.window_height/16 - 8, 0xf8,
" lachrymal.net :: %s Renderer :: Max Views %u :: Max FBs %u :: Max Texture Samplers %u :: Blitting %s :: %u FPS",
caps->supported & BGFX_CAPS_RENDERER_MULTITHREADED ? "Multithreaded" : "Singlethreaded",
caps->limits.maxViews, caps->limits.maxFrameBuffers,
caps->limits.maxTextureSamplers, caps->supported & BGFX_CAPS_TEXTURE_BLIT ? "OK" : "NO",
stat->cpuTimerFreq / stat->cpuTimeFrame);
bgfx::dbgTextPrintf(0, app_state.window_height/16 - 7, 0xf8,
" Project Keishiki :: %s :: Build %s %s :: SDL %i.%i.%i :: bgfx 1.%i :: Dear ImGui %s :: %s",
BX_COMPILER_NAME, __DATE__, __TIME__, SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL,
BGFX_API_VERSION, ImGui::GetVersion(), bgfx::getRendererName(bgfx::getRendererType()));
app_state.bgfx_frame = bgfx::frame();
}
void Run() {
app_state.init_time = SDL_GetTicks64();
app_state.last_time = app_state.init_time;
while (app_state.running.load()) {
app_state.current_time = SDL_GetTicks64();
app_state.delta_t = app_state.current_time - app_state.last_time;
for (SDL_Event current_event; SDL_PollEvent(&current_event) != 0;) {
ImGui_ImplSDL2_ProcessEvent(&current_event);
if (current_event.type == SDL_QUIT) {
app_state.running.store(false);
break;
}
}
Render();
app_state.last_time = app_state.current_time;
}
}
void Shutdown() {
K::Graphics::Shutdown();
K::UI::Shutdown(state);
K::Resource::Shutdown();
bgfx::shutdown();
SDL_DestroyWindow(app_state.window);
SDL_Quit();
}
}
int main(int argc, char *argv[]) {
if (!K::Init())
return EXIT_FAILURE;
K::Run();
K::Shutdown();
return EXIT_SUCCESS;
}