Advertisement

sfml sprite wont draw

Started by April 20, 2015 12:38 PM
1 comment, last by Elit3d 9 years, 8 months ago

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 :/

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.

Hello to all my stalkers.

Advertisement

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.

This topic is closed to new replies.

Advertisement