1
General / Sprites inside a Class
« on: April 11, 2016, 03:48:55 pm »
hi, i have created a class called "Player"
in its header
"Player.h"
its cpp file
Player.cpp
what i am trying to do is to create an object from this class from another cpp file called
newMap.cpp
in the main.cpp
my code does not have throw an error but it crashes everytime i execute it.
the first result that i had was a blank white rectangle. please help
in its header
"Player.h"
#ifndef PLAYER_H
#define PLAYER_H
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
using namespace std;
class Player
{
public:
sf::Sprite sprite(){
sf::Texture texture;
texture.loadFromFile("gunship.png");
texture.setSmooth(true);
sf::Sprite playersprite;
playersprite.setTexture(texture);
return playersprite;
}
#define PLAYER_H
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
using namespace std;
class Player
{
public:
sf::Sprite sprite(){
sf::Texture texture;
texture.loadFromFile("gunship.png");
texture.setSmooth(true);
sf::Sprite playersprite;
playersprite.setTexture(texture);
return playersprite;
}
its cpp file
Player.cpp
#include "Player.h"
Player::Player()
{
}
Player::Player()
{
}
what i am trying to do is to create an object from this class from another cpp file called
newMap.cpp
#include "NewMap.h"
#include "Player.h"
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
int mapPlayers()
{
sf::RenderWindow map1(sf::VideoMode(1180, 600), "GUNSHIP", sf::Style::None);
sf::Font font;
if (!font.loadFromFile("emulogic.ttf"))
return EXIT_FAILURE;
sf::Text gametitle("GUNSHIP", font, 54);
gametitle.setPosition(300,50);
Player p1; //created an object p1 from the class player
while(map1.isOpen())
{
map1.draw(gametitle);
while(map1.pollEvent(event))
{
}
map1.draw(p1.sprite()); //i want to draw the sprite of the object P1 from the class Player
map1.display();
}
return 0;
}
#include "Player.h"
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
int mapPlayers()
{
sf::RenderWindow map1(sf::VideoMode(1180, 600), "GUNSHIP", sf::Style::None);
sf::Font font;
if (!font.loadFromFile("emulogic.ttf"))
return EXIT_FAILURE;
sf::Text gametitle("GUNSHIP", font, 54);
gametitle.setPosition(300,50);
Player p1; //created an object p1 from the class player
while(map1.isOpen())
{
map1.draw(gametitle);
while(map1.pollEvent(event))
{
}
map1.draw(p1.sprite()); //i want to draw the sprite of the object P1 from the class Player
map1.display();
}
return 0;
}
in the main.cpp
#include "NewMap.h"
main()
{
mapPlayers();
return 0;
}
main()
{
mapPlayers();
return 0;
}
my code does not have throw an error but it crashes everytime i execute it.
the first result that i had was a blank white rectangle. please help