-
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.
-
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);
-
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.
-
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.
-
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)
-
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.