mirror of
https://github.com/sam-astro/Z-Sharp.git
synced 2025-12-11 16:22:12 +00:00
Graphics and drawing now work, but there is a HUGE memory leak, probably from creating a new object every frame or something
This commit is contained in:
parent
ee147bd78a
commit
79ec9f6cd7
@ -24,28 +24,35 @@ unordered_map<string, vector<vector<string>>> functionValues;
|
||||
|
||||
boost::any GetVariableValue(const string& varName, const unordered_map<string, boost::any>& variableValues)
|
||||
{
|
||||
auto iA = variableValues.find(varName);
|
||||
string classSubComponent;
|
||||
string baseName = varName;
|
||||
|
||||
if (count(varName, '.') > 0)
|
||||
{
|
||||
classSubComponent = rangeInStr(varName, indexInStr(varName, '.')+1, -1);
|
||||
baseName = split(varName, '.')[0];
|
||||
}
|
||||
|
||||
boost::any outputValue = nullType;
|
||||
|
||||
auto iA = variableValues.find(baseName);
|
||||
auto iB = globalVariableValues.find(baseName);
|
||||
if (iA != variableValues.end())
|
||||
{
|
||||
return iA->second;
|
||||
}
|
||||
outputValue = iA->second;
|
||||
else if (iB != globalVariableValues.end())
|
||||
outputValue = iB->second;
|
||||
else
|
||||
{
|
||||
auto iB = globalVariableValues.find(varName);
|
||||
if (iB != globalVariableValues.end())
|
||||
{
|
||||
return iB->second;
|
||||
}
|
||||
else
|
||||
{
|
||||
return varName;
|
||||
}
|
||||
}
|
||||
outputValue = varName;
|
||||
|
||||
if (count(varName, '.') > 0 && !outputValue.empty())
|
||||
return GetClassSubComponent(outputValue, classSubComponent);
|
||||
else
|
||||
return outputValue;
|
||||
}
|
||||
|
||||
bool IsVar(const string& varName, const unordered_map<string, boost::any>& variableValues)
|
||||
{
|
||||
if (variableValues.find(varName) != variableValues.end())
|
||||
if (variableValues.find(split(varName, '.')[0]) != variableValues.end() && split(varName, '.')[0] != "CPP")
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
@ -58,7 +65,6 @@ vector<boost::any> VarValues(const vector<string>& varNames, const unordered_map
|
||||
for (int varIndex = 0; varIndex < varNames.size(); varIndex++)
|
||||
{
|
||||
string varName = trim(varNames[varIndex]);
|
||||
//cout << varName << endl;
|
||||
|
||||
auto iA = variableValues.find(varName);
|
||||
if (iA != variableValues.end())
|
||||
@ -98,10 +104,10 @@ boost::any EvalExpression(const string& ex, unordered_map<string, boost::any>& v
|
||||
string expression = trim(ex);
|
||||
bool inQuotes = false;
|
||||
|
||||
//CompilerLog("OLDEXPRESSION: |" + expression + "|");
|
||||
//InterpreterLog("OLDEXPRESSION: |" + expression + "|");
|
||||
|
||||
// If no operations are applied, then return self
|
||||
if ((count(expression, '+') == 0 && count(expression, '-') == 0 && count(expression, '*') == 0 && count(expression, '/') == 0 && count(expression, '(') == 0 && count(expression, '^') == 0) || split(expression, '.')[0] == "CPP")
|
||||
if ((count(expression, '+') == 0 && count(expression, '-') == 0 && count(expression, '*') == 0 && count(expression, '/') == 0 && count(expression, '^') == 0) || split(expression, '.')[0] == "CPP")
|
||||
{
|
||||
bool isFunc = IsFunction(split(expression, '(')[0]);
|
||||
if (isFunc && !inQuotes)
|
||||
@ -115,7 +121,7 @@ boost::any EvalExpression(const string& ex, unordered_map<string, boost::any>& v
|
||||
|
||||
y++;
|
||||
}
|
||||
//CompilerLog(split(expression, '(')[0] + " " + AnyAsString(GetVariableValue(split(argContents, ',')[0], variableValues)));
|
||||
//InterpreterLog(split(expression, '(')[0] + " " + AnyAsString(GetVariableValue(split(argContents, ',')[0], variableValues)));
|
||||
return ExecuteFunction(split(expression, '(')[0], VarValues(split(argContents, ','), variableValues));
|
||||
}
|
||||
else if (split(expression, '.')[0] == "CPP" && !inQuotes)
|
||||
@ -128,7 +134,7 @@ boost::any EvalExpression(const string& ex, unordered_map<string, boost::any>& v
|
||||
|
||||
y++;
|
||||
}
|
||||
//CompilerLog(split(expression, '(')[0] + " " + argContents);
|
||||
//InterpreterLog(split(expression, '(')[0] + " " + argContents);
|
||||
return CPPFunction(split(expression, '(')[0], VarValues(split(argContents, ','), variableValues));
|
||||
}
|
||||
else
|
||||
@ -165,7 +171,7 @@ boost::any EvalExpression(const string& ex, unordered_map<string, boost::any>& v
|
||||
|
||||
i++;
|
||||
}
|
||||
//CompilerLog(split(expression, '(')[0] + " " + AnyAsString(GetVariableValue(split(argContents, ',')[0], variableValues)));
|
||||
//InterpreterLog(split(expression, '(')[0] + " " + AnyAsString(GetVariableValue(split(argContents, ',')[0], variableValues)));
|
||||
string returnVal = AnyAsString(ExecuteFunction(name, VarValues(split(argContents, ','), variableValues)));
|
||||
newExpression += returnVal;
|
||||
//cout << newExpression << endl;
|
||||
@ -199,7 +205,7 @@ boost::any EvalExpression(const string& ex, unordered_map<string, boost::any>& v
|
||||
newExpression += expression[i];
|
||||
}
|
||||
}
|
||||
//CompilerLog("NEW EXPRESSION: |" + newExpression + "|");
|
||||
//InterpreterLog("NEW EXPRESSION: |" + newExpression + "|");
|
||||
|
||||
bool addStrings = false;
|
||||
for (int i = 0; i < (int)newExpression.size(); i++)
|
||||
@ -297,8 +303,8 @@ int varOperation(const vector<string>& str, unordered_map<string, boost::any>& v
|
||||
|
||||
boost::any ProcessLine(const vector<vector<string>>& words, int lineNum, unordered_map<string, boost::any>& variableValues)
|
||||
{
|
||||
//CompilerLog(unWrapVec(words[lineNum]));
|
||||
//CompilerLog(AnyAsString(GetVariableValue("out", variableValues)));
|
||||
//InterpreterLog(unWrapVec(words[lineNum]));
|
||||
//InterpreterLog(AnyAsString(GetVariableValue("out", variableValues)));
|
||||
|
||||
if (words[lineNum][0][0] == '/' && words[lineNum][0][1] == '/')
|
||||
return nullType;
|
||||
@ -325,12 +331,18 @@ boost::any ProcessLine(const vector<vector<string>>& words, int lineNum, unorder
|
||||
return nullType;
|
||||
}
|
||||
|
||||
// Check if global variable declaration
|
||||
else if (trim(words[lineNum][0]) == "global")
|
||||
{
|
||||
globalVariableValues[words[lineNum][2]] = EvalExpression(unWrapVec(slice(words[lineNum], 4, -1)), variableValues);
|
||||
return nullType;
|
||||
}
|
||||
|
||||
// Iterate through all types to see if line inits or
|
||||
// re-inits a variable then store it with it's value
|
||||
else if (countInVector(types, trim(words[lineNum][0])) > 0)
|
||||
{
|
||||
variableValues[words[lineNum][1]] = EvalExpression(unWrapVec(slice(words[lineNum], 3, -1)), variableValues);
|
||||
//CompilerLog("new var :: " + words[lineNum][1] + " = " + AnyAsString(variableValues[words[lineNum][1]]));
|
||||
return nullType;
|
||||
}
|
||||
|
||||
@ -524,7 +536,7 @@ int parseSlang(string script)
|
||||
}
|
||||
|
||||
args = replace(args, functName + " ", "");
|
||||
//CompilerLog(args);
|
||||
//InterpreterLog(args);
|
||||
functionContents.push_back(split(args, ','));
|
||||
|
||||
int numOfBrackets = 1;
|
||||
|
||||
@ -10,9 +10,17 @@ using namespace std;
|
||||
|
||||
int LogWarning(const string& warningText);
|
||||
|
||||
// Gets if any is NullType
|
||||
bool any_null(const boost::any& val)
|
||||
{
|
||||
return val.empty();
|
||||
}
|
||||
|
||||
// Will convert type 'any' val to a bool
|
||||
bool AnyAsBool(const boost::any& val)
|
||||
{
|
||||
if (any_null(val))
|
||||
return false;
|
||||
try // Try converting to bool
|
||||
{
|
||||
return any_cast<bool>(val);
|
||||
@ -48,6 +56,8 @@ bool AnyAsBool(const boost::any& val)
|
||||
// Will convert type 'any' val to a string
|
||||
string AnyAsString(const boost::any& val)
|
||||
{
|
||||
if (any_null(val))
|
||||
return "";
|
||||
try // Try converting to string
|
||||
{
|
||||
return any_cast<string>(val);
|
||||
@ -85,6 +95,8 @@ string AnyAsString(const boost::any& val)
|
||||
// Will convert type 'any' val to a float
|
||||
float AnyAsFloat(const boost::any& val)
|
||||
{
|
||||
if (any_null(val))
|
||||
return 0.0f;
|
||||
try // Try converting to float
|
||||
{
|
||||
return any_cast<float>(val);
|
||||
@ -105,11 +117,11 @@ float AnyAsFloat(const boost::any& val)
|
||||
{
|
||||
try
|
||||
{
|
||||
float i = 0;
|
||||
if (any_cast<bool>(val) == true) i = 1;
|
||||
float i = 0.0f;
|
||||
if (any_cast<bool>(val) == true) i = 1.0f;
|
||||
return i;
|
||||
}
|
||||
catch (boost::bad_any_cast) // Does not convert, return
|
||||
catch (boost::bad_any_cast e) // Does not convert, return
|
||||
{
|
||||
LogWarning("invalid conversion to type \'float\'");
|
||||
return 0;
|
||||
@ -122,6 +134,8 @@ float AnyAsFloat(const boost::any& val)
|
||||
// Will convert type 'any' val to an integer
|
||||
int AnyAsInt(const boost::any& val)
|
||||
{
|
||||
if (any_null(val))
|
||||
return 0;
|
||||
try // Try converting to int
|
||||
{
|
||||
return any_cast<int>(val);
|
||||
@ -157,7 +171,7 @@ int AnyAsInt(const boost::any& val)
|
||||
}
|
||||
|
||||
// Gets type of 'any' val
|
||||
// 0 -> int; 1 -> float; 2 -> bool; 3 -> string;
|
||||
// 0 -> int; 1 -> float; 2 -> bool; 3 -> string; 4 -> Sprite; 5 -> Vec2;
|
||||
int any_type(const boost::any& val)
|
||||
{
|
||||
try // Try converting to int
|
||||
@ -186,31 +200,30 @@ int any_type(const boost::any& val)
|
||||
string s = any_cast<string>(val);
|
||||
return 3;
|
||||
}
|
||||
catch (boost::bad_any_cast) // Does not convert, return
|
||||
catch (boost::bad_any_cast) // Try converting to sprite
|
||||
{
|
||||
LogWarning("variable has no type");
|
||||
return -1;
|
||||
try
|
||||
{
|
||||
Sprite s = any_cast<Sprite>(val);
|
||||
return 4;
|
||||
}
|
||||
catch (boost::bad_any_cast) // Try converting to Vec2
|
||||
{
|
||||
try
|
||||
{
|
||||
Vec2 v = any_cast<Vec2>(val);
|
||||
return 5;
|
||||
}
|
||||
catch (boost::bad_any_cast) // Does not convert, return
|
||||
{
|
||||
LogWarning("variable has no type");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Gets if any is NullType
|
||||
bool any_null(const boost::any& val)
|
||||
{
|
||||
/*if (val.type() == typeid(NullType))
|
||||
return true;*/
|
||||
return false;
|
||||
//try // Try converting to Null
|
||||
//{
|
||||
// NullType n = any_cast<NullType>(val);
|
||||
// return true;
|
||||
//}
|
||||
//catch (boost::bad_any_cast)
|
||||
//{
|
||||
// return false;
|
||||
//}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@ -13,6 +13,7 @@
|
||||
#include "anyops.h"
|
||||
#include <boost/any.hpp>
|
||||
#include <SDL.h>
|
||||
#include <ctime>
|
||||
|
||||
using namespace std;
|
||||
using namespace boost;
|
||||
@ -35,19 +36,63 @@ int LogWarning(const string& warningText)
|
||||
return 1;
|
||||
}
|
||||
|
||||
int CompilerLog(const string& logText)
|
||||
int InterpreterLog(const string& logText)
|
||||
{
|
||||
cerr << "\x1B[32mclog: " << logText << "\033[0m\t\t" << endl;
|
||||
time_t timer = time(0);
|
||||
|
||||
tm bt{};
|
||||
#if defined(__unix__)
|
||||
localtime_r(&timer, &bt);
|
||||
#elif defined(_MSC_VER)
|
||||
localtime_s(&bt, &timer);
|
||||
#else
|
||||
static mutex mtx;
|
||||
std::lock_guard<std::mutex> lock(mtx);
|
||||
bt = *localtime(&timer);
|
||||
#endif
|
||||
|
||||
int Hour = bt.tm_hour;
|
||||
int Min = bt.tm_min;
|
||||
int Sec = bt.tm_sec;
|
||||
|
||||
cout << "\x1B[34m[" + to_string(Hour) + ":" + to_string(Min) + ":" + to_string(Sec) + "] \x1B[33mSlang: \x1B[32m" << logText << "\033[0m\t\t" << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int LogCriticalError(const string& errorText)
|
||||
{
|
||||
cerr << "\x1B[31mERROR: " << errorText << "\033[0m\t\t" << endl;
|
||||
time_t timer = time(0);
|
||||
|
||||
tm bt{};
|
||||
#if defined(__unix__)
|
||||
localtime_r(&timer, &bt);
|
||||
#elif defined(_MSC_VER)
|
||||
localtime_s(&bt, &timer);
|
||||
#else
|
||||
static mutex mtx;
|
||||
std::lock_guard<std::mutex> lock(mtx);
|
||||
bt = *localtime(&timer);
|
||||
#endif
|
||||
|
||||
int Hour = bt.tm_hour;
|
||||
int Min = bt.tm_min;
|
||||
int Sec = bt.tm_sec;
|
||||
|
||||
cerr << "\x1B[34m[" + to_string(Hour) + ":" + to_string(Min) + ":" + to_string(Sec) + "] \x1B[33mSlang: \x1B[31mERROR: " << errorText << "\033[0m\t\t" << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
return 2;
|
||||
}
|
||||
|
||||
boost::any GetClassSubComponent(boost::any value, string subComponentName)
|
||||
{
|
||||
// If a Vec2 Class
|
||||
if (any_type(value) == 5)
|
||||
{
|
||||
return any_cast<Vec2>(value).SubComponent(subComponentName);
|
||||
}
|
||||
return nullType;
|
||||
}
|
||||
|
||||
// Initial script processing, which loads variables and functions from builtin
|
||||
int GetBuiltins(const string& s)
|
||||
{
|
||||
@ -71,6 +116,8 @@ int GetBuiltins(const string& s)
|
||||
|
||||
string functName = split(words[lineNum][1], '(')[0];
|
||||
|
||||
InterpreterLog("Load builtin function " + functName);
|
||||
|
||||
string args = "";
|
||||
for (int w = 1; w < (int)words[lineNum].size(); w++) // Get all words from the instantiation line: these are the args
|
||||
{
|
||||
@ -78,7 +125,6 @@ int GetBuiltins(const string& s)
|
||||
}
|
||||
|
||||
args = replace(args, functName + " ", "");
|
||||
CompilerLog(args);
|
||||
functionContents.push_back(split(args, ','));
|
||||
|
||||
int numOfBrackets = 1;
|
||||
@ -95,13 +141,25 @@ int GetBuiltins(const string& s)
|
||||
else
|
||||
{
|
||||
if (words[lineNum][0] == "string")
|
||||
{
|
||||
builtinVarVals[words[lineNum][1]] = StringRaw(words[lineNum][3]);
|
||||
InterpreterLog("Load builtin variable " + words[lineNum][1]);
|
||||
}
|
||||
else if (words[lineNum][0] == "int")
|
||||
{
|
||||
builtinVarVals[words[lineNum][1]] = stoi(words[lineNum][3]);
|
||||
InterpreterLog("Load builtin variable " + words[lineNum][1]);
|
||||
}
|
||||
else if (words[lineNum][0] == "float")
|
||||
{
|
||||
builtinVarVals[words[lineNum][1]] = stof(words[lineNum][3]);
|
||||
InterpreterLog("Load builtin variable " + words[lineNum][1]);
|
||||
}
|
||||
else if (words[lineNum][0] == "bool")
|
||||
{
|
||||
builtinVarVals[words[lineNum][1]] = stob(words[lineNum][3]);
|
||||
InterpreterLog("Load builtin variable " + words[lineNum][1]);
|
||||
}
|
||||
//else
|
||||
// LogWarning("unrecognized type \'" + words[lineNum][0] + "\' on line: " + to_string(lineNum));
|
||||
}
|
||||
@ -123,13 +181,13 @@ boost::any CPPFunction(const string& name, const vector<boost::any>& args)
|
||||
return AnyAsInt(args[0]);
|
||||
else if (name == "CPP.Graphics.Init")
|
||||
{
|
||||
cout << "\x1B[32mInit graphics\033[0m\t\t" << endl;
|
||||
initGraphics(AnyAsString(args[0]), AnyAsInt(args[1]), AnyAsInt(args[2]));
|
||||
InterpreterLog("Init graphics");
|
||||
initGraphics(StringRaw(AnyAsString(args[0])), AnyAsInt(args[1]), AnyAsInt(args[2]));
|
||||
}
|
||||
else if (name == "CPP.Graphics.Sprite")
|
||||
{
|
||||
Sprite s(AnyAsString(args[0]), any_cast<Vec2>(args[1]), any_cast<Vec2>(args[2]), AnyAsFloat(args[3]));
|
||||
Sprite d = any_cast<Sprite>(a);
|
||||
Sprite s(StringRaw(AnyAsString(args[0])), any_cast<Vec2>(args[1]), any_cast<Vec2>(args[2]), AnyAsFloat(args[3]));
|
||||
return s;
|
||||
}
|
||||
else if (name == "CPP.Graphics.Draw")
|
||||
{
|
||||
|
||||
@ -86,7 +86,7 @@ func NSprite(path, x, y, r)
|
||||
}
|
||||
|
||||
// Creates new Vector2 class
|
||||
func Vec2(x, y)
|
||||
func NVec2(x, y)
|
||||
{
|
||||
Vec2 v = CPP.System.Vec2(x, y)
|
||||
return v
|
||||
|
||||
323
Slang/graphics.h
323
Slang/graphics.h
@ -26,70 +26,70 @@ using namespace std;
|
||||
int WINDOW_WIDTH = 1280;
|
||||
int WINDOW_HEIGHT = 720;
|
||||
|
||||
unordered_map<string, int> KEYS =
|
||||
{
|
||||
{"ESCAPE", 0},
|
||||
{"1", 0},
|
||||
{"2", 0},
|
||||
{"3", 0},
|
||||
{"4", 0},
|
||||
{"5", 0},
|
||||
{"6", 0},
|
||||
{"7", 0},
|
||||
{"8", 0},
|
||||
{"9", 0},
|
||||
{"TILDE", 0},
|
||||
{"MINUS", 0},
|
||||
{"EQUALS", 0},
|
||||
{"BACKSPACE", 0},
|
||||
{"TAB", 0},
|
||||
{"Q", 0},
|
||||
{"W", 0},
|
||||
{"E", 0},
|
||||
{"R", 0},
|
||||
{"T", 0},
|
||||
{"Y", 0},
|
||||
{"U", 0},
|
||||
{"I", 0},
|
||||
{"O", 0},
|
||||
{"P", 0},
|
||||
{"BRACKET_L", 0},
|
||||
{"BRACKET_R", 0},
|
||||
{"BACKSLASH", 0},
|
||||
{"A", 0},
|
||||
{"S", 0},
|
||||
{"D", 0},
|
||||
{"F", 0},
|
||||
{"G", 0},
|
||||
{"H", 0},
|
||||
{"J", 0},
|
||||
{"K", 0},
|
||||
{"L", 0},
|
||||
{"COLON", 0},
|
||||
{"QUOTE", 0},
|
||||
{"ENTER", 0},
|
||||
{"SHIFT_L", 0},
|
||||
{"Z", 0},
|
||||
{"X", 0},
|
||||
{"C", 0},
|
||||
{"V", 0},
|
||||
{"B", 0},
|
||||
{"N", 0},
|
||||
{"M", 0},
|
||||
{"COMMA", 0},
|
||||
{"PERIOD", 0},
|
||||
{"SLASH", 0},
|
||||
{"SHIFT_R", 0},
|
||||
{"CTRL_L", 0},
|
||||
{"ALT_L", 0},
|
||||
{"SPACE", 0},
|
||||
{"ALT_R", 0},
|
||||
{"CTRL_R", 0},
|
||||
{"LEFT", 0},
|
||||
{"UP", 0},
|
||||
{"RIGHT", 0},
|
||||
{"DOWN", 0}
|
||||
};
|
||||
unordered_map<string, int> KEYS =
|
||||
{
|
||||
{"ESCAPE", 0},
|
||||
{"1", 0},
|
||||
{"2", 0},
|
||||
{"3", 0},
|
||||
{"4", 0},
|
||||
{"5", 0},
|
||||
{"6", 0},
|
||||
{"7", 0},
|
||||
{"8", 0},
|
||||
{"9", 0},
|
||||
{"TILDE", 0},
|
||||
{"MINUS", 0},
|
||||
{"EQUALS", 0},
|
||||
{"BACKSPACE", 0},
|
||||
{"TAB", 0},
|
||||
{"Q", 0},
|
||||
{"W", 0},
|
||||
{"E", 0},
|
||||
{"R", 0},
|
||||
{"T", 0},
|
||||
{"Y", 0},
|
||||
{"U", 0},
|
||||
{"I", 0},
|
||||
{"O", 0},
|
||||
{"P", 0},
|
||||
{"BRACKET_L", 0},
|
||||
{"BRACKET_R", 0},
|
||||
{"BACKSLASH", 0},
|
||||
{"A", 0},
|
||||
{"S", 0},
|
||||
{"D", 0},
|
||||
{"F", 0},
|
||||
{"G", 0},
|
||||
{"H", 0},
|
||||
{"J", 0},
|
||||
{"K", 0},
|
||||
{"L", 0},
|
||||
{"COLON", 0},
|
||||
{"QUOTE", 0},
|
||||
{"ENTER", 0},
|
||||
{"SHIFT_L", 0},
|
||||
{"Z", 0},
|
||||
{"X", 0},
|
||||
{"C", 0},
|
||||
{"V", 0},
|
||||
{"B", 0},
|
||||
{"N", 0},
|
||||
{"M", 0},
|
||||
{"COMMA", 0},
|
||||
{"PERIOD", 0},
|
||||
{"SLASH", 0},
|
||||
{"SHIFT_R", 0},
|
||||
{"CTRL_L", 0},
|
||||
{"ALT_L", 0},
|
||||
{"SPACE", 0},
|
||||
{"ALT_R", 0},
|
||||
{"CTRL_R", 0},
|
||||
{"LEFT", 0},
|
||||
{"UP", 0},
|
||||
{"RIGHT", 0},
|
||||
{"DOWN", 0}
|
||||
};
|
||||
|
||||
enum Buttons
|
||||
{
|
||||
@ -165,7 +165,7 @@ public:
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
Vec2 operator-(Vec2 const& rhs)
|
||||
{
|
||||
return Vec2(x - rhs.x, y - rhs.y);
|
||||
@ -184,134 +184,157 @@ public:
|
||||
return Vec2(x * rhs, y * rhs);
|
||||
}
|
||||
|
||||
boost::any SubComponent(std::string componentName)
|
||||
{
|
||||
if (componentName == "x")
|
||||
return x;
|
||||
if (componentName == "y")
|
||||
return y;
|
||||
}
|
||||
|
||||
float x, y;
|
||||
};
|
||||
|
||||
struct _RotRect {
|
||||
_Vector2D C;
|
||||
_Vector2D S;
|
||||
float ang;
|
||||
Vec2 C;
|
||||
Vec2 S;
|
||||
float ang;
|
||||
};
|
||||
|
||||
inline void RotateVec2Clockwise(Vec2 * v, float ang)
|
||||
inline void RotateVec2Clockwise(Vec2* v, float ang)
|
||||
{
|
||||
float t,
|
||||
cosa = cos(ang),
|
||||
sina = sin(ang);
|
||||
t = v->x; v->x = t*cosa + v->y*sina; v->y = -t*sina + v->y*cosa;
|
||||
float t,
|
||||
cosa = cos(ang),
|
||||
sina = sin(ang);
|
||||
t = v->x; v->x = t * cosa + v->y * sina; v->y = -t * sina + v->y * cosa;
|
||||
}
|
||||
|
||||
// Rotated Rectangles Collision Detection, Oren Becker, 2001
|
||||
int RotRectsCollision(_RotRect * rr1, _RotRect * rr2)
|
||||
int RotRectsCollision(_RotRect* rr1, _RotRect* rr2)
|
||||
{
|
||||
Vec2 A, B, // vertices of the rotated rr2
|
||||
C, // center of rr2
|
||||
BL, TR; // vertices of rr2 (bottom-left, top-right)
|
||||
Vec2 A, B, // vertices of the rotated rr2
|
||||
C, // center of rr2
|
||||
BL, TR; // vertices of rr2 (bottom-left, top-right)
|
||||
|
||||
float ang = rr1->ang - rr2->ang, // orientation of rotated rr1
|
||||
cosa = cos(ang), // precalculated trigonometic -
|
||||
sina = sin(ang); // - values for repeated use
|
||||
float ang = rr1->ang - rr2->ang, // orientation of rotated rr1
|
||||
cosa = cos(ang), // precalculated trigonometic -
|
||||
sina = sin(ang); // - values for repeated use
|
||||
|
||||
float t, x, a; // temporary variables for various uses
|
||||
float dx; // deltaX for linear equations
|
||||
float ext1, ext2; // min/max vertical values
|
||||
float t, x, a; // temporary variables for various uses
|
||||
float dx; // deltaX for linear equations
|
||||
float ext1, ext2; // min/max vertical values
|
||||
|
||||
// move rr2 to make rr1 cannonic
|
||||
C = rr2->C;
|
||||
C-=rr1->C;
|
||||
// move rr2 to make rr1 cannonic
|
||||
C = rr2->C;
|
||||
C -= rr1->C;
|
||||
|
||||
// rotate rr2 clockwise by rr2->ang to make rr2 axis-aligned
|
||||
RotateVector2DClockwise(&C, rr2->ang);
|
||||
// rotate rr2 clockwise by rr2->ang to make rr2 axis-aligned
|
||||
RotateVec2Clockwise(&C, rr2->ang);
|
||||
|
||||
// calculate vertices of (moved and axis-aligned := 'ma') rr2
|
||||
BL = TR = C;
|
||||
BL-=rr2->S;
|
||||
TR+=rr2->S;
|
||||
// calculate vertices of (moved and axis-aligned := 'ma') rr2
|
||||
BL = TR = C;
|
||||
BL -= rr2->S;
|
||||
TR += rr2->S;
|
||||
|
||||
// calculate vertices of (rotated := 'r') rr1
|
||||
A.x = -rr1->S.y*sina; B.x = A.x; t = rr1->S.x*cosa; A.x += t; B.x -= t;
|
||||
A.y = rr1->S.y*cosa; B.y = A.y; t = rr1->S.x*sina; A.y += t; B.y -= t;
|
||||
// calculate vertices of (rotated := 'r') rr1
|
||||
A.x = -rr1->S.y * sina; B.x = A.x; t = rr1->S.x * cosa; A.x += t; B.x -= t;
|
||||
A.y = rr1->S.y * cosa; B.y = A.y; t = rr1->S.x * sina; A.y += t; B.y -= t;
|
||||
|
||||
t = sina*cosa;
|
||||
t = sina * cosa;
|
||||
|
||||
// verify that A is vertical min/max, B is horizontal min/max
|
||||
if (t < 0)
|
||||
{
|
||||
t = A.x; A.x = B.x; B.x = t;
|
||||
t = A.y; A.y = B.y; B.y = t;
|
||||
}
|
||||
// verify that A is vertical min/max, B is horizontal min/max
|
||||
if (t < 0)
|
||||
{
|
||||
t = A.x; A.x = B.x; B.x = t;
|
||||
t = A.y; A.y = B.y; B.y = t;
|
||||
}
|
||||
|
||||
// verify that B is horizontal minimum (leftest-vertex)
|
||||
if (sina < 0) { B.x = -B.x; B.y = -B.y; }
|
||||
// verify that B is horizontal minimum (leftest-vertex)
|
||||
if (sina < 0) { B.x = -B.x; B.y = -B.y; }
|
||||
|
||||
// if rr2(ma) isn't in the horizontal range of
|
||||
// colliding with rr1(r), collision is impossible
|
||||
if (B.x > TR.x || B.x > -BL.x) return 0;
|
||||
// if rr2(ma) isn't in the horizontal range of
|
||||
// colliding with rr1(r), collision is impossible
|
||||
if (B.x > TR.x || B.x > -BL.x) return 0;
|
||||
|
||||
// if rr1(r) is axis-aligned, vertical min/max are easy to get
|
||||
if (t == 0) {ext1 = A.y; ext2 = -ext1; }
|
||||
// else, find vertical min/max in the range [BL.x, TR.x]
|
||||
else
|
||||
{
|
||||
x = BL.x-A.x; a = TR.x-A.x;
|
||||
ext1 = A.y;
|
||||
// if the first vertical min/max isn't in (BL.x, TR.x), then
|
||||
// find the vertical min/max on BL.x or on TR.x
|
||||
if (a*x > 0)
|
||||
{
|
||||
dx = A.x;
|
||||
if (x < 0) { dx -= B.x; ext1 -= B.y; x = a; }
|
||||
else { dx += B.x; ext1 += B.y; }
|
||||
ext1 *= x; ext1 /= dx; ext1 += A.y;
|
||||
}
|
||||
|
||||
x = BL.x+A.x; a = TR.x+A.x;
|
||||
ext2 = -A.y;
|
||||
// if the second vertical min/max isn't in (BL.x, TR.x), then
|
||||
// find the local vertical min/max on BL.x or on TR.x
|
||||
if (a*x > 0)
|
||||
{
|
||||
dx = -A.x;
|
||||
if (x < 0) { dx -= B.x; ext2 -= B.y; x = a; }
|
||||
else { dx += B.x; ext2 += B.y; }
|
||||
ext2 *= x; ext2 /= dx; ext2 -= A.y;
|
||||
}
|
||||
}
|
||||
// if rr1(r) is axis-aligned, vertical min/max are easy to get
|
||||
if (t == 0) { ext1 = A.y; ext2 = -ext1; }
|
||||
// else, find vertical min/max in the range [BL.x, TR.x]
|
||||
else
|
||||
{
|
||||
x = BL.x - A.x; a = TR.x - A.x;
|
||||
ext1 = A.y;
|
||||
// if the first vertical min/max isn't in (BL.x, TR.x), then
|
||||
// find the vertical min/max on BL.x or on TR.x
|
||||
if (a * x > 0)
|
||||
{
|
||||
dx = A.x;
|
||||
if (x < 0) { dx -= B.x; ext1 -= B.y; x = a; }
|
||||
else { dx += B.x; ext1 += B.y; }
|
||||
ext1 *= x; ext1 /= dx; ext1 += A.y;
|
||||
}
|
||||
|
||||
// check whether rr2(ma) is in the vertical range of colliding with rr1(r)
|
||||
// (for the horizontal range of rr2)
|
||||
return !((ext1 < BL.y && ext2 < BL.y) ||
|
||||
(ext1 > TR.y && ext2 > TR.y));
|
||||
x = BL.x + A.x; a = TR.x + A.x;
|
||||
ext2 = -A.y;
|
||||
// if the second vertical min/max isn't in (BL.x, TR.x), then
|
||||
// find the local vertical min/max on BL.x or on TR.x
|
||||
if (a * x > 0)
|
||||
{
|
||||
dx = -A.x;
|
||||
if (x < 0) { dx -= B.x; ext2 -= B.y; x = a; }
|
||||
else { dx += B.x; ext2 += B.y; }
|
||||
ext2 *= x; ext2 /= dx; ext2 -= A.y;
|
||||
}
|
||||
}
|
||||
|
||||
// check whether rr2(ma) is in the vertical range of colliding with rr1(r)
|
||||
// (for the horizontal range of rr2)
|
||||
return !((ext1 < BL.y&& ext2 < BL.y) ||
|
||||
(ext1 > TR.y && ext2 > TR.y));
|
||||
}
|
||||
|
||||
class Sprite
|
||||
{
|
||||
public:
|
||||
Sprite(std::string path, Vec2 position, Vec2 scale, double angle)
|
||||
: position(position), angle(angle)
|
||||
: position(position), angle(angle), path(path)
|
||||
{
|
||||
rect.x = static_cast<int>(position.x);
|
||||
rect.y = static_cast<int>(position.y);
|
||||
rect.w = scale.x;
|
||||
rect.h = scale.y;
|
||||
}
|
||||
|
||||
void Load()
|
||||
{
|
||||
SDL_Surface* surface = loadSurface(path);
|
||||
texture = SDL_CreateTextureFromSurface(gRenderer, surface);
|
||||
SDL_FreeSurface(surface);
|
||||
|
||||
isLoaded = true;
|
||||
}
|
||||
|
||||
void Draw()
|
||||
{
|
||||
if (texture == NULL)
|
||||
Load();
|
||||
|
||||
rect.y = static_cast<int>(position.y);
|
||||
|
||||
SDL_RenderCopy(gRenderer, texture, NULL, &rect);
|
||||
}
|
||||
|
||||
boost::any SubComponent(std::string componentName)
|
||||
{
|
||||
if (componentName == "position")
|
||||
return position;
|
||||
}
|
||||
|
||||
Vec2 position;
|
||||
double angle;
|
||||
std::string path;
|
||||
SDL_Rect rect{};
|
||||
SDL_Texture* texture;
|
||||
SDL_Texture* texture = NULL;
|
||||
bool isLoaded = false;
|
||||
};/*
|
||||
|
||||
class PlayerScore
|
||||
@ -653,12 +676,12 @@ int updateLoop()
|
||||
//paddleOne.Update(dt);
|
||||
//paddleTwo.Update(dt);
|
||||
|
||||
// Clear the window to black
|
||||
SDL_SetRenderDrawColor(gRenderer, 0x0, 0x0, 0x0, 0xFF);
|
||||
//// Clear the window to black
|
||||
//SDL_SetRenderDrawColor(gRenderer, 0x0, 0x0, 0x0, 0xFF);
|
||||
SDL_RenderClear(gRenderer);
|
||||
|
||||
// Set the draw color to be white
|
||||
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
|
||||
//// Set the draw color to be white
|
||||
//SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
|
||||
|
||||
//// Draw the net
|
||||
//for (int y = 0; y < WINDOW_HEIGHT; ++y)
|
||||
@ -681,7 +704,7 @@ int updateLoop()
|
||||
//playerTwoScoreText.Draw();
|
||||
|
||||
//randomAssSprite.Draw();
|
||||
|
||||
|
||||
ExecuteFunction("Update", vector<boost::any> {});
|
||||
|
||||
// Present the backbuffer
|
||||
@ -691,8 +714,8 @@ int updateLoop()
|
||||
auto stopTime = std::chrono::high_resolution_clock::now();
|
||||
dt = std::chrono::duration<float, std::chrono::milliseconds::period>(stopTime - startTime).count();
|
||||
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int initGraphics(std::string windowTitle, int width, int height)
|
||||
@ -704,12 +727,12 @@ int initGraphics(std::string windowTitle, int width, int height)
|
||||
SDL_Init(SDL_INIT_VIDEO);
|
||||
TTF_Init();
|
||||
|
||||
gWindow = SDL_CreateWindow(windowTitle.c_str(), 40, 40, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN || SDL_WINDOW_RESIZABLE);
|
||||
gWindow = SDL_CreateWindow(windowTitle.c_str(), 40, 40, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
|
||||
gRenderer = SDL_CreateRenderer(gWindow, -1, 0);
|
||||
|
||||
//Get window surface
|
||||
gScreenSurface = SDL_GetWindowSurface(gWindow);
|
||||
|
||||
|
||||
//Sprite randomAssSprite(
|
||||
// Vec2((WINDOW_WIDTH / 2.0f) - (100 / 2.0f), (WINDOW_HEIGHT / 2.0f) - (100 / 2.0f)),
|
||||
// Vec2(100, 100),
|
||||
|
||||
@ -4,7 +4,7 @@ func Main(input, in)
|
||||
{
|
||||
print "PI is: " + PI
|
||||
int x = 1
|
||||
print x
|
||||
print "xis" + x
|
||||
float k = 0
|
||||
print k
|
||||
while x < 5
|
||||
@ -17,9 +17,10 @@ func Main(input, in)
|
||||
|
||||
x += 1
|
||||
}
|
||||
Vec2 aa = Vec2(0, 0)
|
||||
Sprite thisisasprite = CPP.Graphics.Sprite("circle.png", aa, aa, 0)
|
||||
//CPP.Graphics.Init("This is a pong game", 64, 64)
|
||||
Vec2 aa = NVec2(6, 8)
|
||||
print aa.x + " " + aa.y
|
||||
global Sprite thisisasprite = CPP.Graphics.Sprite("./circle.png", aa, aa, 0)
|
||||
CPP.Graphics.Init("This is a pong game", 500, 500)
|
||||
}
|
||||
|
||||
func Update(input)
|
||||
@ -36,5 +37,6 @@ func Update(input)
|
||||
// y = 0
|
||||
// x += 1
|
||||
//}
|
||||
CPP.Graphics.Draw(thisisasprite)
|
||||
print "updating"
|
||||
}
|
||||
|
||||
@ -41,7 +41,7 @@ vector<string> rangeInVec(const vector<string>& str, const int& min, const int&
|
||||
|
||||
vector<string> slice(vector<string> const& v, int min, int max);
|
||||
|
||||
string rangeInStr(const string& str, const int& min, const int& max);
|
||||
string rangeInStr(const string& str, const int& min, int max);
|
||||
|
||||
string unWrapVec(const vector<string>& vec);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user