Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Filesystem Header and Add Centered "Hello SFML" Text #2

Merged
merged 15 commits into from
Jan 22, 2024
Merged
Binary file added resources/OpenSans-Regular.ttf
Binary file not shown.
24 changes: 22 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <SFML/Graphics.hpp>
#include <filesystem>
#include <iostream>
#include "Version.hpp"

static void modifyCurrentWorkingDirectory();
Expand All @@ -20,6 +21,21 @@ int main()
sprite.setPosition((float)window.getSize().x / 2, (float)window.getSize().y / 2);
}

sf::Font font;
if (!font.loadFromFile("resources/OpenSans-Regular.ttf"))
{

ufrshubham marked this conversation as resolved.
Show resolved Hide resolved
std::cerr << "Failed to load font. Application will continue without displaying text.\n";
}
else
{
sf::Text text("Hello SFML", font, 24);
ufrshubham marked this conversation as resolved.
Show resolved Hide resolved
text.setFillColor(sf::Color::White);
sf::FloatRect textRect = text.getLocalBounds();
text.setOrigin(textRect.left + textRect.width / 2.0f, textRect.top + textRect.height / 2.0f);
text.setPosition(sf::Vector2f(window.getSize().x / 2.0f, window.getSize().y / 2.0f));
}

while (window.isOpen())
{
sf::Event event;
Expand All @@ -33,6 +49,10 @@ int main()

window.clear();
window.draw(sprite);
if (font.loadFromFile("resources/OpenSans-Regular.ttf"))
ufrshubham marked this conversation as resolved.
Show resolved Hide resolved
{
window.draw(text);
}
ufrshubham marked this conversation as resolved.
Show resolved Hide resolved
window.display();
}

Expand All @@ -41,9 +61,9 @@ int main()

void modifyCurrentWorkingDirectory()
{
while (!std::filesystem::exists("resources"))
while (!std::filesystem::exists("resources"))
{
std::filesystem::current_path(std::filesystem::current_path().parent_path());
std::filesystem::current_path(std::experimental::filesystem::current_path().parent_path());
ufrshubham marked this conversation as resolved.
Show resolved Hide resolved
}
auto cwd = std::filesystem::current_path();
}
Loading