SFML community forums
Help => Graphics => Topic started by: easy on August 22, 2011, 10:23:55 am
-
Hi!
As the title states, sf::Line and sf::Rectangle does not work for me. I'm using SFML2 snapshot "LaurentGomila-SFML-9cda5d0" which was downloaded on 2011-08-19.
Here's the minimal code:
#include <SFML/Graphics.hpp>
int main()
{
// Create the main window
sf::RenderWindow window(sf::VideoMode(800, 600, 32), "SFML Test");
sf::Color red(255, 0, 0, 255);
sf::Shape line;
line.Line(0, 0, window.GetWidth(), window.GetHeight(), 5, red);
sf::Shape rectangle;
rectangle.Rectangle(100, 100, window.GetWidth() - 200, window.GetHeight() - 200, red);
sf::Shape polygon;
float cx = window.GetWidth() * 0.5f;
polygon.AddPoint(cx - 1, 0, red);
polygon.AddPoint(cx + 1, 0, red);
polygon.AddPoint(cx + 1, window.GetHeight() * 1, red);
polygon.AddPoint(cx - 1, window.GetHeight() * 1, red);
while (window.IsOpened())
{
// Process events
sf::Event Event;
while (window.PollEvent(Event))
{
// Close window
if (Event.Type == sf::Event::KeyPressed && Event.Key.Code == sf::Keyboard::Escape) window.Close();
if (Event.Type == sf::Event::Closed) window.Close();
}
// Clear screen
window.Clear();
window.Draw(line);
window.Draw(rectangle);
window.Draw(polygon);
// Update the window
window.Display();
}
return 0;
}
And the result:
(http://kepfeltoltes.hu/thumb/110822/sfml2-line-rect_www.kepfeltoltes.hu_.jpg) (http://kepfeltoltes.hu/view/110822/sfml2-line-rect_www.kepfeltoltes.hu_.jpg)
As you can see, neither the diagonal line nor the rectangle in the center shows up, only the polygon-line in the middle.
Am I missing something obvious, or is it a bug?
Cheers,
easy
-
Hello:
I think it ought to work if you change the lines to:
line = sf::Shape::Line(0, 0, App.GetWidth(), App.GetHeight(), 5, red);
rectangle = sf::Shape::Rectangle(100, 100, App.GetWidth() - 200, App.GetHeight() - 200, red);
-
Thanks SCPM!
That really solved the problem.
The fixed code is:
#include <SFML/Graphics.hpp>
int main()
{
// Create the main window
sf::RenderWindow window(sf::VideoMode(800, 600, 32), "SFML Test");
sf::Shape line = sf::Shape::Line(0, 0, window.GetWidth(), window.GetHeight(), 5, sf::Color::Red);
sf::Shape rectangle = sf::Shape::Rectangle(200, 200, window.GetWidth() - 400, window.GetHeight() - 400, sf::Color::Green);
sf::Shape circle = sf::Shape::Circle(window.GetWidth() / 2, window.GetHeight() / 2, window.GetHeight() / 5, sf::Color::Blue);
sf::Shape polygon;
float cx = window.GetWidth() * 0.5f;
polygon.AddPoint(cx - 1, 0, sf::Color::White);
polygon.AddPoint(cx + 1, 0, sf::Color::White);
polygon.AddPoint(cx + 1, window.GetHeight() * 1, sf::Color::White);
polygon.AddPoint(cx - 1, window.GetHeight() * 1, sf::Color::White);
while (window.IsOpened())
{
// Process events
sf::Event Event;
while (window.PollEvent(Event))
{
// Close window
if (Event.Type == sf::Event::KeyPressed && Event.Key.Code == sf::Keyboard::Escape) window.Close();
if (Event.Type == sf::Event::Closed) window.Close();
}
// Clear screen
window.Clear();
// Draw objects
window.Draw(line);
window.Draw(rectangle);
window.Draw(circle);
window.Draw(polygon);
// Update the window
window.Display();
}
return 0;
}
Now that it is obvious, I've also found it in the wiki, even though I've already read it:
http://www.sfml-dev.org/tutorials/1.4/graphics-shape.php
sf::Shape Line = sf::Shape::Line(X1, Y1, X2, Y2, Thickness, Color, [Outline], [OutlineColor]);
sf::Shape Circle = sf::Shape::Circle(X, Y, Radius, Color, [Outline], [OutlineColor]);
sf::Shape Rect = sf::Shape::Rectangle(X1, Y1, X2, Y2, Color, [Outline], [OutlineColor]);
Next time I should read more carefully. :)
Thank you!