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

Author Topic: Help on Game Peformance  (Read 6401 times)

0 Members and 2 Guests are viewing this topic.

lerio

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Help on Game Peformance
« on: September 02, 2014, 09:54:25 pm »
Hello!

I'm relatively new in c++ and SFML, last week i've studied a lot and managed to make this simple megaman-like platformer engine, but my friends are having a little issue with the FPS and the game doesn't have almost nothing, just animations, gravity and collisions. The game is set to run at 60fps, but my friend is getting 25~30 fps at his pc, i get 63 fps in mine, but the game is so simple that i can't understand why would someone get 30fps...

the game have only 2 threads at this stage, one for animations, and other for the rest. is this too few for a game?

I've uploaded the source code (with VS2012 build), there's an .exe at the release folder too, so you can test it. would you guys be kind to take a look and give me some advices?

commands are, for now: arrows to move, space to jump, "A" to shoot, "F1" to see the coordinates grid and player hitbox.

https://dl.dropboxusercontent.com/u/9264421/SFML.zip
« Last Edit: September 02, 2014, 09:56:06 pm by lerio »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Help on Game Peformance
« Reply #1 on: September 02, 2014, 10:04:52 pm »
Regarding threads: one would probably be enough. More just make your life complicated and difficult.
As for performance: use a profiler to figure out what's spending CPU time, only then optimize. Also make sure you are doing an optimized release build and not a debug build (the performance difference can be quite drastic).

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Help on Game Peformance
« Reply #2 on: September 02, 2014, 10:09:22 pm »
Quote
the game have only 2 threads at this stage, one for animations, and other for the rest. is this too few for a game?

Really, please don't make me repeat myself....

http://en.sfml-dev.org/forums/index.php?topic=16260.msg116485#msg116485
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Help on Game Peformance
« Reply #3 on: September 02, 2014, 10:20:39 pm »
You should get and run a profiler.
I was going to run perf (a Linux specific performance analysis tool) and see if I can find something easily that way but you made your code use non standard stuff (resources) and I'm not going to read it whole or port it to use normal plain files, especially since I don't know Portuguese. :-\

Quote
the game have only 2 threads at this stage, one for animations, and other for the rest. is this too few for a game?
It's too much for your game and for you ('relatively new in c++').
Back to C++ gamedev with SFML in May 2023

MW2TopTenWORLD

  • Newbie
  • *
  • Posts: 23
    • View Profile
    • Email
Re: Help on Game Peformance
« Reply #4 on: September 02, 2014, 10:28:25 pm »
I get 60 fps with it so I would assume it's a problem with your friends PC or with his graphics card driver...
Notice that I had a super simple game and I was getting 2fps then I upddated my graphics card and if I'm doing something "heavy" it goes to 30fps, something simple about 50-60fps...

btw I'm Portuguese as well :) (from Portugal not from Brasil)

AFS

  • Full Member
  • ***
  • Posts: 115
    • View Profile
Re: Help on Game Peformance
« Reply #5 on: September 03, 2014, 12:37:40 am »
I have nothing to add besides saying that I got 50 FPS tops with some slowdowns to 30, which is very slow for my computer, so yeah, there's definitely something wrong.

As other people said, use a profiler to discover which part of the program is provoking the slowdowns, or just disable parts of the game until you discover the source of the problem.

And yes, two threads for such a simple game is way too much. Avoid threads if possible.

lerio

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: Help on Game Peformance
« Reply #6 on: September 03, 2014, 10:41:20 pm »
Well, i wanted to use 2 loops as a personal choise, one for animations and other for the game. I think it's better for me to use it and its a good threading training. Personally, i think multi-threading are amazing tools and i don't see a reason to avoid it.

I found the solution, the second loop thread was running at full speed, i just slowed it down and now everybody is getting 60fps.

Thank you guys for the profiler tips, it helped a lot.
« Last Edit: September 03, 2014, 10:43:48 pm by lerio »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Help on Game Peformance
« Reply #7 on: September 04, 2014, 02:44:16 am »
Whatever... do as you wish.... the person that has no multithreading experience knows better than everyone else... because we obviously know nothing when we tell you something.....  good luck learning anything when you use personal choice as a reason to blatantly ignore advice from people.

Quote
the second loop thread was running at full speed, i just slowed it down and now everybody is getting 60fps.

It is highly unlikely the true nature of the problem was because the other thread wasn't yielding processor time, this most likely is just a fix of the manifested issue, not the root cause.

Quote
Personally, i think multi-threading are amazing tools and i don't see a reason to avoid it.

And screwdrivers are also amazing for putting a nail into a piece of wood.
« Last Edit: September 04, 2014, 02:58:23 am by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

lerio

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: Help on Game Peformance
« Reply #8 on: September 04, 2014, 04:45:04 am »
Whatever... do as you wish.... the person that has no multithreading experience knows better than everyone else... because we obviously know nothing when we tell you something.....  good luck learning anything when you use personal choice as a reason to blatantly ignore advice from people.

Quote
the second loop thread was running at full speed, i just slowed it down and now everybody is getting 60fps.

It is highly unlikely the true nature of the problem was because the other thread wasn't yielding processor time, this most likely is just a fix of the manifested issue, not the root cause.

Quote
Personally, i think multi-threading are amazing tools and i don't see a reason to avoid it.

And screwdrivers are also amazing for putting a nail into a piece of wood.

You're being rude for no reason, i'm clearly saying that i want to learn it, if i don't use it, i will never learn.
If you think that i'll be better without it, you're probably right, but i still want to use it and learn. I'm not saying that i know better than everyone else, i'm saying that use it is my personal choice, and i think that multithreading is a good tool, i want to learn it, excuse me and please don't take it wrongly.

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Help on Game Peformance
« Reply #9 on: September 04, 2014, 05:30:28 am »
It is nice to see that you want to learn about the possibilities of multi-threading, but you also have to think of a good scenario to learn it in. Multi-threading can make sense, but not in every situation. Your game is not one of them.

At universities, the hardest part of teaching students new concepts is finding examples of where they can be used efficiently. This is also very true of multi-threading as well. Often, I find that the practice scenarios that are conceived are far too synthetic to show any real benefit of multi-threading, but I also understand that coming up with a meaningful task for students to work on for the duration of a week is also a daunting task. A sacrifice had to be made, and any good lecturer will also explain this. The worst that can happen is that one learns how to employ multi-threading, but not where it should be employed. This gives students a false sense of security when they make such decisions, often leading to sub-optimally designed code, because "they learned that employing advanced concepts leads to improved performance". This is normally rectified in more advanced courses that in my opinion should be mandatory, but aren't. I can only hope that these people learn from their experience that their eagerness to simultaneously employ everything they have learned can ultimately be their undoing.

Since you don't seem to have any time constraints, I would recommend coming up with separate projects each targeted at learning individual concepts. It is already enough of a learning adventure working on your own game. You don't have to mix in other concepts just because you can.

Good scenarios to employ multi-threading in generally consist of parallel independent computation that saturates a processor. I need not explain that multi-threading only really started to make sense with the advent of processors that also support it.

The parallel part should hopefully be obvious. Independent means that data of one thread should not (if at all) rely too much on data from another thread, leading to the threads not having to wait for each other. This provides near linear speedup in most cases. The last part about processor saturation is harder to understand. Many beginners tend to base their performance assessments on some task manager that shows processor utilization of the whole system. Obviously, if your code only contributes to a minority of the total (outside of your process as well) work that has to be done, running it over multiple threads won't make much of a difference. This is easily detected using profiling tools, as others have already stated, so it is very strongly recommended to profile before writing multi-threaded code. If you have background knowledge of the algorithms that you employ in your code, and their time and space complexity, it will also help you to determine if and where to optimize using multiple threads.

Based on that, I have to tell you that the best examples to learn from are those that deal with some sort of well known algorithm. Indeed many AAA games that employ multi-threading themselves only do so when executing said algorithms, which often end up being part of external libraries that were already optimized. I don't have any insider information, but I can assume that many of them also pre-maturely optimize other parts of their code and end up eating a lot of processor time through the overhead that is incurred. This is often passed on to the consumers by media as "badly optimized" or even "un-optimized" games. Sometimes they even tell consumers that it simply lies in the nature of what has to be computed that the processor requirements are so high, hoping that laypeople won't suspect any lack of effort on their side.

I know this is asking much of you, but if you really heed what I have said, and want to learn about multi-threading in an isolated environment, this list could be of interest to you. You will probably find a few algorithms you have heard of before on that list as well. Try implementing some of them, first without multi-threading, profile and then using the profiling information, optimize the hot spots. This way, you will get a feeling for the entire process from start to finish as well.

Learning is a good thing. Learning the wrong way is bad.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Strelok

  • Full Member
  • ***
  • Posts: 139
    • View Profile
    • GitHub
Re: Help on Game Peformance
« Reply #10 on: September 04, 2014, 08:40:49 am »
I'm interested, do you recommend any algorithm (of the list) in particular?

lerio

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: Help on Game Peformance
« Reply #11 on: September 04, 2014, 12:33:54 pm »
Thank you binary1248, very enlightening information.

You see, i'm really trying my best to learn, since i didn't take any courses, of course that i'll make a lot of mistakes, but i have no restrictions in exploring and i'll surely give it a try on this list.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Help on Game Peformance
« Reply #12 on: September 04, 2014, 02:56:18 pm »
Quote
You're being rude for no reason

Not rude, just calling it how it is.

Quote
i'm clearly saying that i want to learn it, if i don't use it, i will never learn.

Apparently you didn't get the screwdriver example. To make it clearer, binary1248 said the same thing in different terms.

Quote
Learning is a good thing. Learning the wrong way is bad.

Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

lerio

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: Help on Game Peformance
« Reply #13 on: September 04, 2014, 03:50:12 pm »
Quote
You're being rude for no reason

Not rude, just calling it how it is.

Quote
i'm clearly saying that i want to learn it, if i don't use it, i will never learn.

Apparently you didn't get the screwdriver example. To make it clearer, binary1248 said the same thing in different terms.

Quote
Learning is a good thing. Learning the wrong way is bad.

I got your example and i can associate it with binary1248 aswell. It doesn't mean that i'll follow it straight, but i respect it as an superior programmer friendly advice and will alway keep it in my mind. Personally, i've learned guitar the wrong way, painting the wrong way, animation the wrong way, music composition the wrong way, piano the wrong way (english the wrong way, lol) and i will most likely to learn programming the wrong way, because it's my nature to study and try everything by myself. Still, today i can make animation, music and painting, i've made a lot of mistakes, but now i don't repeat them, because i've seen them, and now i know how to handle them.

If i try to put a nail with a screwdriver at the wall, i'll probably see that this is not a good idea, but it is in my nature to give it a try first and see the problems in first hand. I'm stubborn. I hope i wasn't, but i'm.
 
Even if i read all yours and binary1248's arguments, understand and agree with it, i am still going to try multithreading at the game and learn, the hardest way, why it isn't a good idea. ( and i'll probably post my bad experiences here, so you guys can say "see, i told you!" ;) )
You'll probably think that this is a waste of time and it is really stupid by my part, and you're most likely to be right, but, as i said, it's my nature ;D.

I really appreciate your advices, i'm sorry if i'm a bit confusing or missunderstood something, my first language is not english, i speak portuguese. i needed to read binary post 3 times to fully understand it, lol;
« Last Edit: September 04, 2014, 03:55:14 pm by lerio »

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Help on Game Peformance
« Reply #14 on: September 04, 2014, 04:10:33 pm »
I'm interested, do you recommend any algorithm (of the list) in particular?
Well, here are a few I think could be interesting (and maybe even fun) to implement, and might be of use to you later on, sorted from simple to advanced:
  • Basic matrix multiplication. Everybody who paid attention during maths class should know this one. Care has to be taken to make sure the processing isn't dominated by memory accesses as opposed to the actual arithmetic.
  • Classic divide and conquer sort algorithms like quicksort and merge sort. Generally, most divide and conquer algorithms are suitable for parallelization simply due to their nature.
  • Minimum oriented bounding box could be useful e.g. for tighter mouse click detection, since SFML only makes use of axis aligned bounding boxes for convex polygons.
  • Nearest neighbour search can be useful in cases where you want to find e.g. the closest polygon to a location you clicked with the mouse.
  • Delaunay triangulation for cases where you would want to subdivide your polygons yourself if they aren't convex.
  • Monte Carlo simulations of experiments that rely on probabilistic models.
  • Ray tracing in order to generate images (and maybe even video or real-time graphics) that are more physically correct than what classical rendering would generate.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

 

anything