NouVeL/ADVect/ADVect.cpp
2022-08-18 12:17:43 -04:00

182 lines
4.5 KiB
C++

#include "ADVect.h"
#include "Graphics.h"
#include <SDL2/SDL_syswm.h>
#include <bgfx/bgfx.h>
#include <bgfx/platform.h>
#include <iostream>
namespace {
SDL_Window* window;
// SDL_Renderer* renderer;
std::string m_name;
bool running = false;
u16 m_width = 1280;
u16 m_height = 720;
bool m_keys[65536]{};
std::vector<NVL::Parse::Scene> scenes;
u32 current_scene = 0;
std::vector<NVL::Environment::Variable> m_text{};
NVL::String speaker;
u32 scene_pos = 0;
}
namespace ADVect {
void Init(std::string name, const std::vector<NVL::Parse::Scene>& sc) {
m_name = name;
scenes = sc; // sure make a copy whatever
if (SDL_Init(SDL_INIT_VIDEO)) {
std::cerr << "Failed to init SDL: " << SDL_GetError() << std::endl;
}
window = SDL_CreateWindow(m_name.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, m_width, m_height, SDL_WINDOW_SHOWN);
//renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
//SDL_SetRenderDrawColor(renderer, 50, 50, 50, 255);
bgfx::Init bgfxInit;
bgfxInit.type = bgfx::RendererType::Count; // Automatically choose a renderer.
bgfxInit.resolution.width = m_width;
bgfxInit.resolution.height = m_height;
bgfxInit.resolution.reset = BGFX_RESET_VSYNC;
#if !BX_PLATFORM_EMSCRIPTEN
SDL_SysWMinfo wmi;
SDL_VERSION(&wmi.version);
if (!SDL_GetWindowWMInfo(window, &wmi)) {
std::cerr << "SDL_SysWMinfo could not be retrieved: " << SDL_GetError() << std::endl;
}
#endif
#if BX_PLATFORM_WINDOWS
bgfxInit.platformData.nwh = wmi.info.win.window;
#elif BX_PLATFORM_OSX
bgfxInit.platformData.nwh = wmi.info.cocoa.window;
#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
bgfx::init(bgfxInit);
bgfx::setDebug(BGFX_DEBUG_TEXT);
bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x443355ff, 1.0f, 0);
bgfx::setViewRect(0, 0, 0, m_width, m_height);
ADVect::Graphics::Init();
NVL::Environment::ENVIRONMENT.enter("Say", NVL::Environment::Variable([&](std::vector<NVL::Environment::Variable> args) {
m_text = args;
return NVL::Environment::Type::Nil;
}, 2));
NVL::Environment::ENVIRONMENT.enter("SwitchSpeaker", NVL::Environment::Variable([&](std::vector<NVL::Environment::Variable> args) {
speaker = std::get<NVL::String>(NVL::Environment::Variable(args[0]).value);
return NVL::Environment::Type::Nil;
}, 1));
Advance();
}
void Shutdown() {
bgfx::shutdown();
// SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
void Advance() {
int curr_size = scenes[current_scene].get().size();
while (scene_pos < curr_size) {
if (std::get<std::string>(scenes[current_scene].get()[scene_pos][0].value) == "Say") {
NVL::Environment::Apply(scenes[current_scene].get()[scene_pos]);
scene_pos++;
return;
}
else {
NVL::Environment::Apply(scenes[current_scene].get()[scene_pos]);
scene_pos++;
}
switch (curr_size - scene_pos) {
case 0:
current_scene = -1;
return;
case 1:
return;
default:
if (std::get<std::string>(scenes[current_scene].get()[scene_pos + 1][0].value) == "Say") {
NVL::Environment::Apply(scenes[current_scene].get()[scene_pos]);
scene_pos++;
return;
}
break;
}
}
}
void Update() {
if (m_keys[SDL_SCANCODE_SPACE]) {
if (current_scene == -1) {
running = false;
return;
}
Advance();
m_keys[SDL_SCANCODE_SPACE] = false;
}
}
void Render() {
//SDL_RenderClear(renderer);
bgfx::touch(0);
ADVect::Graphics::RenderString(speaker, 50, 300, { 255, 255, 255, 255 });
ADVect::Graphics::RenderStringMarkdown(m_text[0], 50, 600, { 255, 255, 255, 255 });
bgfx::dbgTextClear();
bgfx::dbgTextPrintf(5, 35, 0xf0, "alivemaster TNBL");
//SDL_RenderPresent(renderer);
//SDL_Delay(10);
bgfx::frame();
}
void Run() {
running = true;
while (running) {
for (SDL_Event event; SDL_PollEvent(&event);) {
switch (event.type) {
case SDL_QUIT:
running = false;
break;
case SDL_KEYDOWN:
m_keys[event.key.keysym.scancode] = true;
break;
case SDL_KEYUP:
m_keys[event.key.keysym.scancode] = false;
break;
}
}
Update();
Render();
}
}
}
int main(int argc, char* argv[]) {
const std::string PJ_DIR = "E:\\Archive\\Projects\\NouVeL\\NVL\\";
std::vector<NVL::Parse::Scene> SCENES = NVL::Parse::ParseFile(PJ_DIR + "dialogue.nvl");
ADVect::Init("Test", SCENES);
ADVect::Run();
ADVect::Shutdown();
return 0;
}