Skip to content

Commit

Permalink
update examples
Browse files Browse the repository at this point in the history
update all examples to match updated code style as well as occasional bug fixes and extra features added.
  • Loading branch information
Hapaxia committed Feb 21, 2025
1 parent 74af2c1 commit 610a14a
Show file tree
Hide file tree
Showing 10 changed files with 293 additions and 257 deletions.
30 changes: 13 additions & 17 deletions examples/bitmapTextExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
// Escape Quit
// Space Swap text string
//
// Please note that this example makes use of C++11 features
//
//////////////////////////////////////////////////////////////////////////////////////////////

#include <SFML/Graphics.hpp>
Expand All @@ -23,21 +21,21 @@

int main()
{
sf::Texture fontSheetTexture;
sf::Texture fontSheetTexture{};
if (!fontSheetTexture.loadFromFile("resources/Selba Ward Bitmap Font 0001.png"))
{
std::cerr << "Could not load font sheet texture." << std::endl;
return EXIT_FAILURE;
}

sw::BitmapFont font;
sw::BitmapText text;
sw::BitmapFont font{};
sw::BitmapText text{};

try
{
// standard setup for a font
font.setExternalTexture(fontSheetTexture);
font.setNumberOfTilesPerRow(16);
font.setNumberOfTilesPerRow(16u);
font.setDefaultTextureRect({ { 0, 0 }, { 8, 8 } });

// customisation for the specific font ("Selba Ward Bitmap Font 0001.png")
Expand All @@ -49,8 +47,8 @@ int main()

// starting values
//font.setBaselines(-1, 256); // this is the default (for all default texture rects)
font.setWidths(4, 256);
font.setStartXs(2, 256);
font.setWidths(4, 256u);
font.setStartXs(2, 256u);

// alpha numeric
font.setBaseline(-4, "gypq");
Expand Down Expand Up @@ -94,20 +92,20 @@ int main()
const std::string defaultString = "012 Str;:zingy! qu,ic(k)jumps 57";
const std::string xxxyyyString = "xxxyyyiijiizzJJIIvvvwwyyxxzzz";
text.setString(prefixString + defaultString);
text.setPosition({ 20, 50 });
text.setScale(2); // scale so we can see the pixels (only accepts unsigned integers - single unsigned int, two unsigned ints, sf::Vector2u)
text.setPosition({ 20.f, 50.f });
text.setScale(2u); // scale so we can see the pixels (only accepts "std::size_t" - single std::size_t, two std::size_t, sf::Vector2<std::size_t>)
//text.Transformable::setScale(3.5f, 9.75f); // it's still possible to scale by fractional amounts by explicitly calling the Transformable method
//text.setTracking(2); // base spacing between characters. default is 1

sf::RenderWindow window(sf::VideoMode({ 550, 100 }), "Bitmap Text example", sf::Style::Default);
window.setFramerateLimit(20);
sf::RenderWindow window(sf::VideoMode({ 550u, 100u }), "Bitmap Text example", sf::Style::Default);
window.setFramerateLimit(20u);
while (window.isOpen())
{
while (const std::optional event = window.pollEvent())
while (const std::optional event{ window.pollEvent() })
{
if (event->is<sf::Event::Closed>())
window.close();
else if (const auto* keyPressed = event->getIf<sf::Event::KeyPressed>())
else if (const auto keyPressed{ event->getIf<sf::Event::KeyPressed>() })
{
if (keyPressed->code == sf::Keyboard::Key::Space)
{
Expand All @@ -118,10 +116,8 @@ int main()
}
}
}
window.clear(sf::Color(0, 64, 32));
window.clear(sf::Color(0u, 64u, 32u));
window.draw(text);
window.display();
}

return EXIT_SUCCESS;
}
4 changes: 0 additions & 4 deletions examples/frameTransitionExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
// Down Assigns "down" direction to all TexCrop transitions
// Up Assigns "up" direction to all TexCrop transitions
//
//
// Please note that this example makes use of C++17 features
// and also requires the SFML library (http://www.sfml-dev.org)
//
//////////////////////////////////////////////////////////////////////////////////////////////

#include <SFML/Graphics.hpp>
Expand Down
42 changes: 19 additions & 23 deletions examples/lineExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
// Right mouse button Move end position of both lines (thick and thin) to current mouse position
// Scroll mouse wheel Change thickness of thick line
//
// Please note that this example makes use of C++11 features
//
//////////////////////////////////////////////////////////////////////////////////////////////

#include <SFML/Graphics.hpp>
Expand All @@ -24,14 +22,14 @@ int main()
float lineThickness{ 45.f };

sw::Line thickLine;
thickLine.setPoint(thickLine.getStartIndex(), { 100, 100 });
thickLine.setPoint(thickLine.getEndIndex(), { 250, 130 });
thickLine.setColor(sf::Color(64, 64, 128));
thickLine.setPoint(thickLine.getStartIndex(), { 100.f, 100.f });
thickLine.setPoint(thickLine.getEndIndex(), { 250.f, 130.f });
thickLine.setColor(sf::Color(64u, 64u, 128u));
thickLine.setThickness(lineThickness);
sw::Line line;
line.setPoint(line.getStartIndex(), { 100, 100 });
line.setPoint(line.getEndIndex(), { 250, 130 });
line.setColor(sf::Color(128, 196, 255));
line.setPoint(line.getStartIndex(), { 100.f, 100.f });
line.setPoint(line.getEndIndex(), { 250.f, 130.f });
line.setColor(sf::Color(128u, 196u, 255u));

struct
{
Expand All @@ -41,35 +39,35 @@ int main()
std::ios_base::iostate sfErrIoState = sf::err().rdstate(); // stores current state of SFML error stream
sf::err().clear(std::ios::failbit); // disables SFML error stream
sf::ContextSettings contextSettings;
contextSettings.antiAliasingLevel = 8;
sf::RenderWindow window(sf::VideoMode({ 800, 600 }), "Line test", sf::Style::Default, sf::State::Windowed, contextSettings);
window.setFramerateLimit(60);
contextSettings.antiAliasingLevel = 8u;
sf::RenderWindow window(sf::VideoMode({ 800u, 600u }), "Line test", sf::Style::Default, sf::State::Windowed, contextSettings);
window.setFramerateLimit(60u);
sf::err().clear(sfErrIoState); // re-enables SFML error stream (re-enstates the stored state)

while (window.isOpen())
{
while (const std::optional event = window.pollEvent())
while (const std::optional event{ window.pollEvent() })
{
if (event->is<sf::Event::Closed>() || event->is<sf::Event::KeyPressed>() && event->getIf<sf::Event::KeyPressed>()->code == sf::Keyboard::Key::Escape)
if (event->is<sf::Event::Closed>() || (event->is<sf::Event::KeyPressed>() && (event->getIf<sf::Event::KeyPressed>()->code == sf::Keyboard::Key::Escape)))
window.close();
else if (const auto* mouseButton = event->getIf<sf::Event::MouseButtonPressed>())
else if (const auto mouseButton{ event->getIf<sf::Event::MouseButtonPressed>() })
{
if (mouseButton->button == sf::Mouse::Button::Left)
isButtonDown.left = true;
else if (mouseButton->button == sf::Mouse::Button::Right)
isButtonDown.right = true;
}
else if (const auto* mouseButton = event->getIf<sf::Event::MouseButtonReleased>())
else if (const auto mouseButton{ event->getIf<sf::Event::MouseButtonReleased>() })
{
if (mouseButton->button == sf::Mouse::Button::Left)
isButtonDown.left = false;
else if (mouseButton->button == sf::Mouse::Button::Right)
isButtonDown.right = false;
}
else if (const auto* mouseWheel = event->getIf<sf::Event::MouseWheelScrolled>())
else if (const auto mouseWheel{ event->getIf<sf::Event::MouseWheelScrolled>() })
{
const float lineThicknessOffset{ 2.f };
if (mouseWheel->delta > 0)
constexpr float lineThicknessOffset{ 2.f };
if (mouseWheel->delta > 0.f)
lineThickness += lineThicknessOffset;
else
lineThickness -= lineThicknessOffset;
Expand All @@ -81,13 +79,13 @@ int main()

if (isButtonDown.left)
{
const sf::Vector2f mousePosition{ sf::Mouse::getPosition(window) };
const sf::Vector2f mousePosition{ window.mapPixelToCoords(sf::Mouse::getPosition(window)) };
line.setPoint(line.getStartIndex(), mousePosition); // "line.getStartIndex()" can be replaced with "sw::Line::PointIndex::Start", "sw::Line::Start", "line.PointIndex.Start", "line.Start" or "0"
thickLine.setPoint(thickLine.getStartIndex(), mousePosition);
}
if (isButtonDown.right)
{
const sf::Vector2f mousePosition{ sf::Mouse::getPosition(window) };
const sf::Vector2f mousePosition{ window.mapPixelToCoords(sf::Mouse::getPosition(window)) };
line.setPoint(line.getEndIndex(), mousePosition); // "line.getEndIndex()" can be replaced with "sw::Line::PointIndex::End", "sw::Line::End", "line.PointIndex.End", "line.End" or "1"
thickLine.setPoint(thickLine.getEndIndex(), mousePosition);
}
Expand All @@ -97,6 +95,4 @@ int main()
window.draw(line);
window.display();
}

return EXIT_SUCCESS;
}
}
38 changes: 17 additions & 21 deletions examples/progressBarExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
// - + (numpad) Adjust progress
// , . (< >) Adjust rotation
//
// Please note that this example makes use of C++11 features
//
//////////////////////////////////////////////////////////////////////////////////////////////

#include <SFML/Graphics.hpp>
Expand All @@ -21,15 +19,15 @@

int main()
{
sf::Texture texture, backgroundTexture;
sf::Texture texture{}, backgroundTexture{};
if (!texture.loadFromFile("resources/uv map.jpg") ||
!backgroundTexture.loadFromFile("resources/BlueYellowGradient.png"))
{
std::cerr << "Unable to load textures." << std::endl;
return EXIT_FAILURE;
}

sf::RenderWindow window(sf::VideoMode({ 800, 600 }), "Progress Bar example");
sf::RenderWindow window(sf::VideoMode({ 800u, 600u }), "Progress Bar example");

sw::ProgressBar progressBar({ 300.f, 40.f });

Expand All @@ -39,14 +37,15 @@ int main()

// customise visual representation
progressBar.setShowBackgroundAndFrame(true);
progressBar.setBackgroundColor(sf::Color(128, 128, 128));
progressBar.setFrameColor(sf::Color(128, 128, 255, 192));
progressBar.setBackgroundColor(sf::Color(128u, 128u, 128u));
progressBar.setFrameColor(sf::Color(128u, 128u, 255u, 192u));
progressBar.setFrameThickness(2.f);
progressBar.setRotation(sf::degrees(-30));
progressBar.setRotation(sf::degrees(-30.f));
progressBar.setScale({ 2.f, 2.f });

// set textures
progressBar.setTexture(texture);
progressBar.setTextureRect({ sf::Vector2i(0, texture.getSize().y / 10), sf::Vector2i(texture.getSize().x, texture.getSize().y / 10 + 1) });
progressBar.setTextureRect({ sf::Vector2i(0, texture.getSize().y / 10), sf::Vector2i(texture.getSize().x, (texture.getSize().y / 10) + 1) });
progressBar.setBackgroundTexture(backgroundTexture);
progressBar.setBackgroundTextureRect({ { 0, 0 }, { 1, static_cast<int>(backgroundTexture.getSize().y) } });

Expand All @@ -57,27 +56,27 @@ int main()
std::vector<sf::CircleShape> markers(3, sf::CircleShape(3.f));
for (auto& marker : markers)
marker.setOrigin({ marker.getRadius(), marker.getRadius() });
markers[0].setFillColor(sf::Color::Red);
markers[1].setFillColor(sf::Color::Yellow);
markers[2].setFillColor(sf::Color::Green);
markers[0u].setFillColor(sf::Color::Red);
markers[1u].setFillColor(sf::Color::Yellow);
markers[2u].setFillColor(sf::Color::Green);

sf::Clock clock;

while (window.isOpen())
{
while (const std::optional event = window.pollEvent())
while (const std::optional event{ window.pollEvent() })
{
if (event->is<sf::Event::Closed>() || event->is<sf::Event::KeyPressed>() && event->getIf<sf::Event::KeyPressed>()->code == sf::Keyboard::Key::Escape)
if (event->is<sf::Event::Closed>() || (event->is<sf::Event::KeyPressed>() && (event->getIf<sf::Event::KeyPressed>()->code == sf::Keyboard::Key::Escape)))
window.close();
}

markers[0].setPosition(progressBar.getAnchorProgressTop());
markers[1].setPosition(progressBar.getAnchorProgressCenter());
markers[2].setPosition(progressBar.getAnchorProgressBottom());
markers[0u].setPosition(progressBar.getAnchorProgressTop());
markers[1u].setPosition(progressBar.getAnchorProgressCenter());
markers[2u].setPosition(progressBar.getAnchorProgressBottom());

float frameTime{ clock.restart().asSeconds() };

if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Add)) // [+] (number pad) increase progress
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Add)) // [+] (number pad) increase progress
progressBar.setRatio(progressBar.getRatio() + frameTime * 0.3f);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Subtract)) // [-] (number pad) decrease progress
progressBar.setRatio(progressBar.getRatio() - frameTime * 0.3f);
Expand All @@ -92,7 +91,4 @@ int main()
window.draw(marker);
window.display();
}


return EXIT_SUCCESS;
}
}
Loading

0 comments on commit 610a14a

Please sign in to comment.