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

Author Topic: Creating your own drawable objects  (Read 10346 times)

0 Members and 4 Guests are viewing this topic.

Phys1k3r

  • Newbie
  • *
  • Posts: 10
    • View Profile
Creating your own drawable objects
« on: September 07, 2012, 04:23:10 pm »
Hello,
I am just a beginner in C++ and even more a beginner in SFML. I use SFML 2.0 with Visual Studio 2010.

For a project I need to paint 3 objects. (2 coordinatesystems, 1 scale).
I wanted to create a class for each object and than I wanted to draw the object on my window.

I created a window with this
 
main(){
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");

// I had something in mind like doing this:

zControl zCon;                      // zControl is the scale class I want to print
xyControl xyCon;                  // xyControl is on coord System I want to draw
angleControl angleCon;       // angleControl is the orther coord System I want to draw

... // do some other stuff

window.draw(zCon);
window.draw(xyCon);
window.draw(angleCon);

... // do other stuff
}


One class is called  zControl, this should be one of the objects, I want to draw.

class zControl : public sf::Shape
{
public:
        zControl(void);
        ~zControl(void);
};
 

I thought that if I inherit from Shape could be a useful Idea, but I do not really know how to do it. I know the basics of abstract classes, but I have no idea how to realize it.

Unfortunately I do understand the tutorials, but there is not explained how to solve my problems.
The Tutorials just show me how to paint Rectangles, Strings, Circles, etc...
I assume it is pretty basic, but I tried a lot of things now for a long time and nothing seems to work.
« Last Edit: September 07, 2012, 04:25:35 pm by Phys1k3r »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10993
    • View Profile
    • development blog
    • Email
Re: Creating your own drawable objects
« Reply #1 on: September 07, 2012, 04:37:30 pm »
Hmmm maybe it's just me, but I don't understand what you really want to do...
If you want to draw your own geometry things, you may want to use sf::ConvexShape and handle the points on your own, or go directly to sf::VertexArray.

If you don't know how to derive from classes and implement the virtual functions, then you haven't understood some basic OOP stuff, which you can read up on various knowledge sources (better FRex? ::) ).

If you understand OOP, then you should maybe take a look at sf::Drawable and sf::Transformable in the SFML documentation. Those base classes can be used to create your own drawable and transformable objects.

So what exactly do you mean by that line:
Quote
For a project I need to paint 3 objects. (2 coordinatesystems, 1 scale).
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Creating your own drawable objects
« Reply #2 on: September 07, 2012, 04:40:15 pm »
You have to define and declare private
virtual void draw(sf::RenderTarget& target,sf::RenderStates states)const
function to do what you want.
For example a class that draws line between two points.
class MyExcitingDrawable : public sf::Drawable
{
private:
virtual void draw(sf::RenderTarget& target,sf::RenderStates states)const;
public:
sf::Vector2f Point1,Point2;
sf::Color Colour;
};
 
void MyExcitingDrawable::draw(sf::RenderTarget& target,sf::RenderStates states)const
{
sf::Vertex line[2];
line[0]=sf::Vertex(Point1,Colour);
line[1]=sf::Vertex(Point2,Colour);
target.draw(line,2,sf::Lines,states);
}
 
« Last Edit: September 07, 2012, 04:43:42 pm by FRex »
Back to C++ gamedev with SFML in May 2023

Phys1k3r

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Creating your own drawable objects
« Reply #3 on: September 07, 2012, 04:58:27 pm »
Hmmm maybe it's just me, but I don't understand what you really want to do...
If you want to draw your own geometry things, you may want to use sf::ConvexShape and handle the points on your own, or go directly to sf::VertexArray.

hm, I've seen this method. But I don't think ConvexShape is what I need.

Quote
If you don't know how to derive from classes and implement the virtual functions, then you haven't understood some basic OOP stuff, which you can read up on various knowledge sources (better FRex? ::) ).

If you understand OOP, then you should maybe take a look at sf::Drawable and sf::Transformable in the SFML documentation. Those base classes can be used to create your own drawable and transformable object.
I know the basics of OOP and the idea of OOP, as I come from java, but I still have troubles with implementation in C++.
I had look at drawable too and even think, that this could be more usefull than using Shape. I have to revise OOP, propably you're right and my knowledge in OPP is too short
Quote
So what exactly do you mean by that line:
Quote
For a project I need to paint 3 objects. (2 coordinatesystems, 1 scale).

I need to decelop a window where you can see 3 constant Objects.
The first object is a coordinate System. On the surface of this coordsystem I want to display a moving point, which coordinates are transmitted by an extern program.

I made a small picture in paint, so you can see what I have in mind.

I thought it is useful to make a class for each of this objects, as I have to make some calculations and some data a transmitted. I didn't want to put all in the window method.

I hope you understand, what I want to do.

Edit:
@FRex

Hey, I already experimented with Drawable.

In the end, I was not able to call the MyDrawable.draw(RenderTarget,RenderStates) method,
because I was not able to transform my RenderWindow into a RenderTarget?

I found this quite strange, as I thought, RenderWindow inherits from RenderTarget, but I had no Idea to solve this


Edit2:
wow, I testet it now with this command:

window.draw(myDrawableTest); // myDrawableTest is a object I generated from myDrawable
and it works!!! Thank you FRex and eXpl0it3r!

I almost had the same code like you before, but I didn't try to use window.draw( ... ).



[attachment deleted by admin]
« Last Edit: September 07, 2012, 05:11:05 pm by Phys1k3r »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Creating your own drawable objects
« Reply #4 on: September 07, 2012, 05:08:53 pm »
You shouldn't call .draw of any drawable.
Quote
Note that inheriting from sf::Drawable is not mandatory, but it allows this nice syntax "window.draw(object)" (...) which is more consistent with other SFML classes.
Back to C++ gamedev with SFML in May 2023

Phys1k3r

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Creating your own drawable objects
« Reply #5 on: September 07, 2012, 05:11:57 pm »
You shouldn't call .draw of any drawable.
Quote
Note that inheriting from sf::Drawable is not mandatory, but it allows this nice syntax "window.draw(object)" (...) which is more consistent with other SFML classes.

ah, crap.
I've read this several times, but I had no real clue what it meant!
Thank you!