Optimization, graphics and builtin updates, and continued working on pong game in Slang

This commit is contained in:
sam-astro 2022-01-13 19:45:03 -05:00
parent 62005b90b4
commit 71ba1d608e
7 changed files with 283 additions and 122 deletions

View File

@ -29,7 +29,7 @@ boost::any GetVariableValue(const string& varName, const unordered_map<string, b
if (count(varName, '.') > 0) if (count(varName, '.') > 0)
{ {
classSubComponent = rangeInStr(varName, indexInStr(varName, '.')+1, -1); classSubComponent = varName.substr(indexInStr(varName, '.')+1, -1);
baseName = split(varName, '.')[0]; baseName = split(varName, '.')[0];
} }
@ -243,7 +243,7 @@ bool BooleanLogic(const string& valA, const string& determinant, const string& v
{ {
boost::any valARealValue = EvalExpression(valA, variableValues); boost::any valARealValue = EvalExpression(valA, variableValues);
boost::any valBRealValue = EvalExpression(valB, variableValues); boost::any valBRealValue = EvalExpression(valB, variableValues);
//InterpreterLog(AnyAsString(valARealValue) + " " + determinant + " " + AnyAsString(valBRealValue) + " : " + to_string(AnyAsString(valARealValue) == AnyAsString(valBRealValue)));
if (determinant == "==") if (determinant == "==")
return AnyAsString(valARealValue) == AnyAsString(valBRealValue); return AnyAsString(valARealValue) == AnyAsString(valBRealValue);
else if (determinant == "!=") else if (determinant == "!=")
@ -264,55 +264,63 @@ bool BooleanLogic(const string& valA, const string& determinant, const string& v
int varOperation(const vector<string>& str, unordered_map<string, boost::any>& variableValues) int varOperation(const vector<string>& str, unordered_map<string, boost::any>& variableValues)
{ {
if (count(str[0], '.') > 0) try
{ {
if (IsVar(split(str[0], '.')[0], variableValues)) if (count(str[0], '.') > 0)
{ {
//InterpreterLog(unWrapVec(vector<string>(str.begin() + 2, str.end()))); if (IsVar(split(str[0], '.')[0], variableValues))
variableValues[split(str[0], '.')[0]] = EditClassSubComponent(variableValues[split(str[0], '.')[0]], str[1], EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues), split(str[0], '.')[1]); {
//InterpreterLog(unWrapVec(vector<string>(str.begin() + 2, str.end())));
variableValues[split(str[0], '.')[0]] = EditClassSubComponent(variableValues[split(str[0], '.')[0]], str[1], EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues), split(str[0], '.')[1]);
return 0;
}
else if (IsVar(split(str[0], '.')[0], globalVariableValues))
{
//InterpreterLog(unWrapVec(vector<string>(str.begin() + 2, str.end())));
globalVariableValues[split(str[0], '.')[0]] = EditClassSubComponent(globalVariableValues[split(str[0], '.')[0]], str[1], EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues), split(str[0], '.')[1]);
return 0;
}
}
else if (IsVar(str[0], variableValues))
{
if (str[1] == "=")
variableValues[str[0]] = EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues);
else if (str[1] == "+=")
variableValues[str[0]] = EvalExpression(str[0] + "+(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variableValues);
else if (str[1] == "-=")
variableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) - AnyAsFloat(EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues));
else if (str[1] == "*=")
variableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) * AnyAsFloat(EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues));
else if (str[1] == "/=")
variableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) / AnyAsFloat(EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues));
else
LogWarning("unrecognized operator \'" + str[1] + "\'");
return 0; return 0;
} }
else if (IsVar(split(str[0], '.')[0], globalVariableValues)) else if (IsVar(str[0], globalVariableValues))
{ {
//InterpreterLog(unWrapVec(vector<string>(str.begin() + 2, str.end()))); if (str[1] == "=")
globalVariableValues[split(str[0], '.')[0]] = EditClassSubComponent(globalVariableValues[split(str[0], '.')[0]], str[1], EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues), split(str[0], '.')[1]); globalVariableValues[str[0]] = EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues);
else if (str[1] == "+=")
globalVariableValues[str[0]] = EvalExpression(str[0] + "+(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variableValues);
else if (str[1] == "-=")
globalVariableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) - AnyAsFloat(EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues));
else if (str[1] == "*=")
globalVariableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) * AnyAsFloat(EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues));
else if (str[1] == "/=")
globalVariableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) / AnyAsFloat(EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues));
else
LogWarning("unrecognized operator \'" + str[1] + "\'");
return 0; return 0;
} }
LogWarning("uninitialized variable or typo in \'" + str[0] + "\'");
return 1;
} }
else if (IsVar(str[0], variableValues)) catch (const std::exception&)
{ {
if (str[1] == "=") LogWarning("uninitialized variable or typo in \'" + str[0] + "\'");
variableValues[str[0]] = EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues); return 1;
else if (str[1] == "+=")
variableValues[str[0]] = EvalExpression(str[0] + "+(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variableValues);
else if (str[1] == "-=")
variableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) - AnyAsFloat(EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues));
else if (str[1] == "*=")
variableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) * AnyAsFloat(EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues));
else if (str[1] == "/=")
variableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) / AnyAsFloat(EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues));
else
LogWarning("unrecognized operator \'" + str[1] + "\'");
return 0;
} }
else if (IsVar(str[0], globalVariableValues))
{
if (str[1] == "=")
globalVariableValues[str[0]] = EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues);
else if (str[1] == "+=")
globalVariableValues[str[0]] = EvalExpression(str[0] + "+(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variableValues);
else if (str[1] == "-=")
globalVariableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) - AnyAsFloat(EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues));
else if (str[1] == "*=")
globalVariableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) * AnyAsFloat(EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues));
else if (str[1] == "/=")
globalVariableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) / AnyAsFloat(EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues));
else
LogWarning("unrecognized operator \'" + str[1] + "\'");
return 0;
}
LogWarning("uninitialized variable or typo in \'" + str[0] + "\'");
return 1;
} }
boost::any ProcessLine(const vector<vector<string>>& words, int lineNum, unordered_map<string, boost::any>& variableValues) boost::any ProcessLine(const vector<vector<string>>& words, int lineNum, unordered_map<string, boost::any>& variableValues)
@ -362,7 +370,7 @@ boost::any ProcessLine(const vector<vector<string>>& words, int lineNum, unorder
// Check existing variables: If matches, then it means // Check existing variables: If matches, then it means
// the variables value is getting changed with an operator // the variables value is getting changed with an operator
else if (IsVar(words[lineNum][0], variableValues) || IsVar(words[lineNum][0], globalVariableValues)) else if (count(words[lineNum][0], '.') == 0 && (IsVar(words[lineNum][0], variableValues) || IsVar(words[lineNum][0], globalVariableValues)))
{ {
// Evaluates what the operator (ex. '=', '+=') does to the value on the left by the value on the right // Evaluates what the operator (ex. '=', '+=') does to the value on the left by the value on the right
varOperation(vector<string>(words[lineNum].begin(), words[lineNum].end()), variableValues); varOperation(vector<string>(words[lineNum].begin(), words[lineNum].end()), variableValues);
@ -370,7 +378,7 @@ boost::any ProcessLine(const vector<vector<string>>& words, int lineNum, unorder
} }
// Check existing variables: To see if class sub component matches // Check existing variables: To see if class sub component matches
else if (IsVar(split(words[lineNum][0], '.')[0], variableValues) || IsVar(split(words[lineNum][0], '.')[0], globalVariableValues)) else if (count(words[lineNum][0], '.') > 0 && IsVar(split(words[lineNum][0], '.')[0], variableValues) || IsVar(split(words[lineNum][0], '.')[0], globalVariableValues))
{ {
// Evaluates what the operator (ex. '=', '+=') does to the value on the left by the value on the right // Evaluates what the operator (ex. '=', '+=') does to the value on the left by the value on the right
varOperation(vector<string>(words[lineNum].begin(), words[lineNum].end()), variableValues); varOperation(vector<string>(words[lineNum].begin(), words[lineNum].end()), variableValues);
@ -380,7 +388,7 @@ boost::any ProcessLine(const vector<vector<string>>& words, int lineNum, unorder
// Gathers while loop contents // Gathers while loop contents
else if (words[lineNum][0] == "while") else if (words[lineNum][0] == "while")
{ {
vector<string> whileContents; vector<vector<string>> whileContents;
vector<string> whileParameters; vector<string> whileParameters;
for (int w = 1; w < (int)words[lineNum].size(); w++) for (int w = 1; w < (int)words[lineNum].size(); w++)
@ -392,24 +400,16 @@ boost::any ProcessLine(const vector<vector<string>>& words, int lineNum, unorder
numOfBrackets += countInVector(words[p], "{") - countInVector(words[p], "}"); numOfBrackets += countInVector(words[p], "{") - countInVector(words[p], "}");
if (numOfBrackets == 0) if (numOfBrackets == 0)
break; break;
whileContents.push_back(""); whileContents.push_back(words[p]);
for (int w = 0; w < (int)words[p].size(); w++)
{
whileContents[(int)whileContents.size() - 1] += words[p][w] + " ";
}
} }
whileContents = removeTabs(whileContents, 1); whileContents = removeTabsWdArry(whileContents, 1);
vector<vector<string>> innerWords;
for (int i = 0; i < (int)whileContents.size(); i++)
innerWords.push_back(split(whileContents[i], ' '));
while (BooleanLogic(whileParameters[0], whileParameters[1], whileParameters[2], variableValues)) while (BooleanLogic(whileParameters[0], whileParameters[1], whileParameters[2], variableValues))
{ {
//Iterate through all lines in while loop //Iterate through all lines in while loop
for (int lineNum = 0; lineNum < (int)whileContents.size(); lineNum++) for (int lineNum = 0; lineNum < (int)whileContents.size(); lineNum++)
{ {
boost::any returnVal = ProcessLine(innerWords, lineNum, variableValues); boost::any returnVal = ProcessLine(whileContents, lineNum, variableValues);
if (!returnVal.empty()) if (!returnVal.empty())
return returnVal; return returnVal;
} }
@ -420,7 +420,7 @@ boost::any ProcessLine(const vector<vector<string>>& words, int lineNum, unorder
// Gathers if statement contents // Gathers if statement contents
else if (words[lineNum][0] == "if") else if (words[lineNum][0] == "if")
{ {
vector<string> ifContents; vector<vector<string>> ifContents;
vector<string> ifParameters; vector<string> ifParameters;
for (int w = 1; w < (int)words[lineNum].size(); w++) for (int w = 1; w < (int)words[lineNum].size(); w++)
@ -433,62 +433,54 @@ boost::any ProcessLine(const vector<vector<string>>& words, int lineNum, unorder
numOfBrackets += countInVector(words[lineNum], "{") - countInVector(words[lineNum], "}"); numOfBrackets += countInVector(words[lineNum], "{") - countInVector(words[lineNum], "}");
if (numOfBrackets == 0) if (numOfBrackets == 0)
break; break;
ifContents.push_back(""); ifContents.push_back(words[lineNum]);
for (int w = 0; w < (int)words[lineNum].size(); w++)
{
ifContents[(int)ifContents.size() - 1] += words[lineNum][w] + " ";
}
lineNum++; lineNum++;
} }
ifContents = removeTabs(ifContents, 1); ifContents = removeTabsWdArry(ifContents, 1);
vector<vector<string>> innerWords;
for (int i = 0; i < (int)ifContents.size(); i++)
innerWords.push_back(split(ifContents[i], ' '));
if (BooleanLogic(ifParameters[0], ifParameters[1], ifParameters[2], variableValues)) if (BooleanLogic(ifParameters[0], ifParameters[1], ifParameters[2], variableValues))
{ {
//Iterate through all lines in if statement //Iterate through all lines in if statement
for (int l = 0; l < (int)ifContents.size(); l++) for (int l = 0; l < (int)ifContents.size(); l++)
{ {
boost::any returnVal = ProcessLine(innerWords, l, variableValues); boost::any returnVal = ProcessLine(ifContents, l, variableValues);
if (!returnVal.empty()) if (!returnVal.empty())
return returnVal; return returnVal;
} }
} }
else if (words.size() > lineNum + 1) //else if (words.size() > lineNum + 1)
if (words[lineNum + 1][0] == "else") // if (words[lineNum + 1][0] == "else")
{ // {
lineNum += 1; // lineNum += 1;
vector<string> elseContents; // vector<string> elseContents;
int numOfBrackets = 1; // int numOfBrackets = 1;
while (lineNum < (int)words.size()) // while (lineNum < (int)words.size())
{ // {
numOfBrackets += countInVector(words[lineNum], "{") - countInVector(words[lineNum], "}"); // numOfBrackets += countInVector(words[lineNum], "{") - countInVector(words[lineNum], "}");
if (numOfBrackets == 0) // if (numOfBrackets == 0)
break; // break;
elseContents.push_back(""); // elseContents.push_back("");
for (int w = 0; w < (int)words[lineNum].size(); w++) // for (int w = 0; w < (int)words[lineNum].size(); w++)
{ // {
elseContents[(int)elseContents.size() - 1] += words[lineNum][w] + " "; // elseContents[(int)elseContents.size() - 1] += words[lineNum][w] + " ";
} // }
lineNum++; // lineNum++;
} // }
elseContents = removeTabs(elseContents, 2); // elseContents = removeTabs(elseContents, 2);
vector<vector<string>> innerWords; // vector<vector<string>> innerWords;
for (int i = 0; i < (int)elseContents.size(); i++) // for (int i = 0; i < (int)elseContents.size(); i++)
innerWords.push_back(split(elseContents[i], ' ')); // innerWords.push_back(split(elseContents[i], ' '));
//Iterate through all lines in else statement // //Iterate through all lines in else statement
for (int lineNum = 0; lineNum < (int)elseContents.size(); lineNum++) // for (int lineNum = 0; lineNum < (int)elseContents.size(); lineNum++)
{ // {
ProcessLine(innerWords, lineNum, variableValues); // ProcessLine(innerWords, lineNum, variableValues);
} // }
return nullType; // return nullType;
} // }
return nullType; return nullType;
} }
//// Gathers else statement contents //// Gathers else statement contents
@ -516,17 +508,16 @@ boost::any ExecuteFunction(const string& functionName, const vector<boost::any>&
//Iterate through all lines in function //Iterate through all lines in function
for (int lineNum = 1; lineNum < (int)words.size(); lineNum++) for (int lineNum = 1; lineNum < (int)words.size(); lineNum++)
{ {
boost::any returnVal = 0;
try try
{ {
returnVal = ProcessLine(words, lineNum, variableValues); boost::any returnVal = ProcessLine(words, lineNum, variableValues);
if (!returnVal.empty())
return returnVal;
} }
catch (const std::exception&) catch (const std::exception&)
{ {
LogCriticalError("\'" + unWrapVec(words[lineNum]) + "\'\n In function: " + functionName + "\n Line: " + to_string(lineNum)); LogCriticalError("\'" + unWrapVec(words[lineNum]) + "\'\n In function: " + functionName + "\n Line: " + to_string(lineNum));
} }
if (!returnVal.empty())
return returnVal;
} }
return nullType; return nullType;
} }

View File

@ -129,6 +129,11 @@ boost::any EditClassSubComponent(boost::any value, string oper, boost::any other
return nullType; return nullType;
} }
bool AxisAlignedCollision(const Sprite& a, const Sprite& b)
{
}
// Initial script processing, which loads variables and functions from builtin // Initial script processing, which loads variables and functions from builtin
int GetBuiltins(const string& s) int GetBuiltins(const string& s)
{ {
@ -217,6 +222,8 @@ boost::any CPPFunction(const string& name, const vector<boost::any>& args)
return AnyAsInt(args[0]); return AnyAsInt(args[0]);
else if (name == "CPP.Math.Lerp") else if (name == "CPP.Math.Lerp")
return lerp(AnyAsFloat(args[0]), AnyAsFloat(args[1]), AnyAsFloat(args[2])); return lerp(AnyAsFloat(args[0]), AnyAsFloat(args[1]), AnyAsFloat(args[2]));
else if (name == "CPP.Math.Abs")
return abs(AnyAsFloat(args[0]));
else if (name == "CPP.Graphics.Init") else if (name == "CPP.Graphics.Init")
{ {
InterpreterLog("Init graphics"); InterpreterLog("Init graphics");

View File

@ -52,6 +52,27 @@ func Lerp(a, b, t)
return out return out
} }
// Get absolute value of x
func Abs(x)
{
float out = CPP.Math.Abs(x)
return out
}
// Convert radians to degrees
func RadToDeg(x)
{
float out = (x * PI) / 180
return out
}
// Convert degrees to radians
func DegToRad(x)
{
float out = (x * 180) / PI
return out
}
// Clamps input between min and max // Clamps input between min and max
func Clamp(input, min, max) func Clamp(input, min, max)
{ {

View File

@ -349,7 +349,7 @@ class Sprite
{ {
public: public:
Sprite(std::string path, Vec2 position, Vec2 scale, double angle) Sprite(std::string path, Vec2 position, Vec2 scale, double angle)
: position(position), angle(angle), path(path) : position(position), angle(angle), path(path), scale(scale)
{ {
rect.x = static_cast<int>(position.x); rect.x = static_cast<int>(position.x);
rect.y = static_cast<int>(position.y); rect.y = static_cast<int>(position.y);
@ -369,6 +369,7 @@ public:
int Draw() int Draw()
{ {
rect.x = static_cast<int>(position.x);
rect.y = static_cast<int>(position.y); rect.y = static_cast<int>(position.y);
SDL_RenderCopy(gRenderer, texture, NULL, &rect); SDL_RenderCopy(gRenderer, texture, NULL, &rect);
return 0; return 0;
@ -382,24 +383,30 @@ public:
return position.x; return position.x;
if (componentName == "position.y") if (componentName == "position.y")
return position.y; return position.y;
if (componentName == "scale")
return scale;
if (componentName == "scale.x")
return scale.x;
if (componentName == "scale.y")
return scale.y;
} }
Sprite EditSubComponent(std::string componentName, std::string oper, boost::any otherVal) Sprite EditSubComponent(std::string componentName, std::string oper, boost::any otherVal)
{ {
if (componentName == "position") if (componentName == "position")
{ {
if(oper == "=") if (oper == "=")
position = any_cast<Vec2>(otherVal); position = any_cast<Vec2>(otherVal);
else if(oper == "+=") else if (oper == "+=")
position += any_cast<Vec2>(otherVal); position += any_cast<Vec2>(otherVal);
else if(oper == "-=") else if (oper == "-=")
position -= any_cast<Vec2>(otherVal); position -= any_cast<Vec2>(otherVal);
else if(oper == "*=") else if (oper == "*=")
position *= AnyAsFloat(otherVal); position *= AnyAsFloat(otherVal);
else if(oper == "/=") else if (oper == "/=")
position /= AnyAsFloat(otherVal); position /= AnyAsFloat(otherVal);
} }
if (componentName == "position.x") else if (componentName == "position.x")
{ {
if (oper == "=") if (oper == "=")
position.x = AnyAsFloat(otherVal); position.x = AnyAsFloat(otherVal);
@ -412,7 +419,7 @@ public:
else if (oper == "/=") else if (oper == "/=")
position.x /= AnyAsFloat(otherVal); position.x /= AnyAsFloat(otherVal);
} }
if (componentName == "position.y") else if (componentName == "position.y")
{ {
if (oper == "=") if (oper == "=")
position.y = AnyAsFloat(otherVal); position.y = AnyAsFloat(otherVal);
@ -425,11 +432,53 @@ public:
else if (oper == "/=") else if (oper == "/=")
position.y /= AnyAsFloat(otherVal); position.y /= AnyAsFloat(otherVal);
} }
else if (componentName == "scale")
{
if (oper == "=")
scale = any_cast<Vec2>(otherVal);
else if (oper == "+=")
scale += any_cast<Vec2>(otherVal);
else if (oper == "-=")
scale -= any_cast<Vec2>(otherVal);
else if (oper == "*=")
scale *= AnyAsFloat(otherVal);
else if (oper == "/=")
scale /= AnyAsFloat(otherVal);
}
else if (componentName == "scale.x")
{
if (oper == "=")
scale.x = AnyAsFloat(otherVal);
else if (oper == "+=")
scale.x += AnyAsFloat(otherVal);
else if (oper == "-=")
scale.x -= AnyAsFloat(otherVal);
else if (oper == "*=")
scale.x *= AnyAsFloat(otherVal);
else if (oper == "/=")
scale.x /= AnyAsFloat(otherVal);
}
else if (componentName == "scale.y")
{
if (oper == "=")
scale.y = AnyAsFloat(otherVal);
else if (oper == "+=")
scale.y += AnyAsFloat(otherVal);
else if (oper == "-=")
scale.y -= AnyAsFloat(otherVal);
else if (oper == "*=")
scale.y *= AnyAsFloat(otherVal);
else if (oper == "/=")
scale.y /= AnyAsFloat(otherVal);
}
return *this; return *this;
} }
Vec2 position; Vec2 position;
Vec2 scale;
double angle; double angle;
std::string path; std::string path;
SDL_Rect rect{}; SDL_Rect rect{};
SDL_Texture* texture; SDL_Texture* texture;
@ -732,8 +781,8 @@ int initGraphics(std::string windowTitle, int width, int height)
SDL_Init(SDL_INIT_VIDEO); SDL_Init(SDL_INIT_VIDEO);
TTF_Init(); TTF_Init();
gWindow = SDL_CreateWindow(windowTitle.c_str(), 40, 40, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN); gWindow = SDL_CreateWindow(windowTitle.c_str(), 40, 40, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN | SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
gRenderer = SDL_CreateRenderer(gWindow, -1, 0, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); gRenderer = SDL_CreateRenderer(gWindow, -1, 0);
//Get window surface //Get window surface
gScreenSurface = SDL_GetWindowSurface(gWindow); gScreenSurface = SDL_GetWindowSurface(gWindow);

View File

@ -4,7 +4,9 @@ int SCREENH = 600
int scoreOne = 0 int scoreOne = 0
int scoreTwo = 0 int scoreTwo = 0
float paddleMoveSpeed = 200 float ballSpeed = -3
float paddleMoveSpeed = 7
func Main(input, in) func Main(input, in)
{ {
@ -25,23 +27,29 @@ func Start()
Vec2 lPaddlePosition = NVec2(15, yPosPaddle) Vec2 lPaddlePosition = NVec2(15, yPosPaddle)
global Vec2 lPaddleTargetPosition = NVec2(15, yPosPaddle) global Vec2 lPaddleTargetPosition = NVec2(15, yPosPaddle)
float rOffset = SCREENW - 15 float rOffset = SCREENW - (paddleScale.x + 15)
Vec2 rPaddlePosition = NVec2(rOffset, yPosPaddle) Vec2 rPaddlePosition = NVec2(rOffset, yPosPaddle)
global Vec2 rPaddleTargetPosition = NVec2(15, yPosPaddle) global Vec2 rPaddleTargetPosition = NVec2(rOffset, yPosPaddle)
global Sprite ballSprite = CPP.Graphics.Sprite("./square.png", ballPosition, ballScale, 0) global Sprite ballSpr = CPP.Graphics.Sprite("./square.png", ballPosition, ballScale, 0)
global Sprite lPaddle = CPP.Graphics.Sprite("./square.png", lPaddlePosition, paddleScale, 0) global Sprite lPaddle = CPP.Graphics.Sprite("./square.png", lPaddlePosition, paddleScale, 0)
global Sprite rPaddle = CPP.Graphics.Sprite("./square.png", rPaddlePosition, paddleScale, 0) global Sprite rPaddle = CPP.Graphics.Sprite("./square.png", rPaddlePosition, paddleScale, 0)
global Vec2 ballVelocity = NVec2(ballSpeed, 0)
} }
func Update(deltaTime) func Update(deltaTime)
{ {
//print deltaTime float FPS = 1 / deltaTime
print "FPS: " + FPS
// Handles Left Paddle Movement
//
if GetKey("W") == true if GetKey("W") == true
{ {
float newX = lPaddle.position.x float newX = lPaddle.position.x
// Subtract from Y to move up, because vertical coordinates are reversed // Subtract from Y to move up, because vertical coordinates are reversed
float newY = lPaddleTargetPosition.y - paddleMoveSpeed * deltaTime float newY = lPaddleTargetPosition.y - paddleMoveSpeed
newY = Clamp(newY, 0, SCREENH - 70) newY = Clamp(newY, 0, SCREENH - 70)
lPaddleTargetPosition = NVec2(newX, newY) lPaddleTargetPosition = NVec2(newX, newY)
} }
@ -49,23 +57,86 @@ func Update(deltaTime)
{ {
float newX = lPaddle.position.x float newX = lPaddle.position.x
// Add to Y to move down, because vertical coordinates are reversed // Add to Y to move down, because vertical coordinates are reversed
float newY = lPaddleTargetPosition.y + paddleMoveSpeed * deltaTime float newY = lPaddleTargetPosition.y + paddleMoveSpeed
newY = Clamp(newY, 0, SCREENH - 70) newY = Clamp(newY, 0, SCREENH - 70)
lPaddleTargetPosition = NVec2(newX, newY) lPaddleTargetPosition = NVec2(newX, newY)
} }
// Lerps from old position to destination smoothly // Lerps from old position to destination smoothly
float oldY = lPaddle.position.y float oldY = lPaddle.position.y
float stopSpeed = deltaTime * 15 float stopSpeed = deltaTime * 6
float newY = lPaddleTargetPosition.y float newY = lPaddleTargetPosition.y
float lerpedY = Lerp(oldY, newY, stopSpeed) float lerpedY = Lerp(oldY, newY, stopSpeed)
//print "0 < " + newY + " < " + SCREENH
lPaddle.position = NVec2(newX, lerpedY) lPaddle.position = NVec2(newX, lerpedY)
print "FPS: " + 1 / deltaTime // Handles Right Paddle Movement
//
if GetKey("UP") == true
{
float newX = rPaddle.position.x
// Subtract from Y to move up, because vertical coordinates are reversed
float newY = rPaddleTargetPosition.y - paddleMoveSpeed
newY = Clamp(newY, 0, SCREENH - 70)
rPaddleTargetPosition = NVec2(newX, newY)
}
if GetKey("DOWN") == true
{
float newX = rPaddle.position.x
// Add to Y to move down, because vertical coordinates are reversed
float newY = rPaddleTargetPosition.y + paddleMoveSpeed
newY = Clamp(newY, 0, SCREENH - 70)
rPaddleTargetPosition = NVec2(newX, newY)
}
// Lerps from old position to destination smoothly
float oldY = rPaddle.position.y
float stopSpeed = deltaTime * 6
float newY = rPaddleTargetPosition.y
float lerpedY = Lerp(oldY, newY, stopSpeed)
rPaddle.position = NVec2(newX, lerpedY)
ballSpr.position += ballVelocity
HandleBallBounce()
// Finally draws all of the sprites // Finally draws all of the sprites
CPP.Graphics.Draw(ballSprite) CPP.Graphics.Draw(ballSpr)
CPP.Graphics.Draw(lPaddle) CPP.Graphics.Draw(lPaddle)
CPP.Graphics.Draw(rPaddle) CPP.Graphics.Draw(rPaddle)
} }
func HandleBallBounce()
{
float ballX = ballSpr.position.x
float ballY = ballSpr.position.y
// Checks if the ball is on the same X coordinate as the left paddle
if ballX <= lPaddle.position.x
{
// Then check if ball is lower than the top edge
float positionAdjustedOffset = lPaddle.position.y
positionAdjustedOffset += lPaddle.scale.y
if ballY <= positionAdjustedOffset
{
// Finally check if ball is higher than the bottom edge
positionAdjustedOffset = lPaddle.position.y
positionAdjustedOffset -= lPaddle.scale.y
print positionAdjustedOffset
if ballY >= positionAdjustedOffset
{
float difference = lPaddle.position.y
difference -= ballY
// float normalizedRelativeIntersectionY = (difference/(lPaddle.scale.y/2))
// float bounceAngle = normalizedRelativeIntersectionY * 1.3089
// float ballVx = ballSpeed*Cos(bounceAngle)
// float ballVy = ballSpeed*-Sin(bounceAngle)
// // Reflect horizontally
// if difference < 10
// {
// float newX = Sin(DegToRad())
// ballVelocity
// }
}
}
}
}

View File

@ -205,6 +205,26 @@ vector<string> removeTabs(const vector<string>& str, const int& amnt) {
return newStr; return newStr;
} }
vector<vector<string>> removeTabsWdArry(const vector<vector<string>>& str, const int& amnt) {
vector<vector<string>> newWds;
for (int i = 0; i < (int)str.size(); i++)
{
//newWds.push_back(rangeInVec(str[i], amnt, -1));
newWds.push_back(vector<string>());
for (int c = 0; c < (int)str[i].size(); c++)
{
if (str[i][c][0] != '\t' || c >= amnt)
newWds[i].push_back(str[i][c]);
else
newWds[i].push_back(str[i][c].substr(1, -1));
}
}
return newWds;
}
vector<string> rangeInVec(const vector<string>& str, const int& min, int max) { vector<string> rangeInVec(const vector<string>& str, const int& min, int max) {
if (max == -1) if (max == -1)
max = (int)str.size(); max = (int)str.size();

View File

@ -39,9 +39,11 @@ int countInVector(const vector<string>& str, const string& ch);
string Vec2Str(const vector<string>& str); string Vec2Str(const vector<string>& str);
vector<vector<string>> removeTabsWdArry(const vector<vector<string>>& str, const int& amnt);
vector<string> removeTabs(const vector<string>& str, const int& amnt); vector<string> removeTabs(const vector<string>& str, const int& amnt);
vector<string> rangeInVec(const vector<string>& str, const int& min, const int& max); vector<string> rangeInVec(const vector<string>& str, const int& min, int max);
vector<string> slice(vector<string> const& v, int min, int max); vector<string> slice(vector<string> const& v, int min, int max);