diff --git a/DMS/Connection.h b/DMS/Connection.h index 48f1da0..ad6c0d3 100644 --- a/DMS/Connection.h +++ b/DMS/Connection.h @@ -1,32 +1,50 @@ #pragma once #include #include +//#include +// Todo make this thread safe (I'm trying) namespace multi { template class connection { std::vector> funcs; - std::vector _funcs; + std::vector> _funcs; + std::string name; + //std::mutex mtx; public: + connection() { name = std::string("Connection_") + typeid(T).name(); } + connection(std::string name) { + this->name = name; + } int32_t connect(std::function func) { funcs.push_back(func); return (int32_t)funcs.size() - 1; } - int32_t connect(void(*func)()) { + int32_t connect(std::function func) { _funcs.push_back(func); return (_funcs.size()) * -1; } void fire(T arg) { + if (funcs.size() == 0) return; // Calls everything in the funcs and _funcs list - for (auto f : funcs) { - f(arg); + //std::lock_guard lck(mtx); + for (size_t i = 0; i < funcs.size(); i++) { + funcs[i](arg); } fire(); // Get the _funcs list as well } - void fire() { - // Calls everything in the funcs list only - for (auto f : _funcs) { - f(); + void fire(bool lock = true) { + if (_funcs.size() == 0) return; + if (lock) { + //std::lock_guard lck(mtx); + for (size_t i = 0; i < _funcs.size(); i++) { + _funcs[i](); + } + return; + } + //std::lock_guard lck(mtx); + for (size_t i = 0; i < _funcs.size(); i++) { + _funcs[i](); } } void remove(int32_t id) { @@ -44,7 +62,7 @@ namespace multi { int32_t operator+=(std::function func) { return connect(func); } - int32_t operator+=(void(*func)()) { + int32_t operator+=(std::function func) { return connect(func); } }; diff --git a/DMS/DMS.cpp b/DMS/DMS.cpp index 9f206a5..8ae13e8 100644 --- a/DMS/DMS.cpp +++ b/DMS/DMS.cpp @@ -3,7 +3,6 @@ #include #include "pch.h" #include "dms.h" -#include "gui.h" #include "actors.h" using namespace dms; @@ -22,40 +21,50 @@ int main() sf::RenderWindow window(sf::VideoMode(1024, 768, 32), "Background Test"); - gui::gui& root = gui::Root(); - //gui::framebase testframe(gui::DualDim(0, 0, 100, 100), gui::DualDim(0, 0, 0, 0)); - - gui::gui& test = root.newFrame(gui::DualDim(0, 0, 100, 100), gui::DualDim(0, 0, 0, 0)); - multi::alarm alarmtest(&run, 3, [&](multi::alarm* a) { - test.Offset.Position.Set(10, 10); + sf::Texture backgroundIMG; + backgroundIMG.loadFromFile("background.jpg"); + sf::Sprite sprite; + sf::Vector2u size = backgroundIMG.getSize(); + sprite.setTexture(backgroundIMG); + sprite.setOrigin(0, 0); + + state->invoker.registerFunction("setBG", [&](void* self, dms::dms_state* state, dms::dms_args* args) -> dms::value { + if (args->size() > 0 && (*args).args[0].type == dms::datatypes::string) { + try { + backgroundIMG.loadFromFile((*args).args[0].getString()); + } + catch (std::exception e) { + return dms::value(dms::utils::concat("Cannot load texture: \"", (*args).args[0].getString(), "\" from file!"), dms::datatypes::error); + } + return dms::nil; + } + return dms::value("Invalid argument, string expected for first argument!", dms::datatypes::error); }); - //std::cout << "Testing " << test.Parent; - //gui::framebase frame(gui::DualDim(0,0,100,100), gui::DualDim(0, 0, 0, 0)); - //sf::Texture texture; - //texture.loadFromFile("background.jpg"); - //sf::Sprite sprite; - //sf::Vector2u size = texture.getSize(); - //sprite.setTexture(texture); - //sprite.setOrigin(0, 0); + sf::Font font; + font.loadFromFile("font.ttf"); - //sf::Font font; - //font.loadFromFile("font.ttf"); - - //sf::Text text("Hello this is a test|!", font); - //text.setCharacterSize(30); - //text.setStyle(sf::Text::Bold); - //text.setFillColor(sf::Color::Black); - //auto test = text.getGlobalBounds(); - //std::cout << test.left << "," << test.top << "," << test.width << "," << test.height << std::endl; - //text.setPosition(11, 768 - 110 - (test.height-test.top)); + sf::Text text("Hello this is a test|!", font); + text.setCharacterSize(30); + text.setStyle(sf::Text::Bold); + text.setFillColor(sf::Color::Black); + auto test = text.getGlobalBounds(); + text.setPosition(11, 768 - 110 - (test.height-test.top)); ////std::cout << test << std::endl; - //sf::RectangleShape rectangle; - //rectangle.setSize(sf::Vector2f(1004, 100)); - //rectangle.setOutlineColor(sf::Color::Red); - //rectangle.setOutlineThickness(1); - //rectangle.setPosition(10, 768-110); + state->OnText += [&](dms::message msg) { + text.setString(dms::utils::concat(msg.chara->getName(),": ",msg.text)); + }; + state->OnAppendText += [&](dms::message msg) { + text.setString(dms::utils::concat(text.getString().toAnsiString(), msg.text)); + }; + + sf::RectangleShape rectangle; + rectangle.setSize(sf::Vector2f(1004, 100)); + rectangle.setOutlineColor(sf::Color::Red); + rectangle.setOutlineThickness(1); + rectangle.setPosition(10, 768-110); + //// run the program as long as the window is open while (window.isOpen()) @@ -67,6 +76,10 @@ int main() // "close requested" event: we close the window if (event.type == sf::Event::Closed) window.close(); + /*else if (event.type == sf::Event::Resized) { + sf::FloatRect visibleArea(0, 0, event.size.width, event.size.height); + window.setView(sf::View(visibleArea)); + }*/ } // clear the window with black color @@ -74,19 +87,19 @@ int main() if (!state->next(mem)) { // We should clean up some stuff here! + // We should exit and show an error if one exists + //std::cout << state->err.err_msg; break; } + // Update the runner run.update(); // draw everything here... - - gui::Draw(&window); - //testframe.Draw(&window); - //window.draw(sprite); - //window.draw(rectangle); - //window.draw(text); + window.draw(sprite); + window.draw(rectangle); + window.draw(text); // end the current frame window.display(); diff --git a/DMS/DMS.vcxproj b/DMS/DMS.vcxproj index 01d3582..69958dd 100644 --- a/DMS/DMS.vcxproj +++ b/DMS/DMS.vcxproj @@ -133,6 +133,7 @@ false C:\SFML\include;%(AdditionalIncludeDirectories) pch.h + MinSpace Console @@ -151,7 +152,6 @@ NDEBUG;_CONSOLE;%(PreprocessorDefinitions) true stdcpp17 - MinSpace C:\SFML\include;%(AdditionalIncludeDirectories) diff --git a/DMS/Invoker.cpp b/DMS/Invoker.cpp index daef6d3..88c27b9 100644 --- a/DMS/Invoker.cpp +++ b/DMS/Invoker.cpp @@ -3,14 +3,14 @@ #include "dms_state.h" #include "utils.h" namespace dms { - bool Invoker::registerFunction(std::string str, value (*f)(void*, dms_state*, dms_args*)) { + bool Invoker::registerFunction(std::string str, dms_func f) { if (preventOverwriting && funcs.count(str)) { return false; } funcs.insert_or_assign(str, f); return true; } - bool Invoker::registerFunction(std::string str, value (*f)(void*, dms_state*, dms_args*), bool preventoverride) { + bool Invoker::registerFunction(std::string str, dms_func f, bool preventoverride) { if (preventoverride && funcs.count(str)) { return false; } @@ -39,10 +39,10 @@ namespace dms { state->push_error(errors::error{ errors::non_existing_function, utils::concat("Attempt to call '",str,"' a nil value!") }); return value(datatypes::error); } - std::unordered_map Invoker::Export() { + std::unordered_map Invoker::Export() { return funcs; } - void Invoker::Import(std::unordered_map tempf) { + void Invoker::Import(std::unordered_map tempf) { for (auto const& x : tempf) { // Copy the contents of the imported invoker into diff --git a/DMS/Invoker.h b/DMS/Invoker.h index f5e620b..0415e07 100644 --- a/DMS/Invoker.h +++ b/DMS/Invoker.h @@ -1,21 +1,23 @@ #pragma once #include "pch.h" #include "errors.h" +#include namespace dms { + typedef std::function dms_func; struct dms_state; class Invoker { - std::unordered_mapfuncs; + std::unordered_mapfuncs; void* self = nullptr; public: bool preventOverwriting = true; - bool registerFunction(std::string str, value (*f)(void*, dms_state*, dms_args*)); - bool registerFunction(std::string str, value (*f)(void*, dms_state*, dms_args*), bool preventoverride); + bool registerFunction(std::string str, dms_func); + bool registerFunction(std::string str, dms_func, bool preventoverride); void _init(void* ref); value Invoke(std::string str, dms_state* state, dms_args* args); value Invoke(std::string str, void* ref, dms_state* state, dms_args* args); // Exports the methods from an Invoker object - std::unordered_map Export(); + std::unordered_map Export(); // Imports methods from another Invoker, this will add and overwrite any method with the same name - void Import(std::unordered_map tempf); + void Import(std::unordered_map tempf); }; } diff --git a/DMS/actors.h b/DMS/actors.h index 92207e1..2ce2605 100644 --- a/DMS/actors.h +++ b/DMS/actors.h @@ -24,7 +24,7 @@ namespace multi { this->milli = milli; t.start(); } - alarm(runner* run, void(*func)(alarm*)) : mbase(mtype::mAlarm,run) { + alarm(runner* run, std::function func) : mbase(mtype::mAlarm,run) { OnRing += func; milli = 1000; t.start(); @@ -93,7 +93,7 @@ namespace multi { }; class loop : public mbase { public: - loop(runner* run,void(*func)(loop*)) : mbase(mtype::mLoop,run) { + loop(runner* run, std::function func) : mbase(mtype::mLoop,run) { OnLoop += func; } void act(int id) override { @@ -161,7 +161,7 @@ namespace multi { long long milli; timer t; public: - tloop(runner* run, long long milli, void(*func)(tloop*)) : mbase(mtype::mTLoop, run) { + tloop(runner* run, long long milli, std::function func) : mbase(mtype::mTLoop, run) { OnLoop += func; this->milli = milli; } diff --git a/DMS/background2.jpg b/DMS/background2.jpg new file mode 100644 index 0000000..9a0dee5 Binary files /dev/null and b/DMS/background2.jpg differ diff --git a/DMS/core.cpp b/DMS/core.cpp index 3204080..533271f 100644 --- a/DMS/core.cpp +++ b/DMS/core.cpp @@ -23,7 +23,7 @@ namespace dms { return "nil"; } value concat(void* self, dms_state* state, dms_args* args) { - + return dms::value(); } void init(dms_state* state) { state->invoker.registerFunction("print", print); diff --git a/DMS/dms_state.h b/DMS/dms_state.h index a117281..63fa78f 100644 --- a/DMS/dms_state.h +++ b/DMS/dms_state.h @@ -9,7 +9,12 @@ #include "memory.h" #include "dms_list.h" #include "comparisons.h" +#include "Connection.h" namespace dms { + struct message { + character* chara; + std::string text; + }; struct Handler; value blockInvoke(void*, dms_state*, dms_args*); struct dms_state @@ -76,6 +81,10 @@ namespace dms { // This is called once and once only. Dynamically loading code is not a thing! void init(); bool hasError(); + // Connections + multi::connection OnText; + multi::connection OnAppendText; + multi::connection HandleHalt; private: // From what I gathered //std::mutex memory_mutex; diff --git a/DMS/dms_state_interpret.cpp b/DMS/dms_state_interpret.cpp index e1023c7..bf7faf3 100644 --- a/DMS/dms_state_interpret.cpp +++ b/DMS/dms_state_interpret.cpp @@ -271,7 +271,7 @@ namespace dms { auto ret = inv->Invoke(fname, cust.c, this, &args); if (assn.type != datatypes::nil) { if (!assign(assn, ret)) { - return false; + return error(ret.getString()); } } } @@ -300,7 +300,7 @@ namespace dms { ret = getEnvironment(obj)->Invoke(funcname, this, &args); } if (ret.type == datatypes::error) - return false; + return error(ret.getString()); if (assn.type != datatypes::nil) { if (!assign(assn, ret)) { return false; @@ -567,8 +567,12 @@ namespace dms { } break; case APND: - if (!handler->handleMessageAppend(this, c->args.args[0].resolve(this).getPrintable())) - return false; + OnAppendText.fire(message{ + speaker, + c->args.args[0].resolve(this).getPrintable() + }); + //if (!handler->handleMessageAppend(this, c->args.args[0].resolve(this).getPrintable())) + // return false; break; case CHAR: { @@ -578,8 +582,12 @@ namespace dms { } case DISP: { - if (!handler->handleMessageDisplay(this, c->args.args[0].resolve(this).getPrintable())) - return false; + OnText.fire(message{ + speaker, + c->args.args[0].resolve(this).getPrintable() + }); + /*if (!handler->handleMessageDisplay(this, c->args.args[0].resolve(this).getPrintable())) + return false;*/ } break; case ASGN: @@ -807,7 +815,7 @@ namespace dms { ret = getEnvironment(obj)->Invoke(funcname, this, &args); } if (ret.type == datatypes::error) - return false; + return error(ret.getString()); if (assn.type != datatypes::nil) { if (!assign(assn, ret)) { return false; @@ -833,8 +841,9 @@ namespace dms { else { ret = invoker.Invoke(funcname, this, &args); } - if (ret.type == datatypes::error) - return false; + if (ret.type == datatypes::error) { + return error(ret.getString()); + } if (assn.type != datatypes::nil) { if (!assign(assn, ret)) { return false; @@ -1048,6 +1057,7 @@ namespace dms { //wait(); //sleep(700); std::cout << std::endl; + //HandleHalt.fire(this); // We need a way to keep things running while also stopping the state from running. Shouldn't be too bad break; case WAIT: sleep((int)(n_c->args.args[0].n * 1000)); @@ -1074,8 +1084,12 @@ namespace dms { } break; case APND: - if (!handler->handleMessageAppend(this, n_c->args.args[0].resolve(this).getPrintable())) - return false; + OnAppendText.fire(message{ + speaker, + n_c->args.args[0].resolve(this).getPrintable() + }); + //if (!handler->handleMessageAppend(this, c->args.args[0].resolve(this).getPrintable())) + // return false; break; case CHAR: { @@ -1085,8 +1099,12 @@ namespace dms { } case DISP: { - if (!handler->handleMessageDisplay(this, n_c->args.args[0].resolve(this).getPrintable())) - return false; + OnText.fire(message{ + speaker, + n_c->args.args[0].resolve(this).getPrintable() + }); + /*if (!handler->handleMessageDisplay(this, c->args.args[0].resolve(this).getPrintable())) + return false;*/ } break; case ASGN: diff --git a/DMS/dump.bin b/DMS/dump.bin index 8dc1d1c..dac9ba1 100644 Binary files a/DMS/dump.bin and b/DMS/dump.bin differ diff --git a/DMS/dump.txt b/DMS/dump.txt index 2dd69d1..9a2bd4a 100644 --- a/DMS/dump.txt +++ b/DMS/dump.txt @@ -35,7 +35,7 @@ Line <12> newline Line <13> newline Line <14> newline Line <14> newline -Line <15> name music +Line <15> name music2 Line <15> equal = Line <15> name loadMusic Line <15> parao ( @@ -43,7 +43,7 @@ Line <15> string test2.ogg Line <15> parac ) Line <15> newline Line <15> newline -Line <16> name music2 +Line <16> name music Line <16> equal = Line <16> name loadMusic Line <16> parao ( @@ -59,308 +59,334 @@ Line <18> bracketc ] Line <18> newline Line <18> newline Line <19> newline -Line <20> name music -Line <20> dot dot -Line <20> name play -Line <20> parao ( -Line <20> parac ) Line <20> newline -Line <20> newline -Line <21> name music -Line <21> dot . -Line <21> name setVolume -Line <21> parao ( -Line <21> number 5 -Line <21> parac ) Line <21> newline -Line <21> newline -Line <22> name while -Line <22> parao ( -Line <22> name music -Line <22> dot . -Line <22> name getStatus -Line <22> parao ( -Line <22> parac ) -Line <22> not ! -Line <22> equal = -Line <22> string stopped -Line <22> parac ) -Line <22> cbracketo { -Line <22> cbracketc } Line <22> newline -Line <22> newline -Line <23> name print -Line <23> parao ( -Line <23> string Sound finished! -Line <23> parac ) -Line <23> newline Line <23> newline +Line <24> name wait +Line <24> number 1 Line <24> newline Line <24> newline +Line <25> name setBG +Line <25> parao ( +Line <25> string background2.jpg +Line <25> parac ) +Line <25> newline Line <25> newline Line <26> newline +Line <26> newline +Line <27> name Ryan +Line <27> colon : +Line <27> string Hello how are you? Line <27> newline +Line <27> newline +Line <28> name wait +Line <28> number 2 Line <28> newline +Line <28> newline +Line <29> name Bob +Line <29> colon : +Line <29> string I'm good how are you? Line <29> newline +Line <29> newline +Line <30> name wait +Line <30> number 2 Line <30> newline +Line <30> newline +Line <31> name Ryan +Line <31> colon : +Line <31> string I am great :D +Line <31> newline Line <31> newline Line <32> newline +Line <32> newline +Line <33> name a +Line <33> equal = +Line <33> number 0 +Line <33> newline Line <33> newline Line <34> newline +Line <35> name while +Line <35> parao ( +Line <35> true true +Line <35> parac ) +Line <35> cbracketo { Line <35> newline +Line <35> newline +Line <36> name a +Line <36> equal = +Line <36> name a +Line <36> plus + +Line <36> number 1 Line <36> newline +Line <36> newline +Line <37> cbracketc } +Line <37> newline Line <37> newline Line <38> newline Line <39> newline -Line <39> newline -Line <40> bracketo [ -Line <40> name SelectSong -Line <40> colon : -Line <40> name function -Line <40> parao ( -Line <40> parac ) -Line <40> bracketc ] Line <40> newline -Line <40> newline -Line <41> control -Line <41> string Pick song: -Line <41> cbracketo { Line <41> newline -Line <41> newline -Line <42> string Music 1 -Line <42> cbracketo { Line <42> newline -Line <42> newline -Line <43> ret -Line <43> name music Line <43> newline -Line <43> newline -Line <44> cbracketc } Line <44> newline -Line <44> newline -Line <45> string Music 2 -Line <45> cbracketo { Line <45> newline -Line <45> newline -Line <46> ret -Line <46> name music2 Line <46> newline -Line <46> newline -Line <47> cbracketc } Line <47> newline -Line <47> newline -Line <48> cbracketc } -Line <48> newline Line <48> newline Line <49> newline -Line <49> newline -Line <50> bracketo [ -Line <50> name player -Line <50> colon : -Line <50> name function -Line <50> parao ( -Line <50> name song -Line <50> seperator , -Line <50> name food -Line <50> parac ) -Line <50> bracketc ] Line <50> newline -Line <50> newline -Line <51> control -Line <51> string What you wanna do? -Line <51> cbracketo { Line <51> newline -Line <51> newline -Line <52> string play/resume -Line <52> cbracketo { Line <52> newline Line <52> newline -Line <53> name song -Line <53> dot dot -Line <53> name play +Line <53> bracketo [ +Line <53> name SelectSong +Line <53> colon : +Line <53> name function Line <53> parao ( Line <53> parac ) +Line <53> bracketc ] Line <53> newline Line <53> newline -Line <54> name player -Line <54> parao ( -Line <54> name song -Line <54> parac ) +Line <54> control +Line <54> string Pick song: +Line <54> cbracketo { Line <54> newline Line <54> newline -Line <55> cbracketc } +Line <55> string Music 1 +Line <55> cbracketo { Line <55> newline Line <55> newline -Line <56> string pause -Line <56> cbracketo { +Line <56> ret +Line <56> name music Line <56> newline Line <56> newline -Line <57> name song -Line <57> dot . -Line <57> name pause -Line <57> parao ( -Line <57> parac ) +Line <57> cbracketc } Line <57> newline Line <57> newline -Line <58> name player -Line <58> parao ( -Line <58> name song -Line <58> parac ) +Line <58> string Music 2 +Line <58> cbracketo { Line <58> newline Line <58> newline -Line <59> cbracketc } +Line <59> ret +Line <59> name music2 Line <59> newline Line <59> newline -Line <60> string stop -Line <60> cbracketo { +Line <60> cbracketc } Line <60> newline Line <60> newline -Line <61> name song -Line <61> dot . -Line <61> name stop -Line <61> parao ( -Line <61> parac ) +Line <61> cbracketc } Line <61> newline Line <61> newline -Line <62> name player -Line <62> parao ( -Line <62> name song -Line <62> parac ) Line <62> newline Line <62> newline -Line <63> cbracketc } +Line <63> bracketo [ +Line <63> name player +Line <63> colon : +Line <63> name function +Line <63> parao ( +Line <63> name song +Line <63> seperator , +Line <63> name food +Line <63> parac ) +Line <63> bracketc ] Line <63> newline Line <63> newline -Line <64> string newSong +Line <64> control +Line <64> string What you wanna do? Line <64> cbracketo { Line <64> newline Line <64> newline -Line <65> name song -Line <65> dot . -Line <65> name stop -Line <65> parao ( -Line <65> parac ) +Line <65> string play/resume +Line <65> cbracketo { Line <65> newline Line <65> newline -Line <66> name player -Line <66> parao ( Line <66> name song +Line <66> dot dot +Line <66> name play +Line <66> parao ( Line <66> parac ) Line <66> newline Line <66> newline -Line <67> cbracketc } +Line <67> name player +Line <67> parao ( +Line <67> name song +Line <67> parac ) Line <67> newline Line <67> newline -Line <68> string quit -Line <68> exit -Line <68> number 0 +Line <68> cbracketc } Line <68> newline Line <68> newline -Line <69> cbracketc } +Line <69> string pause +Line <69> cbracketo { Line <69> newline Line <69> newline +Line <70> name song +Line <70> dot . +Line <70> name pause +Line <70> parao ( +Line <70> parac ) Line <70> newline Line <70> newline -Line <71> bracketo [ -Line <71> name Ryan -Line <71> colon : -Line <71> name char -Line <71> bracketc ] +Line <71> name player +Line <71> parao ( +Line <71> name song +Line <71> parac ) Line <71> newline Line <71> newline -Line <72> name age -Line <72> equal = -Line <72> number 21 +Line <72> cbracketc } Line <72> newline Line <72> newline -Line <73> name money -Line <73> equal = -Line <73> number 1000 +Line <73> string stop +Line <73> cbracketo { Line <73> newline Line <73> newline -Line <74> name lname -Line <74> equal = -Line <74> string Ward +Line <74> name song +Line <74> dot . +Line <74> name stop +Line <74> parao ( +Line <74> parac ) Line <74> newline Line <74> newline -Line <75> name known -Line <75> equal = -Line <75> true true +Line <75> name player +Line <75> parao ( +Line <75> name song +Line <75> parac ) Line <75> newline Line <75> newline +Line <76> cbracketc } Line <76> newline -Line <77> name calm -Line <77> colon : -Line <77> string ./path/to/file +Line <76> newline +Line <77> string newSong +Line <77> cbracketo { Line <77> newline Line <77> newline -Line <78> name excited -Line <78> colon : -Line <78> string ./path/to/file +Line <78> name song +Line <78> dot . +Line <78> name stop +Line <78> parao ( +Line <78> parac ) Line <78> newline Line <78> newline +Line <79> name player +Line <79> parao ( +Line <79> name song +Line <79> parac ) Line <79> newline Line <79> newline -Line <80> bracketo [ -Line <80> name John -Line <80> colon : -Line <80> name char -Line <80> bracketc ] +Line <80> cbracketc } Line <80> newline Line <80> newline -Line <81> name lname -Line <81> equal = -Line <81> string Johnson +Line <81> string quit +Line <81> exit +Line <81> number 0 Line <81> newline Line <81> newline -Line <82> name age -Line <82> equal = -Line <82> number 16 +Line <82> cbracketc } Line <82> newline Line <82> newline -Line <83> name money -Line <83> equal = -Line <83> number 100000 Line <83> newline Line <83> newline -Line <84> name known -Line <84> equal = -Line <84> true true +Line <84> bracketo [ +Line <84> name Ryan +Line <84> colon : +Line <84> name char +Line <84> bracketc ] Line <84> newline Line <84> newline +Line <85> name age +Line <85> equal = +Line <85> number 21 Line <85> newline Line <85> newline -Line <86> bracketo [ -Line <86> name Bob -Line <86> colon : -Line <86> name char -Line <86> bracketc ] +Line <86> name money +Line <86> equal = +Line <86> number 1000 Line <86> newline Line <86> newline +Line <87> name lname +Line <87> equal = +Line <87> string Ward Line <87> newline +Line <87> newline +Line <88> name known +Line <88> equal = +Line <88> true true +Line <88> newline Line <88> newline -Line <89> name lname -Line <89> equal = -Line <89> string Johnson Line <89> newline -Line <90> name unknown -Line <90> equal = -Line <90> string Some Random Guy +Line <90> name calm +Line <90> colon : +Line <90> string ./path/to/file Line <90> newline Line <90> newline -Line <91> name age -Line <91> equal = -Line <91> number 24 +Line <91> name excited +Line <91> colon : +Line <91> string ./path/to/file Line <91> newline Line <91> newline -Line <92> name money -Line <92> equal = -Line <92> number 100 Line <92> newline Line <92> newline -Line <93> name excited +Line <93> bracketo [ +Line <93> name John Line <93> colon : -Line <93> string path/to/file +Line <93> name char +Line <93> bracketc ] Line <93> newline Line <93> newline -Line <93> eof +Line <94> name lname +Line <94> equal = +Line <94> string Johnson +Line <94> newline +Line <94> newline +Line <95> name age +Line <95> equal = +Line <95> number 16 +Line <95> newline +Line <95> newline +Line <96> name money +Line <96> equal = +Line <96> number 100000 +Line <96> newline +Line <96> newline +Line <97> name known +Line <97> equal = +Line <97> true true +Line <97> newline +Line <97> newline +Line <98> newline +Line <98> newline +Line <99> bracketo [ +Line <99> name Bob +Line <99> colon : +Line <99> name char +Line <99> bracketc ] +Line <99> newline +Line <99> newline +Line <100> newline +Line <101> newline +Line <102> name lname +Line <102> equal = +Line <102> string Johnson +Line <102> newline +Line <103> name unknown +Line <103> equal = +Line <103> string Some Random Guy +Line <103> newline +Line <103> newline +Line <104> name age +Line <104> equal = +Line <104> number 24 +Line <104> newline +Line <104> newline +Line <105> name money +Line <105> equal = +Line <105> number 100 +Line <105> newline +Line <105> newline +Line <106> name excited +Line <106> colon : +Line <106> string path/to/file +Line <106> newline +Line <106> newline +Line <106> eof diff --git a/DMS/gui.h b/DMS/gui.h index 3e532dd..f8ad1a3 100644 --- a/DMS/gui.h +++ b/DMS/gui.h @@ -6,12 +6,11 @@ #include #include "multibase.h" namespace gui { + struct gframe; + struct gtext; + struct gimage; using namespace std; using namespace dms; - /// - /// This is the number of gui elements that exist! It isn't constant, but set as a read only variable. - /// - const int GuiElementCount = 0; /* textbox textbutton @@ -31,11 +30,14 @@ namespace gui { struct Dim { const float x = 0; const float y = 0; - Dim() : x(0), y(0) {} - Dim(const Dim &d) : x(d.x),y(d.y) {} - Dim(float x, float y) : x(x),y(y) {} - inline void Set(Dim d) { const_cast(x) = d.x; const_cast(y) = d.y; OnValueChanged.fire(this); } - inline void Set(float x, float y) { const_cast(this->x) = x; const_cast(this->y) = y; OnValueChanged.fire(this);} + void init() { + OnValueChanged = multi::connection("DimConnection"); + } + Dim() : x(0), y(0) { init(); } + Dim(const Dim &d) : x(d.x),y(d.y) { init(); } + Dim(float x, float y) : x(x),y(y) { init(); } + inline void Set(Dim d) { if (x == d.x && y == d.y) return; const_cast(x) = d.x; const_cast(y) = d.y; OnValueChanged.fire(this); } + inline void Set(float x, float y) { if (this->x == x && this->y == y) return; const_cast(this->x) = x; const_cast(this->y) = y; OnValueChanged.fire(this);} void operator=(const Dim& dd) { Set(dd); } @@ -54,10 +56,13 @@ namespace gui { Dim Position; Dim Size; void init() { + OnValueChanged = multi::connection("DualDimConnection"); Position.OnValueChanged += [&](Dim* d) { + std::cout << "Hi ddp" << std::endl; OnValueChanged.fire(this); }; Size.OnValueChanged += [&](Dim* d) { + std::cout << "Hi dds" << std::endl; OnValueChanged.fire(this); }; } @@ -65,11 +70,12 @@ namespace gui { DualDim(const DualDim& d) : Position(d.Position.x,d.Position.y), Size(d.Size.x,d.Size.y) {init(); } DualDim(Dim pos, Dim size) : Position(pos.x,pos.y), Size(size.x,size.y) { init(); } DualDim(float x, float y, float x2, float y2) : Position(x,y),Size(x2,y2) { init(); } - inline void Set(DualDim d){ Position.Set(d.Position); Size.Set(d.Size);OnValueChanged.fire(this);} - inline void Set(Dim pos, Dim size) { Position.Set(pos); Size.Set(size); OnValueChanged.fire(this);} - inline void Set(float x, float y, float x2, float y2) { Position.Set(x, y), Size.Set(x2, y2); OnValueChanged.fire(this);} + inline void Set(DualDim d){ if (d.Position.x == this->Position.x && d.Position.y == this->Position.y && d.Size.x == this->Size.x && d.Size.y == this->Size.y) return; Position.Set(d.Position); Size.Set(d.Size);} + inline void Set(Dim pos, Dim size) { Position.Set(pos); Size.Set(size);} + inline void Set(float x, float y, float x2, float y2) { Position.Set(x, y), Size.Set(x2, y2); } DualDim operator=(const DualDim& dd) { Set(dd); + return *this; } multi::connection OnValueChanged; }; @@ -112,44 +118,37 @@ namespace gui { gui* Parent = nullptr; vector Children; bool Visible = true; + bool Active = true; //map Named; - gui() { - const_cast(GuiElementCount) += 1; - - } + gui(); gui(DualDim offset, DualDim scale) : gui() { Offset.Set(offset); Scale.Set(scale);} gui(float x,float y,float w,float h,float sx,float sy,float sw,float sh) : gui() { Offset.Set(x, y, w, h); Scale.Set(sx, sy, sw, sh); } - void SetDualDim(DualDim offset,DualDim scale) {Offset = offset;Scale.Set(scale);} virtual void Draw(sf::RenderWindow* window) { return; // Override this. } - - inline vector& GetChildren() {return Children;} - inline void GetAllChildren(vector& ref) { - auto temp = GetChildren(); - for (int i = 0; i < temp.size(); i++) { - ref.push_back(temp[i]); - temp[i]->GetAllChildren(ref); + virtual void updateValues(DualDim offset, DualDim scale) { + return; // Override this. + } + inline vector* GetChildren() {return &Children;} + inline void GetAllChildren(vector* ref) { + vector* temp = GetChildren(); + for (size_t i = 0; i < temp->size(); i++) { + ref->push_back((*temp)[i]); + (*temp)[i]->GetAllChildren(ref); } } - inline vector GetAllChildren() { - auto temp = GetChildren(); - vector everything; - everything.reserve(GuiElementCount); // Reserve Space for all the current elements that exist - for (int i = 0; i < temp.size(); i++) { - everything.push_back(temp[i]); - temp[i]->GetAllChildren(everything); - } - return everything; - } + // Constructors + gframe& newFrame(DualDim offset, DualDim scale = { 0,0,0,0 }); - gui& newFrame(DualDim offset, DualDim scale); + //Standard mathods + virtual void setColor(sf::Color c) {}; + void SetDualDim(DualDim offset, DualDim scale) { Offset.Set(offset); Scale.Set(scale); } //Connections and objects to inheret multi::connection OnPressed; @@ -162,35 +161,51 @@ namespace gui { multi::connection OnMouseEnter; multi::connection OnMouseExit; - multi::connection OnUpdate; }; - struct framebase : public gui { + struct gframe : public gui { sf::RectangleShape rect; - framebase(DualDim offset, DualDim scale) : gui(offset,scale) { + gframe(DualDim offset, DualDim scale, gui* parent) : gui(offset, scale) { + Parent = parent; Type = gui_types::frame; - rect.setFillColor(sf::Color(120, 120, 120, 255)); - rect.setOutlineColor(sf::Color::Red); - rect.setSize(offset.Size); - rect.setPosition(offset.Position); - Offset.OnValueChanged += [&](DualDim* o) { - rect.setSize(o->Size); - rect.setPosition(o->Position); - }; - Scale.OnValueChanged += [](DualDim* s) { - - }; + updateValues(Offset, Scale); + //rect.setSize(offset.Size); + //rect.setPosition(offset.Position); } - void Draw(sf::RenderWindow* window) override { - window->draw(rect); + inline void SetColor(sf::Color& c) { + rect.setFillColor(c); + } + inline void SetBorderColor(sf::Color& c) { + rect.setOutlineColor(c); + } + inline void SetBorderSize(float bs) { + rect.setOutlineThickness(bs); + } + inline void SetAlpha(float alpha) { + auto c = rect.getFillColor(); + c.a = alpha; + SetColor(c); + c = rect.getOutlineColor(); + c.a = alpha; + SetBorderColor(c); + } + inline void Draw(sf::RenderWindow* window) override { + window->draw(this->rect); + } + private: + void updateValues(DualDim offset, DualDim scale) override { + AbsolutePosition = Dim(Parent->AbsolutePosition.x*scale.Position.x+offset.Position.x,Parent->AbsolutePosition.y*scale.Position.y+offset.Position.y); + AbsoluteSize = Dim(Parent->AbsoluteSize.x * scale.Size.x + offset.Size.x, Parent->AbsoluteSize.y * scale.Size.y + offset.Size.y); + rect.setPosition(AbsolutePosition); + rect.setSize(AbsoluteSize); } }; - struct imagebase : public framebase { + struct gimage : public gframe { sf::Texture texture; - imagebase(std::string imagepath, DualDim offset, DualDim scale) : framebase(offset,scale) { + gimage(std::string imagepath, DualDim offset, DualDim scale, gui* parent) : gframe(offset,scale,parent) { Type = gui_types::image; texture.loadFromFile(imagepath); sf::Sprite sprite; @@ -199,8 +214,8 @@ namespace gui { sprite.setOrigin(0, 0); } }; - struct textbase : public framebase { - textbase(std::string text, sf::Font font, DualDim offset, DualDim scale) : framebase(offset, scale) { + struct gtext : public gframe { + gtext(std::string text, sf::Font font, DualDim offset, DualDim scale,gui* parent) : gframe(offset, scale, parent) { Type = gui_types::text; } }; diff --git a/DMS/guiimpl.cpp b/DMS/guiimpl.cpp index ea6e81a..0b35662 100644 --- a/DMS/guiimpl.cpp +++ b/DMS/guiimpl.cpp @@ -1,21 +1,54 @@ +#include "pch.h" #include "gui.h" +#include "actors.h" +#include namespace gui { - gui& gui::newFrame(DualDim offset, DualDim scale) { - gui* fb = new framebase(offset, scale); - fb->Parent = this; + int GuiElementCount = 0; + const size_t ANChilds = 128; + gui::gui() { + GuiElementCount += 1; + Offset.OnValueChanged.connect([&](DualDim* d) { + std::cout << "hey hey hey" << std::endl; + updateValues(Offset, Scale); + }); + Scale.OnValueChanged.connect([&](DualDim* d) { + std::cout << "hey hey hey" << std::endl; + updateValues(Offset, Scale); + }); + } + gframe& gui::newFrame(DualDim offset, DualDim scale) { + gframe* fb = new gframe(offset, scale,this); Children.push_back(fb); return *fb; } // Draw Everything gui _Root; - std::vector childs; // This tends to always grow, rarely strinking. So make it once so we arent allocating meme each loop! + int lastcount = -1; + std::vector* childs = new std::vector(ANChilds); // This tends to always grow, rarely strinking. So make it once so we arent allocating memes each loop! void Draw(sf::RenderWindow* window) { _Root.AbsoluteSize = window->getSize(); - childs = _Root.GetAllChildren(); - for (int i = 0; i < childs.size(); i++) { - childs[i]->Draw(window); + if (lastcount != GuiElementCount) { + std::cout << "New Element Added/Removed!" << std::endl; + lastcount = GuiElementCount; + childs->clear(); + _Root.GetAllChildren(childs); + } + for (int i = 0; i < childs->size(); i++) { + if ((*childs)[i]->Visible); + (*childs)[i]->Draw(window); } } + /*multi::runner runner; + multi::loop updateloop(&runner, [](multi::loop* loop) { + _Root.GetAllChildren(childs); + for (int i = 0; i < childs.size(); i++) { + if (childs[i]->Active) + childs[i]->OnUpdate.fire(childs[i]); + } + }); + std::thread thread([]() { + runner.mainloop(); + });*/ gui& Root() { return _Root; } diff --git a/DMS/multiimpl.cpp b/DMS/multiimpl.cpp index 25be226..3538ed6 100644 --- a/DMS/multiimpl.cpp +++ b/DMS/multiimpl.cpp @@ -1,3 +1,4 @@ +#include "pch.h" #include "actors.h" namespace multi { bool runner::update() { diff --git a/DMS/test.dms b/DMS/test.dms index 6f16f08..318a48e 100644 --- a/DMS/test.dms +++ b/DMS/test.dms @@ -12,16 +12,29 @@ version 0.2 using extendedDefine // this code will reside within -music = loadMusic("test2.ogg") -music2 = loadMusic("test.ogg") +music2 = loadMusic("test2.ogg") +music = loadMusic("test.ogg") [main] // Let's extend the base feature set - music.play() - music.setVolume(5) - while(music.getStatus()!="stopped"){} - print("Sound finished!") + //music.play() + //music.setVolume(50) + //while(music.getStatus()!="stopped"){} + //print("Sound finished!") + wait 1 + setBG("background2.jpg") + Ryan: "Hello how are you?" + wait 2 + Bob: "I'm good how are you?" + wait 2 + Ryan: "I am great :D" + + a=0 + // {} Still errors out, this needs to be fixed!!! + while(true){ + a=a+1 + } // a = 0 // while (true){ // asm {