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

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

0 Members and 1 Guest are viewing this topic.

s3rius

  • Newbie
  • *
  • Posts: 21
    • View Profile
Several questions regarding SFML and programming concepts
« on: December 23, 2010, 04:27:15 am »
Hey there, I've migrated to SFML not long ago and am an amateur programmer who is trying to make his own small game.

I'm doing good progress on the basic stuff, but as I'm looking forward and trying to decide how to best build my game I ran into a few things I'm insecure about.
I'd love some guidance on a few things:

1) How well does SFML handle multiple threads? Is there a limit as to how many threads can run at the same time or does it drastically impair performance, even if most threads are sleeping?

The reason why I'm asking is that I want to construct event-driven functions for certain things.
E.g. I want to be able to hook a certain function to a custom event like the player figure reaching a certain point on the map.
The called function could then send a radio message to the help, sleep for a few seconds and then send a second message. For that sleeping I'd need to thread all of these functions.
They'd generally not do a lot of stuff, as the performance-heavy chunk of the game is being taken care of in the main thread. They'd mostly serve to set-off in-game events and stuff.

2) I am already implementing a save/load function to store and read map files which contain the terrain data for each game level.
But I was wondering if there is a way to do the same for scripts.

Let's take the above example: In one level I want to send some messages to the player once he reaches a certain point. No problem. But when he plays a different level there are other things meant to happen.
The easiest way to manage this is to just create all the relevant functions for all levels in some cpp file and call them depending on the loaded level.
But in this case they'd clutter up the game.exe and I'd have to change the source code everytime I wanted to change any level's scripts.

So instead I'd like to be able to load the scripts from seperate files, like being able to dynamically load and unload cpp files (which probably won't work).
However, I have absolutely no idea how to do that.
The only thing that comes to my mind is creating a completely new small scripting language which is then read by the game and evaluated, but that's a HUGE amount of work.

3) My map has a tile-base terrain similar to a chess board with 19x25 squares. I noticed that, when drawing all these sprites, the game's frame rate goes down to about 23 (before that it was 60).
I'm running Win 7 x64 on a pretty decent computer and I updated my graphic drivers just a day ago - so at least this shouldn't be an issue.
However I can't believe that drawing a couple of small sprites is supposed to have such a high impact.
Are there any other things I can do to squeeze some better performance out of that?
Here's basically how I'm drawing it:

Code: [Select]

void draw(){
//This sprite array probably shouldn't be constructed everytime this function runs
//but only once - but I doubt that this alone is a great performance hit.
sf::Sprite sp[2] = { sf::Sprite(SomeTerrainImage),
                        sf::Sprite(SomeOtherTerrainImage) };

int i=0;

//map[][] is a char array containing the terrain type of each square.
//Depending on it's value the game should draw a different tile image.

for(short wi=0; wi < w; wi++){
for(short hi=0; hi < h; hi++){
i=map[hi][wi];
sp[i].SetPosition(hi*32.f, wi*32.f);
window->Draw(sp[i]);
}
}
}


4) Last but not least; I've asked a question in http://www.sfml-dev.org/forum/viewtopic.php?t=3718 which still baffles me. An answer to that would be great!

Many thanks in advance for some enlightenment!

~s3r

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Re: Several questions regarding SFML and programming concept
« Reply #1 on: December 23, 2010, 04:54:17 am »
Quote from: "s3rius"
1) How well does SFML handle multiple threads? Is there a limit as to how many threads can run at the same time or does it drastically impair performance, even if most threads are sleeping?

SFML doesn't handle threads for you. It implements an interface to the operating system so that we get a generic way to handle threads on each platform.

So how many threads you can have depends on if the OS have a limit for it. But for Windows and Linux, I don't think that will be any problem.

And when it comes to threads, performance and sleeping. First a sleeping thread isn't really sleeping, it just tells the operating system that I got nothing to do for x amount of milliseconds, so it got nothing to do with performance directly. And then the operating system most often prioritize some other threads to run. And then the performance, it depends on the number of cores in the platform and how the threads "interface" with each others. For instance, a computer with 4 cores will never run more than 4 threads at the same time (unless it's Intel hyper-threading then it's 8 I think), so no matter how many threads you throw at it, it won't go faster, it will actually go slower. And if you share common data between the threads then in order to make use of the data safe you will have to make the threads wait on each others. And so on and so on.

Quote from: "s3rius"

The reason why I'm asking is that I want to construct event-driven functions for certain things.
E.g. I want to be able to hook a certain function to a custom event like the player figure reaching a certain point on the map.
The called function could then send a radio message to the help, sleep for a few seconds and then send a second message. For that sleeping I'd need to thread all of these functions.
They'd generally not do a lot of stuff, as the performance-heavy chunk of the game is being taken care of in the main thread. They'd mostly serve to set-off in-game events and stuff.

That's a very bad way to do it and is absolutely not suited for being put in an own thread. You're making it much more complicated than the problem actually is. I'll give you an example for how to do it properly and why it's bad idea to thread it and help you out with number 2 and 3(if nobody beats me to it) when I wake up. The clock is 04:53 here and I need to sleep. :)
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Several questions regarding SFML and programming concepts
« Reply #2 on: December 23, 2010, 08:05:39 am »
I leave 1) to Groogy, but here is my opinion on the last two questions.

2) Using scripts is a very good idea, but you don't need to create your own language, just use one of the several that are available. The two most popular are Lua and Python. You'll then find C++ APIs that help you (more or less easily) communicate between C++ and the script language. The easiest (and which I recommend strongly) is Luabind (for Lua) and boost.python (for Python). They are both similar, the former is based on the latter.

3) Are you running in release mode, with all optimizations enabled?
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 #3 on: December 23, 2010, 05:24:39 pm »
I'll have to help you after christmas. I thought it was the 20th yesterday but apparently it's the 23th. Here in Sweden we celebrate Christmas on Christmas Eve, so I got a lot to do and can't help you until around 25-26th.

Hope you can wait. And merry Christmas everyone!
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Silvah

  • Guest
Several questions regarding SFML and programming concepts
« Reply #4 on: December 23, 2010, 08:39:18 pm »
Quote from: "Laurent"
The easiest (and which I recommend strongly) is Luabind (for Lua) and boost.python (for Python).
Yeah! But... there are people who don't have an eternity or two only to wait for the code to compile. ;)

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 #5 on: December 23, 2010, 08:52:03 pm »
Quote from: "Silvah"
Quote from: "Laurent"
The easiest (and which I recommend strongly) is Luabind (for Lua) and boost.python (for Python).
Yeah! But... there are people who don't have an eternity or two only to wait for the code to compile. ;)


I use Google's V8 for my game engine.... :twisted:
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Several questions regarding SFML and programming concepts
« Reply #6 on: December 23, 2010, 09:44:07 pm »
Quote
Yeah! But... there are people who don't have an eternity or two only to wait for the code to compile

I prefer spending time waiting for the compiler to generate the code, rather than writing it myself ;)
Laurent Gomila - SFML developer

Silvah

  • Guest
Several questions regarding SFML and programming concepts
« Reply #7 on: December 23, 2010, 10:23:43 pm »
That would be good, if the compilation weren't sooooooo slooooooow. Sure, it's C++, the code compiles slowly anyway, but all these boostisms make it compile incredibly slow even by C++ standards. I'd rather use some other library that does more or less the same thing without slowing down the compilation by several orders of magnitude.

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 #8 on: December 23, 2010, 10:36:20 pm »
If you don't got anything against Javascript, Danes or Google, then V8 is for you :D
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Several questions regarding SFML and programming concepts
« Reply #9 on: December 23, 2010, 10:40:56 pm »
Quote
That would be good, if the compilation weren't sooooooo slooooooow. Sure, it's C++, the code compiles slowly anyway, but all these boostisms make it compile incredibly slow even by C++ standards. I'd rather use some other library that does more or less the same thing without slowing down the compilation by several orders of magnitude.

It's not slow because it's boost ;)
It's slow because it makes the compiler create the code for you (heavy use of template metaprogramming). Faster would mean less code generated, which means more code written manually. You can't have both.

Moreover, class bindings are usually not the kind of stuff that changes often. So you don't have to suffer from the huge compile times often.

Of course you can use the plain C API of the script language, but then be prepared to write a huge amount of non-generic of low-level code.
Laurent Gomila - SFML developer

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Several questions regarding SFML and programming concepts
« Reply #10 on: December 23, 2010, 10:41:21 pm »
Quote
If you don't got anything against Javascript, Danes or Google, then V8 is for you

How does it interfaces with C++?
Laurent Gomila - SFML developer

Silvah

  • Guest
Several questions regarding SFML and programming concepts
« Reply #11 on: December 23, 2010, 10:49:43 pm »
Quote from: "Laurent"
It's not slow because it's boost ;)
It's slow because it makes the compiler create the code for you (heavy use of template metaprogramming).
Heavy use of templates is exactly one of these boostisms I'm talking about. I call them that way, because boost is perhaps the most widely-known library that makes use of them. Even when used by a library that has nothing to do with boost, they're still called boostisms. ;)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Several questions regarding SFML and programming concepts
« Reply #12 on: December 23, 2010, 11:03:22 pm »
Quote
Heavy use of templates is exactly one of these boostisms I'm talking about. I call them that way, because boost is perhaps the most widely-known library that makes use of them. Even when used by a library that has nothing to do with boost, they're still called boostisms.

Ok, and I agree. But do you mean that they are useless? Or at least, that one could write a similar library with much less templates?
Laurent Gomila - SFML developer

s3rius

  • Newbie
  • *
  • Posts: 21
    • View Profile
Several questions regarding SFML and programming concepts
« Reply #13 on: December 23, 2010, 11:03:52 pm »
Quote from: "Groogy"
I'll have to help you after christmas. I thought it was the 20th yesterday but apparently it's the 23th. Here in Sweden we celebrate Christmas on Christmas Eve, so I got a lot to do and can't help you until around 25-26th.

Hope you can wait. And merry Christmas everyone!


No problem! I appreciate that you take the time to help me at all :)

About my second question:
I'll go with lua I think. I won't use LuaBind or any other library for now since I don't think the gain is worth it for now. I only need the external scripts for rather basic stuff.

About 3:
Nevermiiind~ with all optimizations I get the full 60fps.

Could someone take a look at question 4? Not a huge problem but it bugs me.

Silvah

  • Guest
Several questions regarding SFML and programming concepts
« Reply #14 on: December 23, 2010, 11:32:01 pm »
Quote from: "Laurent"
But do you mean that they are useless?
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.

Quote from: "Laurent"
Or at least, that one could write a similar library with much less templates?
It depends. How much similar the new library should be to the library in question? Drop-in replacement probably would be very hard, if not impossible, but a thing that just lets me use C++ functions/classes as if they were Lua/Python ones and vice versa without requiring me to write boring details certainly is feasible.

 

anything