From 664c6de66685d2ca209bd4e364198204ff16fc4e Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Sun, 22 May 2022 16:39:53 -0400 Subject: [PATCH] Implement new functions for easier parsing, pixel scaling, and started making threading --- ZSharp/Main.cpp | 17 +++++++++++++++ ZSharp/ZS.h | 5 +++++ ZSharp/builtin.h | 25 +++++++++++++++++---- ZSharp/graphics.h | 15 ++++++++----- ZSharp/strops.cpp | 34 +++++++++++++++++++++++++++++ ZSharp/strops.h | 4 ++++ examples/Platformer/mariostill.png | Bin 0 -> 297 bytes examples/Platformer/script.zs | 24 +++++++++++++------- 8 files changed, 107 insertions(+), 17 deletions(-) create mode 100644 examples/Platformer/mariostill.png diff --git a/ZSharp/Main.cpp b/ZSharp/Main.cpp index 7e9cb5d..a76cfd1 100644 --- a/ZSharp/Main.cpp +++ b/ZSharp/Main.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #if UNIX #include @@ -401,6 +402,22 @@ boost::any ProcessLine(const vector>& words, int lineNum, unorder return nullType; } + // Check if it is a SplitThread call + else if (startsWith(words.at(lineNum).at(0), "SplitThread")) + { + vector lineContents = removeTabs(words.at(lineNum), 10); + cout << "debug: " << words.at(lineNum).at(0) << endl; + //lineContents.at(0) = betweenChars(lineContents.at(0), '(', ')'); + + //cout << "debug: " << lineContents.at(0) << endl; + + //if (betweenChars(lineContents.at(0), '(', ')') == "") + // std::thread thread_obj(ExecuteFunction, trim(split(lineContents.at(0), '(')[0]), vector()); + //else + // std::thread thread_obj(ExecuteFunction, trim(split(lineContents.at(0), '(')[0]), VarValues(split(RMParenthesis("(" + split(unWrapVec(rangeInVec(lineContents, 0, (int)lineContents.size() - 2)), '(')[1]), ','), variableValues)); + return nullType; + } + // Check if global variable declaration else if (trim(words.at(lineNum).at(0)) == "global") { diff --git a/ZSharp/ZS.h b/ZSharp/ZS.h index dd6ccde..175beef 100644 --- a/ZSharp/ZS.h +++ b/ZSharp/ZS.h @@ -137,5 +137,10 @@ func GetKey(keyName) bool b = ZS.Input.GetKey(keyName) return b } + +//func SplitThread(function) +//{ +// ZS.System.SplitThread(function) +//} )" ; \ No newline at end of file diff --git a/ZSharp/builtin.h b/ZSharp/builtin.h index 5a181fb..c7e65ca 100644 --- a/ZSharp/builtin.h +++ b/ZSharp/builtin.h @@ -12,6 +12,7 @@ #include #include #include +#include #include "strops.h" #include "graphics.h" @@ -86,6 +87,10 @@ float lerp(float a, float b, float f) return a + f * (b - a); } +inline bool fileExists(const std::string& name) { + struct stat buffer; + return (stat(name.c_str(), &buffer) == 0); +} void PrintColored(std::string text, std::string fgColor, std::string bgColor, bool isError) { @@ -197,8 +202,11 @@ int LogCriticalError(const string& errorText) PrintColored("ZSharp: ", yellowFGColor, "", true); PrintColored(errorText, redFGColor, "", true); cerr << std::endl; + cout << "Press Enter to Continue"; + cin.ignore(); + exit(1); //cerr << "\x1B[34m[" + to_string(Hour) + ":" + to_string(Min) + ":" + to_string(Sec) + "] \x1B[33mZSharp: \x1B[31mERROR: " << errorText << "\033[0m\t\t" << endl; - exit(EXIT_FAILURE); + //exit(EXIT_FAILURE); return 2; } @@ -314,7 +322,7 @@ int GetBuiltins(std::string s) } builtinFunctionValues[functName] = functionContents; //cout << functName << " is \n" << Vec2Str(functionContents) << endl << endl; - } + } else { if (words.at(lineNum).at(0) == "string") @@ -351,7 +359,7 @@ int GetBuiltins(std::string s) } return 0; - } +} // Executes boost::any ZSFunction(const string& name, const vector& args) @@ -373,10 +381,16 @@ boost::any ZSFunction(const string& name, const vector& args) #if DEVELOPER_MESSAGES == true InterpreterLog("Init graphics"); #endif - initGraphics(StringRaw(AnyAsString(args.at(0))), AnyAsInt(args.at(1)), AnyAsInt(args.at(2))); + if (args.size() <= 3) + initGraphics(StringRaw(AnyAsString(args.at(0))), AnyAsInt(args.at(1)), AnyAsInt(args.at(2)), 1); + else + initGraphics(StringRaw(AnyAsString(args.at(0))), AnyAsInt(args.at(1)), AnyAsInt(args.at(2)), AnyAsInt(args.at(3))); } else if (name == "ZS.Graphics.Sprite") { + if (!fileExists(StringRaw(AnyAsString(args.at(0))))) + LogCriticalError("Failed to create 'Sprite' object: \"" + StringRaw(AnyAsString(args.at(0))) + "\""); + Sprite s(StringRaw(AnyAsString(args.at(0))), any_cast(args.at(1)), any_cast(args.at(2)), AnyAsFloat(args.at(3))); return s; } @@ -386,6 +400,9 @@ boost::any ZSFunction(const string& name, const vector& args) any_cast(args.at(0)).Load(); else if (name == "ZS.Graphics.Text") { + if (!fileExists(StringRaw(AnyAsString(args.at(1))))) + LogCriticalError("Failed to create 'Text' object: \"" + StringRaw(AnyAsString(args.at(1))) + "\""); + Text t(StringRaw(AnyAsString(args.at(0))), StringRaw(AnyAsString(args.at(1))), any_cast(args.at(2)), AnyAsFloat(args.at(3)), AnyAsFloat(args.at(4)), (Uint8)AnyAsFloat(args.at(5)), (Uint8)AnyAsFloat(args.at(6)), (Uint8)AnyAsFloat(args.at(7))); return t; } diff --git a/ZSharp/graphics.h b/ZSharp/graphics.h index 283504e..5f1c67a 100644 --- a/ZSharp/graphics.h +++ b/ZSharp/graphics.h @@ -26,6 +26,7 @@ using namespace boost; int WINDOW_WIDTH = 1280; int WINDOW_HEIGHT = 720; +int PIXEL_SCALE = 1; unordered_map KEYS = { @@ -191,7 +192,7 @@ public: { x *= rhs; y *= rhs; - + return *this; } @@ -523,7 +524,7 @@ public: { rect.x = position.x; rect.y = position.y; - + font = TTF_OpenFont(pathToFont.c_str(), fontSize); Load(); @@ -534,7 +535,7 @@ public: SDL_Color color = { r, g, b }; SDL_Surface* surface = TTF_RenderText_Solid(font, content.c_str(), color); - + texture = SDL_CreateTextureFromSurface(gRenderer, surface); TTF_SizeText(font, content.c_str(), &rect.w, &rect.h); @@ -1017,17 +1018,21 @@ int updateLoop() return 0; } -int initGraphics(std::string windowTitle, int width, int height) +int initGraphics(std::string windowTitle, int width, int height, int pixelScale) { WINDOW_WIDTH = width; WINDOW_HEIGHT = height; + PIXEL_SCALE = pixelScale; // Initialize SDL components SDL_Init(SDL_INIT_VIDEO); TTF_Init(); - gWindow = SDL_CreateWindow(windowTitle.c_str(), 40, 40, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN | SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); + gWindow = SDL_CreateWindow(windowTitle.c_str(), 40, 40, WINDOW_WIDTH * PIXEL_SCALE, WINDOW_HEIGHT * PIXEL_SCALE, SDL_WINDOW_SHOWN | SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); gRenderer = SDL_CreateRenderer(gWindow, -1, 0); + SDL_RenderSetLogicalSize(gRenderer, 256 * PIXEL_SCALE, 224 * PIXEL_SCALE); + SDL_RenderSetScale(gRenderer, PIXEL_SCALE, PIXEL_SCALE); + SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, 0); //Get window surface gScreenSurface = SDL_GetWindowSurface(gWindow); diff --git a/ZSharp/strops.cpp b/ZSharp/strops.cpp index be2fa03..a807b36 100644 --- a/ZSharp/strops.cpp +++ b/ZSharp/strops.cpp @@ -150,6 +150,40 @@ int countNoOverlap(const string& str, const char& searchFor, const char& ch1, co return cnt; } +string betweenChars(const string& str, const char& openChar, const char& closeChar) +{ + string content = ""; + + int waitingForClose = 0; + + for (int i = 0; i < (int)str.size(); i++) + { + if (waitingForClose > 0 && !(str[i] == closeChar && waitingForClose == 1)) + content += str[i]; + + if (str[i] == openChar) + waitingForClose++; + else if (str[i] == closeChar) + waitingForClose--; + } + + return content; +} + +bool startsWith(const string& str, const string& lookFor) +{ + if (str.empty() || lookFor.size() > str.size()) + return false; + + for (int i = 0; i < (int)lookFor.size(); i++) + { + if (str[i] != lookFor[i]) + return false; + } + + return true; +} + int countOutsideParenthesis(const string& str, const char& searchFor) { int cnt = 0; diff --git a/ZSharp/strops.h b/ZSharp/strops.h index f709655..00aaad7 100644 --- a/ZSharp/strops.h +++ b/ZSharp/strops.h @@ -34,6 +34,8 @@ int count(const string& str, const char& ch); int countNoOverlap(const string& str, const char& searchFor, const char& ch1, const char& ch2); +string betweenChars(const string& str, const char& openChar, const char& closeChar); + int countOutsideParenthesis(const string& str, const char& searchFor); int indexInStr(const string& str, const char& ch); @@ -62,4 +64,6 @@ string replace(const string& str, const string& strToReplace, const string& repl bool isEscaped(const string& str, int curChar); +bool startsWith(const string& str, const string& lookFor); + #endif diff --git a/examples/Platformer/mariostill.png b/examples/Platformer/mariostill.png new file mode 100644 index 0000000000000000000000000000000000000000..ceabf77b98ce84c99071a7339c2e4be8309fddc6 GIT binary patch literal 297 zcmV+^0oMMBP)Px#<4Ht8R5*==Q?U($Fc3TtYou_&0#>Aq;4(v`0w!PrCP-2-M9Pd{DPn;X6s!@+ zX=3yLe#kXo|KIt1whJhD9P1$G&aZc&^=6YAc~7j8#ph{G|GhN;fMJ@^TZ6+TcTv(o z+f^Zw;+J9>>#&U6VVQOPFihMD9a9lzsqLzutxm}RaJzmIPfb?Zq>wpe0?~!@h6PGj zrC_HJ$r8>Rpqo=o-GU@L&vT$|s7N}6-kKDi^9HtAn{BfS(mv*Z1~=Wcjev>9FtdBG vMc%6IzJ++_wK-LQ1}Tqit5#|a|3G`c4xD3O&2