NouVeL/ADVect/ADVect.cpp

194 lines
5 KiB
C++

#include "ADVect.h"
#include "Graphics.h"
#include <SDL2/SDL_syswm.h>
#include <bgfx/bgfx.h>
#include <bgfx/platform.h>
#include <chrono>
#include <iostream>
namespace {
SDL_Window* window;
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;
char* date;
}
namespace ADVect {
void Init(std::string name, const std::vector<NVL::Parse::Scene>& sc) {
std::time_t now_time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
date = std::ctime(&now_time);
date[10] = '\0';
date = &date[4];
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;
return;
}
window = SDL_CreateWindow(m_name.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, m_width, m_height, SDL_WINDOW_SHOWN);
if (window == NULL) {
std::cerr << "ADV: Failed to create window: " << SDL_GetError() << std::endl;
return;
}
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 << "ADV: 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
if (!bgfx::init(bgfxInit)) {
std::cerr << "ADV: Failed bgfx initialization" << std::endl;
return;
};
bgfx::setDebug(BGFX_DEBUG_TEXT);
bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x443322ff, 1.0f, 0);
bgfx::setViewRect(0, 0, 0, m_width, m_height);
bgfx::setViewMode(0, bgfx::ViewMode::Sequential);
ADVect::Graphics::Init(m_width, m_height);
NVL::Environment::ENVIRONMENT.enter(u"Say", NVL::Environment::Variable([&](std::vector<NVL::Environment::Variable> args) {
m_text = args;
return NVL::Environment::Type::Nil;
}, 2));
NVL::Environment::ENVIRONMENT.enter(u"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() {
Graphics::Shutdown();
bgfx::shutdown();
SDL_DestroyWindow(window);
SDL_Quit();
}
void Advance() {
size_t curr_size = scenes[current_scene].get().size();
while (scene_pos < curr_size) {
if (std::get<NVL::String>(scenes[current_scene].get()[scene_pos][0].value) == u"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<NVL::String>(scenes[current_scene].get()[scene_pos + 1][0].value) == u"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() {
bgfx::touch(0);
ADVect::Graphics::DrawRandomShit();
ADVect::Graphics::RenderString(speaker, 100, 150, 0xFFFFFFFF, ADVect::Graphics::TEXT_BOLD);
ADVect::Graphics::RenderStringMarkup(m_text, 300, 90, 0xFFFFFFFF);
bgfx::dbgTextClear();
bgfx::dbgTextPrintf(0, 44, 0xF0, "NouVeL x ADVect :: %s 2022", date);
bgfx::dbgTextPrintf(0, 43, 0xF0, "Current Position: %u", scene_pos);
bgfx::dbgTextPrintf(0, 42, 0xF0, "Current Scene: %s", NVL::to_std_string(scenes[current_scene].name).c_str());
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 = "..\\..\\..\\..\\NVL\\";
std::vector<NVL::Parse::Scene> SCENES = NVL::Parse::ParseFile(PJ_DIR + "dialogue.nvl");
ADVect::Init("ADV", SCENES);
ADVect::Run();
ADVect::Shutdown();
return 0;
}