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 0000000..ceabf77 Binary files /dev/null and b/examples/Platformer/mariostill.png differ diff --git a/examples/Platformer/script.zs b/examples/Platformer/script.zs index 5d3d504..34cbbb6 100644 --- a/examples/Platformer/script.zs +++ b/examples/Platformer/script.zs @@ -1,5 +1,6 @@ -int g_screenw = 900 -int g_screenh = 600 +int g_screenw = 256 +int g_screenh = 224 +int g_resolutionScale = 3 float g_playerWalkSpeed = 400 float g_playerRunSpeed = 700 @@ -14,8 +15,15 @@ include "./extra-include.zs" func Main() { + //SplitThread(ThreadedFunction()) + // Immediately creates the window, then Start(), then the game loop. The game loop calls Update() every frame - ZS.Graphics.Init("Platformer game", g_screenw, g_screenh) + ZS.Graphics.Init("Platformer game", g_screenw, g_screenh, g_resolutionScale) +} + +func ThreadedFunction() +{ + print "threaded:" } func Start() @@ -25,14 +33,14 @@ func Start() global Vec2 g_screencenter = NVec2(centerX, centerY) Vec2 playerPos = g_screencenter - Vec2 playerScale = NVec2(40, 40) - global Sprite g_playerSprite = ZS.Graphics.Sprite("./square.png", playerPos, playerScale, 0) + Vec2 playerScale = NVec2(16, 16) + global Sprite g_playerSprite = ZS.Graphics.Sprite("./mariostill.png", playerPos, playerScale, 0) - Vec2 groundPos = NVec2(g_screencenter.x, 500) + Vec2 groundPos = NVec2(g_screencenter.x, 192) Vec2 groundScale = NVec2(256, 16) global Sprite g_groundSprite = ZS.Graphics.Sprite("./square.png", groundPos, groundScale, 0) - Vec2 instructionsPos = NVec2(centerOfScreen.x, g_screenh - 60) + Vec2 instructionsPos = NVec2(centerOfScreen.x, centerOfScreen.y) global Text g_instructionsText = ZS.Graphics.Text("Use Arrow Keys or WASD to Move, and Spacebar to Jump", "./arial.ttf", instructionsPos, 20, 0, 255, 255, 255) global Vec2 g_playerTargetPosition = playerPos @@ -47,7 +55,7 @@ func Start() func Update(deltaTime) { float fps = 1 / deltaTime print "FPS: " + fps - TestInclude() + //TestInclude() //// Test automatic conversion from bool to int //int c = GetKey("A")