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

Author Topic: [2.1] Custom drawing shapes  (Read 4049 times)

0 Members and 1 Guest are viewing this topic.

detlion1643

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
[2.1] Custom drawing shapes
« on: July 18, 2014, 04:09:57 pm »
Hello everyone,

Obligatory first post "thanks for the great libraries"!

With that out of the way, I have found a couple of posts and a wiki page regarding rounded rectangle shapes. However, none of them make sense anymore as I believe something must've been changed with sf::Shape in SFML 2.1. All of the examples create a new class inheriting from sf::Shape (or even previously from that creating an sf::Shape).

So I went about creating an entire class that inherits from sf::Shape (based on the example in the wiki). After everything was done, creating a new instance of that class results in an error message: "object of abstract class type ***::***::RoundedRectangle is not allowed:"

Now I'm still new to c++ (know a ton of c# and used to override .Net paint events (GID/GDI+) to custom draw controls) but I feel like I just gotta reach out to the forum here...

I'm unsure where to go from here. From reading, it seems like I'd have to plot every usable point and use a vertex array with the sf::LineStrip primitive to connect them all? If so, any ideas for plotting the points on the corners? Is there a different better way?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [2.1] Custom drawing shapes
« Reply #1 on: July 18, 2014, 04:41:59 pm »
Quote
object of abstract class type ***::***::RoundedRectangle is not allowed:
It means you didn't override all the pure virtual functions of the sf::Shape base class. Or got one of the function prototypes wrong.

Quote
I'm unsure where to go from here. From reading, it seems like I'd have to plot every usable point and use a vertex array with the sf::LineStrip primitive to connect them all? If so, any ideas for plotting the points on the corners? Is there a different better way?
You won't have to deal with vertex arrays if you make your custom shape class work.
Laurent Gomila - SFML developer

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: [2.1] Custom drawing shapes
« Reply #2 on: July 18, 2014, 04:45:27 pm »
Hi, and welcome! :)

(based on the example in the wiki)
It would be nice to know which example you are refering to (could you link it?) and what you changed.

sf::LineStrip
If you're using a Vertex Array, sf::Linestrip is for infinitely thin lines so would be most useful for lines/splines and hollow shapes. sf::Trianglestrip and some of the other types would be more useful for solid shapes.

If so, any ideas for plotting the points on the corners? Is there a different better way?
Better way than what?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

detlion1643

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: [2.1] Custom drawing shapes
« Reply #3 on: July 18, 2014, 05:26:43 pm »
Here is where I based my custom class off of: https://github.com/SFML/SFML/wiki/Source:-Draw-Rounded-Rectangle

Here is my class:
namespace Graphics
        {

                class RoundedRectangle : public sf::Shape
                {
                        private:
                                sf::Vector2f size;
                                float cornerradius;
                                unsigned int cornerpoints;
                        public:
                                RoundedRectangle(int, int, sf::Vector2f, int, unsigned int, float, sf::Color);
                                void setSize(const sf::Vector2f& Size);
                                const sf::Vector2f& getSize();
                                void setCornerRadius(float CornerRadius);
                                float getCornerRadius();
                                void setCornerPoints(unsigned int CornerPoints);
                                virtual unsigned int getPointCount();
                                virtual sf::Vector2f getPoint(unsigned int Index);
                };

        }

Implementation:
#pragma region Graphics

                EZFile::Graphics::RoundedRectangle::RoundedRectangle(int Left, int Top, sf::Vector2f Size, int CornerRadius, unsigned int CornerPoints, float OutlineThickness, sf::Color OutlineColor)
                {
                        setPosition(Left, Top);
                        size = Size;
                        cornerradius = CornerRadius;
                        cornerpoints = CornerPoints;
                        setOutlineThickness(OutlineThickness);
                        setOutlineColor(OutlineColor);
                        update();

                }
                void EZFile::Graphics::RoundedRectangle::setSize(const sf::Vector2f& Size)
                {
                        size = Size;
                        update();
                }

                const sf::Vector2f& EZFile::Graphics::RoundedRectangle::getSize()
                {
                        return size;
                }

                void EZFile::Graphics::RoundedRectangle::setCornerRadius(float CornerRadius)
                {
                        cornerradius = CornerRadius;
                        update();
                }

                float EZFile::Graphics::RoundedRectangle::getCornerRadius()
                {
                        return cornerradius;
                }

                void EZFile::Graphics::RoundedRectangle::setCornerPoints(unsigned int CornerPoints)
                {
                        cornerpoints = CornerPoints;
                }

                unsigned int EZFile::Graphics::RoundedRectangle::getPointCount()
                {
                        return cornerpoints * 4;
                }

                sf::Vector2f EZFile::Graphics::RoundedRectangle::getPoint(unsigned int Index)
                {
                        if(Index >= cornerpoints*4)
                                return sf::Vector2f(0,0);

                        float deltaAngle = 90.0f/(cornerpoints-1);
                        sf::Vector2f center;
                        unsigned int centerIndex = Index/cornerpoints;
                        unsigned int offset = 0;
                        static const float pi = 3.141592654f;

                        switch(centerIndex)
                        {
                                case 0: center.x = size.x - cornerradius; center.y = cornerradius; break;
                                case 1: center.x = cornerradius; center.y = cornerradius; break;
                                case 2: center.x = cornerradius; center.y = size.y - cornerradius; break;
                                case 3: center.x = size.x - cornerradius; center.y = size.y - cornerradius; break;
                        }

                        return sf::Vector2f(cornerradius*cos(deltaAngle*(Index-centerIndex)*pi/180)+center.x,
                        cornerradius*sin(deltaAngle*(Index-centerIndex)*pi/180)-center.y);
                }

#pragma endregion Graphics

And how I'm trying to use it (based on the first line of the example in the link:
/// Usage example:
/// \code
/// sf::RoundedRectangleShape roundedRectangle;
/// ....

EZFile::Graphics::RoundedRectangle shp;


Here is the other link I looked at:http://en.sfml-dev.org/forums/index.php?topic=973.0

This is just the beginning of a variety of controls I am making. My first control (button) will make a normal rectangle, but if it has a cornerradius of greater than 0, it uses that and makes a rounded rectangle instead. I'm planning on lots of controls (checkboxes, radio buttons, scrollable lists) with each one having options of different styles/etc.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [2.1] Custom drawing shapes
« Reply #4 on: July 18, 2014, 05:38:08 pm »
The virtual functions are const.
Laurent Gomila - SFML developer

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: [2.1] Custom drawing shapes
« Reply #5 on: July 18, 2014, 06:54:17 pm »
This is just the beginning of a variety of controls I am making. My first control (button) will make a normal rectangle, but if it has a cornerradius of greater than 0, it uses that and makes a rounded rectangle instead. I'm planning on lots of controls (checkboxes, radio buttons, scrollable lists) with each one having options of different styles/etc.
Unless this is ment to be a learning experience, why don't you just use one of the many existing GUI libraries?

detlion1643

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: [2.1] Custom drawing shapes
« Reply #6 on: July 18, 2014, 07:50:44 pm »
... Thanks Laurent. That fixed up the custom class based off sf::Shape.

It's mostly meant as a learning experience for right now. Gotta start somewhere. Thanks for the gui links though, I might end up with one of them for now.

It took me a lot of learning to even get my framework project up and running with my main project. With includes/links/etc.

I've got lots more learning to do, but this is a great start. Thanks again guys!