SFML community forums

Help => Graphics => Topic started by: Faenrir on November 12, 2020, 08:15:39 am

Title: Correct method for drawing stuff in the window
Post by: Faenrir on November 12, 2020, 08:15:39 am
Hi all !
I've started working with the SFML again after years of not touching it...
So i'm working on a small raycasting engine and was wondering what was the correct method for drawing stuff in the window...
Specifically, i need to access and modify the individual pixels of the image to write the correct color in there and then draw that image on the window.

Additionally, i'd like being able to use only a portion of the screen for that view, no matter the actual resolution of the window (bigger than the play screen which will be either 320x240 or 640x480 for the oldschool feel).

Right now here's how i'm doing things:

buffer is a sf::Image
drawBuffer is a sf::Texture
spriteBuffer is a sf::Sprite

For each pixel on the game screen, i'm setting the color (after getting it from the texture) with
buffer.setPixel(x,y, color);
then when it's done
        drawBuffer.loadFromImage(buffer);
        spriteBuffer.setTexture(drawBuffer);
        window->draw(spriteBuffer);

I'm pretty sure that's not ideal (especially since my basic engine isn't very computation heavy and it's too slow right now) so i'd like to know how to improve this.
Thanks!
Title: Re: Correct method for drawing stuff in the window
Post by: Laurent on November 12, 2020, 09:54:50 am
Hi

This is basically the way to go, you just need to take the fastest path:
Title: Re: Correct method for drawing stuff in the window
Post by: Faenrir on November 12, 2020, 10:19:25 am
Hey, thanks for the quick answer !
I'll modify the code and see how it goes ^^
Title: Re: Correct method for drawing stuff in the window
Post by: Faenrir on November 23, 2020, 08:57:26 pm
Having some issues since the uint8_t *buffer needs to be updated with rgba values instead of a sf:color and thus is 4 times as big as the actual image.

Quote
Update a part of the texture from an array of pixels.

The size of the pixel array must match the width and height arguments, and it must contain 32-bits RGBA pixels.

So i'm not quite sure how to do this since the uint8_t can't "contain 32-bits RGBA pixels".
Title: Re: Correct method for drawing stuff in the window
Post by: Laurent on November 24, 2020, 08:06:03 am
Each uint8_t of your buffer is of course a single component of your color, thus each color uses 4 elements of the buffer.

// modify pixel at (x, y) in buffer of uint8_t:
auto index = (x + y * image_width) * 4;
buffer[index + 0] = red;
buffer[index + 1] = green;
buffer[index + 2] = blue;
buffer[index + 3] = alpha;
Title: Re: Correct method for drawing stuff in the window
Post by: Faenrir on November 24, 2020, 11:20:24 am
Yes, but then that means the buffer is 4 times bigger than the actual image?
That's what i was doing but it's not working correctly for some reason (i get a small part of the image with 4 times the graphics).
I'll try to figure out what goes wrong since i have the correct logic.
Thanks
Title: Re: Correct method for drawing stuff in the window
Post by: Faenrir on November 24, 2020, 03:46:51 pm
Found the issue, i just forgot to multiply the index by 4 which resulted in the weird behavior.
Thanks again !
Title: Re: Correct method for drawing stuff in the window
Post by: Laurent on November 24, 2020, 07:58:53 pm
Ah, sorry! I fixed my code above ;)