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

Author Topic: Application hanging up.  (Read 3815 times)

0 Members and 1 Guest are viewing this topic.

Nabeel Rehman

  • Newbie
  • *
  • Posts: 13
    • View Profile
Application hanging up.
« on: April 16, 2017, 07:29:28 pm »
hi guys,
i'm coding a tank game and i'm stuck at the point where tank shoot bullets, below is the unit of code i used to shoot bullets i've marked up where i think my applications hanging up. Please Help :-\
----------------------------------------------
#include <Windows.h>
#include <SFML\Graphics.hpp>
#include <SFML\Window.hpp>
#include <vector>
#include <thread>

using namespace sf;
using namespace std;
#define X_COL 0
#define Y_COL 1

RenderWindow window(VideoMode(600, 600, 32), "Test 32");
Texture texture;
vector<Sprite> sprite(10, Sprite(texture));
float ammo[10][2];

int CALLBACK WinMain(_In_ HINSTANCE hInstance, _In_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow)
{
   
   texture.loadFromFile("bullet.png");
   sprite[0].setPosition(window.getSize().x / 2, window.getSize().y / 2);
   sprite[0].setOrigin(texture.getSize().x / 2, texture.getSize().y / 2);
   sprite[0].setRotation(360);

   for (int i = 0; i < 10; i++)
   {
      for (int j = 0; j < 2; j++)
      {
         ammo[j] = -1.0f;
      }
   }

   thread thr;
   int curntRound = 0;
   float angle;

   while (window.isOpen())
   {
      Event _event;
      while (window.pollEvent(_event))
      {
         if (_event.type == Event::Closed)
            window.close();
      }

      if (Keyboard::isKeyPressed(Keyboard::Space))
      {
         ammo[curntRound][X_COL] = sprite[0].getPosition().x;          // Application hang up here.
         ammo[curntRound][Y_COL] = sprite[0].getPosition().y;          // it only insert x y points at
                                                                                                                           // first iteration in 'ammo' array
         curntRound++;
      }
      window.clear(Color::Black);
      window.display();
   }
   return 0;
}
void fire()
{
   for (int i = 0; i < 10; i++)
   {
      if (ammo[X_COL] != 1.0f && ammo[Y_COL] != -1.0f)
         sprite[0].setPosition(++ammo[X_COL], ammo[Y_COL]);
      window.draw(sprite[0]);
   }
}
« Last Edit: April 16, 2017, 07:32:58 pm by Nabeel Rehman »

Turbine

  • Full Member
  • ***
  • Posts: 100
    • View Profile
Re: Application hanging up.
« Reply #1 on: April 16, 2017, 08:14:32 pm »
What the heck, that's an interestingly designed SFML program. Why not try and base it more on some of the more standard examples?

For instance, Window is being created on the stack, rather than in main.

Nabeel Rehman

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Application hanging up.
« Reply #2 on: April 16, 2017, 08:43:31 pm »
Well i've found a way to fire bullets but the problem is if space bar is pressed it just show long continuous image of my ' bullet '.
-----------------------------------------
#define X_COL 0
#define Y_COL 1
#define EMPTY_CELL -1.0f
RenderWindow window(VideoMode(600, 600, 32), "Test 32");
float ammo[100][2];

int CALLBACK WinMain(_In_ HINSTANCE hInstance, _In_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow)
{
        Texture texture;
        Sprite sprite;
        texture.loadFromFile("bullet.png");
        sprite.setTexture(texture);
        sprite.setPosition(window.getSize().x / 2, window.getSize().y / 2);
        sprite.setOrigin(texture.getSize().x / 2, texture.getSize().y / 2);
        sprite.setRotation(360);

        #pragma region Initialize 'ammo'

        for (int i = 0; i < 100; i++)
        {
                for (int j = 0; j < 2; j++)
                {
                        ammo[i][j] = EMPTY_CELL;
                }
        }

        #pragma endregion

        thread thr;
        int crntRound = 0;
        float angle;
        window.setKeyRepeatEnabled(false);
        while (window.isOpen())
        {
                Event _event;
                while (window.pollEvent(_event))
                {
                        if (_event.type == Event::Closed)
                                window.close();
                }
                window.clear(Color::Black);
                if (Keyboard::isKeyPressed(Keyboard::Space))
                {
                       
                                if (crntRound != 100)
                                {
                                        ammo[crntRound][X_COL] = 300;
                                        ammo[crntRound][Y_COL] = 300;
                                        crntRound++;
                                }
                                else
                                {
                                        crntRound = 0;
                                }
                }
               
                for (int i = 0; i < 100; i++)
                {
                        if (ammo[i][X_COL] != EMPTY_CELL && ammo[i][Y_COL] != -EMPTY_CELL)
                        {
                                if (!(ammo[i][X_COL] > 600 || ammo[i][Y_COL] > 600))
                                        sprite.setPosition(++ammo[i][X_COL], ammo[i][Y_COL]);
                                else
                                {
                                        ammo[i][X_COL] = EMPTY_CELL;
                                        ammo[i][Y_COL] = EMPTY_CELL;
                                }
                        }
                        window.draw(sprite);
                }
                window.display();
        }
        return 0;
}
« Last Edit: April 17, 2017, 12:15:27 pm by Nabeel Rehman »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Application hanging up.
« Reply #3 on: April 17, 2017, 01:49:07 am »
Please post code within code tags: [code=cpp] [/code]

You could fix your constant bullet firing by using a timer to delay the next bullet by not firing until a certain duration has passed, resetting the time when the bullet does get fired.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

JayhawkZombie

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: Application hanging up.
« Reply #4 on: April 17, 2017, 03:51:08 am »
You could also fire a bullet only when the user initially presses down on the space key

if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Space) {
  //Launch here
}

Nabeel Rehman

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Application hanging up.
« Reply #5 on: April 17, 2017, 07:10:35 pm »
Thank's a lot guys, finally it's working  ;D