NouVeL/ADVect/ADVect.cpp

189 lines
4.8 KiB
C++
Raw Normal View History

2022-05-09 01:50:09 -04:00
#include "ADVect.h"
#include "Graphics.h"
2022-08-18 12:17:43 -04:00
#include <SDL2/SDL_syswm.h>
#include <bgfx/bgfx.h>
#include <bgfx/platform.h>
2022-08-22 02:15:25 -04:00
#include <chrono>
2022-05-09 01:50:09 -04:00
#include <iostream>
2022-08-18 12:17:43 -04:00
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;
2022-08-22 02:15:25 -04:00
2022-08-18 12:17:43 -04:00
}
2022-05-09 01:50:09 -04:00
namespace ADVect {
2022-08-18 12:17:43 -04:00
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;
2022-08-21 16:24:13 -04:00
return;
2022-05-09 01:50:09 -04:00
}
2022-08-18 12:17:43 -04:00
window = SDL_CreateWindow(m_name.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, m_width, m_height, SDL_WINDOW_SHOWN);
2022-08-21 16:24:13 -04:00
if (window == NULL) {
2022-08-22 02:15:25 -04:00
std::cerr << "ADV: Failed to create window: " << SDL_GetError() << std::endl;
2022-08-21 16:24:13 -04:00
return;
}
2022-08-18 12:17:43 -04:00
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)) {
2022-08-22 02:15:25 -04:00
std::cerr << "ADV: SDL_SysWMinfo could not be retrieved: " << SDL_GetError() << std::endl;
2022-08-18 12:17:43 -04:00
}
#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
2022-08-21 16:24:13 -04:00
if (!bgfx::init(bgfxInit)) {
2022-08-22 02:15:25 -04:00
std::cerr << "ADV: Failed bgfx initialization" << std::endl;
2022-08-21 16:24:13 -04:00
return;
};
2022-08-22 02:15:25 -04:00
bgfx::setDebug(BGFX_DEBUG_TEXT);
bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x443322ff, 1.0f, 0);
2022-08-18 12:17:43 -04:00
bgfx::setViewRect(0, 0, 0, m_width, m_height);
2022-08-22 02:15:25 -04:00
bgfx::setViewMode(0, bgfx::ViewMode::Sequential);
2022-08-18 12:17:43 -04:00
ADVect::Graphics::Init(m_width, m_height);
2022-05-09 03:46:44 -04:00
2022-08-22 02:15:25 -04:00
NVL::Environment::ENVIRONMENT.enter(u"Say", NVL::Environment::Variable([&](std::vector<NVL::Environment::Variable> args) {
2022-08-18 12:17:43 -04:00
m_text = args;
2022-05-09 03:46:44 -04:00
return NVL::Environment::Type::Nil;
2022-08-18 12:17:43 -04:00
}, 2));
2022-05-09 03:46:44 -04:00
2022-08-22 02:15:25 -04:00
NVL::Environment::ENVIRONMENT.enter(u"SwitchSpeaker", NVL::Environment::Variable([&](std::vector<NVL::Environment::Variable> args) {
2022-08-18 12:17:43 -04:00
speaker = std::get<NVL::String>(NVL::Environment::Variable(args[0]).value);
2022-05-09 03:46:44 -04:00
return NVL::Environment::Type::Nil;
}, 1));
Advance();
2022-05-09 01:50:09 -04:00
}
2022-08-18 12:17:43 -04:00
void Shutdown() {
2022-08-21 16:24:13 -04:00
Graphics::Shutdown();
bgfx::shutdown();
2022-08-18 12:17:43 -04:00
SDL_DestroyWindow(window);
2022-05-09 01:50:09 -04:00
SDL_Quit();
}
2022-08-18 12:17:43 -04:00
void Advance() {
2022-08-21 16:24:13 -04:00
size_t curr_size = scenes[current_scene].get().size();
2022-08-18 12:17:43 -04:00
while (scene_pos < curr_size) {
2022-08-22 02:15:25 -04:00
if (std::get<NVL::String>(scenes[current_scene].get()[scene_pos][0].value) == u"Say") {
2022-08-18 12:17:43 -04:00
NVL::Environment::Apply(scenes[current_scene].get()[scene_pos]);
scene_pos++;
2022-05-09 03:46:44 -04:00
return;
}
else {
2022-08-18 12:17:43 -04:00
NVL::Environment::Apply(scenes[current_scene].get()[scene_pos]);
scene_pos++;
2022-05-09 03:46:44 -04:00
}
2022-08-18 12:17:43 -04:00
switch (curr_size - scene_pos) {
2022-05-09 03:46:44 -04:00
case 0:
2022-08-18 12:17:43 -04:00
current_scene = -1;
2022-05-09 03:46:44 -04:00
return;
case 1:
return;
default:
2022-08-22 02:15:25 -04:00
if (std::get<NVL::String>(scenes[current_scene].get()[scene_pos + 1][0].value) == u"Say") {
2022-08-18 12:17:43 -04:00
NVL::Environment::Apply(scenes[current_scene].get()[scene_pos]);
scene_pos++;
2022-05-09 03:46:44 -04:00
return;
}
break;
}
}
}
2022-08-18 12:17:43 -04:00
void Update() {
2022-05-09 03:46:44 -04:00
if (m_keys[SDL_SCANCODE_SPACE]) {
2022-08-18 12:17:43 -04:00
if (current_scene == -1) {
running = false;
2022-05-09 03:46:44 -04:00
return;
}
Advance();
m_keys[SDL_SCANCODE_SPACE] = false;
}
}
2022-08-18 12:17:43 -04:00
void Render() {
bgfx::touch(0);
2022-08-22 02:15:25 -04:00
ADVect::Graphics::DrawRandomShit();
ADVect::Graphics::RenderString(speaker, 100, 150, 0xFFFFFFFF, ADVect::Graphics::TEXT_BOLD);
ADVect::Graphics::RenderStringMarkup(m_text, 300, 90, 0xFFFFFFFF);
2022-08-18 12:17:43 -04:00
bgfx::dbgTextClear();
2022-08-23 18:02:15 -04:00
bgfx::dbgTextPrintf(0, 44, 0xF0, "NouVeL x ADVect :: %s %s", __DATE__, __TIME__);
2022-08-22 02:15:25 -04:00
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());
2022-05-09 01:50:09 -04:00
2022-08-18 12:17:43 -04:00
bgfx::frame();
2022-05-09 01:50:09 -04:00
}
2022-05-09 03:46:44 -04:00
2022-08-18 12:17:43 -04:00
void Run() {
running = true;
while (running) {
2022-05-09 01:50:09 -04:00
for (SDL_Event event; SDL_PollEvent(&event);) {
switch (event.type) {
case SDL_QUIT:
2022-08-18 12:17:43 -04:00
running = false;
2022-05-09 01:50:09 -04:00
break;
2022-05-09 03:46:44 -04:00
case SDL_KEYDOWN:
m_keys[event.key.keysym.scancode] = true;
break;
case SDL_KEYUP:
m_keys[event.key.keysym.scancode] = false;
break;
2022-05-09 01:50:09 -04:00
}
2022-05-09 03:46:44 -04:00
}
Update();
2022-05-09 01:50:09 -04:00
Render();
}
}
}
int main(int argc, char* argv[]) {
const std::string PJ_DIR = "..\\..\\..\\..\\NVL\\";
2022-05-09 03:46:44 -04:00
std::vector<NVL::Parse::Scene> SCENES = NVL::Parse::ParseFile(PJ_DIR + "dialogue.nvl");
2022-05-09 01:50:09 -04:00
2022-08-22 02:15:25 -04:00
ADVect::Init("ADV", SCENES);
2022-08-18 12:17:43 -04:00
ADVect::Run();
2022-05-09 01:50:09 -04:00
2022-08-18 12:17:43 -04:00
ADVect::Shutdown();
2022-05-09 01:50:09 -04:00
return 0;
}