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

Author Topic: SFML Game Dev Guide - C++ to C# blockade  (Read 15235 times)

0 Members and 2 Guests are viewing this topic.

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
SFML Game Dev Guide - C++ to C# blockade
« on: November 10, 2014, 09:17:41 pm »
Hey Guys!

I love the SFML Game Dev Guide a lot. Also when it comes a bit tricky thinking over from C++ to C# -but until now I managed it with your help and google.

Sadly nothing runs perfect in coding anyway and therefore I'm in front of a new wall which I can't climb with my novice knowledge.

I now arrived at SceneNodes - a topic I really am interested in. But as it goes - C++ knocks me out and I don't know what the book is talking about:

class SceneNode
{
public:
          typedef std::unique_ptr<SceneNode> Ptr;
public:
           SceneNode();
private:
           std::vector<Ptr> mChildren;
           SceneNode* mParent;
};


I have no idea how to convert this anyhow into C# - The main problem is that I dont get the semantics with these pointers here good enough. If I would understand it right away I might be able to do it in C#. But I just don't get what they suppose to manage here :D

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: SFML Game Dev Guide - C++ to C# blockade
« Reply #1 on: November 10, 2014, 09:22:51 pm »
I'm sure a much more thorough answer will arrive shortly from someone much more knowledgeable than me, but:

C# is a garbage-collected language.  So most memory management problems and solutions in C++ simply won't have an analogue.  If you ignore everything to do with memory management, then std::unique_ptr is essentially no different from a regular pointer, so you can get away with pretending that that class isn't even there and just focus on the important conceptual part, namely:  A SceneNode has pointers to other SceneNodes, one of them is its parent, and the others are its children.
« Last Edit: November 10, 2014, 09:29:38 pm by Ixrec »

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: SFML Game Dev Guide - C++ to C# blockade
« Reply #2 on: November 11, 2014, 07:59:25 am »
class SceneNode
    {
        private List<SceneNode> mChildren;
        private SceneNode mParent;

        public SceneNode()
        {
            mChildren = new List<SceneNode>();
            mParent = new SceneNode();
        }

        public void AttachChild(SceneNode child)
        {
            child.mParent = this;
            mChildren.Add(child);
        }

        public SceneNode DetachChild(SceneNode child)
        {
            child.mParent = null;
            mChildren.Remove(child);
            return child;
        }
    }

Thats how I solved it... does it fit to the needs? I think yes - just courios what you say

So u know about the methods I added I quote the books descriptions:

Quote
Node insertion and removal
Now we write an interface to insert or remove child nodes into or from a scene node.
We do this by adding the following two functions:
void SceneNode::attachChild(Ptr child)
{
child->mParent = this;
mChildren.push_back(std::move(child));
}

SceneNode::Ptr SceneNode::detachChild(const SceneNode& node)
{
auto found = std::find_if(mChildren.begin(), mChildren.end(),
[&] (Ptr& p) -> bool { return p.get() == &node; });
assert(found != mChildren.end());

Ptr result = std::move(*found);
result->mParent = nullptr;
mChildren.erase(found);
return result;
}
« Last Edit: November 11, 2014, 09:53:41 am by Falke88 »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: SFML Game Dev Guide - C++ to C# blockade
« Reply #3 on: November 11, 2014, 08:55:25 am »
That looks correct to me.  Hopefully someone who actually knows C# and is familiar with the book's code will show up to confirm that.

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: SFML Game Dev Guide - C++ to C# blockade
« Reply #4 on: November 11, 2014, 09:23:57 am »
For you guys who are bored ;)

I have a next issue going on under the Topic: Making scene nodes drawable

The issue is - like I've read not an unusual one - that there isn't an easy way to override Draw(x,y) of the Drawable Interface in SFML.NET because it's not virtual. I'm gonna lurk for the solution to get straight with the C++ solution anyhow. Mybe you guys can throw in some informations already to this issue !

Updates will follow...


Greets Charlie

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: SFML Game Dev Guide - C++ to C# blockade
« Reply #5 on: November 11, 2014, 09:24:19 am »
That looks correct to me.  Hopefully someone who actually knows C# and is familiar with the book's code will show up to confirm that.

I bet sb. will  8)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML Game Dev Guide - C++ to C# blockade
« Reply #6 on: November 11, 2014, 09:43:01 am »
Quote
SceneNode result = mChildren[mChildren.IndexOf(child)];
This line is pretty much useless, "result" is "child", obviously.

Quote
The issue is - like I've read not an unusual one - that there isn't an easy way to override Draw(x,y) of the Drawable Interface in SFML.NET because it's not virtual.
It is. Drawable is an interface in SFML.Net.
Laurent Gomila - SFML developer

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: SFML Game Dev Guide - C++ to C# blockade
« Reply #7 on: November 11, 2014, 09:52:35 am »
Quote
SceneNode result = mChildren[mChildren.IndexOf(child)];
This line is pretty much useless, "result" is "child", obviously.

Quote
The issue is - like I've read not an unusual one - that there isn't an easy way to override Draw(x,y) of the Drawable Interface in SFML.NET because it's not virtual.
It is. Drawable is an interface in SFML.Net.

Ha...yea you're right...mixed up minds with this C++ to C# transfers  ::)
« Last Edit: November 11, 2014, 10:18:45 am by Falke88 »

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: SFML Game Dev Guide - C++ to C# blockade
« Reply #8 on: November 11, 2014, 03:42:02 pm »
It's me again - came a lot further and broke through SceneNodes pretty well!

sf::Vector2f SceneNode::getWorldPosition() const
{
   return getWorldTransform() * sf::Vector2f();
}


Here it's not clear for me how I can reach something like sf::Vector2f in .NET since Vector2f is only a struct type.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: SFML Game Dev Guide - C++ to C# blockade
« Reply #9 on: November 11, 2014, 04:03:02 pm »
And? What prevents you from multiplying a transform with a vector?

Please don't ask every minor thing. Have a look at the SFML.NET documentation and try to understand the classes and their functionality. Consult the source code in case of doubt.

By the way, other people might already have ported the book or parts of it to C#, you could see if you find something.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: SFML Game Dev Guide - C++ to C# blockade
« Reply #10 on: November 11, 2014, 04:15:39 pm »
Meh sorry for asking so much - It's just I'm working fulltime on it and therefor questions arise pretty continuesly.

  public Transform GetWorldPosition()
        {
            return GetWorldTransform() * new Vector2f(0.0f, 0.0f);
        }

Quote
Error 3   an implicit conversion of type 'SFML.System.Vector2f' into 'SFML.Graphics.Transform' is not possible.   C:\Users\FalkeXY\Desktop\SFML_Game_Dev\Game\SceneNode.cs   71   20   Tutorial_Game

Quote
And? What prevents you from multiplying a transform with a vector?

That's what actually prevents me from it

-

And don't get me wrong that I didn't tried everything I know or googled for it already. I spend some time searching for the solution. But since this forum is the only reliable source for asking I have no other choice than asking here. Waiting for nothing isn't really an option if we all are honest.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML Game Dev Guide - C++ to C# blockade
« Reply #11 on: November 11, 2014, 04:21:36 pm »
Your code is ok, you just have the return type of GetWorldPosition() wrong.
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: SFML Game Dev Guide - C++ to C# blockade
« Reply #12 on: November 11, 2014, 04:22:03 pm »
Okay, that was not clear from your previous post. Please provide always details and what you've tried already when asking.

The Transform class contains an overloaded operator for Vector2f, see here. Are you using an older version? One alternative is calling TransformPoint() directly.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: SFML Game Dev Guide - C++ to C# blockade
« Reply #13 on: November 11, 2014, 04:33:24 pm »
Your code is ok, you just have the return type of GetWorldPosition() wrong.

Ah dang...bad bugsearch from my side here sorry guys...what a silly mistake.
I will take the tip with the C# conversion of the tutorial book with me now so I don't need to bugger you that often.


greets

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: SFML Game Dev Guide - C++ to C# blockade
« Reply #14 on: November 11, 2014, 04:49:31 pm »
As indicated above, there's an existing C# port. Even if you do yours for learning purposes, looking at existing source code would solve many of your problems immediately.

Thus... always search a bit ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything