Antialiased text as an extra Text option, update platformer

This commit is contained in:
sam-astro 2022-05-25 17:34:06 -04:00
parent 70a2c8ce7e
commit a1dcb55468
5 changed files with 100 additions and 37 deletions

View File

@ -279,10 +279,12 @@ boost::any EvalExpression(const string& ex, unordered_map<string, boost::any>& v
bool BooleanLogic(const string& valA, const string& determinant, const string& valB, unordered_map<string, boost::any>& variableValues)
{
if(valA)
boost::any valARealValue = EvalExpression(valA, variableValues);
if(valB)
boost::any valBRealValue = EvalExpression(valB, variableValues);
boost::any valARealValue;
boost::any valBRealValue;
if(valA != "")
valARealValue = EvalExpression(valA, variableValues);
if(valB != "")
valBRealValue = EvalExpression(valB, variableValues);
#if DEVELOPER_MESSAGES == true
InterpreterLog(AnyAsString(valARealValue) + " " + determinant + " " + AnyAsString(valBRealValue) + " : " + AnyAsString(valA) + " " + determinant + " " + AnyAsString(valB) + " : " + to_string(AnyAsString(valARealValue) == AnyAsString(valBRealValue)));
#endif
@ -527,9 +529,9 @@ boost::any ProcessLine(const vector<vector<string>>& words, int& lineNum, unorde
//Iterate through all lines in while loop
for (int lineNum = 0; lineNum < (int)whileContents.size(); lineNum++)
{
if(startsWith(whileContents.at(0), "continue"))
if(startsWith(whileContents.at(0).at(0), "continue"))
break; // Stops iterating through lines and return to beginning
if(startsWith(whileContents.at(0), "break"))
if(startsWith(whileContents.at(0).at(0), "break"))
return nullType; // Stops iterating through lines and leave while loop
boost::any returnVal = ProcessLine(whileContents, lineNum, variableValues);
if (!returnVal.empty())
@ -559,7 +561,7 @@ boost::any ProcessLine(const vector<vector<string>>& words, int& lineNum, unorde
}
// If the statement is already false, don't bother gathering the contents
if(BooleanLogic(whileParameters.at(0), whileParameters.at(1), whileParameters.at(2), variableValues) == false){
if(BooleanLogic(ifParameters.at(0), ifParameters.at(1), ifParameters.at(2), variableValues) == false){
lineNum++;
while (lineNum < (int)words.size())
{
@ -589,7 +591,7 @@ boost::any ProcessLine(const vector<vector<string>>& words, int& lineNum, unorde
//Iterate through all lines in if statement
for (int l = 0; l < (int)ifContents.size(); l++)
{
if(startsWith(whileContents.at(0), "break"))
if(startsWith(ifContents.at(0).at(0), "break"))
return nullType; // Stops iterating through lines and leave while loop
boost::any returnVal = ProcessLine(ifContents, l, variableValues);
if (!returnVal.empty())

View File

@ -403,8 +403,16 @@ boost::any ZSFunction(const string& name, const vector<boost::any>& args)
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;
if (args.size() <= 8)
{
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)), true);
return t;
}
else
{
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)), AnyAsBool(args.at(8)));
return t;
}
}
else if (name == "ZS.Graphics.DrawText")
any_cast<Text>(args.at(0)).Draw();

View File

@ -523,8 +523,8 @@ public:
class Text
{
public:
Text(std::string content, std::string pathToFont, Vec2 position, float fontSize, double angle, Uint8 r, Uint8 g, Uint8 b)
: position(position), angle(angle), content(content), pathToFont(pathToFont), fontSize(fontSize), r(r), g(g), b(b)
Text(std::string content, std::string pathToFont, Vec2 position, float fontSize, double angle, Uint8 r, Uint8 g, Uint8 b, bool antialias)
: position(position), angle(angle), content(content), pathToFont(pathToFont), fontSize(fontSize), r(r), g(g), b(b), antialias(antialias)
{
rect.x = position.x;
rect.y = position.y;
@ -538,8 +538,12 @@ public:
{
SDL_Color color = { r, g, b };
SDL_Surface* surface = TTF_RenderText_Blended(font, content.c_str(), color);
SDL_Surface* surface;
if (antialias)
surface = TTF_RenderText_Blended(font, content.c_str(), color);
else
surface = TTF_RenderText_Solid(font, content.c_str(), color);
texture = SDL_CreateTextureFromSurface(gRenderer, surface);
TTF_SizeText(font, content.c_str(), &rect.w, &rect.h);
@ -559,7 +563,11 @@ public:
{
SDL_Color color = { r, g, b };
SDL_Surface* surface = TTF_RenderText_Blended(font, content.c_str(), color);
SDL_Surface* surface;
if (antialias)
surface = TTF_RenderText_Blended(font, content.c_str(), color);
else
surface = TTF_RenderText_Solid(font, content.c_str(), color);
SDL_DestroyTexture(texture);
texture = SDL_CreateTextureFromSurface(gRenderer, surface);
@ -604,6 +612,8 @@ public:
return content;
if (componentName == "pathToFont")
return pathToFont;
if (componentName == "antialias")
return antialias;
return 0;
}
@ -711,12 +721,19 @@ public:
else if (oper == "+=")
content += AnyAsString(otherVal);
}
else if (componentName == "antialias")
{
if (oper == "=")
antialias = AnyAsBool(otherVal);
}
// Updates changes to text
Update();
return *this;
}
bool antialias = true;
Vec2 position;
Vec2 scale;
float fontSize;

View File

@ -203,7 +203,7 @@ string betweenChars(const string& str, const char& openChar, const char& closeCh
for (int i = (int)str.size()-1; i >=0; i--)
{
if (str[i] == closeChar){
endPos = i-(startPos-1);
endPos = i-(startPos); // or startPos-1 idk I cant do math right now
break;
}
}

View File

@ -2,17 +2,20 @@ int g_screenw = 256
int g_screenh = 224
int g_resolutionScale = 3
float g_playerWalkSpeed = 400
float g_playerRunSpeed = 700
float g_jumpHeight = 20
float g_playerWalkSpeed = 150
float g_playerRunSpeed = 210
float g_jumpHeight = 200
float g_currPlayerSpeed = 400
bool g_running = false
bool g_colliding = false
float g_gravitySpeed = -86
float g_gravitySpeed = 86
include "./extra-include.zs"
bool g_jumping = false
float g_jumpingTime = 0
//include "./extra-include.zs"
func Main()
{
@ -29,20 +32,25 @@ func ThreadedFunction()
func Start()
{
float centerX = g_screenw / 2
float centerY = g_screenh / 2
global Vec2 g_screencenter = NVec2(g_screenw / 2, g_screenh / 2)
global Sprite g_playerSprite = ZS.Graphics.Sprite("./mariostill.png", g_screencenter, NVec2(16, 16), 0)
global Sprite g_groundSprite = ZS.Graphics.Sprite("./square.png", NVec2(g_screencenter.x, 192), NVec2(256, 16), 0)
global Text g_instructionsText = ZS.Graphics.Text("Use Arrow Keys or WASD to Move and Spacebar to Jump", "./arial.ttf", NVec2(centerOfScreen.x, centerOfScreen.y), 11, 0, 255, 255, 255)
global Text g_instructionsText = ZS.Graphics.Text("Use Arrow Keys or WASD to Move and Spacebar to Jump", "./arial.ttf", NVec2(g_screencenter.x, g_screencenter.y), 8, 0, 255, 255, 255)
g_instructionsText.antialias = false
global Vec2 g_playerTargetPosition = playerPos
global Vec2 g_playerTargetPosition = g_screencenter
}
func Update(deltaTime) {
func Update(deltaTime)
{
float fps = 1 / deltaTime
print "FPS: " + fps + "\r"
g_jumpingTime += deltaTime
//print "FPS: " + fps
//TestInclude()
//// Test automatic conversion from bool to int
@ -52,33 +60,61 @@ func Update(deltaTime) {
// Shift key lets you sprint
g_running = GetKey("SHIFT_L")
if g_running == true {
if g_running == true
{
g_currPlayerSpeed = g_playerRunSpeed
}
if g_running == false {
if g_running == false
{
g_currPlayerSpeed = g_playerWalkSpeed
}
// Move Left And Right
if GetKey("D") == true
{
g_playerTargetPosition.x += g_currPlayerSpeed * deltaTime
}
if GetKey("A") == true
{
float newX = g_playerTargetPosition.x - g_currPlayerSpeed * deltaTime
g_playerTargetPosition = NVec2(newX, g_playerSprite.position.y)
}
if GetKey("D") == true
{
float newX = g_playerTargetPosition.x + g_currPlayerSpeed * deltaTime
g_playerTargetPosition = NVec2(newX, g_playerSprite.position.y)
g_playerTargetPosition.x -= g_currPlayerSpeed * deltaTime
}
// Apply gravity
g_colliding = Colliding(g_playerSprite, g_groundSprite)
if g_colliding == false {
g_playerTargetPosition.y -= deltaTime * g_gravitySpeed
if g_colliding == false
{
if g_jumping == false
{
g_playerTargetPosition.y += deltaTime * g_gravitySpeed
}
if g_jumping == true
{
g_playerTargetPosition.y -= (g_jumpHeight*deltaTime) + (deltaTime*g_gravitySpeed*-g_jumpingTime*5)
print g_jumpingTime
}
}
if g_colliding == true
{
if GetKey("SPACE") == false
{
if g_jumpingTime > 1
{
g_jumping = false
}
}
if GetKey("SPACE") == true
{
g_jumping = true
print "jump" + g_jumpingTime
g_jumpingTime = 0
g_playerTargetPosition.y -= 2
}
}
// Lerps from old position to destination smoothly
float stopSpeed = deltaTime * 7
float stopSpeed = deltaTime * 15
float lerpedX = Lerp(g_playerSprite.position.x, g_playerTargetPosition.x, stopSpeed)
g_playerSprite.position = NVec2(lerpedX, g_playerTargetPosition.y)