Skip to content
Open

Dev #11

Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions include/Silver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class SpriteRenderer : public Component {
setShape(newShape);
cleanShape = newShape;

useRelativePivot = false;
useRelativePivot = true;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Default pivot behavior changed from absolute to relative

The default value for useRelativePivot was changed from false to true, which means sprites will now use relative pivot positioning by default (pivot as a percentage of sprite size) instead of absolute positioning.

This change offers better flexibility when handling sprites of different sizes, but could affect existing code expecting the previous default behavior.

#!/bin/bash
# Search for any instances where SpriteRenderer is created without explicitly setting useRelativePivot
# to check for potential impact of this change

echo "Searching for SpriteRenderer instantiations that might be affected by the default pivot change:"
rg -A 3 "new SpriteRenderer\(" --type cpp
rg -A 3 "AddComponent<SpriteRenderer>\(" --type cpp
rg -A 3 "= SpriteRenderer\(" --type cpp

pivotFactor = Vector2(0.5f, 0.5f); // Default pivot factor
}

Expand Down Expand Up @@ -141,7 +141,7 @@ class SpriteRenderer : public Component {
std::string getShape();
void setShape(std::string target);
void alignShapeTo(double align);
bool useRelativePivot = false;
bool useRelativePivot = true;
Vector2 pivot = Vector2(0, 0);
Vector2 pivotFactor = Vector2(0.5, 0.5);

Expand Down Expand Up @@ -199,7 +199,7 @@ class SpriteRenderer : public Component {

}
private:
Vector2 RotatePoint(int column, int line); //Helper function to rotate around the pivot
Vector2 RotatePoint(double column, double line); //Helper function to rotate around the pivot
std::string shape = "";
std::string cleanShape = "";
int spriteHeight = 0;
Expand Down
10 changes: 5 additions & 5 deletions src/SilverCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,8 @@ void Camera::RenderFrame() {
if (consoleWidth > maxLeftWidth + maxRightWidth + cameraScale.x) cameraScale -= maxLeftWidth + maxRightWidth;
if (consoleHeight > topTextLinesCount + bottomTextLinesCount + cameraScale.y) cameraScale -= topTextLinesCount + bottomTextLinesCount;


if (cameraScale.x == 0 || cameraScale.y == 0 ||
cameraScale.z == 0)
if(!cameraScale.z) cameraScale.z = 1;
if (cameraScale.x == 0 || cameraScale.y == 0)
return;

// Detect console scale changes
Expand Down Expand Up @@ -271,8 +270,9 @@ void Camera::RenderFrame() {
cameraScale.z == 0)
continue;

if (scale.x == 0.0f || scale.y == 0.0f || scale.z == 0.0f) continue;

if (scale.x == 0.0f || scale.y == 0.0f) continue;
if(scale.z == 0.0f) scale.z = 1;

// Check if the object is part of UI or SpriteRenderer
if (obj->GetComponent<UI>() != nullptr) {
location.x = round(obj->GetComponent<Transform>()->position.x + position.x - abs(cameraScale.x) / 2);
Expand Down
62 changes: 52 additions & 10 deletions src/SilverSpriteRendering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ Vector2 SpriteRenderer::GetSize() {



Vector2 SpriteRenderer::RotatePoint(int column, int line) {
Vector2 SpriteRenderer::RotatePoint(double column, double line) {
Vector2 pivot = this->GetPivot();
int height = 0, width = 0;

Expand Down Expand Up @@ -355,17 +355,59 @@ std::string SpriteRenderer::getShape() {
void SpriteRenderer::setShape(std::string target) {
shape = target;
cleanShape = StripAnsi(ProcessMarkdown(shape));
spriteHeight = 0;
spriteWidth = 0;
std::string line;

ss.str(cleanShape);
while (std::getline(ss, line, '\n')) {
spriteHeight++;
spriteWidth = std::max(spriteWidth, static_cast<int>(line.size()));
}
ansiExtracted = ExtractAnsi(ProcessMarkdown(shape));

Vector2 size = GetSize();
spriteHeight = size.y;
spriteWidth = size.x;
}

void SpriteRenderer::alignShapeTo(double align) {
align = std::clamp(align, 0.0, 1.0);

int spriteWidth = 0;
{
std::stringstream temp(cleanShape);
std::string line;
while (std::getline(temp, line, '\n')) {
spriteWidth = std::max(spriteWidth, static_cast<int>(line.size()));
}
}

std::stringstream alignedClean;
std::vector<std::vector<std::string>> alignedAnsi;

{
std::stringstream shapeStream(cleanShape);
std::string line;
size_t lineIndex = 0;

ansiExtracted = ExtractAnsi(shape);
while (std::getline(shapeStream, line, '\n')) {
int padding = static_cast<int>((spriteWidth - line.size()) * align);
alignedClean << std::string(padding, ' ') << line << '\n';

if (lineIndex < ansiExtracted.size()) {
const std::vector<std::string>& ansiLine = ansiExtracted[lineIndex];
std::vector<std::string> paddedAnsi;

// Add empty strings as padding on the left
paddedAnsi.resize(padding, "");

// Copy original line
paddedAnsi.insert(paddedAnsi.end(), ansiLine.begin(), ansiLine.end());

alignedAnsi.push_back(std::move(paddedAnsi));
}

++lineIndex;
}
}

cleanShape = alignedClean.str();
ss = std::stringstream(cleanShape);
ansiExtracted = std::move(alignedAnsi);
}



26 changes: 14 additions & 12 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#include "Silver.hpp"
#include <chrono>
#include <iostream>
#include <thread>

int main() {
// Use std::shared_ptr for c1 to ensure shared_from_this works
SPActor c1 = std::make_shared<Actor>();
c1->AddComponent<Camera>();

SPActor test = std::make_shared<Actor>("test", "1");
Rectangle(test, Rect(0,0,5,5), 0);
c1->GetComponent<Camera>()->RenderFrame();
Hold();
return 0;
Actor c1;
c1.AddComponent<Camera>();

Actor actor("alert", "This is your first window\n<red>SILVER!</red>");

actor.GetComponent<Transform>()->position = Vector3Zero;
//actor.GetComponent<SpriteRenderer>()->alignShapeTo(1.0f);

actor.AddObject();

c1.GetComponent<Camera>()->StartVideo();
Hold();

return 0;
}