SFML community forums
Help => General => Topic started by: smash 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 (http://www17.zippyshare.com/v/11290692/file.html)
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 (http://www35.zippyshare.com/v/10708057/file.html)
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 :)
-
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 (http://www.c-plusplus.de/forum/viewtopic-var-t-is-264268.html), 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. ;)
-
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
-
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:
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:
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?
-
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
-
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 (http://magazin.c-plusplus.de/artikel/Aufbau%20der%20STL%20-%20Teil%201%3A%20Container) is a very good german introduction.
By the way, you could tell the guys at www.c-plusplus.de that your problem is solved. ;)