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.


Topics - ingwik

Pages: [1] 2
1
Window / Is there an easy way to create a HD movie from a game window?
« on: April 10, 2017, 02:49:51 pm »
I haven't used SFML for a long while, delving into G suite and pedagogical resources for elementary schools instead.

In all this, I thought I would do a longer instructional video in 16:9 format with different animations. Problem is that few (none?) free programs offer animated 2D capacity with correct timing. But SFML do. The most valuable part for me is the possibility to use "views" to move around upon a larger picture.

What I need to know though, is if there is any kind of reasonable easy way to catch what happens in a game window sized to 16:9 and record it in full HD?

I need to be able to do two things; putting up a video on Youtube that might be of just reasonable good quality, but also have to have a HD quality example for my colleagues and pupils. Kids these days have problems with low res films in education :)

2
Network / Easy example for client server in LAN
« on: April 16, 2016, 09:39:49 pm »
I'm a teacher, and right now I have 1:1 PC in my class. I know there is a future for games in education. Gamification makes kid learn faster, since it's fun. Even more when kids can cooperate, using more computers. Even say 24 PC's connected to a single game server in the class room. But how?

I would love to se an example in the wiki where someone better than me can make an easy example of a server with 2-3 connected players just moving a square around on the screen at the same time.

I understand all other sfml-modules fairly well by now, but the network module is still a mystery. If I had just a simple, as easy as possible, working example like this. Sending x + y coordinates from one client to the server and then to all other clients, I think I should be able to do the same thing with sprites. But as it is right now, I'm stuck to single player games. And that's a pity when sfml can accomplish so much more than most other graphical frameworks for 2D games around.

3
So, I downloaded VS 2013 Community Edition together with corresponding SFML 2.3.1 (Visual C++ 12 (2013) - 32-bit) files today. I installed it all according to the instructions in the homepage. But it doesn't work...

When I start a new console Project, I Make sure it is an empty Project (check that check box). Add a .cpp file manually. Link all files (I tried both static and dynamic) to the Project and run the small code snippet from the example.

The odd thing is that I can build the code both in debug and release mode in VS. The folders for release and debug are created inside the Projects folder. But no .exe-files are created. Thus, when I hit F5 key, I get a warning that the compiler can't find the Projects exe-file. When I hit CTRL+F5 I get a console window with:  '"c:\users\name\documents\visual studio 2013\Projects\ConsoleApplication1\Debug\ConsoleApplication1.exe"' is not recognized as an internal or external command, operable program or batch file. Press any key to continue . . .

Ah, maybe there is some kind of path problem, I thought and scanned the whole computer for .exe-files created today, but they didn't show up. Conclusion - no exe-files have been created.

Why? I relly can't tell myself. This way to start a project used to work fine in old VS 2010... The only big difference I can see is that in VS 2010, I added a .cpp file myself when I started an empty Project and named it main.cpp. In VS 2013 It names itself automatically to Source1.cpp. But I doubt that is the problem since the build works without flaws.

Has anyone any ideas? I doubt it is an sfml error but instead some kind of fault in VS.

/Ingemar

4
General discussions / What version of VS Express 2013? (Solved)
« on: July 13, 2015, 11:42:40 am »
So, after a long time, I decided to use SFML to create short movies to be used for flipped classroom in elementary school. I used SFML a couple of years ago and I haven´t found any other tool as versatile as SFML.

*So, I have a PC with W 8.1 personal 64.
*I want to use the latest binding, 2.3.1, together with Thor. (I didn't realize how useful Thor is and failed that part last time I tried SFML).
*If I choose the libraries for "Visual C++ 12 (2013) - 32-bit", can I use that with Thor?
*What free version of Visual studio express shall I install? On the homepage ( https://www.visualstudio.com/en-us/products/visual-studio-express-vs.aspx ) there are these different versions to choose from:
-Community 2013
-Express 2013 for Web
-Express 2013 for Windows
-Express 2013 for Windows Desktop
-Team Foundation Server 2013 Express

Which one of these will be free and work with the SFML library I intend to download? (Visual C++ 12 (2013) - 32-bit, if it works with Thor)

Yours sincerely:
Ingemar

5
General / SFML and win 8.1
« on: December 20, 2013, 09:39:58 pm »
I thought hit me tonight. Is it possible to make sfml-games that can be used both on a pc and a phone with win 8.1 by using common Visual studio and c++? And if not, why?

6
Network / Simple TCP connect between 2 PC's doesn't work
« on: March 18, 2012, 10:02:57 pm »
Environment: Win 7 Enterprise 64, VC++ 2010 express, SFML 1.6

I'm trying to make a simple game. A boy is running from a cow. The boy is running on the server PC and the cow is running on the client. It's a simple test to see if it's possible to run a two player game over a LAN. I've followed the tutorials but somehow, I'm stuck.

The class both inherit from (could as well has been a struct, I think):
Code: [Select]
class spelare
 {
 public:
  sf::Uint8   ID; //0=boy, 1=cow
  sf::Int32 hastighetX; //velocity, x
  sf::Int32 hastighetY; //velocity y
  sf::Sprite sprite; //Sprite with pic
 };


The packet, more or less a copy from the lesson:
Code: [Select]
sf::Packet &operator <<(sf::Packet &Packet, const spelare &C)
 {return Packet << C.ID << C.hastighetX << C.hastighetY;}

 sf::Packet &operator >>(sf::Packet &Packet, spelare &C)
 { return Packet >> C.ID >> C.hastighetX >> C.hastighetY;}


Function for a server, this function seems to work just fine.
 
Code: [Select]
void RunServer(unsigned short Port)
 {
    sf::SocketTCP Server;

    if (!Server.Listen(Port))
        return;
    std::cout << "Listen on port " << Port << ", wait for connect... " << std::endl;

    sf::IPAddress ClientAddress;
    sf::SocketTCP Client;
    Server.Accept(Client, &ClientAddress);
    std::cout << "Client connected : " << ClientAddress << std::endl;
 }


And the client, seems to work too.
Code: [Select]
void RunClient(unsigned short Port)
{
    sf::IPAddress ServerAddress;
    do
    {
        std::cout << "IP to connect to? : ";
        std::cin  >> ServerAddress;
    }
    while (!ServerAddress.IsValid());
    sf::SocketTCP Client;

    if (Client.Connect(Port, ServerAddress) != sf::Socket::Done)
        return;
    std::cout << "Connected to server " << ServerAddress << std::endl;
 }


These two functions are run once, at the startup before any windows are shown. I suppose they will be valid for the rest of the session.

Then, I have a function to send the packet with information. Since I never recieve anything, I'm not really sure if it works, but I think something is sent...

Code: [Select]
 void PlayerSend(class spelare &s)
{
    spelare C = {s.ID, s.hastighetX, s.hastighetY};
    sf::Packet RegularPacket; //Packet created
    sf::SocketTCP Client; //Socket created
    RegularPacket << C; //Packet recieve it's three values

   if (C.ID == 0)
  {std::cout << "Boy sent to client: " << std::endl;}
  else
  {std::cout << "Cow sent to client: " << std::endl;}

    if (Client.Send(RegularPacket) != sf::Socket::Done)
        return;
}


And finally, the recieving function that doesn't seem to work at all.

Code: [Select]
void PlayerRecieve(class spelare &s)
  {
  sf::Packet RegularPacket; //Packet created
  sf::SocketTCP Client;
 //Client.SetBlocking(false);
  spelare C; //Create new C
 
      if (Client.Receive(RegularPacket) != sf::Socket::Done)
   return;
 //Nothing below this point ever activates

    if (RegularPacket >> C)
    {
        if (C.ID == 0)
    {std::cout << "Boy recieved from server: " << std::endl;}
   else
    {std::cout << "Cow recieved from server: " << std::endl;}

    }
   else //No regular packet C recieved
    {std::cout << "Nothing recieved from server: " << std::endl;}
  }


The "PlayerSend" function fires every time an arrow key is pressed so it should be possible for the other PC to have the player moved.

The "PlayerRecieve" function is fired just before the cow and boy are moved. As it is now, it's possible for the client to move the cow and the server to move the boy and it seems as if the two computers are connected somehow.

I can't find what I'm doing wrong. I have followed the tutorial but obviously I have made something wrong. If someone else who is better than I can show me what might be wrong, I'll apprciate it a lot.

Yours sincerely

Ingemar

(The complete code, even though the comments are in swedish, can be found here at the bottom of the page)

7
SFML projects / Simple game showing collision detects, with source.
« on: February 08, 2012, 11:44:47 am »
This game is the first more or less complete game I’ve done. It’s produced to show two different ways of collision detect: bounding box and radius calculation. You just change one row of code by un-comment it to see how it works. As it is now, it checks the coordinates for the mouse; originally the functions checked two sprites and some remnants from that remain in the radius computing. Bear with that.

The game was created as a demonstration game in the Swedish high school, so I hope you’ll understand the names of the variables even though they aren’t English. I’ve commented as much as possible of the code, just to clarify how it works.

How do you make a game out of it?
*I used SFML 1.6, I assume you can use the same code for 2.0 without problem.
*Copy the code and paste it into an SFML project.
*Get the picture of the flags (just one) and save it in the project folder
*Compile and run
*When the name of the Nordic country or area appears, you click on the corresponding flag. If you choose wrong, or click on the background, you’ll get error points.
*A red and a green staple show how many correct, and how many erroneous clicks you’ve made. At 27 of either the game is over.

The debug version is considerable slower than the release version, so use the release version if you want to use it as a game, or change the velocity value at the move command (one place in the code) to make all move faster.

//Ingemar

Code: [Select]
//Get the picture of the flags here:
// http://www.biblioteken.fi/File/d8811b0a-b714-4911-a69f-d634b8b952a8/width/397/height/119/nordiskt_flaggor.jpg

// You’ll get different flags from the Nordic countries
// moving on the screen, click on the corresponding country - flag ,
// hit SPACE for a new country.

#include <iostream>
#include <vector>
#include <string>
#include <SFML\System.hpp>
#include <SFML\Graphics.hpp>
#include <SFML\Window.hpp>

#define SFML_STATIC //No extra DLL-files

using namespace std;

class flagga  //The flag class
{
public:
//Arraynumber = unique, hastighet = speed, flagga_x and y = coordinates
flagga (int  arraynummer, float hastighet, float flagga_x, float flagga_y);
~flagga(){};

int arraynummer; //Which country corresponds
float  hastighet; //How fast is the flag moving
float flagga_x; // Where is it in X
float flagga_y; // Where is it in Y

sf::Sprite flaggsprite; //The sprite used
};
//Pre-generated values at contruction time.
flagga::flagga (int  ut_arraynummer, float ut_hastighet, float ut_flagga_x, float ut_flagga_y)
{
       arraynummer=ut_arraynummer;
       hastighet=ut_hastighet;
       flagga_x=ut_flagga_x;
       flagga_y=ut_flagga_y;
}

//Check bounding box hit
bool Box_check_hit(float musx, float musy, sf::Sprite &maolsprite);
//Check radius hit
bool Cirkular_check_hit (float musx, float musy, sf::Sprite &maolsprite);

int main(int argc, char **argv)
{ //Main starts

/* Fill in variables within main */
double muskoordinat_x = 0.0f; //mouse x
double muskoordinat_y = 0.0f; //mouse y
unsigned int flagg_i = 0; //list item counter
int landsnummer  = 5; //What country will we get
std::string landsnamn = "Hit the swedish flag - \n[SPACE] for new flag"; //Name of the country
sf::String uttext; //Text to be printed on screen
int flaggans_hastighet = 0.0f;
int Raett = 0; //How many correct hits?
int Fel = 0; // How many incorrect hits?


/* Load the picture of all the flags */
sf::Image flaggbild; //Create an empty image holder named flaggbild
if (!flaggbild.LoadFromFile("nordiskt_flaggor.jpg")) return EXIT_FAILURE;
//Fill it with the picture: nordiskt_flaggor.jpg

std::vector<flagga> Flagglista; //Create a vector with class instances

//Add the flags with custom velocity
int r1, r2 =0;
r1 = 1.0f;
r2 = 10.0f;

flaggans_hastighet = sf::Randomizer::Random(r1, r2);
Flagglista.push_back( flagga (0, flaggans_hastighet, 400.0f, 100.0f) ); // (0=Denmark)
                           //0=Denmark, velocity, 400= xposition 100 = yposition
flaggans_hastighet = sf::Randomizer::Random(r1, r2);
Flagglista.push_back( flagga (1, flaggans_hastighet, 400.0f, 150.0f) ); //Place a new flag in the vector (1=Faroe Islands)
flaggans_hastighet = sf::Randomizer::Random(r1, r2);
 Flagglista.push_back( flagga (2, flaggans_hastighet, 400.0f, 200.0f) ); //Place a new flag in the vector (2=Iceland)
 flaggans_hastighet = sf::Randomizer::Random(r1, r2);
  Flagglista.push_back( flagga (3, flaggans_hastighet, 400.0f, 250.0f) ); //Place a new flag in the vector (3=Finland)
       flaggans_hastighet = sf::Randomizer::Random(r1, r2);
        Flagglista.push_back( flagga (4, flaggans_hastighet, 400.0f, 300.0f) ); //Place a new flag in the vector (4=Norway)
        flaggans_hastighet = sf::Randomizer::Random(r1, r2);
         Flagglista.push_back( flagga (5, flaggans_hastighet, 400.0f, 350.0f) ); //Place a new flag in the vector (5=Sweden)
         flaggans_hastighet = sf::Randomizer::Random(r1, r2);
          Flagglista.push_back( flagga (6, flaggans_hastighet, 400.0f, 400.0f) ); //Place a new flag in the vector (6=Sapmi)
          flaggans_hastighet = sf::Randomizer::Random(r1, r2);
           Flagglista.push_back( flagga (7, flaggans_hastighet, 400.0f, 450.0f) ); //Place a new flag in the vector (7=Aaland)
               flaggans_hastighet = sf::Randomizer::Random(r1, r2);
                Flagglista.push_back( flagga (8, flaggans_hastighet, 400.0f, 500.0f) ); //Place a new flag in the vector (8=Greenland)

/* Create render window that can’t be resized */
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Guess the flags of the Nordic countries", sf::Style::Close);


for (flagg_i=0; flagg_i < Flagglista.size(); flagg_i++)
  { //Walk through the list
       //Give the image to the sprite and put it on the game board
       Flagglista.at(flagg_i).flaggsprite.SetImage(flaggbild); //give the picture to the sprite

       //Choose correct part of the sheet to show.
       // Since all flags have different size and uneven positions
       // all flag coordinates must be handmade,
       // size is 70x50 for all
       if ( Flagglista.at(flagg_i).arraynummer == 0)
               Flagglista.at(flagg_i).flaggsprite.SetSubRect(sf::IntRect(4,2,74,52));

       if ( Flagglista.at(flagg_i).arraynummer == 1)
               Flagglista.at(flagg_i).flaggsprite.SetSubRect(sf::IntRect(82,2,152,52));

       if ( Flagglista.at(flagg_i).arraynummer == 2)
               Flagglista.at(flagg_i).flaggsprite.SetSubRect(sf::IntRect(155,2,225,52));

       if ( Flagglista.at(flagg_i).arraynummer == 3)
               Flagglista.at(flagg_i).flaggsprite.SetSubRect(sf::IntRect(233,2,303,52));

       if ( Flagglista.at(flagg_i).arraynummer == 4)
               Flagglista.at(flagg_i).flaggsprite.SetSubRect(sf::IntRect(322,2,392,52));

       if ( Flagglista.at(flagg_i).arraynummer == 5) //Nästa rad
               Flagglista.at(flagg_i).flaggsprite.SetSubRect(sf::IntRect(36,66,106,116));

       if ( Flagglista.at(flagg_i).arraynummer == 6)
               Flagglista.at(flagg_i).flaggsprite.SetSubRect(sf::IntRect(124,66,194,116));

       if ( Flagglista.at(flagg_i).arraynummer == 7)
               Flagglista.at(flagg_i).flaggsprite.SetSubRect(sf::IntRect(200,66,270,116));

       if ( Flagglista.at(flagg_i).arraynummer == 8)
               Flagglista.at(flagg_i).flaggsprite.SetSubRect(sf::IntRect(285,66,355,116));

       Flagglista.at(flagg_i).flaggsprite.SetPosition(Flagglista.at(flagg_i).flagga_x, Flagglista.at(flagg_i).flagga_y);
   } //End of walking through the flag list



while (App.IsOpened())
 { //while 1 start, game loop in action

      sf::Event Event; //Check if mouse or keyboard is used
      while (App.GetEvent(Event))
        { //while 2, check events
        if (Event.Type == sf::Event::Closed) //Hit [x] symbole?
        App.Close(); // close the program

        if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape)) //ESC key pressed
        App.Close(); // close the program

                 if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Space)) //SPACE tangent
                 {
 if (landsnamn == "CORRECT!" || landsnamn == "ERROR!")
  { //string doesn’t contain a name of a country
  landsnummer  = sf::Randomizer::Random(0, 8); //Välj ett land
  if (Flagglista[landsnummer].arraynummer == 0)
  landsnamn = "Denmark";
  if (Flagglista[landsnummer].arraynummer == 1)
  landsnamn = " Faroe Islands ";
  if (Flagglista[landsnummer].arraynummer == 2)
  landsnamn = "Iceland";
  if (Flagglista[landsnummer].arraynummer == 3)
  landsnamn = "Finland";
  if (Flagglista[landsnummer].arraynummer == 4)
  landsnamn = "Norway";
  if (Flagglista[landsnummer].arraynummer == 5)
  landsnamn = "Sweden";
  if (Flagglista[landsnummer].arraynummer == 6)
  landsnamn = "Sapmi";
  if (Flagglista[landsnummer].arraynummer == 7)
  landsnamn = "Aaland";
  if (Flagglista[landsnummer].arraynummer == 8)
  landsnamn = "Greenland";
  } //string doesn’t contain a name of a country
 }

     //Where did you click the mouse?
     if (Event.MouseButton.Button == sf::Mouse::Left)
        { //left mouse button
         muskoordinat_x = App.GetInput().GetMouseX();
         //What x-coordinate did we clik on?
        muskoordinat_y = App.GetInput().GetMouseY();
         //What x-coordinate did we clik on?

        for (flagg_i=0; flagg_i < Flagglista.size(); flagg_i++)
           { //Walk through the flag list and check for hits
           //Bounding box
           if (Box_check_hit(muskoordinat_x, muskoordinat_y, Flagglista.at(flagg_i).flaggsprite) == true)
         
           //Radius
           //if (Cirkular_check_hit(muskoordinat_x, muskoordinat_y, Flagglista.at(flagg_i).flaggsprite) == true)
              {//if we got a hit
               if (Flagglista.at(flagg_i).arraynummer == landsnummer)
               { //Correct guess
                landsnamn = "CORRECT!”;
                Raett = Raett + 1;                                
                } //end of correct guess
                else
                if (Flagglista.at(flagg_i).arraynummer != landsnummer)
                { //Incorrect guess
                landsnamn = "ERROR!";
                Fel = Fel + 1;
                } // end of incorrect guess
              }//end of hit    
            } //End of walking through the list

          if (landsnamn != "CORRECT!" && landsnamn != "ERROR!")
           Fel = Fel + 1; // Hit beside all flags
          } //end left mouse button

        } //End of while 2, check events

           if (Raett > 26) //Winner
                   landsnamn="YOU WON!";
           if (Fel > 26) //You loose
                    landsnamn="LOOSER!";

      uttext.SetText(landsnamn);//String with country
      uttext.SetFont(sf::Font::GetDefaultFont());
      uttext.SetSize(50);
      uttext.SetPosition(20.f, 20.f); //place the string on the game board


      /* Show the game for the player  */

    for (flagg_i=0; flagg_i < Flagglista.size(); flagg_i++)
        { //Walk through the list
        if(Flagglista.at(flagg_i).flaggsprite.GetPosition().x < 0 || Flagglista.at(flagg_i).flaggsprite.GetPosition().x > 800 - 70)
//Flag is outside the edge
         {
         Flagglista.at(flagg_i).hastighet = Flagglista.at(flagg_i).hastighet * -1; //Change direction
          }
       Flagglista.at(flagg_i).flaggsprite.Move(Flagglista.at(flagg_i).hastighet * 0.033,0); //Move the flag
          0.33 to change the velocity to a lower, float, value
        } //End of walking through the list


      App.Clear(sf::Color(100, 100, 100)); //Dark grey background

      /*Show the sprites */

      App.Draw(sf::Shape::Rectangle(0,570,10 + (Raett * 30), 580, sf::Color(0, 255, 0) )); //Green, grows with correct answers
      App.Draw(sf::Shape::Rectangle(0,585,10 + (Fel * 30), 595, sf::Color(255, 0, 0) )); //Red, grows with wrong answers or miss clicks
      //Win/loose at 27 correct/errors

       App.Draw(uttext); //Show the country name


      for (flagg_i=0; flagg_i < Flagglista.size(); flagg_i++)
      { //Walk through the list
        //Draw the flags
        App.Draw(Flagglista.at(flagg_i).flaggsprite);
      } //End of walk through the list

      App.Display(); //Show the game board or the player

} //while 1 ends, end of game loop

Flagglista.clear(); //avoid memory leaks

return 0; //last row
} //End of main

/* Function descriptions */

//BOX******************************************************************
bool Box_check_hit(float musx, float musy, sf::Sprite &maolsprite)
                   //mouse x and y, sprite is flag sprite.
// check if we clicked inside a flag
{
bool klickat_inuti = false; //return value
double maolx = maolsprite.GetPosition().x;
double maoly = maolsprite.GetPosition().y;

//Flags are 50 x 70 in size

//  inside left edge/inside right edge/inside top/inside bottom        
if ((musx > (maolx)) && (musx < maolx + 70) && musy  > maoly  && musy < maoly  + 50 )
klickat_inuti = true; //return value

return klickat_inuti;
}

//RADIus***************************************************
bool Cirkular_check_hit (float musx, float musy, sf::Sprite &maolsprite)
                         //mouse x and y, sprite is flag sprite.
//Check if we clicked inside radius (50 * 1.4……)
{
bool har_traeffat = false; //return value
double avstaond = 0.0; //distance to middle point
double traeffavstaond = 0.0; //At what distance will you hit?
double tempx; //distance in x-value
double tempy; //distance in y-value

//get the mouse coordinates
//musx and  musy are the mouse coordinates
//maolx och maoly are the flags coordinates at the center

//Re-calculate it from top left corner to center.
double maolx = maolsprite.GetPosition().x + 35;
double maoly = maolsprite.GetPosition().y + 25;

double maol_radie = (1.4142135623730950488016887242097 * 50) / 2;

//When do we hit?
traeffavstaond = maol_radie;

if (musx == maolx  && musy != maoly ) //both on same line in x
      {
      avstaond = musy - maoly;
      if (avstaond <= traeffavstaond) //within hit distance
      har_traeffat = true;
      }
else if (musy == maoly && musx != maolx ) // both on same line in y
     {
      avstaond = musx - maolx;
      if (avstaond <= traeffavstaond) // within hit distance
      har_traeffat = true;
     }
else if(musx != maolx && musy != maoly) // different places
      { //Use pythagoras theorem
      tempx = musx-maolx;
      tempy = musy-maoly;
      // Calculate hypotenusan/distance by calculating
      // square from x-distance ^2 + y-distance ^2
      avstaond = sqrt ( tempx * tempx + tempy * tempy);
      if (avstaond <= traeffavstaond) //within hit distance
      har_traeffat = true;
      } // End, using pythagoras theorem
else if (musx == maolx && musy == maoly) //The very same spot
      har_traeffat = true;

return har_traeffat;
}

8
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?

9
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?

10
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)

11
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?

12
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.

13
Python / What version of Python for Windows 7 64 bit?
« on: November 17, 2011, 09:47:08 am »
So, I am a bit curious about other implementations than SFML for c++ when teaching youngsters programming and my eyes fell on PySFML. C++ is tough to learn if you've never done any kind of programming before (and python not too tough for me to learn I hope :). But then, before I try something stupid that might spoil a lot of time for me, I have some questions that I would love to get answered:

What is best, PySFML 1.6 or PySFML 2.0? When I startwed teaching SFML for C++ I choose 1.6 but that might have been a bad move and I really don't want to repeat it.

PySFML is not a compiled language (as far as I know) so it shouldn't matter if I use a 32 bit or a 64 bit environment, or am I wrong?

Most important; what kind of Python distribution do I need to use on the computer if I have Windows 7 enterprise 64 bit. Current versions are 2.7.2 or 3.2.2. Can I use the latest or do I need to use an older version?

14
Graphics / Animation show twice, why?
« on: November 13, 2011, 01:21:57 pm »
I'm using VC++ 2010 express, Win 7 64 enterprise and SFML 1.6

I'm trying to make a simple function to show an explosion. In the end, it's supposed to be part of an explosion class. Righht now, it should fire once when the enter key is pressed, but it doesn't.

I have two errors in the code that I don't understand:
1: The function fires twice. It shouldn't but I can't find any solutuion to the problem.
2: Copyrect fail, I see the whole bitmap-map and not copyrect squares creating an animation sequence.

The picture is:
http://cdn.pimpmyspace.org/media/pms/c/b9/9e/ez/xplosion17.png


Code:
Code: [Select]
#include <iostream>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

#define SFML_STATIC //No extra DLL

using namespace std;    // to show the console


class Explosion
{
public :
    sf::Sprite Sprite; //The explosion sprite
};


void pang(float x, float y, Explosion &Klassinstans, sf::Image bildfilsnamn, sf::RenderWindow &App );
//Function to show explosion, forward declaration

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

      //load pic
 sf::Image explosionsbildmap;
   //Size of spritemap  5 x 5 squares 64x64 pixels big
 if (!explosionsbildmap.LoadFromFile("xplosion17.png"))
     {
     cout << "Can't find: xplosion17.png " << endl;
     }

 //Create a copy of the class
 Explosion Explosionskopia;



      sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Test av explosion");
     // Creating render window
         while (App.IsOpened())
            { //while 1 start



sf::Event Event; //Check if event is used

              while (App.GetEvent(Event))
                { //while 2 start
                   if (Event.Type == sf::Event::Closed)
                       App.Close();//shut down program

if (App.GetInput().IsKeyDown(sf::Key::Return))
//Show the explosion
  pang(100, 100, Explosionskopia, explosionsbildmap, App);


   
          App.Clear(sf::Color(0, 100, 0)); //Green background
          App.Display(); //Show changes


} //while 1 ends
 } //while 2 ends

 } //main ends***************************************



//Function description
void pang(float x, float y, Explosion &Klassinstans, sf::Image bildfilsnamn, sf::RenderWindow &App )
{
int rutorY = 0; //max 5, pictures y
int rutorX = 0; //max 5, pictures x

int bildbredd = 64; //width of frame
int bildhojd = 64; //height of frame
sf::Clock ExplosionClock;
//Create a timer for the explosion
float visningstid = 0.1; // 0.1 sec between each frame


Klassinstans.Sprite.SetPosition(x,y);  
//Place the explosion on the game board

Klassinstans.Sprite.SetImage(bildfilsnamn);
ExplosionClock.Reset(); //make sure the clock i 0
while ( rutorY < 5)
{
while (rutorX < 5)
{

if ((ExplosionClock.GetElapsedTime() > visningstid - 0.0) && (ExplosionClock.GetElapsedTime() < visningstid))
            Klassinstans.Sprite.SetSubRect(sf::IntRect(bildbredd-64,bildhojd-64,bildbredd,bildhojd)); //Show first frame

//Check what happens in console
cout << "koordinater"<< bildbredd-64 << " , " << bildhojd-64 << " , " << bildbredd << " , " << bildhojd << " , " << endl;
cout << "tidskod " << visningstid << endl;

App.Draw(Klassinstans.Sprite);//Change picture
App.Display(); //Show changed picture

bildbredd = bildbredd + 64;
visningstid = visningstid + visningstid;
rutorX = rutorX + 1;
}

rutorX = 0;//Begin at first pic in next row

bildhojd = bildhojd + 64;
bildbredd = 64;
rutorY = rutorY + 1; //Switch to next row
//Show result in console
cout << "Y=" << rutorY << endl;
}



}//pang ends

15
General / How do I bring an SFML game out on the Internet?
« on: October 22, 2011, 12:32:43 pm »
I admit, when programming in different languages, I'm a beginner with C/Pascal knowledge given to me in the eighties and acute employed as a substitute C++ teacher this- and next semester. I'm having three classes in highschool (beginner/intermediate/advanced) where pupils are taught C++ and I decided to use SFML for the two more advanced classes to attract their interest for programming in a better way than just using the console window in vc++ which has always been the only choice in this school.

The main problem now for me is that several of my pupils want to make games they can put out for on-line gaming. I know that theoretically, it should be possible, even though C++ might not be the language of choice for it. It really doesn't have to either since they are learning "programming", the choice of using C++ in teaching programming was made long before I came to the school. And since I have to learn C++ well in advance to the pupils, I could aswell learn another programming language well in advance before the actual lessons I have.

When I look at the different versions of SFML, I see three different ones that might be the solution:

*.Net. It can be programmed through VC++, but if I make a game using SFML and .Net standard, how hard is it to make it on-line playable?

*Python, it's used as server side programming language and the same pupils whom I'm teaching how to program C++ are taught by me how to put up apache web servers too, so it shouldn't be that hard, I think. Or am I wrong? How hard is it to make an on-line playable game in Python using SFML if you have control over your own Apache server?

*Ruby. Is that the same as Ruby on rails? I have never used it and it seems to be hard to understand. Still, there is an SFML version for it. As far as I know, Ruby and Python are about the same and it seems that Python would be a better choice, but am I wrong on that?

If it's playable on the Internet this way, then it should also be playable in an Iphone or Android that can show homepages, or am I wrong there too? I hope someone with better knowledge than I can give some kind of answer to these questions.

And besides, since it's a school I'm working in, I can only use free alternatives, nothing that cost any kind of money.

Pages: [1] 2
anything