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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - ingwik

Pages: 1 2 [3] 4 5
31
SFML projects / Nebula Frontiers - MMO Space Combat/Trading
« on: January 31, 2012, 08:51:47 pm »
It looks interesting enough. A good game is not based on fancy graphics but on enjoyable ideas. One question though, the "bubbles" emitting from the ship instead of smoke, is that an animation or some kind of particle generation?

32
General / 3 keys ok, but 4 halts the input. Why? (Solved)
« on: January 31, 2012, 08:35:35 pm »
That link to Microsoft really explained keyboard ghosting well, and the little applet is valuable for the pupils since they can check what combinations that do work on their keyboards and wich one that don't during the testing phase.  Later, it will be network games anyway.

Thanks a lot for the help.

33
General / 3 keys ok, but 4 halts the input. Why? (Solved)
« on: January 31, 2012, 11:42:04 am »
We are using HP proBook 6450b laptops with Win 7 Enterprise 64, VC++ 2010 express and sfml 1.6

So, we have made some small games this week and to make them two player games, we're sitting two at each laptop. One person use keyboard keys A, S, W and X to control a player, the other use the arrow keys. Both players sprites move around in the same render window.

The problem is that if the two persons who play are using two keys or more simultaneously, the player that is last to hit two fail and no key input is read into the game (both want tomove to NW at the same time for instance). If one player use two keys at once (going NW for instance), and the other just one (going west for instance), no problem arise.

Is there a way to overcome this problem so both players can use several keys at once? Is the issue sfml or hardware based? Does it depend on what keys we use for the game?

34
General / How do I create speed=0 when no key is pressed? (SOLVED)
« on: January 30, 2012, 11:42:35 am »
Thanks, I didn't know it was a bool value. Using:

Code: [Select]
if (!App.GetInput().IsKeyDown(sf::Key::Down))
 {std::cout << "Down is not pressed " << std::endl;}


works as a charm. Topic solved.

35
General / How do I create speed=0 when no key is pressed? (SOLVED)
« on: January 29, 2012, 12:20:33 pm »
Environment: Win 7 Enterprise 64, SFML 1.6, Visual C++ express 2010
I need some help to solve this problem, or to give me some ideas how to move on.

I have a platform game, just a test bed and without animations. In the code I have a small class for players, and in the class I store speed in X and Y as doubles: dspeed_X and dspeed_Y and a sprite. I create a player at program start named ball1.

When I hit the L/R arrow keys, I have this code to accelerate the ball:

Code: [Select]
               if (App.GetInput().IsKeyDown(sf::Key::Left)) //Move left
                {
                        if (ball1.dspeed_X > -0.1)
                        {ball1.dspeed_X = -0.1;}
                        else
                        (ball1.dspeed_X = (ball1.dspeed_X - (-ball1.dspeed_X * ElapsedTime))/2);
                }


       if (App.GetInput().IsKeyDown(sf::Key::Right))  //Move right
                {
                    if (ball1.dspeed_X < 0.1)
                        {ball1.dspeed_X = 0.1;}
                        else
                        (ball1.dspeed_X = (boll1.dspeed_X + (boll1.dspeed_X * ElapsedTime))/2);
                }


Further down in the code, I add the movement (dspeed_Y  is calculated depending on if the sprite is standing on firm ground or not).

Code: [Select]
ball1.sprite.Move(ball1.dspeed_X, ball1.dspeed_Y); //Move the ball


 The problem is that I want the ball to stand still, preferably decelerate/continually loose speed until it reach 0,  when I don't press any key at all, and I just can't figure out how to do it. If I make a Windows program, I  can use key down and key up commands, but SFML seems to only have key down.

So the question is:
How do I do to move a sprite with the arrow keys, that loose speed and finally get 0 speed when no key at all is pressed?

36
General / How to release memory when deleting a player class instance?
« on: January 08, 2012, 01:20:04 pm »
Everything works now. But I'm still puzzled by some facts:

I forgot to add #include <vector> but everything works anyway.

I don't send the image to the class instance by pointer or reference, just:
Code: [Select]
sf::Image Image;
if (!Image.LoadFromFile("grenade.png")) //load picture            
  std::cout << "Can't find picture: grenade.png " << std::endl;  
And at creation:
Code: [Select]
vGrenadelist.push_back( Panzergrenade(1.0f,Tank1.sprite.GetRotation(), true) );
vGrenadelist.at(grenade_i).Sprite.SetImage(Image);
grenade_i++;


The picture works just fine.

37
General / How to release memory when deleting a player class instance?
« on: January 08, 2012, 11:24:17 am »
Quote from: "Nexus"
You must make sure that the sf::Image referenced by the sf::Sprite remains alive. It is better not to store the sf::Image inside the grenade class. Create a single image and share it for multiple sprites.


I always thought keeping image and sprite inside the same class was a sure way to keep them connected and avoiding wite squares (*sighs and walks back to the drawing table*).

38
General / How to release memory when deleting a player class instance?
« on: January 08, 2012, 10:55:17 am »
I have changed all code now so it use vectors instead of arrays. It works about the same, but the graphic is just white squares now. Both the pictures for the grenades and the pictures for the explosions.  I can't figure out why.

The class declaration is the same.

The grenade vector:
Code: [Select]
std::vector<Panzergrenade> vGrenadelist;

Creation:
Code: [Select]
vGrenadelist.push_back( Panzergrenade(1.0f, Tank1.sprite.GetRotation(), true) );

Positioning it below the tank
Code: [Select]
vGrenadelist.at(grenade_i).Sprite.SetPosition(Tank1.sprite.GetPosition().x,Tank1.sprite.GetPosition().y);

I won't get an error message showing that the program is unable to load the graphic, I just get a white square instead of the grenade.

39
General / How to release memory when deleting a player class instance?
« on: January 07, 2012, 05:02:05 pm »
Thanks Nexus, I appreciate your help here.

So, to get it stright, I've looked up vector and it seems good:
http://www.cplusplus.com/reference/stl/vector/

Three question, if you or someone else knows:
-What makes vector better than list or deque when it comes to store tankgrenade class instances?
-Is it possible to delete certain class instance in a vector? (since it seems to be impossible with an array).
-Is it possible to delete a complete vector with all its class instances when the program ends so the memory can be returned (since it seems to be impossible with an array).

Quote from: "Nexus"

By the way, you should make your attributes private, use the constructor initializer list and a uniform naming convention ;)


It's public just too keep down the amount of errors, making debugging easier.

What do you mean with "use the constructor initializer"? I 've read about it but never understood why it's a better way.

Uniform naming convention? Like this? http://geosoft.no/development/cppstyle.html#Naming Conventions I'm trying to understand C++ right now so naming convention is not on the priority list, but you're right. I'm a bit sloppy with it.

40
General / How to release memory when deleting a player class instance?
« on: January 07, 2012, 02:42:41 pm »
This is a problem connected to C++, I think, but I can't find a proper solution for it. Best I've found this far is from here:
http://www.cprogramming.com/tutorial/dynamic_memory_allocation.html
but it doesn't work for me.

I have a class named panzergrenade. Whenever a tank in the game shoot a grenade, I create a new grenade, put it in an array and have it live in the game until it hits something

When it hit, I make the grenade invisible and create another class instance which is an explosion, added to another array, at the very same place.

When the explosion start, I want to delete the panzergrenade to make room in the array for new ones and to retain memory, same for the explosion when it has finished exploding, but I get a debug error doing that.

Code: [Select]
class Panzergrenade
{
public :
Panzergrenade(double  out_velocity, double out_angel, bool out_is_drawable); //construction
~Panzergrenade(){};
double target_x;
double target_y;
double velocity;
double angel;
bool is_drawable;
sf::Sprite Sprite; // sprite for the grenade
sf::Image Image; //imagefile, just an image and no spritesheet
};
Panzergrenade::Panzergrenade(double  out_velocity, double out_angel, bool out_is_drawable);
{
if (!Image.LoadFromFile("grenade.png")) //load picture    
  {        
  std::cout << "Can't find picture: grenade.png " << std::endl;  
  }
velocity=out_velocity;
angel=out_angel;
is_drawable = out_is_drawable;
Sprite.SetRotation(angel);//Give the same angel as the tank when creatde
Sprite.SetImage(Image); //Give correct picture to the class
}


//Array to save the grenades in, no problem there
Code: [Select]
     
       Panzergrenade *grenadelist[100];//max 100 grenades in game
       int grenade_i=0; //0-100 shows where we are in the list
       int si =0; //counter 0-100 when checking items in list


And then, at creation:
Code: [Select]

grenadelist[grenade_i] =  new Panzergrenade(1.0f,tank1.sprite.GetRotation(), true);
grenade_i++;

//velocity / what tank the rotation is copied from / if it should be drawn
 

What I do right now, when a grenade hit, is that I just set the velocity to 0 and put is_drawable=false.
The grenade exists but it isn't shown anymore. An explosion is created on top of the invisible grenade and is also set to invisible after the explosion is finished.

This works fine for 100 grenades, the problem start to come if I want to have more than 100 grenades.
If I reset grenade_i to 0 and try to add a new grenade on the occupide place, no grenade is created at all. No error messages, nothing.
I assume this could create a horrible memory leak if it actually worked though, since the first created grenade would loose it's pointer adress and  become impossible to find for deletion.

If I instead of making the grenade invisible tries to delete it:
Code: [Select]
delete [] grenadelist[si];
The program compiles but when the actual deletion is about take place, the program crasches with:

Code: [Select]

Debug Assertion Failed!
Expression:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

(I've also tried:
Code: [Select]
delete [si] grenadelist;to no avail, it crasches too).

If I try to delete the whole list when closing down the program with:
Code: [Select]
delete [] grenadelist;
I get another error:

Code: [Select]

Debug Assertion Failed!
Expression:_CrtIsValidHeapPointer(pUserData)


So the questions are:
    *How am I supposed to delete an item in an array to free the memory and make it possible to save a new class instance in it's place?
    *How do I delete a full list of class instances put in an array when the program ends to release memory in a nice way?
    *If I fill an array, how do I do if I want to reuse a place in the array for a new class instance? (starting from 0 again)

41
Audio / How to handle many sounds simultaneously?
« on: January 06, 2012, 07:30:13 pm »
I have a general question about handling sounds in SFML games. My first thoughts about it began when I made a small game/test bed with a tank that was shooting a lot of bullets, actually the idea was to always have about 5 bullets in the air all the time. I have two buffers and two sounds, one for each buffer. When I hit the space key I shoot a grenade with my tank and a shoot-sound is heard. It works just perfect but that happens among the events in the program.

Further down inside the game loop, I have a code that check if the grenade has hit a wall and if so; the grenade disappears and another explosion sound, connected to the second soundbuffer, is supposed to be heard by this code:
Code: [Select]
explosionsound.Play();    
This doesn't work, instead, a buzzing sound as from a bug is heard so I assume it means that the sound is re-started over and over again, 60 times/sec and that is the reason for the peculiar sound.

Another solution to that problem that I found in the forum is adding a check:
Code: [Select]
explosionsound.Play();
while(explosionsound.GetStatus() == sf::Sound::Status::Playing) {}


Now, it explodes, but it continues to explode over and over again, sounds like a loop even though I haven't made it looping and keeps sounding even if I only shoot a single shot. And the game freeze to a halt. Something goes wrong when putting the Play() commando inside the game loop, compared to connecting it to an event where everything works as expected. It's obvvious that I'm doing something wrong when I try to play these sounds inside the game loop.

This made me wonder, and I have read through a lot of different solution in the forum; what is the best way to handle multiple explosion sounds in SFML?

Solution one:
I have one sound buffer with the explosion sound and then I make 20 sounds, seems that no more than that should be heard at the same time anyway. I have an iterator that check where I am 1-20 and if the game plays a sopund I just check if a sound is playing or not with, for instance sound number 10:
Code: [Select]
if(explosionsound10.GetStatus() != sf::Sound::Status::Playing)
 { explosionsound10.Play();}

A safe but cluttered way with no memory leaks.

Solution two
I use new and delete. I have the same buffer created but instead, I make a new sound each time I need to play the sound of an explosion.
Code: [Select]
sf::Sound *explosionsound10 = new sf::Sound(soundbuffer_with_explosionsoundfile);
 explosionsound10->Play();
 while(explosionsound10->GetStatus() == sf::Sound::Status::Playing) {}


I keep track over the amont of sounds created and when the game ends, I delete all created sounds to avoid memory leaks. One problem with this is that I think SFML can hold 512 sounds, and I assume that means trouble if more than 512 grenades are shot during a gaming session.

Finally, is there a third solution that I haven't thought about? How are other game creators handling many sounds played simultaneously inside the game loop?

42
General / Precompiled SFML 1.6 for 64 Bit Windows
« on: November 27, 2011, 07:26:22 pm »
I've gone through that:
http://www.sfml-dev.org/forum/viewtopic.php?t=6036

You can find my compiled SFML 1.6 static libraries for Win7 64 bit, vc++ 2010 express here:
http://dl.dropbox.com/u/8595437/w7enterprise_64__vs2010xp_sfml16.zip

43
General / Renderwindow displayed in function disrupts the game.
« on: November 23, 2011, 09:01:50 am »
Environment: win 7Enterprise 64 bit, SFML 1.6, VC++ 2010 express

I have problems with the sf:Renderwindow in animations. The problem is that when I put an animation sequence inside a function and call the renderwindow inside the function, the animationframes are stacked on top of each other. To circumvent that I can call renderwindow.clear() between each frame, but that will disrupt the game que and delete everything in the game while the animation runs.

The code for the animation I created can be found here:
http://www.sfml-dev.org/forum/viewtopic.php?t=6282

Is there a solution to how you can make an animation inside a function and run it  without halting the game completely while the animation run? Since I see animations of explosions in other games that don't disrupt the game I know there must be a solution even though I can't figure it out myself.

44
Graphics / Animation show twice, why?
« on: November 18, 2011, 03:23:19 pm »
Your solution worked too Laurent. I realized I was counting the frames in the explosion bitmap, instead of counting time. To get the correct animation sequence, I had to do like this:


Code: [Select]

#include <iostream>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

#define SFML_STATIC //Se till så att det inte behövs extra DLL-filer
using namespace std;    // utifall att konsollen behövs för felsökning

class Explosion
{
public :
   sf::Sprite Sprite; // en per instans
};


void pang(float x, float y, Explosion &Klassinstans, sf::Image bildfilsnamn, sf::RenderWindow &App );
//Funktionen som initierar explosionen

int main (int argc, char *argv)
 { //main startar

               //ladda in bild för explosionen
          sf::Image explosionsbildmap;
                  //explosionens spritemap är 5 x 5 rutor 64x64 pixels stora
          if (!explosionsbildmap.LoadFromFile("xplosion17.png"))
    {
    cout << "Kan inte hitta bilden: xplosion17.png " << endl;
    }

          //Skapa en kopia av explosionen
          Explosion Explosionskopia;

         sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Test av explosion");
        // Skapa fönstret vi skall testa explosionen i
        while (App.IsOpened())
           { //while 1 startar

                        sf::Event Event; //kolla om mus/tangentbord används

             while (App.GetEvent(Event))
               { //while 2 börjar
                 
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                      App.Close();//avsluta programmet

if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Return))
    //Visa upp explosionen om man trycker ner return-knappen
                     pang(100, 100, Explosionskopia, explosionsbildmap, App);

                    //Slutligen visar vi upp ändringarna om och om igen många gånger i sekunden
                    App.Clear(sf::Color(0, 100, 0)); //rensa allt i fönstret och ersätt med grönfärg
                    App.Display(); //visa upp ändringarna för användaren
                 } //while 1 slutar
                          } //while 2 slutar

} //main slutar

void pang(float x, float y, Explosion &Klassinstans, sf::Image bildfilsnamn, sf::RenderWindow &App )
{
   bool avsluta=false;
        int rutorY = 0; //max 5, bildrutor i y-led
        int rutorX = 0; //max 5, bildrutor i x led
       
        int bildbredd = 64; //varje enskild rutas bredd
        int bildhojd = 64; //varje enskild rutas höjd
        sf::Clock ExplosionClock;
        //Skapa en timer för explosionen
        float visningstid = 0.01f;


        Klassinstans.Sprite.SetPosition(x,y);  
        //Placera ut den på rätt plats på spelplanen

        Klassinstans.Sprite.SetImage(bildfilsnamn);
        ExplosionClock.Reset(); //Se till så att klockan verkligen är 0

        while ( rutorY < 5) //Så länge man är på någon rad
        {//while i y-led
                while (ExplosionClock.GetElapsedTime() < (visningstid * 5)) //Så länge man är på någon bild i en rad
                { //while i x-led


if (ExplosionClock.GetElapsedTime() < visningstid && ExplosionClock.GetElapsedTime() > 0.0)
{
bildbredd = 64; //Gå till första bilden
}
else if (ExplosionClock.GetElapsedTime() < (visningstid * 2) && ExplosionClock.GetElapsedTime() > visningstid)
{
bildbredd = 128; //Gå till bild 2
}
else if (ExplosionClock.GetElapsedTime() < (visningstid * 3) && ExplosionClock.GetElapsedTime() > (visningstid * 2))
{
bildbredd = 192; //Gå till bild 3
}
else if (ExplosionClock.GetElapsedTime() < (visningstid * 4) && ExplosionClock.GetElapsedTime() > (visningstid * 3))
{
bildbredd = 256; //Gå till nästa bild
}
else if (ExplosionClock.GetElapsedTime() < (visningstid * 5) && ExplosionClock.GetElapsedTime() > (visningstid * 4))
{
bildbredd = 320; //Gå till nästa bild
}
               
Klassinstans.Sprite.SetSubRect(sf::IntRect(bildbredd-64,bildhojd-64,bildbredd,bildhojd)); //Visa  bilden
                        App.Draw(Klassinstans.Sprite);//Rita ut explosionen
                        App.Display(); //visa upp ändringarna för användaren
                     

                } //slut i x-led
ExplosionClock.Reset(); // sätter om klockan till noll
                bildhojd = bildhojd + 64;//Hoppa ner en rad
                rutorY = rutorY + 1; //Öka till nästa rad
        } //while i y-led



}//pang slutar

45
Python / What version of Python for Windows 7 64 bit?
« on: November 18, 2011, 10:43:05 am »
Thank you very much for your answer.

Why 64 bit?
Well, all students in my high school (1 to 1 laptop policy) have Win  7 enterprise edition 64 bit system, so it felt natural to "go with the flow" and not look at older technology. There is a 64 bit version of Python 2.7.2 to install in Windows  too so I figured that since Python is a scripted system, then it shouldn't be a hassle using 64 bit rather than the older 32 bit standard.

Why VC++ 2010?
VC++ 2010 express is already installed in the pupils laptops since we are using that version for SFML 1.6 for C++  now. The problem for me as a teacher is that I think Python might be easier to learn how to program with than C++. C++ has a quite high abstraction level and is a bit hard to get a grasp on. I have this thought that it might be better for the pupils to learn Python the first 6-12 months and then move on to C++ the rest of the time (it's a 2 year course in all).

What I should do then is to:
*Remove VC++ 2010 express and install VC++ 2008 express instead
*install Python 2.7.2, 32 bit version
*Download SFML 2.0
*Compile it in a 32 bit system (?) with VC++ 2008
(Is it even possible to compile for the 32 bit platform if I have a 64 bit PC to do it with?)

Pages: 1 2 [3] 4 5