Sign in to follow this  
Elit3d

sfml sprite wont draw

Recommended Posts

So I am starting on doing some class stuff with SFML problem is, my sprite doesnt seem to be drawing correctly, can anyone see any errors with my code?

 

Player.h

#pragma once
#include "Game.h"


class Player
{
public:
	Player();
	~Player();

	void Draw(sf::RenderWindow &window);

	int playerSpeed = 1;

private:
	sf::Texture playerTexture;
	sf::Sprite playerImage;
};

Player.cpp

#include "Player.h"

Player::Player()
{
	if (!playerTexture.loadFromFile("images/player.png"))
	{
		std::cout << "Can't load the player image";
	}

	playerImage.setTexture(playerTexture);
	playerImage.setPosition(200, 200);
}


Player::~Player()
{
}


void Player::Draw(sf::RenderWindow &window){
	window.draw(playerImage);
	std::cout << "DRAWN";
}

Game.cpp

#include "Game.h"

Game::Game()
{
}


Game::~Game()
{
}

void Game::Setup(){
}

void Game::Update(sf::RenderWindow &window){
	//std::cout << "TEST";
	Setup();
	Draw(window);
}

void Game::Draw(sf::RenderWindow &window){
	Player player;
	player.Draw(window);
}

main.cpp

#include <SFML/Graphics.hpp>
#include "Game.h"

int main()
{
	sf::RenderWindow window(sf::VideoMode(800, 600), "SFML works!");
	
	while (window.isOpen())
	{
		sf::Event event;
		while (window.pollEvent(event))
		{
			if (event.type == sf::Event::Closed)
				window.close();
		}

		Game game;
		game.Update(window);


		window.clear();
		window.display();
	}

	return 0;
}

It prints "DRAWN" fine, just doesnt seem to draw the image :/

Share this post


Link to post
Share on other sites

You draw in game.Update().

Then you clear the window, probably erasing the stuff you drew inside game.Update().

Then you display the cleared window.

 

As an aside, I would recommend splitting updates from draws, to prevent this kind of thing.

Share this post


Link to post
Share on other sites

You draw in game.Update().

Then you clear the window, probably erasing the stuff you drew inside game.Update().

Then you display the cleared window.

 

As an aside, I would recommend splitting updates from draws, to prevent this kind of thing.

 

AHA! Yes I forgot that you have to draw below window.clear, I make this mistake everytime, normally have to look at my recent projects. Many thanks for this pointer :) Draw and Update are now seperate.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this