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

Author Topic: Thor 2.0 released!  (Read 341761 times)

0 Members and 1 Guest are viewing this topic.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Thor 2.0
« Reply #345 on: May 27, 2014, 09:10:27 am »
Lolilolight, this is the thread about Thor, not ODFAEG. Nobody who reads this thread wants to know how your code looks.

And please read the license text when you're using my code:
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.

I would appreciate if you could add a note to the original license text that states that you have written or adapted this code, not I. Thank you :)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
Re: Thor 2.0
« Reply #346 on: May 27, 2014, 05:17:44 pm »
Ok nexus.  :)

Mutoh

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Thor 2.0
« Reply #347 on: June 02, 2014, 03:37:20 am »
Ever thought about adding "clamp" functions to the VectorAlgebra libraries, Nexus? :)

template <typename T>
void clamp(sf::Vector2<T>& vector, T maxLength)
{
        if (squaredLength(vector) > std::pow(maxLength, 2))
        {
                auto angle = polarAngle(vector);

                vector.x = maxLength;
                vector.y = 0;

                setPolarAngle(vector, angle);
        }
}

template <typename T>
sf::Vector2<T> clamped(const sf::Vector2<T>& vector, T maxLength)
{
        sf::Vector2<T> copy = vector;
        clamp(copy, maxLength);

        return copy;
}

This is the piece of code that I've added to the library to suit my needs. Just thought I'd comment. :y

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Thor 2.0
« Reply #348 on: June 02, 2014, 07:46:01 am »
sf::Vector2<T> clamped(const sf::Vector2<T>& vector, T maxLength)
{
    sf::Vector2<T> copy = vector;

=>

sf::Vector2<T> clamped(sf::Vector2<T> vector, T maxLength)
{
 
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Thor 2.0
« Reply #349 on: June 02, 2014, 10:04:28 am »
That might be an option for a future version, but I would probably provide a method that also handles the minimum length. For me, the term "clamp" denotes bounds on two sides.

Plus, your version can be made more efficient, there's no need to compute the angle:
template <typename T>
void clamp(sf::Vector2<T>& vector, T maxLength)
{
    T square = squaredLength(vector);
    if (square > maxLength * maxLength)
        vector = maxLength * vector / TrigonometricTraits<T>::sqrt(square);
}
Maybe setLength() could also be reused.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Skamer

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Thor 2.0
« Reply #350 on: June 05, 2014, 06:15:00 pm »
I have a question.

I need know the keys(keyboard or mouse) that have been set in a Thor::Action.
Does it give a way to know them?
« Last Edit: June 05, 2014, 06:18:41 pm by Skamer »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Thor 2.0
« Reply #351 on: June 05, 2014, 08:30:46 pm »
No. The information is type-erased, so that actions can work with all possible kinds of events and realtime input conditions. The user can even specify his own custom actions.

Why do you need to know it?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Skamer

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Thor 2.0
« Reply #352 on: June 06, 2014, 01:05:06 pm »
I have created a new class called PlayerInput that inherited de thor::ActionMap<std::string> in order to create two new method : loadFromFile and saveToFile.

- LoadFromFile  : it reads the xml file (PlayerInput.xml for example) that contains the player input saved and add them to mActionMap (i had to set it as protected).
- SaveToFile :  the same that LoadFromFile but it saves instead of to load player input.

The goal is to load/save directly player input from/to a xml file from this class.

Thanks you for your answer. It's not a problem, i will do it in another class that will contain a map<std::string,std::string> with for example the first string, the action name and the second string, the keyboard key string.
« Last Edit: June 06, 2014, 01:07:16 pm by Skamer »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Thor 2.0
« Reply #353 on: June 06, 2014, 03:04:46 pm »
I have created a new class called PlayerInput that inherited de thor::ActionMap<std::string> in order to create two new method : loadFromFile and saveToFile.
Why do people always inherit? That's not how C++ works. Almost none of the classes in Thor are intended as a base class, for good reasons. Instead, you should store the action map as a member. I'm sure not all of its methods make sense for PlayerInput anyway; see also LSP.

I guess I'll have to make massive use of the final keyword once all compilers support it :D


The goal is to load/save directly player input from/to a xml file from this class.
Okay. I was already asked for serialization earlier. The problem is, I cannot provide it in a generic way. Simple mappings like action X is bound to key Y are just the beginning; users can specify their own custom actions over which I have no control; and different actions can be combined with logical operators.

Another problem is also that custom actions are packed into std::function, which doesn't allow to extract the original functor in the general case. So I would have to store the events/real-time conditions separately in each action. But I don't know if it's even possible in all situations...

So if you can contrain the complexity to simple mappings on user side, a serialization will be much simpler. But these conversion functions can be useful for you, maybe you're using them already.
« Last Edit: June 06, 2014, 03:06:43 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Thor 2.0
« Reply #354 on: June 07, 2014, 11:07:45 pm »
I guess I'll have to make massive use of the final keyword once all compilers support it :D

You don't need final to make it impossible to inherit from a class. A little creative use of virtual inheritance can do the job  ;)

class Uninheritable
{
protected:
    virtual ~Uninheritable() {}
};

struct DontInheritFromMe : virtual private Uninheritable
{
};

struct BadBoy : public DontInheritFromMe
{
};

int main()
{
    DontInheritFromMe d;
    BadBoy b;
}
 

and then you'll get nice compiler errors when someone tries to inherit :

Code: [Select]
$ clang++ src/test.cc
src/test.cc:13:8: error: inherited virtual base class 'Uninheritable' has private destructor
struct BadBoy : public DontInheritFromMe
       ^
src/test.cc:9:28: note: declared private here
struct DontInheritFromMe : virtual private Uninheritable
                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/test.cc:20:12: note: implicit default constructor for 'BadBoy' first required here
    BadBoy b;
           ^
« Last Edit: June 07, 2014, 11:23:43 pm by Jesper Juhl »

select_this

  • Full Member
  • ***
  • Posts: 130
  • Current mood: just ate a pinecone
    • View Profile
    • darrenferrie.com
Re: Thor 2.0
« Reply #355 on: June 07, 2014, 11:30:48 pm »
I've not seen that trick before - I like it because it feels devious :)
Follow me on Twitter, why don'tcha? @select_this

Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
Re: Thor 2.0
« Reply #356 on: June 07, 2014, 11:39:27 pm »
Quote
Okay. I was already asked for serialization earlier. The problem is, I cannot provide it in a generic way. Simple mappings like action X is bound to key Y are just the beginning; users can specify their own custom actions over which I have no control; and different actions can be combined with logical operators.

Another problem is also that custom actions are packed into std::function, which doesn't allow to extract the original functor in the general case. So I would have to store the events/real-time conditions separately in each action. But I don't know if it's even possible in all situations...

I think this should be possible, but not with your structure because you packed your action into std::function.
But if you do the inverse I think this is possible. (pack the functions into your actions)

Your not forced to pack only std::function but also basic function pointers (if you really need to get the pointer), and the functions parameters into a tuple. (if you want to call to function with different object's parameters for the serialisation)

I think you can do serialisation with that. :P

And if you want to use inheritance you can define an interface (by example ActionListener) and some function to redefine, you create an object of your interface, you link it to your action and you call the function on the interface.

I think this is how java does.
« Last Edit: June 07, 2014, 11:45:49 pm by Lolilolight »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Thor 2.0
« Reply #357 on: June 08, 2014, 01:35:07 pm »
Indeed an interesting trick, Jesper :)

It comes at a high cost though. And generally, I go with the C++ philosophy and don't want to enforce correct usage, but rather encourage it. There will always be unintended uses... as long as I show how the API is supposed to be used, that should be enough.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Thor 2.0
« Reply #358 on: June 09, 2014, 12:35:47 am »
Right. I wasn't really advocating that you use it. I was just showing that you could do it  ;)

tyrion

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Thor 2.0
« Reply #359 on: August 26, 2014, 02:02:14 am »
Hi,

after a long time i have a new problem with thor, that i couldnt solve till now ....
I am trying to push an event manually in code:
      
void init()
{
thor::ActionMap<InputCmd::MyAction>::CallbackSystem system;
thor::ActionMap<InputCmd::MyAction> mMyActionmap;

mMyActionmap[InputCmd::KeyPressed] = Action(Event::KeyPressed);
mMyActionmap[InputCmd::Forward] = Action(toKeyboardKey("Key_W"), Action::Hold); //hold Action
       
system.connect(InputCmd::KeyPressed, ActionHandler(this));
system.connect(InputCmd::Forward, std::bind(&Controller::move, this, InputCmd::Forward));

}

void update()
{
mMyActionmap.update(*mView.mWindow);

// Forward actions to callbacks: Invokes onResize() in case of Event::Resized events
mMyActionmap.invokeCallbacks(system, mView.mWindow);
}

void manualEvent()
{
sf::Event eventTest;
eventTest.type = sf::Event::EventType::KeyPressed;
eventTest.key.code = sf::Keyboard::Key::W;
mMyActionmap.pushEvent(eventTest); //push event here
}

 

what works:
- pressing and holding the key "K" in the window triggers the "Forward" event
- calling manualEvent()  and change Action::Hold to Action::PressOnce fires the "Forward" event

what doesn't work:
- calling manualEvent() with Action::Hold doesn't trigger the "Forward" event, just the "Keypressed" event

Shouldn't the hold event fire, if the "keypressed" event is fired, without keyreleased?
do i miss somthing?