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

Author Topic: Correct method for drawing stuff in the window  (Read 2683 times)

0 Members and 1 Guest are viewing this topic.

Faenrir

  • Newbie
  • *
  • Posts: 7
    • View Profile
Correct method for drawing stuff in the window
« 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!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Correct method for drawing stuff in the window
« Reply #1 on: November 12, 2020, 09:54:50 am »
Hi

This is basically the way to go, you just need to take the fastest path:
  • replace sf::Image with a an array of uint8_t for buffer, with direct access for modifications
  • use drawBuffer.update(buffer) rather than loadFromImage
  • this won't make a difference, but you don't need to repeat the call to setTexture if it's always the same
« Last Edit: November 12, 2020, 01:06:05 pm by Laurent »
Laurent Gomila - SFML developer

Faenrir

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Correct method for drawing stuff in the window
« Reply #2 on: November 12, 2020, 10:19:25 am »
Hey, thanks for the quick answer !
I'll modify the code and see how it goes ^^

Faenrir

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Correct method for drawing stuff in the window
« Reply #3 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".

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Correct method for drawing stuff in the window
« Reply #4 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;
« Last Edit: November 24, 2020, 07:58:33 pm by Laurent »
Laurent Gomila - SFML developer

Faenrir

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Correct method for drawing stuff in the window
« Reply #5 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
« Last Edit: November 24, 2020, 03:38:20 pm by Faenrir »

Faenrir

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Correct method for drawing stuff in the window
« Reply #6 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 !

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Correct method for drawing stuff in the window
« Reply #7 on: November 24, 2020, 07:58:53 pm »
Ah, sorry! I fixed my code above ;)
Laurent Gomila - SFML developer

 

anything