I'm having a problem where my GameObject doesn't display the image I've assigned it to display.
I gave it a valid image path to a file of a format which is supported by the library I'm using.
However creating a object of only my Sprite class seems to work (the image that should be displayed, gets displayed).
GameObject.h
#pragma once
#include "Sprite.h"
class GameObject
{
public:
GameObject();
GameObject(std::string m_texturePath, float m_x, float m_y);
~GameObject();
void Update();
void Draw(sf::RenderWindow &m_window);
void HandleEvents(float m_deltaTime);
private:
Sprite _sprite;
};
GameObject.cpp
#include "GameObject.h"
GameObject::GameObject()
{
}
GameObject::GameObject(std::string m_texturePath, float m_x, float m_y)
{
_sprite = Sprite(m_texturePath, m_x, m_y);
}
void GameObject::Draw(sf::RenderWindow &m_window)
{
_sprite.Draw(m_window);
}
Sprite.h
#pragma once
#include <SFML\Graphics.hpp>
#include <string>
#include <iostream>
class Sprite
{
public:
Sprite();
Sprite(std::string m_texturePath, float m_x, float m_y);
~Sprite();
void Draw(sf::RenderWindow &m_window);
void SetPosition(float m_x, float m_y);
void SetVisible(bool m_visible);
private:
sf::Sprite _sprite;
sf::Texture _texture;
bool _visible = true;
bool _created = false;
};
Sprite.cpp
#include "Sprite.h"
Sprite::Sprite()
{
}
Sprite::Sprite(std::string m_texturePath, float m_x, float m_y)
{
if (_created == false)
{
if (_texture.loadFromFile(m_texturePath) == false)
{
std::cout << "Failed to load texture : " << m_texturePath << std::endl;
_created = false;
return;
}
_sprite.setTexture(_texture);
_sprite.setPosition(m_x, m_y);
_created = true;
}
}
void Sprite::Draw(sf::RenderWindow &m_window)
{
if (_visible == true)
{
m_window.draw(_sprite);
}
}
void Sprite::SetVisible(bool m_visible)
{
_visible = m_visible;
}
void Sprite::SetPosition(float m_x, float m_y)
{
_sprite.setPosition(m_x, m_y);
}
MainMenuState.h
#pragma once
#include "GameState.h"
#include "Sprite.h"
#include "GameObject.h"
class MainMenuState : public GameState
{
public:
MainMenuState();
~MainMenuState();
void Update();
void HandleEvents(float m_deltaTime);
void Draw(sf::RenderWindow &m_window);
private:
GameObject gameObject;
};
MainMenuState.cpp
#include "MainMenuState.h"
MainMenuState::MainMenuState()
{
gameObject = GameObject("src/Textures/test.png", 10, 10);
}
void MainMenuState::Update()
{
gameObject.Update();
}
void MainMenuState::Draw(sf::RenderWindow &m_window)
{
gameObject.Draw(m_window);
}
I removed some of the functions which didn't do anything yet and also removed functions which were never gonna be called to reduce the amount of code.
I'm not sure if this still fits in the general-programming section since it has parts of the SFML library in it.