2 Replies - 338 Views - Last Post: 29 June 2012 - 07:42 PM Rate Topic: -----

#1 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • Posts: 545
  • Joined: 12-June 08

Weird SFML problem

Posted 29 June 2012 - 01:37 PM

I am not sure what is causing this but there are two problems that I need help with. One of them is that the title of the window, isn't what I set it as. I set it as "Shooter" and it shows up as some weird characters that aren't on the keyboard. I am not sure what is causing this but if someone has had a similar problem I would like to know what is going on. Now for my real problem, I am trying to get the bullet image to load into the game but whenever I try to shoot the game crashes. So, I tried to figure out what was wrong with it by putting debug statements into the code but I can't do that because when the game crashes it makes my computer beep and it outputs random characters into the console window so I can't see me debug text and see what went wrong. Do I have SFML linked incorrectly or something? I'm not sure because everything worked fine up until I've been trying to load this image. Thank you for any help, it is very appreciated :)

main.cpp
#include "Globals.hpp"
#include "Player.hpp"

float updateClock(sf::Clock*);

int main(int argc, char* argv[]) {
	sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, BPP), 
		"Shooter");
	sf::Clock clock;

	Player player(sf::Vector2f(400.f, 300.f), sf::Vector2f(25.f, 25.f), 
		&window);

	while (window.isOpen()) {
		sf::Event event;
		while (window.pollEvent(event)) {
			if (event.type == sf::Event::Closed) 
				window.close();
		}
		float timeStep = updateClock(&clock);
		player.update(timeStep);
		window.clear();
		player.render();
		window.display();
	}
	cin.get();
	return 0;
}

float updateClock(sf::Clock* clock) {
	sf::Time time = clock->restart();
	long delta = time.asMilliseconds();
	float timeStep = delta / 1000.f;
	return timeStep;
}


Player.hpp
#ifndef PLAYER_HPP
#define PLAYER_HPP

#include "Globals.hpp"
#include "Bullet.hpp"

class Player {
public:
	Player(sf::Vector2f, sf::Vector2f, sf::RenderWindow*);
	~Player();
	
	sf::RenderWindow* getWindow();
	sf::Vector2f getPosition();
	sf::Vector2f getSize();
	float getAngle();

	void render();
	void update(float);
private:
	float m_speed;
	float m_angle;
	Bullet* m_bullet;

	sf::RenderWindow* m_window;
	sf::Vector2f m_position;
	sf::Vector2f m_size;
	sf::Vector2f m_velocity;
	sf::RectangleShape m_rect;
};

#endif


Player.cpp
#include "Player.hpp"

Player::Player(sf::Vector2f position, sf::Vector2f size,
	sf::RenderWindow* window) {
		m_speed = 150.f;
		m_angle = 0.f;
		m_position = position;
		m_size = size;
		m_velocity = sf::Vector2f(0.f, 0.f);
		m_window = window;
		m_rect = sf::RectangleShape();
		m_rect.setPosition(m_position);
		m_rect.setSize(m_size);
		m_rect.setFillColor(sf::Color::Blue);
		m_rect.setOrigin(m_size.x / 2, m_size.y / 2);
		m_rect.setRotation(m_angle);
		m_bullet = NULL;
}

Player::~Player() {
	cout << "Player Destructor starting..." << endl;
	if (m_window)
		m_window = NULL;
	if (m_bullet) {
		delete m_bullet;
		m_bullet = NULL;
	}
	cout << "Player destructor ending." << endl;
}

sf::RenderWindow* Player::getWindow() {
	return m_window;
}

sf::Vector2f Player::getPosition() {
	return m_position;
}

sf::Vector2f Player::getSize() {
	return m_size;
}

float Player::getAngle() {
	return m_angle;
}

void Player::render() {
	if (m_window) {
		m_window->draw(m_rect);
		if (m_bullet) {
			cout << "bullet render" << endl;
			sf::Sprite sprite;
			sprite.setTexture(m_bullet->getTexture());
			sprite.setPosition(m_bullet->getPosition());
			sprite.setRotation(m_angle);
			m_window->draw(sprite);
			cout << "bullet render done" << endl;
		}
	}
}

void Player::update(float timeStep) {
	if (m_window) {
		sf::Vector2f velocity = sf::Vector2f(0.f, 0.f);
		if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) 
			velocity.y = -m_speed;
		if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
			velocity.y = m_speed;
		if (sf::Keyboard::isKeyPressed(sf::Keyboard::D/>))
			velocity.x = m_speed;
		if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
			velocity.x = -m_speed;
		if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
			if (m_bullet == NULL) {
				m_bullet = new Bullet(m_position, m_angle, 100);
			}
		}
		m_position.x += velocity.x * timeStep;
		m_position.y += velocity.y * timeStep;
		m_rect.setPosition(m_position);
		sf::Vector2i mouse = sf::Mouse::getPosition(*m_window);
		sf::Vector2f mouse_position = m_window->convertCoords(mouse);
		sf::Vector2f angle = mouse_position - m_position;
		m_angle = atan2(angle.y, angle.x);
		m_angle *= 180.f / PI;
		//cout << m_angle << endl;
		m_rect.setRotation(m_angle);
		if (m_bullet) {
			if (!m_bullet->update(timeStep)) {
				delete m_bullet;
				m_bullet = NULL;
			}
		}
	}
}


Bullet.hpp
#ifndef BULLET_HPP
#define BULLET_HPP

#include "Globals.hpp"

class Bullet {
public:
	Bullet(sf::Vector2f, float, int);
	~Bullet();

	bool update(float);

	sf::Texture getTexture();
	sf::Vector2f getPosition();
private:
	float m_angle;
	float m_speed;
	int m_life;

	sf::Texture m_texture;
	sf::Vector2f m_position;
};

#endif


Bullet.cpp
#include "Bullet.hpp"

Bullet::Bullet(sf::Vector2f position, float angle, int life) {
	m_position = position;
	m_angle = angle;
	m_speed = 200.f;
	m_life = life;
	cout << "loading bullet image..." << endl;
	m_texture.loadFromFile("bullet.png");
	cout << "loaded bullet image..." << endl;
}

Bullet::~Bullet() {
	cout << "Bullet Destructor" << endl;
}


bool Bullet::update(float timeStep) {
	float angle = m_angle * PI / 180.f;
	m_position.x += (m_speed * cos(angle)) * timeStep;
	m_position.y += (m_speed * sin(angle)) * timeStep;
	if (--m_life <= 0) return false;
	return true;
}

sf::Texture Bullet::getTexture() {
	return m_texture;
}

sf::Vector2f Bullet::getPosition() {
	return m_position;
}


Globals.hpp
#ifndef GLOBALS_HPP
#define GLOBALS_HPP

#include <SFML/Graphics.hpp>
#include <iostream>
#include <cmath>

#define PI 3.14159265f
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#define BPP 32

using namespace std;

#endif


Is This A Good Question/Topic? 0
  • +

Replies To: Weird SFML problem

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 2034
  • View blog
  • Posts: 6,058
  • Joined: 05-May 12

Re: Weird SFML problem

Posted 29 June 2012 - 04:35 PM

Silly question: Why not just attach a debugger and step through the code instead of watching the console window and trying to discern what is happening?
Was This Post Helpful? 0
  • +
  • -

#3 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1394
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Weird SFML problem

Posted 29 June 2012 - 07:42 PM

Quote

I set it as "Shooter" and it shows up as some weird characters that aren't on the keyboard.

Quote

Do I have SFML linked incorrectly or something?
You probably have a mixture of debug and release CRTs across the binaries. SFML exposes a C++ API, meaning standard library objects are being operated on. In general, you want to keep CRT versions consistent, but particularly so in this case, where CRT objects are being operated on.

Let's say you are building a debug version of your program. That means you link to the debug CRT, which requires that you link to a version of SFML that was built against the debug CRT as well.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1