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

Author Topic: Pass int/char array to sf::Uint8 array?  (Read 1876 times)

0 Members and 1 Guest are viewing this topic.

LakySimi1

  • Newbie
  • *
  • Posts: 35
    • View Profile
Pass int/char array to sf::Uint8 array?
« on: April 19, 2024, 11:52:48 pm »
Hi everyone,
there is a way to pass an int or maybe better, a char array to sf::Uint8* pixels = new sf::Uint8[WIDTH * HEIGHT * 4] ?

Honestly, i'm ignorant, i want to build the array of pixels in a classic int array, which is faster to manipulate, and then show it on screen.

Right now instead i am manipulating pixels.

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Pass int/char array to sf::Uint8 array?
« Reply #1 on: April 21, 2024, 03:03:00 am »
If you mean for passing to the texture update method, as long as the size is correct (and the data makes sense) you can pass in any kind of array if you cast the pointer type.
int *pixels = new int[WIDTH * HEIGHT];
texture.update((const std::uint8_t*) pixels);

LakySimi1

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Pass int/char array to sf::Uint8 array?
« Reply #2 on: April 21, 2024, 01:46:20 pm »
Good, thanks!

My code does not work due to the following:

char* pixels = new char [1280 * 720 * 4];
char Array [1280 * 720 * 4];

for (int i = 0; i < 1280 * 720 * 4; i++)
    Array[i] = 42;

for (int i = 0; i < 1280 * 720 * 4; i++)
    pixels[i] = Array[i]; // <- does not work, if i change "Array[i]" with a number, such as "42", it does work.
 

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Pass int/char array to sf::Uint8 array?
« Reply #3 on: April 21, 2024, 02:23:39 pm »
One thing to be careful of is where that Array is created and which compiler you are using.
Local variables like arrays are created on the stack (whereas things made using "new" are on the heap). Visual Studio C++ only allocates 1MB for the stack by defaultr, but your array is using almost 4MB. So overflowing the stack could cause an issue.

Otherwise it should be fine.

LakySimi1

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Pass int/char array to sf::Uint8 array?
« Reply #4 on: April 21, 2024, 03:53:11 pm »
Thank you, i confirm, reducing sizes make it work..

i was trying to make faster the process of writing in pixels accessing it only 1 time, but it seems to not help, even if i do

for (int i = 0; i < 1280 * 720 * 4; i++)
    pixels[i] = 42;

I don't have an increase in performance(/fps)

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Pass int/char array to sf::Uint8 array?
« Reply #5 on: April 21, 2024, 11:30:33 pm »
If we're dealing with 32bit colour (usually are), a char is only a quarter of the colour. But an int can hold an entire colour at once. So right now you are working with quarter pixels instead of whole pixels.

Having
unsigned int* pixels = new unsigned int [1280 * 720];

for (int i = 0; i < 1280 * 720; i++)
    pixels[i] = 0xff0080ff;  // that should be orange on an RGBA 32 bit pixel, which is what SFML uses
Means you are working one pixel at a time, and the for loop code does a quarter of the work.

 

anything