Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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;
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
115 changes: 95 additions & 20 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,30 +355,105 @@ 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;

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;
}
}

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

void SpriteRenderer::alignShapeTo(double align) {
if (align < 0.0) align = 0.0;
if (align > 1.0) align = 1.0;
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 alignedShape;
std::string line;
std::stringstream ss(cleanShape);

while (std::getline(ss, line, '\n')) {
int padding = static_cast<int>((spriteWidth - line.size()) * align);
alignedShape << std::string(padding, ' ') << line << '\n';
std::stringstream alignedClean;
std::vector<std::vector<std::string>> alignedAnsi;

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

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 = alignedShape.str();

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


31 changes: 19 additions & 12 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
#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", "Hello World!\na");

actor.GetComponent<Transform>()->position = Vector3Zero;

actor.GetComponent<Transform>()->scale = Vector3(1,1,1);
actor.GetComponent<SpriteRenderer>()->alignShapeTo(1);
actor.AddObject();

c1.GetComponent<Camera>()->RenderFrame();

Hold();

return 0;

}