Graphics working, and started pong game

This commit is contained in:
sam-astro 2022-01-12 22:18:40 -05:00
parent b3ac3ce18f
commit 3e6c84add6
6 changed files with 68 additions and 43 deletions

View File

@ -58,7 +58,7 @@ bool IsVar(const string& varName, const unordered_map<string, boost::any>& varia
return false; return false;
} }
vector<boost::any> VarValues(const vector<string>& varNames, const unordered_map<string, boost::any>& variableValues) vector<boost::any> VarValues(const vector<string>& varNames, unordered_map<string, boost::any>& variableValues)
{ {
vector<boost::any> realValues; vector<boost::any> realValues;
@ -66,7 +66,8 @@ vector<boost::any> VarValues(const vector<string>& varNames, const unordered_map
{ {
string varName = trim(varNames[varIndex]); string varName = trim(varNames[varIndex]);
auto iA = variableValues.find(varName); realValues.push_back(EvalExpression(varName, variableValues));
/*auto iA = variableValues.find(varName);
if (iA != variableValues.end()) if (iA != variableValues.end())
{ {
realValues.push_back(iA->second); realValues.push_back(iA->second);
@ -78,7 +79,7 @@ vector<boost::any> VarValues(const vector<string>& varNames, const unordered_map
realValues.push_back(iB->second); realValues.push_back(iB->second);
else else
realValues.push_back(varName); realValues.push_back(varName);
} }*/
} }
return realValues; return realValues;
@ -267,34 +268,14 @@ int varOperation(const vector<string>& str, unordered_map<string, boost::any>& v
{ {
if (IsVar(split(str[0], '.')[0], variableValues)) if (IsVar(split(str[0], '.')[0], variableValues))
{ {
if (str[1] == "=") //InterpreterLog(unWrapVec(vector<string>(str.begin() + 2, str.end())));
EditClassSubComponent(variableValues[split(str[0], '.')[0]], str[1], EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues), split(str[0], '.')[1]); 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]);
else if (str[1] == "+=")
EditClassSubComponent(variableValues[split(str[0], '.')[0]], str[1], EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues), split(str[0], '.')[1]);
else if (str[1] == "-=")
EditClassSubComponent(variableValues[split(str[0], '.')[0]], str[1], EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues), split(str[0], '.')[1]);
else if (str[1] == "*=")
EditClassSubComponent(variableValues[split(str[0], '.')[0]], str[1], EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues), split(str[0], '.')[1]);
else if (str[1] == "/=")
EditClassSubComponent(variableValues[split(str[0], '.')[0]], str[1], EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues), split(str[0], '.')[1]);
else
LogWarning("unrecognized operator \'" + str[1] + "\'");
return 0; return 0;
} }
else if (IsVar(split(str[0], '.')[0], globalVariableValues)) else if (IsVar(split(str[0], '.')[0], globalVariableValues))
{ {
if (str[1] == "=") //InterpreterLog(unWrapVec(vector<string>(str.begin() + 2, str.end())));
EditClassSubComponent(globalVariableValues[split(str[0], '.')[0]], str[1], EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues), split(str[0], '.')[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]);
else if (str[1] == "+=")
EditClassSubComponent(globalVariableValues[split(str[0], '.')[0]], str[1], EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues), split(str[0], '.')[1]);
else if (str[1] == "-=")
EditClassSubComponent(globalVariableValues[split(str[0], '.')[0]], str[1], EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues), split(str[0], '.')[1]);
else if (str[1] == "*=")
EditClassSubComponent(globalVariableValues[split(str[0], '.')[0]], str[1], EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues), split(str[0], '.')[1]);
else if (str[1] == "/=")
EditClassSubComponent(globalVariableValues[split(str[0], '.')[0]], str[1], EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues), split(str[0], '.')[1]);
else
LogWarning("unrecognized operator \'" + str[1] + "\'");
return 0; return 0;
} }
} }

View File

@ -30,6 +30,18 @@ public:
boost::any nullType; boost::any nullType;
float clamp(float v, float min, float max)
{
if (v < min) return min;
if (v > max) return max;
return v;
}
float lerp(float a, float b, float f)
{
return a + f * (b - a);
}
int LogWarning(const string& warningText) int LogWarning(const string& warningText)
{ {
cerr << "\x1B[33mWARNING: " << warningText << "\033[0m\t\t" << endl; cerr << "\x1B[33mWARNING: " << warningText << "\033[0m\t\t" << endl;
@ -203,6 +215,8 @@ boost::any CPPFunction(const string& name, const vector<boost::any>& args)
return tan(AnyAsFloat(args[0])); return tan(AnyAsFloat(args[0]));
else if (name == "CPP.Math.Round") else if (name == "CPP.Math.Round")
return AnyAsInt(args[0]); return AnyAsInt(args[0]);
else if (name == "CPP.Math.Lerp")
return lerp(AnyAsFloat(args[0]), AnyAsFloat(args[1]), AnyAsFloat(args[2]));
else if (name == "CPP.Graphics.Init") else if (name == "CPP.Graphics.Init")
{ {
InterpreterLog("Init graphics"); InterpreterLog("Init graphics");

View File

@ -45,6 +45,13 @@ func Round(input)
return out return out
} }
// Linearly interpolates between a and b by t
func Lerp(a, b, t)
{
float out = CPP.Math.Lerp(a, b, t)
return out
}
// Clamps input between min and max // Clamps input between min and max
func Clamp(input, min, max) func Clamp(input, min, max)
{ {
@ -102,6 +109,5 @@ func NVec2(x, y)
func GetKey(keyName) func GetKey(keyName)
{ {
bool b = CPP.Input.GetKey(keyName) bool b = CPP.Input.GetKey(keyName)
print b
return b return b
} }

View File

@ -19,8 +19,10 @@
#include <SDL_ttf.h> #include <SDL_ttf.h>
#include <SDL_image.h> #include <SDL_image.h>
#include <string> #include <string>
#include <any>
using namespace std; using namespace std;
using namespace boost;
int WINDOW_WIDTH = 1280; int WINDOW_WIDTH = 1280;
int WINDOW_HEIGHT = 720; int WINDOW_HEIGHT = 720;
@ -212,7 +214,7 @@ public:
return y; return y;
} }
int EditSubComponent(std::string componentName, std::string oper, boost::any otherVal) Vec2 EditSubComponent(std::string componentName, std::string oper, boost::any otherVal)
{ {
if (componentName == "x") if (componentName == "x")
{ {
@ -240,6 +242,7 @@ public:
else if (oper == "/=") else if (oper == "/=")
y /= AnyAsFloat(otherVal); y /= AnyAsFloat(otherVal);
} }
return *this;
} }
float x, y; float x, y;
@ -381,7 +384,7 @@ public:
return position.y; return position.y;
} }
int 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")
{ {
@ -422,6 +425,7 @@ public:
else if (oper == "/=") else if (oper == "/=")
position.y /= AnyAsFloat(otherVal); position.y /= AnyAsFloat(otherVal);
} }
return *this;
} }
Vec2 position; Vec2 position;
@ -706,14 +710,14 @@ int updateLoop()
SDL_RenderClear(gRenderer); SDL_RenderClear(gRenderer);
ExecuteFunction("Update", vector<boost::any> {}); ExecuteFunction("Update", vector<boost::any> {dt});
// Present the backbuffer // Present the backbuffer
SDL_RenderPresent(gRenderer); SDL_RenderPresent(gRenderer);
// Calculate frame time // Calculate frame time
auto stopTime = std::chrono::high_resolution_clock::now(); auto stopTime = std::chrono::high_resolution_clock::now();
dt = std::chrono::duration<float, std::chrono::milliseconds::period>(stopTime - startTime).count(); dt = std::chrono::duration<float, std::chrono::milliseconds::period>(stopTime - startTime).count() / 1000.0f;
} }
return 0; return 0;

View File

@ -5,5 +5,6 @@
using namespace std; using namespace std;
boost::any ExecuteFunction(const string& functionName, const vector<boost::any>& inputVarVals); boost::any ExecuteFunction(const string& functionName, const vector<boost::any>& inputVarVals);
boost::any EvalExpression(const string& ex, unordered_map<string, boost::any>& variableValues);
#endif #endif

View File

@ -1,9 +1,10 @@
int SCREENW = 780 int SCREENW = 900
int SCREENH = 500 int SCREENH = 600
int scoreOne = 0 int scoreOne = 0
int scoreTwo = 0 int scoreTwo = 0
Vec2 paddleScale = NVec2(16, 70)
float paddleMoveSpeed = 200
func Main(input, in) func Main(input, in)
{ {
@ -22,29 +23,47 @@ func Start()
float yPosPaddle = yPosBall - paddleScale.y / 2 float yPosPaddle = yPosBall - paddleScale.y / 2
Vec2 lPaddlePosition = NVec2(15, yPosPaddle) Vec2 lPaddlePosition = NVec2(15, yPosPaddle)
global Vec2 lPaddleTargetPosition = NVec2(15, yPosPaddle)
float rOffset = SCREENW - 15 float rOffset = SCREENW - 15
Vec2 rPaddlePosition = NVec2(rOffset, yPosPaddle) Vec2 rPaddlePosition = NVec2(rOffset, yPosPaddle)
global Vec2 rPaddleTargetPosition = NVec2(15, yPosPaddle)
global Sprite ballSprite = CPP.Graphics.Sprite("./square.png", ballPosition, ballScale, 0) global Sprite ballSprite = 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)
} }
func Update() func Update(deltaTime)
{ {
Vec2 as = NVec2(16, 70) //print deltaTime
print "updating"
if GetKey("W") == true if GetKey("W") == true
{ {
print "W" float newX = lPaddle.position.x
as.x += 4 // Subtract from Y to move up, because vertical coordinates are reversed
// lPaddle.position.y += 1 float newY = lPaddleTargetPosition.y - paddleMoveSpeed * deltaTime
newY = Clamp(newY, 0, SCREENH - 70)
lPaddleTargetPosition = NVec2(newX, newY)
} }
if GetKey("S") == true
{
float newX = lPaddle.position.x
// Add to Y to move down, because vertical coordinates are reversed
float newY = lPaddleTargetPosition.y + paddleMoveSpeed * deltaTime
newY = Clamp(newY, 0, SCREENH - 70)
lPaddleTargetPosition = NVec2(newX, newY)
}
// Lerps from old position to destination smoothly
float oldY = lPaddle.position.y
float stopSpeed = deltaTime * 15
float newY = lPaddleTargetPosition.y
float lerpedY = Lerp(oldY, newY, stopSpeed)
print "0 < " + newY + " < " + SCREENH
lPaddle.position = NVec2(newX, lerpedY)
print as.x + " , " + as.y
// Finally draws all of the sprites
CPP.Graphics.Draw(ballSprite) CPP.Graphics.Draw(ballSprite)
CPP.Graphics.Draw(lPaddle) CPP.Graphics.Draw(lPaddle)
CPP.Graphics.Draw(rPaddle) CPP.Graphics.Draw(rPaddle)