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

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

0 Members and 1 Guest are viewing this topic.

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: SFML Game Dev Guide - C++ to C# blockade
« Reply #15 on: November 13, 2014, 01:52:11 pm »
Hey Bros!

Asking Time again :P Just want to check if this would be redundant or not.

class CommandQueue
    {
        Queue<Command> queue;

        public bool isEmpty()
        {
            return queue.Count == 0;
        }

        public void Push(Command command)
        {
            queue.Enqueue(command);
        }
        public Command Pop()
        {
            return queue.Dequeue();
        }
    }


In this class I forward every method with the method of the Queue<T> object. Why can't I just declare
Queue<T> public and access their methods like: CommandQueue.queue.Enqueue(command)

Is it just that it looks nicer with
CommandQueue.Push(command)
There just a design thingie?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: SFML Game Dev Guide - C++ to C# blockade
« Reply #16 on: November 13, 2014, 02:33:19 pm »
Encapsulation. In C++, nobody needs access to the size or back element of std::queue, so only the required operations are provided. The implementation could be exchanged without affecting the interface. Also, it makes it possible to forward-declare the CommandQueue (not relevant in C#). 
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 #17 on: November 14, 2014, 09:30:31 am »
Hey Guys - new issue here. Since the C# Port guy made things a bit different I can't rely of his code.
Actually it's not a syntax issue more a semantic one.

struct AircraftMover
{
   AircraftMover(float vx, float vy)
   : velocity(vx, vy)
{
}
   void operator() (SceneNode& node, sf::Time) const
{
     Aircraft& aircraft = static_cast<Aircraft&>(node);
     aircraft.accelerate(velocity);
}
   sf::Vector2f velocity;
};

That's the code in the book. Please focus on the fact that the method operator() here receives these usual parameters of type SceneNode and Time.

After that there is a definition for HandlePlayerRealtimeInput:

void Player::handleRealtimeInput(CommandQueue& commands)
{
   const float playerSpeed = 30.f;
   if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
   {
      Command moveLeft;
      moveLeft.category = Category::PlayerAircraft;
      moveLeft.action = derivedAction<Aircraft>(
      AircraftMover(-playerSpeed, 0.f));
      commands.push(moveLeft);
   }
}


Here you can see that the Commad object moveLeft within its function pointer action (if it isnt a function pointer nvm - thats not the point here I think) receives AircraftMover() (which is the constructor as I can see that far)

Now...since the operator() method of AircraftMover isn't called explicit I'm sure that it's called within the AircraftMover Constructor. (Be sure that I'm not hip with C++ therefore I have to estimate what happens and relate it to C# designs).

That are all things which I could handle with C#.
The big BUT here is...

How can the operator() method of AircraftMover receive any references to SceneNode and Time ? Since the AircraftMover Constructor isn't receiving any of these (only float_x and float_y parameters). Neither there is any inheritance to something which contains a SceneNode nor Time.

I really browsed the C# Port and ascending pages of the Tutorial Book - but couldn't find any explaination what is happening there.

Besides that problem I really miss the definition of the HandleEvent Method of the Player class. Only the HandlePlayerRealtimeInput receives a defintion with the implemenation of the Command class etc. as you can see above. Would be nice to have some clear code on that method aswell (Again - the C# Port has another approach on that problem - therefore I can't take it for this here)


I hope I won't waste your time on this - but I'm very motivated to complete this tutorial with all dents and stones which may occur. Since I'm a beginner coder I need your help often. But HEY - I learned a lot here and with the book - so you can be sure my question activity will lower and answer posts will raise ;)


greets Charlie
« Last Edit: November 14, 2014, 10:29:56 am by Falke88 »

Ztormi

  • Jr. Member
  • **
  • Posts: 71
  • Web developer by day. Game developer by night.
    • View Profile
Re: SFML Game Dev Guide - C++ to C# blockade
« Reply #18 on: November 14, 2014, 12:26:22 pm »
Browsing through this

Quote
Now...since the operator() method of AircraftMover isn't called explicit I'm sure that it's called within the AircraftMover Constructor. (Be sure that I'm not hip with C++ therefore I have to estimate what happens and relate it to C# designs).

...

How can the operator() method of AircraftMover receive any references to SceneNode and Time ? Since the AircraftMover Constructor isn't receiving any of these (only float_x and float_y parameters). Neither there is any inheritance to something which contains a SceneNode nor Time.


No, operator() is an operator overload. In this case it overloads call operation. You can also overload typical operations such as add or substract. Operator()-method is executed when you call aircraftmover instance. In the link I posted it's located in SceneNode-class.

void SceneNode::onCommand(const Command& command, sf::Time dt)
{
    // Command current node, if category matches
      if (command.category & getCategory())
          command.action(*this, dt);  

     // Command children
     FOREACH(Ptr& child, mChildren)
    child->onCommand(command, dt);
}
 

SceneNode and time are also passed to parameters there as you can see.

You can't overload call operator in C# so you'll have to handle that in a different way.
Here's a list of overloadable operators in C# http://msdn.microsoft.com/en-us/library/8edha89s.aspx
« Last Edit: November 14, 2014, 12:38:42 pm by Ztormi »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: SFML Game Dev Guide - C++ to C# blockade
« Reply #19 on: November 14, 2014, 01:21:58 pm »
Now...since the operator() method of AircraftMover isn't called explicit I'm sure that it's called within the AircraftMover Constructor.
No. You see the constructor's definition right above, and there is no operator() call. The function object is invoked later, to apply functions to scene nodes. The whole point of storing functions in std::function is to not invoke them directly, but in a deferred way as a callback.

In C#, the equivalent would be delegates -- or simply interfaces that are implemented. For the interface approach, consult the C# port.

Besides that problem I really miss the definition of the HandleEvent Method of the Player class.
In C++, it's here.
« Last Edit: November 14, 2014, 01:24:58 pm by Nexus »
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 #20 on: November 14, 2014, 04:56:04 pm »
Thank you guys very much! Now I get the problem here much better. Well might be a bit tricky to receive the same solution as being able to overload the call operator. Thanks for the Laurent Link to see the Dev Books Codes more clearly - missed that completly it seems.

 :-*

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: SFML Game Dev Guide - C++ to C# blockade
« Reply #21 on: November 14, 2014, 05:04:08 pm »
Well might be a bit tricky to receive the same solution as being able to overload the call operator.
I mentioned two alternatives in C#, so why would you want to do that?
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 #22 on: November 14, 2014, 05:38:23 pm »
Well might be a bit tricky to receive the same solution as being able to overload the call operator.
I mentioned two alternatives in C#, so why would you want to do that?

You misunderstood me here - I read your alternatives and will use them for that solution here - Still it will be tricky because I'm no Pro  :D

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: SFML Game Dev Guide - C++ to C# blockade
« Reply #23 on: November 14, 2014, 05:39:59 pm »
Nobody was born a pro, you need to invest some time into learning C# ;)
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 #24 on: November 14, 2014, 05:51:49 pm »
Nobody was born a pro, you need to invest some time into learning C# ;)

I'm on it at fullspeed chief ;) - being an Airborne Ranger and spending all my freetime for coding <3

BTW thats my solution which pretty much fits very well:

  class AircraftMover
    {
        private Vector2f velocity;

        public AircraftMover(float x, float y)
        {
            velocity = new Vector2f(x, y);
        }
        public void Execute(SceneNode node, Time dt)
        {
            ((Aircraft)node).SetVelocity(((Aircraft)node).GetVelocity() + velocity);
        }
    }

Note: Keeping the Downcast up still...for now.

public void HandleRealTimeInput(CommandQueue commands)
        {
            const float playerSpeed = 30.0f;

            if(Keyboard.IsKeyPressed(Keyboard.Key.Left))
            {
                Command moveLeft = new Command();
                moveLeft.SetCategory(Category.PlayerAircraft);
                moveLeft.action = new AircraftMover(-playerSpeed, 0.0f).Execute;
            }
        }

I made action a delegate already from the beginning because I kinda thought it might fit. Cool thing that I can define the AircraftMover class and directly through the DOT operator access its method - in fact made it flush in one row *proud of me*

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: SFML Game Dev Guide - C++ to C# blockade
« Reply #25 on: November 15, 2014, 11:21:53 pm »
The C++ Book code tells me to use Transformable::move() therefore I found Transformable.Translate which receives a Vector2f. I check if the code for Pressing "Left" as Userinput reaches the mVelocity there and it does - mVelocity is being changed.

Still no change on the sprites on execution - trying to locate the bug. Kinda think that the Translate Method isn't working as intended.



class Entity : SceneNode
    {
        Vector2f mVelocity;

        public Vector2f GetVelocity() { return mVelocity; }
        public void SetVelocity(float x, float y) { mVelocity = new Vector2f(x, y); }
        public void SetVelocity(Vector2f velocity) {  mVelocity = velocity; }

        protected override void UpdateCurrent(Time dt)
        {
            Transform.Translate(mVelocity * dt.AsSeconds());
        }
    }


Alternatively using:
Position += mVelocity * dt.AsSeconds();
Is working just that you know - but I would like to use Translate :/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: SFML Game Dev Guide - C++ to C# blockade
« Reply #26 on: November 15, 2014, 11:58:45 pm »
You can't use Transform.Translate. Transform is a read-only member, it's just that C# provides nothing to enforce this, unlike C++.

The equivalent to entity.move(v) is entity.Position += v.
Laurent Gomila - SFML developer

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: SFML Game Dev Guide - C++ to C# blockade
« Reply #27 on: November 16, 2014, 07:58:42 am »
You can't use Transform.Translate. Transform is a read-only member, it's just that C# provides nothing to enforce this, unlike C++.

The equivalent to entity.move(v) is entity.Position += v.

If its a read only member how come its defined that way in the .dll :

public void Translate(Vector2f offset);
public void Translate(float x, float y);

Accepting two overloads as a method and returning void? doesn't sound more me as something that only can be read :/ - mybe I misundestand read only a bit here

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: SFML Game Dev Guide - C++ to C# blockade
« Reply #28 on: November 16, 2014, 10:08:28 am »
The Transform class itself is not read-only. What I said is that the Transform member of Transformable is read-only.

// you can do this
Transform t1 = new Transform();
t1.Translate(10, 20);

// ... but not this
Transformable t2 = new Transformable();
t2.Transform.Translate(10, 20);
Laurent Gomila - SFML developer

 

anything