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

Author Topic: Extending sf::Vertex  (Read 1155 times)

0 Members and 1 Guest are viewing this topic.

jjamesdk

  • Newbie
  • *
  • Posts: 3
    • View Profile
Extending sf::Vertex
« on: March 10, 2015, 04:48:49 am »
Hi all,

I was attempting to extend sf::Vertex, by creating my own custom vertex class and inheriting from this. I am able to create the methods I want fine, and sf::VertexArray will append with my custom Vertex class. However, when I try to access my new methods from an index within my sf::VertexArray container, I only have access to to the existing sf::Vertex properties, such as position, color, and texCoords. The strange thing is I can access my new methods outside of the sf::VertexArray object, just not within it.

Am I going about this the wrong way? or was this just not intended to work this way?

- Thanks All

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Extending sf::Vertex
« Reply #1 on: March 10, 2015, 07:45:05 am »
Quote
Am I going about this the wrong way?
Yes.

What do you want to achieve? What does your derived class add?
Laurent Gomila - SFML developer

jjamesdk

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Extending sf::Vertex
« Reply #2 on: March 10, 2015, 01:00:37 pm »
Heya, Thanks for the response

Well basically, I'm making a Flexible Grid system consisting of Nodes, that can contain other properties, such
as having a status of being occupied or not, having an Id, etc. I am using composition saying each Node has a Vertex (this is my custom Vertex, extending sf::Vertex). However, I need to have some kind of mapping back to the Node it's in with a key pair value for when I operate on it through the index in the sf::VertexArray. In example, say I have NodeId set to one, when this nodes vertex is getting appended I would also be setting it's nodeKey to NodeId. Then when iterating over the array I can say, if nodeKey == NodeId, do something. So ultimately I am adding a method for getting/setting nodeKey in this case.

See my github for a better idea
https://github.com/jjamesdk/MazeMania/wiki/Snapshots-throughout-the-SDLC

Thanks again
« Last Edit: March 10, 2015, 01:07:44 pm by jjamesdk »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10835
    • View Profile
    • development blog
    • Email
Re: Extending sf::Vertex
« Reply #3 on: March 10, 2015, 01:32:16 pm »
Code design aside the problem you think to have is standard C++ behavior. The sf::VertexArray only knows how to deal with sf::Vertex. Since you derive from sf::Vertex your own class becomes polymorphic in the sf::VertexArray and thus you can only access the base class methods.
You might want look up the chapter on polymorphism again. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

jjamesdk

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Extending sf::Vertex
« Reply #4 on: March 10, 2015, 01:42:51 pm »
Ahh! that may be it, I'll check it out. Thanks my friend :). C++ is a beast