I have created a button class that just contains a texture and sprite. However, when I create an array of these Buttons, window.draw(buttons[0].sprite) doesn't draw the sprite.
Here is the class "Button":
#include <stdio.h>
#include <SFML/Graphics.hpp>
#include "ResourcePath.hpp"
class Button {
public:
Button();
Button(sf::Texture myTexture, sf::Vector2f pos);
sf::Sprite sprite;
sf::Texture texture;
};
...
#include "Button.h"
Button::Button() {
}
Button::Button(sf::Texture myTexture, sf::Vector2f pos) {
texture = myTexture;
if (!texture.loadFromFile(resourcePath() + "cute_image.jpg")) {
return EXIT_FAILURE;
}
sprite = sf::Sprite(texture);
sprite.setPosition(pos);
}
An here is my main():
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include "ResourcePath.hpp"
#include "Button.h"
int main(int, char const**)
{
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
sf::Texture texture;
if (!texture.loadFromFile(resourcePath() + "cute_image.jpg")) {
return EXIT_FAILURE;
}
Button *buttonArr = new Button[5];
buttonArr[0] = Button(texture, sf::Vector2f(0, 0));
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed) {
window.close();
}
}
window.clear();
window.draw(buttonArr[0].sprite);
window.display();
}
return EXIT_SUCCESS;
}
A window opens, but the screen remains white. Interestingly, if I keep everything the same, but make buttons a single button rather than an array, everything works fine.
Any help here would be much appreciated.