This is an entire, minimal, runnable example, stripped from anything that wasn't related to the problem. It's not idiomatic C++ and there are style problems, but for now I'm interested in understanding why this does not work.
The sprite seems to draw as a white square, even though I *think* I'm properly keeping the texture alive.
In my draw function I clear everything in magenta. If I don't move the sprite, only a small white square on the top left is drawn. This confirms that the sprite, of the right size, is drawn; but it's just white instead of the correct image.
#define SFML_STATIC
#include <stdint.h>
typedef uint64_t u64;
#define TILE_SIZE 32
#define WIDTH 640
#define HEIGHT 640
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
struct State {
sf::Texture dirt_texture;
sf::Sprite dirt_sprite;
sf::RenderWindow* window;
};
void update(State state) {
}
void draw(State state) {
state.window->clear(sf::Color::Magenta);
for (u64 x = 0; x < 64; x++) {
for (u64 y = 0; y < 64; y++) {
state.dirt_sprite.setPosition(x * TILE_SIZE, y * TILE_SIZE);
state.window->draw(state.dirt_sprite);
}
}
state.window->display();
}
State new_state(sf::RenderWindow* window) {
State state;
state.window = window;
if (!state.dirt_texture.loadFromFile("art/dirt.png")) {
printf("Error loading dirt!!!!\n");
}
state.dirt_sprite.setTexture(state.dirt_texture);
return state;
}
int main() {
sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "My window");
State state = new_state(&window);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
update(state);
draw(state);
}
return 0;
}
I do get this warning in the console output:
An internal OpenGL call failed in Texture.cpp(98).
Expression:
glFlush()
Error description:
GL_INVALID_OPERATION
The specified operation is not allowed in the current state.