In my current project I need to fill a 256x224 screen with the pixel values from an array. The array is declared this way:
uint8_t *vram = new uint8_t[7168];
So as you might have noticed, every bit in this array represent one pixel in the screen (256 x 224 / 8 = 7168). My current attempt to render a screen looks like this: (it is executed at 60hz)
int x = 0;
int y = 0;
for (int i = 0; i < 7168; i++)
{
//read the byte
uint8_t b = vram[i];
for (int j = 7; j >= 0; j--)
{
//read 1 bit from the original byte
bool pixel = (b >> j) & 1;
SetPixel(x, y, pixel);
//if end of line reached, go to next line
if (((x + 1) % 256) == 0)
{
x = 0;
y++;
}
else
{
x++;
}
}
}
and my SetPixel
function:
void SetPixel(int x, int y, Uint32 pixel)
{
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * 4;
*(Uint32 *)p = pixel;
}
My window and surface declarations:
window = SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 256, 224, SDL_WINDOW_OPENGL);
surface = SDL_GetWindowSurface(window);
Currently I have two big problems:
- My surface isn't been changed, doesn't matter what I try
- I'm not even sure if this method is efficient, looking at profiling data from Visual Studio it doesn't seem to be
So, how could I render a black and white screen from an byte array in a somewhat efficient way?
Any help will be very appreciated!
SDL_UpdateWindowSurface( window )
after making your changes to the surface pixels? \$\endgroup\$