rudimentary serialization + NVL Director

This commit is contained in:
lachrymaL 2023-04-08 02:33:37 -04:00
parent 3a40e5697d
commit 6903c2d3ca
No known key found for this signature in database
GPG key ID: F3640ACFA174B1C1
10 changed files with 140 additions and 103 deletions

View file

@ -3,6 +3,7 @@
#include "Graphics.h" #include "Graphics.h"
#include "Track.h" #include "Track.h"
#include "Compiler.h" #include "Compiler.h"
#include "Director.h"
#include <SDL.h> #include <SDL.h>
#include <SDL_main.h> #include <SDL_main.h>
@ -25,16 +26,14 @@ namespace {
u16 window_height = 720; u16 window_height = 720;
u64 init_time, last_time, current_time, delta_t; u64 init_time, last_time, current_time, delta_t;
bool running = false; bool running = false, rolling = true;
bool draw_ui = true; bool draw_ui = true;
bool m_keys[65536]{}; // terrible bool m_keys[65536]{}; // terrible
bool mouse[256]{ true }; // terrible bool mouse[256]{ true }; // terrible
std::vector<NVL::Parse::Scene> scenes; std::unique_ptr<NVL::Director::Director> director;
u32 current_scene = 0;
u32 scene_pos = 0;
ADVect::MarkupTextTransitionTrack m_text{ ADVect::MarkupTextTransitionTrack m_text{
.current{}, .current{},
@ -65,8 +64,8 @@ namespace {
} }
namespace ADVect { namespace ADVect {
bool Init(std::string name, const std::vector<NVL::Parse::Scene>& sc) { bool Init(std::string name, const NVL::Compiler::NVLGraph& g) {
scenes = sc; // sure make a copy whatever director = std::make_unique<NVL::Director::Director>(g);
if (SDL_Init(SDL_INIT_VIDEO)) { if (SDL_Init(SDL_INIT_VIDEO)) {
std::cerr << "ADV: Failed to init SDL: " << SDL_GetError() << std::endl; std::cerr << "ADV: Failed to init SDL: " << SDL_GetError() << std::endl;
@ -140,21 +139,15 @@ namespace ADVect {
void Advance() { void Advance() {
if (!m_text.transition.done) m_text.transition.done = true; if (!m_text.transition.done) m_text.transition.done = true;
else { else if (director->active) {
size_t curr_size = scenes[current_scene].get().size(); rolling = true;
while (scene_pos < curr_size && std::get<NVL::String>(scenes[current_scene].get()[scene_pos][0].value) != u"Say") { director->Advance();
NVL::Environment::Apply(scenes[current_scene].get()[scene_pos]);
scene_pos++;
} }
if (curr_size == scene_pos) { else
running = false; running = false;
return;
} if (running && rolling)
if (std::get<NVL::String>(scenes[current_scene].get()[scene_pos][0].value) == u"Say") { Advance();
NVL::Environment::Apply(scenes[current_scene].get()[scene_pos]);
scene_pos++;
}
}
} }
void Update() { void Update() {
@ -207,8 +200,7 @@ namespace ADVect {
bgfx::dbgTextPrintf(0, 0, 0xf8, " %u FPS", stat->cpuTimerFreq / stat->cpuTimeFrame); bgfx::dbgTextPrintf(0, 0, 0xf8, " %u FPS", stat->cpuTimerFreq / stat->cpuTimeFrame);
bgfx::dbgTextPrintf(0, 1, 0xf8, " NouVeL x ADVect :: %s :: Build %s %s", BX_COMPILER_NAME, __DATE__, __TIME__); bgfx::dbgTextPrintf(0, 1, 0xf8, " NouVeL x ADVect :: %s :: Build %s %s", BX_COMPILER_NAME, __DATE__, __TIME__);
bgfx::dbgTextPrintf(0, 2, 0xf8, " 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, 2, 0xf8, " 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, 3, 0xf8, " Current Position: %u", scene_pos); bgfx::dbgTextPrintf(0, 3, 0xf8, " NVL Director State: %s", NVL::to_std_string(director->description).c_str());
bgfx::dbgTextPrintf(0, 4, 0xf8, " Current Scene: %s", NVL::to_std_string(scenes[current_scene].name).c_str());
bgfx::frame(); bgfx::frame();
} }
@ -254,7 +246,6 @@ namespace ADVect {
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
std::filesystem::current_path("E:\\Archive\\Projects\\NouVeL\\ADVect\\runtime"); std::filesystem::current_path("E:\\Archive\\Projects\\NouVeL\\ADVect\\runtime");
//std::filesystem::current_path("/Users/lachrymal/Projects/NouVeL/ADVect/runtime/"); //std::filesystem::current_path("/Users/lachrymal/Projects/NouVeL/ADVect/runtime/");
NVL::Environment::ENVIRONMENT.enter({ NVL::Environment::ENVIRONMENT.enter({
{ {
u"Say", u"Say",
@ -262,6 +253,7 @@ int main(int argc, char* argv[]) {
NVL::Environment::MarkupString str = NVL::Environment::UnpackMarkupVariable(std::get<std::vector<NVL::Environment::Variable>>(args[0].value)); NVL::Environment::MarkupString str = NVL::Environment::UnpackMarkupVariable(std::get<std::vector<NVL::Environment::Variable>>(args[0].value));
size_t len = str.length(); size_t len = str.length();
m_text.change(current_time, str, len * 10, [len](NVL::Number x) { return static_cast<size_t>(x * len); }); m_text.change(current_time, str, len * 10, [len](NVL::Number x) { return static_cast<size_t>(x * len); });
rolling = false;
return NVL::Environment::Type::Nil; return NVL::Environment::Type::Nil;
}, 1) }, 1)
}, },
@ -302,19 +294,27 @@ int main(int argc, char* argv[]) {
} }
}); });
std::vector<NVL::Parse::Scene> SCENES = NVL::Parse::ParseFile("dialogue.nvl"); //std::vector<NVL::Parse::Scene> scenes = NVL::Parse::ParseFile("dialogue.nvl");
if (SCENES.empty()) { //if (scenes.empty()) {
std::cerr << "Main: Empty NVL parse, check file" << std::endl; // std::cerr << "Main: Empty NVL parse, check file" << std::endl;
return EXIT_FAILURE; // return EXIT_FAILURE;
} //}
//auto a = NVL::Compiler::Compile(SCENES, 0); //auto a = NVL::Compiler::Compile(scenes, 0);
//auto b = NVL::Compiler::Serialize(a); //auto b = NVL::Compiler::Serialize(a);
//std::istringstream c{b.str()}; std::ifstream f;
//auto d = NVL::Compiler::Deserialize(c); f.open("mmoker.nvlb", std::ios::binary);
if (!ADVect::Init("ADV Test", SCENES)) return EXIT_FAILURE; std::stringstream c;
c << f.rdbuf();
f.close();
std::istringstream d(c.str());
auto e = NVL::Compiler::Deserialize(d);
if (!ADVect::Init("ADV Test", e)) return EXIT_FAILURE;
ADVect::Run(); ADVect::Run();
ADVect::Shutdown(); ADVect::Shutdown();

BIN
ADVect/runtime/mmoker.nvlb Normal file

Binary file not shown.

View file

@ -17,7 +17,9 @@
}, },
"vendor": { "vendor": {
"microsoft.com/VisualStudioSettings/CMake/1.0": { "microsoft.com/VisualStudioSettings/CMake/1.0": {
"hostOS": [ "Windows" ] "hostOS": [
"Windows"
]
} }
} }
}, },
@ -33,12 +35,23 @@
}, },
"vendor": { "vendor": {
"microsoft.com/VisualStudioSettings/CMake/1.0": { "microsoft.com/VisualStudioSettings/CMake/1.0": {
"hostOS": [ "Linux" ] "hostOS": [
"Linux"
]
}, },
"microsoft.com/VisualStudioRemoteSettings/CMake/1.0": { "microsoft.com/VisualStudioRemoteSettings/CMake/1.0": {
"sourceDir": "$env{HOME}/.vs/$ms{projectDirName}" "sourceDir": "$env{HOME}/.vs/$ms{projectDirName}"
} }
} }
},
{
"name": "x64-release",
"displayName": "x64 Release",
"inherits": "windows-default",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release",
"CMAKE_MSVC_RUNTIME_LIBRARY": "MultiThreaded"
}
} }
] ]
} }

View file

@ -5,7 +5,7 @@ cmake_minimum_required (VERSION 3.8)
project (NVL) project (NVL)
include_directories("include") include_directories("include")
add_library (NVL STATIC "Parser.cpp" "Environment.cpp" "Environment.h" "Common.h" "Compiler.cpp" "Compiler.h" "Director.h") add_library (NVL STATIC "Parser.cpp" "Environment.cpp" "Environment.h" "Common.h" "Compiler.cpp" "Compiler.h" "Director.h" "Director.cpp")
# add_executable (NVL "NouVeL.cpp" "Parser.cpp" "Environment.cpp" "Environment.h" "Common.h" ) # add_executable (NVL "NouVeL.cpp" "Parser.cpp" "Environment.cpp" "Environment.h" "Common.h" )
# TODO: Add tests and install targets if needed. # TODO: Add tests and install targets if needed.

View file

@ -69,7 +69,7 @@ namespace {
return vs; return vs;
} }
case Type::Nil: case Type::Nil:
return {}; return {Type::Nil};
case Type::Number: case Type::Number:
case Type::StaticReference: case Type::StaticReference:
{ {
@ -119,6 +119,7 @@ namespace NVL::Compiler {
// TODO route nodes when JUMP // TODO route nodes when JUMP
n.sequence.push_back(s); n.sequence.push_back(s);
n.description = scenes[entry_scene_index].name;
} }
nodes.push_back(n); nodes.push_back(n);
return NVLGraph{ nodes, procedure_names }; return NVLGraph{ nodes, procedure_names };
@ -133,6 +134,7 @@ namespace NVL::Compiler {
} }
SerializeDirect(s, static_cast<u16>(g.nodes.size())); SerializeDirect(s, static_cast<u16>(g.nodes.size()));
for (const _DirectedGraphNode& n : g.nodes) { for (const _DirectedGraphNode& n : g.nodes) {
SerializeString(s, n.description);
SerializeDirect(s, static_cast<u16>(n.sequence.size())); SerializeDirect(s, static_cast<u16>(n.sequence.size()));
for (const Vector<Environment::Variable>& c : n.sequence) { for (const Vector<Environment::Variable>& c : n.sequence) {
SerializeDirect(s, static_cast<u16>(c.size())); SerializeDirect(s, static_cast<u16>(c.size()));
@ -156,6 +158,7 @@ namespace NVL::Compiler {
DeserializeDirect(buf, size); DeserializeDirect(buf, size);
for (u16 i = 0; i < size; i++) { for (u16 i = 0; i < size; i++) {
_DirectedGraphNode n{}; _DirectedGraphNode n{};
n.description = DeserializeString(buf);
u16 size2{}; u16 size2{};
DeserializeDirect(buf, size2); DeserializeDirect(buf, size2);
for (u16 j = 0; j < size2; j++) { for (u16 j = 0; j < size2; j++) {

View file

@ -8,6 +8,7 @@
namespace NVL::Compiler { namespace NVL::Compiler {
struct _DirectedGraphNode { struct _DirectedGraphNode {
String description;
Vector<Vector<Environment::Variable>> sequence; Vector<Vector<Environment::Variable>> sequence;
}; };
@ -16,7 +17,7 @@ namespace NVL::Compiler {
Vector<String> static_refs; Vector<String> static_refs;
}; };
Environment::Variable StaticEval(Vector<String>& refs, const Parse::Object& obj); Environment::Variable StaticEval(const Environment::Environment& env, Vector<String>& refs, const Parse::Object& obj);
NVLGraph Compile(const Vector<Parse::Scene>& scenes, i32 entry_scene_index); NVLGraph Compile(const Vector<Parse::Scene>& scenes, i32 entry_scene_index);

View file

@ -1,15 +1,35 @@
#include "Director.h" #include "Director.h"
namespace NVL::Director { namespace NVL::Director {
Director(const Compiler::NVLGraph& g, const Environment::Environment env) : g(g), current_position(0), environment(env) { Director::Director(const Compiler::NVLGraph& g) :
g(g), current_position(0), active(true), description(g.nodes[0].description), current_node(g.nodes[0]) {
for (const String& s : g.static_refs) for (const String& s : g.static_refs)
resolve.push_back(env.get(s)); resolve.push_back(Environment::ENVIRONMENT.get(s));
} }
String& GetSceneName() { Environment::Variable Director::CollectVariable(const Environment::Variable& v) {
if (v.type == Environment::Type::StaticReference)
return this->resolve[static_cast<u16>(std::get<Number>(v.value))];
else if (v.type == Environment::Type::Array) {
Vector<Environment::Variable> a{};
for (const auto& x : std::get<Vector<Environment::Variable>>(v.value))
a.push_back(CollectVariable(x));
return Environment::Variable(a);
}
else
return v;
} }
void Director::Advance() { void Director::Advance() {
if (current_position >= current_node.sequence.size()) {
this->active = false;
return;
}
const auto& cmd = this->current_node.sequence[current_position];
Vector<Environment::Variable> args{};
for (u32 i = 1; i < cmd.size(); i++) {
args.push_back(CollectVariable(cmd[i]));
}
std::get<std::function<Environment::Variable(Vector<Environment::Variable>)>>(CollectVariable(cmd[0]).value)(args);
this->current_position++;
} }
void Director::GetBacklog() { void Director::GetBacklog() {

View file

@ -6,15 +6,16 @@
namespace NVL::Director { namespace NVL::Director {
class Director { class Director {
private: private:
u64 current_position; u64 current_position{};
const Compiler::NVLGraph& g; const Compiler::_DirectedGraphNode& current_node{};
const Environment::Environment& environment; const Compiler::NVLGraph& g{};
Vector<Environment::Variable> resolve; Vector<Environment::Variable> resolve{};
Environment::Variable CollectVariable(const Environment::Variable& v);
public: public:
bool active; const String& description;
bool active = false;
Director() = delete; Director() = delete;
Director(const Compiler::NVLGraph& g, const Environment::Environment env); Director(const Compiler::NVLGraph& g);
String& GetSceneName();
void Advance(); void Advance();
void GetBacklog(); void GetBacklog();
void Save(); void Save();

View file

@ -1,10 +1,10 @@
#include "Environment.h" #include "Environment.h"
#include "Parser.h"
#include <stdexcept> #include <stdexcept>
namespace NVL::Environment { namespace NVL::Environment {
Environment ENVIRONMENT; Environment ENVIRONMENT;
Variable::Variable() : type(Type::Nil), length(0) { } Variable::Variable() : type(Type::Nil), length(0) { }
Variable::Variable(Type t) : type(t), length(0) { Variable::Variable(Type t) : type(t), length(0) {
if (t != Type::Nil) if (t != Type::Nil)
@ -45,7 +45,7 @@ namespace NVL::Environment {
env[name] = p; env[name] = p;
} }
Variable& Environment::get(const String& name) { const Variable& Environment::get(const String& name) const {
return env.at(name); return env.at(name);
} }
@ -79,11 +79,10 @@ namespace NVL::Environment {
Vector<Variable> args{}; Vector<Variable> args{};
Variable f = Eval(c[0]); Variable f = Eval(c[0]);
u32 i = 1; for (u32 i = 1; i < c.size(); i++) {
while (i < c.size()) { args.push_back(Eval(c[i]));
args.push_back(Eval(c[i++]));
} }
if (f.length != -1 && f.length != i - 1) throw std::runtime_error("Function arity mismatch"); if (f.length != -1 && f.length != c.size() - 1) throw std::runtime_error("Function arity mismatch");
return std::get<std::function<Variable(Vector<Variable>)>>(f.value)(args); return std::get<std::function<Variable(Vector<Variable>)>>(f.value)(args);
} }

View file

@ -6,7 +6,7 @@
#include "Common.h" #include "Common.h"
namespace NVL::Environment { namespace NVL::Environment {
enum class Type { StaticReference, Procedure, Number, String, Array, Nil }; enum class Type { Nil, StaticReference, Procedure, Number, String, Array };
struct Variable { struct Variable {
Type type; Type type;
@ -29,7 +29,7 @@ namespace NVL::Environment {
void enter(const String& name, Variable p); void enter(const String& name, Variable p);
void enter(std::initializer_list<std::pair<String, Variable>> p); void enter(std::initializer_list<std::pair<String, Variable>> p);
void set(const String& name, Variable p); void set(const String& name, Variable p);
Variable& get(const String& name); const Variable& get(const String& name) const;
bool exists(const String& name); bool exists(const String& name);
}; };