Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Texture stops updating midway through  (Read 1382 times)

0 Members and 1 Guest are viewing this topic.

yuxuwu

  • Newbie
  • *
  • Posts: 2
    • View Profile
Texture stops updating midway through
« on: August 22, 2017, 05:43:19 pm »
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!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Texture stops updating midway through
« Reply #1 on: August 22, 2017, 06:40:33 pm »
Quote
it seems like the texture for my sprite only loads midway through
What does that mean? Can you show a screenshot?
Laurent Gomila - SFML developer

texus

  • Sr. Member
  • ****
  • Posts: 499
    • View Profile
    • TGUI
    • Email
Re: Texture stops updating midway through
« Reply #2 on: August 22, 2017, 07:01:24 pm »
Every line contains WINSIZE_X pixels and 4 bytes per pixel. You are correctly multiplying x by 4, but forgot to also multiply WINSIZE_X by 4.
E.g. the second line starts at byte 3200 and not at byte 800, thus the pixels should be set like this:
pixels[(y*WINSIZE_X*4)+(4*x)]       = 255; // R
pixels[(y*WINSIZE_X*4)+(4*x)+1]     = 255; // G
pixels[(y*WINSIZE_X*4)+(4*x)+2]     = 0; // B
pixels[(y*WINSIZE_X*4)+(4*x)+3]     = 200; // A
TGUI: C++ SFML GUI

yuxuwu

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Texture stops updating midway through
« Reply #3 on: August 22, 2017, 07:06:45 pm »
Thank you! The fix seemed to do the trick. Can't believe that flew over my head.

 

anything