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

Author Topic: Several questions regarding SFML and programming concepts  (Read 5840 times)

0 Members and 1 Guest are viewing this topic.

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Several questions regarding SFML and programming concepts
« Reply #15 on: December 23, 2010, 11:41:53 pm »
Quote from: "Laurent"
Quote
If you don't got anything against Javascript, Danes or Google, then V8 is for you

How does it interfaces with C++?


It's made in C++ and designed to be used in C++. So pretty well. Their example for how to make your own Javascript shell with it is here: http://code.google.com/p/v8/source/browse/trunk/samples/shell.cc?r=6117

Here's a guide on how to embedd it: http://code.google.com/apis/v8/embed.html

Though documentation is pretty scarce but they got doxygen commentaries on their code.

But if you want to use Javascript in a object-oriented way(using objects in the actual scripts) I recommend that you steal my object and class objects. Since the default object-oriented system in Javascript is a mess.
*EDIT* Forgot to add link: https://github.com/Groogy/Javascript-Object
All it does is implement things that we expect, like being able to call the "super" method of the current called method and so on.

Hope you don't get confused s3rius :P
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Several questions regarding SFML and programming concepts
« Reply #16 on: December 24, 2010, 12:19:20 am »
Laurent, some time ago you mentioned Lua and Python bindings in the context of your CAMP library. Have they been developped yet?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Several questions regarding SFML and programming concepts
« Reply #17 on: December 24, 2010, 07:56:32 am »
Quote
I mean that overusing and misusing the language features (most of boostisms qualify as either the former or the latter) is not the best way to program. Especially when they make so much pressure on the poor compiler as template voodoo does.

I don't think they misuse or overuse the language. But well... it's christmas and I don't feel like starting such a discussion -- we should rather focus on the OP's questions ;)

Quote
It's made in C++ and designed to be used in C++. So pretty well. Their example for how to make your own Javascript shell with it is here: http://code.google.com/p/v8/source/browse/trunk/samples/shell.cc?r=6117

Looks really cool.

Quote
Laurent, some time ago you mentioned Lua and Python bindings in the context of your CAMP library. Have they been developped yet?

The Lua binding is still in beta, but I think it's already working pretty well (you can check the "camp-lua" branch on github). There's no Python binding yet.
Laurent Gomila - SFML developer

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Several questions regarding SFML and programming concepts
« Reply #18 on: December 25, 2010, 02:36:45 pm »
Aight I'm finally going to help you out :D Hope you had a great Christmas(If you celebrate it of course, otherwise it's just I hope you had a great day :D)

Okay, so let's first establish what you want to create. What you want in your game is that the player potentially triggers something and an action is accordingly taken. That's known as an action-trigger system. The system is basically that you have a trigger object that checks if it has been triggered, you can make this as advanced as you want, if it is triggered then you tell it's action object to run. This is just scratching the surface of the system but you'll learn more if you try and fail yourself first, I'll always be here to help you again.

Anyway let's say we do it like, when something is triggered, we run the action in it's own thread. Then what if two actions are run that both want to output some kind of messages to the player? Like a message box as this this one. Then the two threads would start overriding each others text. Things like this is fixable but it gives much more headache than needed. If you want an action to wait 2 seconds but not lock the whole system then you can just simply have a trigger that takes a specific elapsed time. All it needs inside itself is a sf::Clock and a float telling it when to trigger. Looking something like this:

Code: [Select]

class TimedTrigger : public Trigger
{
public:
        bool Update(Engine &engine)
        {
                if(myClock.GetElapsedTime() > myTimeToTrigger)
                {
                        TriggerAction();
                        return true
                }
                return false;
        }
private:
        float myTimeToTrigger;
        sf::Clock myClock;
};

Why I have the Engine argument there is because I think of the Update() of being a pure virtual method in the Trigger class. And we need some object to check against if we are supposed to trigger or not. A more complex way to do this(but it removes the virtual call) is to make the trigger receive signals that give various information and make it only trigger when it get's an interesting signal like in this case maybe: "Signal.Application.Update" and take the delta time passed with the signal. If we would like to know if some character moved, then it would be: "Signal.Entity.Moved" and use the coordinates passed to check if we want to trigger. This is just an example using the same system as me of course.

Aight, Like I said, try yourself and fail and then come back and tell me what you think you made wrong(reflect a little bit) and I'll give you a more in-depth view of how the action-trigger system should be built.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

s3rius

  • Newbie
  • *
  • Posts: 21
    • View Profile
Several questions regarding SFML and programming concepts
« Reply #19 on: December 25, 2010, 11:10:20 pm »
Thanks for the response!
Yes, a trigger-action system is basically what I want. I know the concept from the editors of Warcraft and Starcraft, but didn't know they're actually called like that.

However, with the implementation like you suggested wouldn't it look somewhat like this?


Code: [Select]

void SendMessage1(){
SendToPlayer("Enemies incoming!")
}
void SpawnEnemies(){
SpawnUnit(a,b);
SpawUnit(c,d);
}
void SendMessage2(){
SendToPlayer("Oh, they arrived already!")
}
void DoEffects(){
SpawnSFX(e,f);
}

void TimedTrigger(void (*ptr)(void), fixed timeDelay);

void WhenPlayerDoesStuff(Event &event){

//When this "trigger" is triggered we want to do the following things:
//1) Display some message to the player.
//2) Spawn some additional enemies.
//3) Display another message.
//4) Then add some explosion effects
//Between every action we want 2 seconds to pass.

TimedTrigger( &SendMessage1, 2.0);
TimedTrigger( &SpawnEnemies, 4.0);
TimedTrigger( &SendMessage2, 6.0);
TimedTrigger( &DoEffects, 8.0);
}


WhenPlayerDoesStuff() would be the function called by my event handler when event X happens.
But, especially when there's a few more things happening, this is pretty inconvenient - having to create a new function for every time I want a delay.

What'd be much better and easier to modify would be something like this:

Code: [Select]

void WhenPlayerDoesStuffConvenient(Event &event){
Wait(2.0); //Or Sleep(x)
SendToPlayer("Enemies incoming!")
Wait(2.0);
SpawnUnit(a,b);
SpawUnit(c,d);
Wait(2.0);
SendToPlayer("Oh, they arrived already!")
Wait(2.0);
SpawnSFX(e,f);
}


That's why I thought about using seperate threads in the first place.

Regarding your example with the message window being overwritten:
I could just lock the message window when one thread is using it and unlock it as soon as the message has been displayed for a certain time. I could even add debug output which informs me when a thread attempted to use the message window if it's still in use. Should be easy tracking the rest :)
So that's not really the issue - and your idea wouldn't really prevent this either, unless I overlooked something.

PS:
I was thinking and I guess I could make TimedTrigger class which basically works like a queue, where I can append all orders at once and it'll execute them one after another, waiting (like your example) when a sleep order is issued.

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Several questions regarding SFML and programming concepts
« Reply #20 on: December 26, 2010, 01:14:52 am »
Quote from: "s3rius"
However, with the implementation like you suggested wouldn't it look somewhat like this?

Not really but works, I was thinking in more of the lines of classes and objects. Would make it much more advanced but simpler to extend.

Quote from: "s3rius"

That's why I thought about using seperate threads in the first place.

Regarding your example with the message window being overwritten:
I could just lock the message window when one thread is using it and unlock it as soon as the message has been displayed for a certain time. I could even add debug output which informs me when a thread attempted to use the message window if it's still in use. Should be easy tracking the rest :)
So that's not really the issue - and your idea wouldn't really prevent this either, unless I overlooked something.

Well you'll still have tons of problem, like I said, it adds more headache than solve the problem. For instance, you have to create these locks for EVERYTHING in the engine that any event/action might read, modify and so on and so on. You are bound to miss something somewhere, and the more locks you add the more likly that you create a deadlock somewhere which might even just come at what seems like random.

So more or less, it's a bad way to design any system and is a great misuse of parallel programming.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Several questions regarding SFML and programming concepts
« Reply #21 on: December 26, 2010, 01:26:07 am »
Here's a more complete example of Action-Trigger to get you going

Actions Example:
Code: [Select]

class Action
{
public:
protected:
        friend class Trigger;

        virtual void Run() = 0; /* The code to run */
};

class SpawnCreepAction : Action
{
public:
        SpawnCreepAction(CreepType &aType, sf::Vector2f aPosition);

protected:
        void Run(); /* Create the new creep */

private:
        CreepType &myType;
        sf::Vector2f aPosition;
};


Triggers Example:
Code: [Select]

class Trigger
{
public:
        Trigger(Action *anAction);
       
        virtual bool Update(Engine &anEngine) = 0;
protected:
        Action *myAction;
};

class TimedTrigger : public Trigger
{
public:
        TimedTrigger(Action * anAction, float someTime) : Trigger(anAction)
        {
                myTimeToTrigger = someTime;
        }

        bool Update(Engine &engine)
        {
                if(myClock.GetElapsedTime() > myTimeToTrigger)
                {
                        myAction->Run();
                        return true
                }
                return false;
        }
private:
        float myTimeToTrigger;
        sf::Clock myClock;
};


And in use:
Code: [Select]

SpawnCreepAction *action = new SpawnCreepAction(someCreepType, andSomePosition);
TimedTrigger *trigger = new TimedTrigger(action, 2);


Just forget everything I said about messaging/signals. That was more specific to my game engine and how I would personally do it.

This will result in less work than if you would have used threads actually. The threaded approach looks easier at a glance but will in practice be much harder to implement. The difference here is that this one isn't as straightforward and requires that you implement a system which will be capable of "creating" and "modifying" itself. Though in the long run, you'll gain more on this, both performance wise and in development.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

 

anything