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

Author Topic: Different behaviours on Linux/Windows  (Read 2920 times)

0 Members and 1 Guest are viewing this topic.

smash

  • Newbie
  • *
  • Posts: 6
    • View Profile
Different behaviours on Linux/Windows
« on: April 05, 2010, 01:38:08 pm »
Hallo everybody :)

A started to create a game with SFML in c++, a space shooter.

Here is the Code: space-game.zip

So far I have a player (controlling with arrow-keys) weapons for this player (switching with L-Controll) an of course fireing this weapons (with space).

Later there have to be enemies and bosses, highscores etc...

I developed this game on linux (ubuntu 32 bit) and there the game is working really great on linux.

If i put this code on my Windows XP PC and build the game there the behavior of the game is different.

Instead of 50 Bullets I got only 38 there and instead of 10 Missiles I got only 4.

On both (windows an linux) I use g++ and SFML 1.5


Here is the Win32 Debug Executable (with gfx, ready to run): space-game-bin.zip
Here you could see my Problem and you are able to Debug it..


What did I wrong? Did I make a misstake with the memory and the pointers?? (if yes, why its running perfect on linux?)

I hope someone could help me :)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Different behaviours on Linux/Windows
« Reply #1 on: April 05, 2010, 05:21:15 pm »
I don't think the error is related to SFML. Probably, you produce undefined behaviour in your application. When you rely on something that isn't guaranteed by the C++ standard, results may be different on distinct compilers/platforms.

As already stated here, you should either try to debug it yourself or show a minimal, error-related example directly inside the [code]-tags of the forum. Not everyone feels like scouring a lot of unknown code for mistakes. ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

smash

  • Newbie
  • *
  • Posts: 6
    • View Profile
Different behaviours on Linux/Windows
« Reply #2 on: April 05, 2010, 06:06:12 pm »
If I would know, where the error related code is, I would fix it...

On Linux it's working on Windows it's not working... I dont know which code lines have errors...


greets

Osbios

  • Newbie
  • *
  • Posts: 23
    • View Profile
Different behaviours on Linux/Windows
« Reply #3 on: April 05, 2010, 07:12:14 pm »
OK, I just was trying to get better at reading alien code (Code that is not from me or not older then some hours :P )

This is not exacly a problem you can expect other people to solve for you. Because obviously it has nothing to do with SFML.

Here is the problem:
Code: [Select]
void Weapon::Update(void)
{
    for(unsigned int i = 0; i < Projectiles.size(); i++)
    {
if(Projectiles[i]->getYPos() < 0)
{
delete Projectiles[i];
Projectiles.erase(Projectiles.begin()+i);
break;
}
Projectiles[i]->Update();
    }
}


You update projectiles that you did not even fire!
And because the position variables are not set until you fire them there are basicly random. So Projectiles->getYPos() < 0 is randomly true and will remove the projectiles.

This code will do it:

Code: [Select]
void Weapon::Update(void)
{
    for(unsigned int i = 0; i < Projectiles.size(); i++)
    {
if(Projectiles[i]->isShot())
{
if(Projectiles[i]->getYPos() < 0)
{
delete Projectiles[i];
Projectiles.erase(Projectiles.begin()+i);
break;
}
Projectiles[i]->Update();
        }
    }
}


But one simple question: Why do you create objects for projectiles that are not fired? Why do you not just use a simple integer to hold the numbers of projectiles you have and create projectile objects only if you fire them?

smash

  • Newbie
  • *
  • Posts: 6
    • View Profile
Different behaviours on Linux/Windows
« Reply #4 on: April 05, 2010, 07:46:14 pm »
I love you :)

thank you very much!!

yeah of course it's no SFML, problem... thats the reason why I post it also at a c++ community.

and... I dont know, why I am creating the projectiles, before I shoot them... it was my first idea and it was working pretty good.

i think i will use an integer to store the no. of bullets...

thank you for your great help and this idea!


greets

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Different behaviours on Linux/Windows
« Reply #5 on: April 05, 2010, 09:23:40 pm »
You could also use the STL containers to store multiple objects of the same type. They count the number of elements automatically. They do a lot of work for you, for example you don't have to worry about new and delete anymore. Here is a very good german introduction.

By the way, you could tell the guys at www.c-plusplus.de that your problem is solved. ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: