NOTE: This is a repost of my previous question from stack overflow that I am having issues getting an answer for.
http://stackoverflow.com/questions/23717167/sfml-program-crashes-when-trying-to-draw-sprite-from-vector[
^]
I am writing this relatively simple program to fire a small projectile from a moving character when the space bar is pressed.
The program compiles without any errors(in code blocks) and runs until the space bar is pressed. Instead of launching the projectile, the program promptly crashes.
I think I have been able to isolate the crash to the line where I draw the projectiles to the screen(ln. 83). I just used cout statements on the lines before and after and the second statement didn't show.
for (int ii = 0; ii < inMotion.size(); ii++)
{
window.draw(inMotion[ii].bulletSprite);
}
I believe that all of my code will be necessary to address this issue so I have posted all of it.
#include <iostream>
#include <SFML/Graphics.hpp>
#include <string.h>
#include <math.h>
#include <vector>
using namespace std;
class Projectile
{
public:
Projectile(int, string);
void moveBullet();
sf::Sprite bulletSprite;
private:
sf::Texture bulletTexture;
int speed;
string bulletTextureString;
};
Projectile::Projectile(int s, string t)
{
speed = s;
bulletTextureString = t;
if(!bulletTexture.loadFromFile((bulletTextureString).c_str()))
{
cout << "error";
}
bulletSprite.setTexture(bulletTexture);
}
void Projectile::moveBullet()
{
bulletSprite.move(speed, 0);
}
class thePlayer
{
public:
void Fire(sf::Time);
void Walk();
void Draw(sf::RenderWindow&);
void Create(string);
sf::Time lastShot;
private:
vector<Projectile> inMotion;
sf::Texture playerTexture;
sf::Sprite playerSprite;
string playerTextureString;
int moveSpeed = 5;
};
void thePlayer::Create(string s)
{
playerTextureString = s;
if(!playerTexture.loadFromFile((playerTextureString).c_str()))
{
cout << "error";
}
playerSprite.setTexture(playerTexture);
}
void thePlayer::Fire(sf::Time time)
{
sf::Time shotDelay = sf::milliseconds(100);
if(time - lastShot >= shotDelay)
{
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
{
Projectile bullet(15, "fireball.png");
bullet.bulletSprite.setPosition(playerSprite.getPosition());
inMotion.push_back(bullet);
}
}
for(int ii = 0; ii < inMotion.size(); ii++)
{
inMotion[ii].moveBullet();
}
}
void thePlayer::Draw(sf::RenderWindow& window)
{
for (int ii = 0; ii < inMotion.size(); ii++)
{
window.draw(inMotion[ii].bulletSprite);
}
window.draw(playerSprite);
}
void thePlayer::Walk(){
int x = 0;
int y = 0;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
x-= moveSpeed;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
x+= moveSpeed;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
y-= moveSpeed;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
y+= moveSpeed;
}
playerSprite.move(x, y);
}
class Game
{
public:
void Update(sf::RenderWindow&, sf::Time);
void Start(sf::Time);
sf::Clock clock;
thePlayer player;
private:
};
void Game::Start(sf::Time time)
{
player.Create("block.png");
player.lastShot = time;
}
void Game::Update(sf::RenderWindow& window, sf::Time time)
{
player.Fire(time);
player.Walk();
player.Draw(window);
}
int main()
{
sf::RenderWindow window(sf::VideoMode(1000, 1000), "Menu");
window.setFramerateLimit(60);
Game theGame;
sf::Clock clock;
sf::Time startTime = clock.restart();
theGame.Start(startTime);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color::White);
sf::Time elapsed = clock.getElapsedTime();
theGame.Update(window, elapsed);
window.display();
}
return 0;
}
Thanks so much for the help.
EDIT: I tried using the debugger but couldn't figure anything out. A link to a good article on codeblocks debugging would be great.