// small SFML demo written by T.T.H. on 4. Feb. 2010 to demonstrate:
// 1. line position "wobbling" based on render window size
// 2. line thickness "invisibility" with diagonal lines
// (already mentioned here: http://www.sfml-dev.org/forum/viewtopic.php?t=1828 )
//
// forum post:
// http://www.sfml-dev.org/forum/viewtopic.php?t=2145
//
// I'm using:
// - branches/sfml2 revision 1369 being statically linked
// - Visual C++ .NET 2003 (compiler version 7.1.6030, "Professional" edition)
// - German Windows XP SP3 32 Bit
// - NVIDIA Quadro NVS 285 (some onboard thingie with some default driver)
#include <SFML/Graphics.hpp>
int myrand(int max)
{
return (int) ((float) max * (rand() / (RAND_MAX + 1.0)));
}
int main(int argc, char** argv)
{
// create render window
sf::RenderWindow MyRenderWindow(sf::VideoMode(300, 300), "My SFML Window");
// create image
sf::Image MyImage;
MyImage.Create(200, 200, sf::Color(0, 0, 255, 255));
// create sprite
sf::Sprite MySprite(MyImage);
MySprite.SetPosition(50.0f, 50.0f);
// create lines
float X(50.0f);
float Y(50.0f);
float Length(200.0f);
sf::Shape MyLineHorizontal(sf::Shape::Line(X, Y, X + Length, Y , 1.0f, sf::Color(255, 0, 0)));
sf::Shape MyLineVertical (sf::Shape::Line(X, Y, X , Y + Length, 1.0f, sf::Color(255, 0, 0)));
sf::Shape MyLineDiagonal (sf::Shape::Line(X, Y, X + Length, Y + Length, 1.0f, sf::Color(255, 0, 0)));
// the big loop
srand((unsigned int) time(NULL));
bool Running(true);
while (Running)
{
// randomly set render window size
MyRenderWindow.SetSize(300 + myrand(50), 300 + myrand(50));
// update view of window to avoid wicked streching and enforce pixel precision
MyRenderWindow.SetView(sf::View(sf::FloatRect(0, 0, (float) MyRenderWindow.GetWidth(), (float) MyRenderWindow.GetHeight())));
// clear window
MyRenderWindow.Clear(sf::Color(0, 255, 0));
// draw sprite
MyRenderWindow.Draw(MySprite);
// draw lines
MyRenderWindow.Draw(MyLineHorizontal);
MyRenderWindow.Draw(MyLineVertical);
MyRenderWindow.Draw(MyLineDiagonal);
// display window
MyRenderWindow.Display();
// process events
sf::Event MyEvent;
while (MyRenderWindow.GetEvent(MyEvent))
{
// window closed
if (MyEvent.Type == sf::Event::Closed) Running = false;
// escape key pressed
if ((MyEvent.Type == sf::Event::KeyPressed) && (MyEvent.Key.Code == sf::Key::Escape)) Running = false;
}
}
return EXIT_SUCCESS;
}