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-27 03:04:39 -04:00
|
|
|
#include <bx/math.h>
|
2022-08-18 12:17:43 -04:00
|
|
|
|
2022-05-09 01:50:09 -04:00
|
|
|
#include <iostream>
|
2022-08-27 03:04:39 -04:00
|
|
|
#include <map>
|
2022-08-18 12:17:43 -04:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
SDL_Window* window;
|
|
|
|
|
|
|
|
std::string m_name;
|
2022-08-27 03:04:39 -04:00
|
|
|
volatile bool running = false;
|
2022-08-18 12:17:43 -04:00
|
|
|
|
|
|
|
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-27 03:04:39 -04:00
|
|
|
struct ImageTrack {
|
|
|
|
std::string name;
|
|
|
|
ADVect::Graphics::ImageTexture* current = nullptr;
|
|
|
|
i32 pos_x = 0, pos_y = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<ImageTrack> tracks_img{};
|
|
|
|
ImageTrack* find_image_track(const std::string& s) {
|
|
|
|
for (auto& t : tracks_img)
|
|
|
|
if (t.name == s) {
|
|
|
|
return &t;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
bool add_image_track(const std::string& s) {
|
|
|
|
if (find_image_track(s) == nullptr) {
|
|
|
|
ImageTrack t;
|
|
|
|
t.name = s;
|
|
|
|
tracks_img.push_back(std::move(t));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
std::cerr << "ADV: Redefinition of ImageTrack " << s << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
2022-08-18 12:17:43 -04:00
|
|
|
}
|
2022-05-09 01:50:09 -04:00
|
|
|
|
|
|
|
namespace ADVect {
|
2022-08-27 03:04:39 -04:00
|
|
|
bool Init(std::string name, const std::vector<NVL::Parse::Scene>& sc) {
|
2022-08-18 12:17:43 -04:00
|
|
|
m_name = name;
|
|
|
|
scenes = sc; // sure make a copy whatever
|
|
|
|
|
|
|
|
if (SDL_Init(SDL_INIT_VIDEO)) {
|
2022-08-27 03:04:39 -04:00
|
|
|
std::cerr << "ADV: Failed to init SDL: " << SDL_GetError() << std::endl;
|
|
|
|
return false;
|
2022-05-09 01:50:09 -04:00
|
|
|
}
|
2022-08-18 12:17:43 -04:00
|
|
|
|
2022-08-27 03:04:39 -04:00
|
|
|
window = SDL_CreateWindow(m_name.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, m_width, m_height, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
|
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-27 03:04:39 -04:00
|
|
|
return false;
|
2022-08-21 16:24:13 -04:00
|
|
|
}
|
2022-08-27 03:04:39 -04:00
|
|
|
|
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-27 03:04:39 -04:00
|
|
|
return false;
|
2022-08-21 16:24:13 -04:00
|
|
|
};
|
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-27 03:04:39 -04:00
|
|
|
float view[16];
|
|
|
|
bx::mtxTranslate(view, 0.f, 0.f, 1.0f);
|
|
|
|
float proj[16];
|
|
|
|
bx::mtxOrtho(proj, 0.0f, m_width, 0.0f, m_height, 0.1f, 100.0f, 0.f, bgfx::getCaps()->homogeneousDepth);
|
|
|
|
bgfx::setViewTransform(0, view, proj);
|
|
|
|
|
|
|
|
if (!ADVect::Graphics::Init(m_width, m_height)) {
|
|
|
|
std::cerr << "ADV: Graphics init failed" << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
2022-08-18 12:17:43 -04:00
|
|
|
|
2022-05-09 03:46:44 -04:00
|
|
|
Advance();
|
2022-08-27 03:04:39 -04:00
|
|
|
return true;
|
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-27 03:04:39 -04:00
|
|
|
while (scene_pos < curr_size && 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++;
|
|
|
|
}
|
|
|
|
if (curr_size == scene_pos) {
|
|
|
|
running = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
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++;
|
2022-05-09 03:46:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-18 12:17:43 -04:00
|
|
|
void Update() {
|
2022-05-09 03:46:44 -04:00
|
|
|
if (m_keys[SDL_SCANCODE_SPACE]) {
|
|
|
|
Advance();
|
|
|
|
m_keys[SDL_SCANCODE_SPACE] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-18 12:17:43 -04:00
|
|
|
void Render() {
|
|
|
|
bgfx::touch(0);
|
|
|
|
|
2022-08-27 03:04:39 -04:00
|
|
|
// ADVect::Graphics::DrawFunkyAhhShit();
|
|
|
|
for (auto& t : tracks_img) {
|
|
|
|
if (t.current != nullptr)
|
|
|
|
ADVect::Graphics::DrawTextureImage(*t.current, t.pos_x, t.pos_y);
|
|
|
|
}
|
2022-08-22 02:15:25 -04:00
|
|
|
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-27 03:04:39 -04:00
|
|
|
bgfx::dbgTextPrintf(0, 44, 0xF0, "NouVeL x ADVect :: %s :: %s %s", BX_COMPILER_NAME, __DATE__, __TIME__);
|
|
|
|
bgfx::dbgTextPrintf(0, 43, 0xF0, "SDL %i.%i.%2i :: bgfx 1.%i :: %s", SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL, BGFX_API_VERSION, bgfx::getRendererName(bgfx::getRendererType()));
|
|
|
|
bgfx::dbgTextPrintf(0, 42, 0xF0, "Current Position: %u", scene_pos);
|
|
|
|
bgfx::dbgTextPrintf(0, 41, 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[]) {
|
2022-08-20 22:12:11 -04:00
|
|
|
const std::string PJ_DIR = "..\\..\\..\\..\\NVL\\";
|
2022-05-09 01:50:09 -04:00
|
|
|
|
2022-08-27 03:04:39 -04:00
|
|
|
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));
|
|
|
|
|
|
|
|
NVL::Environment::ENVIRONMENT.enter(u"ImageTrack", NVL::Environment::Variable([](std::vector<NVL::Environment::Variable> args) {
|
|
|
|
add_image_track(NVL::to_std_string(std::get<NVL::String>(NVL::Environment::Variable(args[0]).value)));
|
|
|
|
return NVL::Environment::Type::Nil;
|
|
|
|
}, 1));
|
|
|
|
|
|
|
|
NVL::Environment::ENVIRONMENT.enter(u"Show", NVL::Environment::Variable([](std::vector<NVL::Environment::Variable> args) {
|
|
|
|
std::string name = NVL::to_std_string(std::get<NVL::String>(NVL::Environment::Variable(args[0]).value));
|
|
|
|
ImageTrack* t = find_image_track(name);
|
|
|
|
if (t == nullptr) {
|
|
|
|
std::cerr << "ADV: Cannot find ImageTrack " << name << std::endl;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
t->current = ADVect::Graphics::GetImageTextureFromFile(NVL::to_std_string(std::get<NVL::String>(NVL::Environment::Variable(args[1]).value)));
|
|
|
|
}
|
|
|
|
return NVL::Environment::Type::Nil;
|
|
|
|
}, 2));
|
|
|
|
|
|
|
|
std::vector<NVL::Parse::Scene> SCENES = NVL::Parse::ParseFile(PJ_DIR + "dante_de.nvl");
|
|
|
|
if (SCENES.empty()) return EXIT_FAILURE;
|
|
|
|
|
|
|
|
if (!ADVect::Init("ADV", SCENES)) return EXIT_FAILURE;
|
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-08-27 03:04:39 -04:00
|
|
|
return EXIT_SUCCESS;
|
2022-05-09 01:50:09 -04:00
|
|
|
}
|