Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Line and Rectangle does not show up  (Read 3769 times)

0 Members and 1 Guest are viewing this topic.

easy

  • Full Member
  • ***
  • Posts: 146
    • MSN Messenger - easy82.contact@gmail.com
    • View Profile
    • Email
Line and Rectangle does not show up
« 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:

Code: [Select]
#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:



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

SCPM

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Line and Rectangle does not show up
« Reply #1 on: August 22, 2011, 12:33:30 pm »
Hello:
I think it ought to work if you change the lines to:

Code: [Select]

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

easy

  • Full Member
  • ***
  • Posts: 146
    • MSN Messenger - easy82.contact@gmail.com
    • View Profile
    • Email
Line and Rectangle does not show up
« Reply #2 on: August 22, 2011, 01:16:19 pm »
Thanks SCPM!

That really solved the problem.

The fixed code is:

Code: [Select]
#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

Code: [Select]
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!

 

anything