Implement new functions for easier parsing, pixel scaling, and started making threading

This commit is contained in:
sam-astro 2022-05-22 16:39:53 -04:00
parent 74df9cdaf7
commit 664c6de666
8 changed files with 107 additions and 17 deletions

View File

@ -23,6 +23,7 @@
#include <unordered_map>
#include <stdio.h>
#include <codecvt>
#include <thread>
#if UNIX
#include <unistd.h>
@ -401,6 +402,22 @@ boost::any ProcessLine(const vector<vector<string>>& words, int lineNum, unorder
return nullType;
}
// Check if it is a SplitThread call
else if (startsWith(words.at(lineNum).at(0), "SplitThread"))
{
vector<string> 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<boost::any>());
//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")
{

View File

@ -137,5 +137,10 @@ func GetKey(keyName)
bool b = ZS.Input.GetKey(keyName)
return b
}
//func SplitThread(function)
//{
// ZS.System.SplitThread(function)
//}
)"
;

View File

@ -12,6 +12,7 @@
#include <SDL.h>
#include <ctime>
#include <math.h>
#include <sys/stat.h>
#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;
}
@ -351,7 +359,7 @@ int GetBuiltins(std::string s)
}
return 0;
}
}
// Executes
boost::any ZSFunction(const string& name, const vector<boost::any>& args)
@ -373,10 +381,16 @@ boost::any ZSFunction(const string& name, const vector<boost::any>& 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<Vec2>(args.at(1)), any_cast<Vec2>(args.at(2)), AnyAsFloat(args.at(3)));
return s;
}
@ -386,6 +400,9 @@ boost::any ZSFunction(const string& name, const vector<boost::any>& args)
any_cast<Sprite>(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<Vec2>(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;
}

View File

@ -26,6 +26,7 @@ using namespace boost;
int WINDOW_WIDTH = 1280;
int WINDOW_HEIGHT = 720;
int PIXEL_SCALE = 1;
unordered_map<string, int> KEYS =
{
@ -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);

View File

@ -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;

View File

@ -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

Binary file not shown.

After

Width:  |  Height:  |  Size: 297 B

View File

@ -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")