Hello,
I'm new to SFML so I've been stuck on this for a little while now, but it seems like the texture for my sprite only loads midway through. I want to display a 2D array of pixels to a window, so I wanted to see if I could fill up an entire window with a solid color as a test.
Here's a snippet of my code:
sf::RenderWindow window(sf::VideoMode(WINSIZE_X, WINSIZE_Y), "My Window");
sf::Sprite sprite;
sf::Texture texture;
sf::Uint8 *pixels = new sf::Uint8[WINSIZE_X * WINSIZE_Y * 4];
if (!texture.create(WINSIZE_X, WINSIZE_Y)) {
std::cout << "Error: texture could not be created" << std::endl;
}
while(window.isOpen()) {
//Update texture
for(int y = 0; y < WINSIZE_Y; y++) {
for(int x = 0; x < WINSIZE_X; x++) {
pixels[(y*WINSIZE_X)+(4*x)] = 255; // R
pixels[(y*WINSIZE_X)+(4*x)+1] = 255; // G
pixels[(y*WINSIZE_X)+(4*x)+2] = 0; // B
pixels[(y*WINSIZE_X)+(4*x)+3] = 200; // A
}
}
texture.update(pixels);
//Update sprite with texture
sprite.setTexture(texture);
//Clear window
window.clear(sf::Color::Black);
//Draw everything
window.draw(sprite);
window.display();
}
WINSIZE_X and WINSIZE_Y are defined as 800 and 600 respectively.
Sorry if this is a stupid question, but thanks in advanced for any assistance!