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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Nightzus

Pages: [1]
1
Graphics / Re: Circular sectors with texture
« on: January 28, 2014, 04:13:19 pm »
Indeed. It seemed weird at first glance but it is indeed a rather good solution.

I'm getting some little artefacts but I'll see if I can get rid of them later.

Thank you very much.


For those who are interested, here is the modified code:

EDIT: this solution only works if the texture has the same size than the circle.
EDIT2: I've updated the following code. It should work with any texture smaller or larger than the circle and produces no visual glitches.

void ArcCircle::setPortion(float portion)
{
        m_portion = portion > 1.0 ? 1.0 : portion;
        m_portion = portion < 0 ? 0 : portion;

        if (getTexture())
        {
                Vector2i offset(0,0);
                Vector2u size = getTexture()->getSize();
                Vector2u halfSize(size.x / 2, size.y / 2);
                Vector2f factor((float) halfSize.x / m_radius, (float) halfSize.y / m_radius);

                switch ((int) (m_portion * 100) / 25)
                {
                        case 0:
                                offset.x = round(getPoint(getPointCount() - 1).x * factor.x);
                                setTextureRect(IntRect(halfSize.x, 0, offset.x - halfSize.x, halfSize.y));
                                break;
                        case 1:
                                offset.y = round(getPoint(getPointCount() - 1).y * factor.y);
                                setTextureRect(IntRect(halfSize.x, 0, halfSize.x, offset.y));
                                break;
                        case 2:
                                offset.x = round(getPoint(getPointCount() - 1).x * factor.x);
                                setTextureRect(IntRect(offset.x, 0, size.x - offset.x, size.y));
                                break;
                        case 3:
                                setTextureRect(IntRect(0, 0, size.x, size.y));
                                break;
                        default:
                                setTextureRect(IntRect(0, 0, size.x, size.y));
                                break;
                }
        }

        update();
}
 

2
Graphics / Re: Circular sectors with texture
« on: January 28, 2014, 03:03:32 pm »
I'm pretty sure I'm not doing anything fancy to apply the texture but... here is the complete code:

#include <SFML/Graphics/RenderWindow.hpp>
#include "arc_circle.hpp"
#include <SFML/Graphics/Texture.hpp>
#include <SFML/Window/Event.hpp>

using namespace sf;

int main()
{
        RenderWindow window(VideoMode(72, 72), "Test");

        VideoMode desktop = VideoMode::getDesktopMode();
        Vector2i pos(desktop.width / 2, desktop.height / 2);
        pos.x -= window.getSize().x / 2;
        pos.y -= window.getSize().y / 2;

        window.setPosition(pos);

        ArcCircle life(36);
        Texture lifeText;
        lifeText.loadFromFile("life.png");
        //life.setTexture(&lifeText);

        life.setPortion(0.3);

    while (window.isOpen())
        {
                // check all the window's events that were triggered since the last iteration of the loop
                sf::Event event;
                while (window.pollEvent(event))
                {
                        // "close requested" event: we close the window
                        if (event.type == sf::Event::Closed)
                                window.close();
                }

                window.clear(Color::Black);

                window.draw(life);

                window.display();
        }

        return 0;
}

#include <SFML/Graphics/Shape.hpp>

class ArcCircle : public sf::Shape
{
        private:
                int m_radius;
                int m_pointCount;
                float m_portion;

        public:
                explicit ArcCircle(int radius = 0, int pointCount = 50) : m_radius(radius), m_pointCount(pointCount), m_portion(1.0)
                {
                        update();
                }

                void setRadius(int radius) {m_radius = radius; update();};
                int getRadius() const {return m_radius;};

                void setPortion(float portion)
                {
                        m_portion = portion > 1.0 ? 1.0 : portion;
                        m_portion = portion < 0 ? 0 : portion;
                        update();
                };
                float getPortion() const {return m_portion;};

                virtual unsigned int getPointCount() const;
                virtual sf::Vector2f getPoint(unsigned int index) const;
};

#include "arc_circle.hpp"

unsigned int ArcCircle::getPointCount() const
{
        return m_pointCount + 1;
}

sf::Vector2f ArcCircle::getPoint(unsigned int index) const
{
        static const float pi = 3.141592654f;

        if (index == 0)
                return sf::Vector2f(m_radius, m_radius);

        float angle = m_portion * (index-1) * 2 * pi / (m_pointCount - 1);
        angle -= pi / 2;
        float x = std::cos(angle) * m_radius;
        float y = std::sin(angle) * m_radius;

        return sf::Vector2f(m_radius + x, m_radius + y);
}

I think it might be that when I call update(), it internally calls updateTextureCoords() which tries to match "every point" of the texture to those of the shape. But since update is not virtual, I cannot rewrite it nor updateTextureCoords()

3
Graphics / Circular sectors with texture
« on: January 28, 2014, 02:52:26 pm »
Hello all.

I'm working on a basic HUD that would display the life of the player. I'm planning on using a circular life bar like this:



Obviously, as long as the player's life decreases, the circle should hide a corresponding part of itself and should become a circular sector like this:



I've been currently working without any textures and the results seem encouraging. I've derived sf::Shape and reimplementing these methods:

unsigned int ArcCircle::getPointCount() const
{
        return m_pointCount + 1;
}

sf::Vector2f ArcCircle::getPoint(unsigned int index) const
{
        static const float pi = 3.141592654f;

        if (index == 0)
                return sf::Vector2f(m_radius, m_radius);

        float angle = m_portion * (index-1) * 2 * pi / (m_pointCount - 1);
        angle -= pi / 2;
        float x = std::cos(angle) * m_radius;
        float y = std::sin(angle) * m_radius;

        return sf::Vector2f(m_radius + x, m_radius + y);
}

where
m_portion
denotes the portion in percentage of the circle to draw.

This works quite fine... until I've tried using textures. It appears that the texture is fully drawn inside the portion of the circle displayed, which gives weird results. Here with/without texture:




Does anyone have an idea how to solve this issue?

4
Window / Re: Window is automatically enlarged
« on: July 31, 2013, 01:03:41 pm »
Ok, so this is clearly due to this extension thing.

Thank you for your answers ;)

5
Window / Re: Window is automatically enlarged
« on: July 31, 2013, 11:55:39 am »
I have just installed SFML 2.1, and it did not work.

I tried the most minimalist example, just displaying a window, and the same bug appeared. But after a few tests and after playing with displaying the size on a Event::Resized, I think I know where it comes from and SFML is probably not the problem !

I am using Gnome 3 as Desktop Manager, and I have been toying with extensions. One of them removes the black top bar and lets it reappear when the cursor goes on top. After disabling it, it became clear : the top bar is approximately 30px as well as title bars of windows. So it is about 60 extra pixels. Given that I want a drawable window height of 720 and that I have at most 768 due to my resolution, I only get 708 of drawable height. I suppose SFML, in this case, decides to resize the window in order to fit the available height (and not keeping the ratio, but this is not the problem) and the available width as well.
So maybe the tweak that I am using to remove the top bar is not a) well implemented b) not supported by SFML, so that SFML "thinks" that only 708 pixels are available and decides to resize.

The last thing I do not understand is why then when it takes the decision to resize, the height occupies the whole height - that is the space normally taken by top bar. It is like it thinks that the top bar is there, so decides to resize, and at the resize no longer thinks the top bar is displayed...

But now that I understood what was going wrong, I'll try to figure a software solution out :)

6
Window / Window is automatically enlarged
« on: July 31, 2013, 01:52:25 am »
Hi guys !

I've been playing with SFML for a while and something is bothering me at the moment.

When I create a 800x600 window, no problem. But if I want a 1280x720 one, then the window is automatically maximized to take the largest possible screen portion. My resolution is 1360x760, and when I display the result of window.getSize(), I get 1360x732 or so, which corresponds to the maximum size given a rough 30 pixel title bar height.

I'd say the behaviour is like the window size was too large so that it automatically resizes it to fit the desktop resolution...

I'm running on Ubuntu 13.04 and using SFML 2.0. Maybe 2.1 fixed the problem, but I looked in the changelog and nothing pointed that out, but I may be missing something...

Do you have any clue on the issue ?

Thank you in advance !

Pages: [1]