Jump to content
  • Advertisement
Sign in to follow this  
Elit3d

sfml sprite wont draw

This topic is 1185 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!