in c++ SDL2 i would want to access pixels and draw on a 1-bit surface same way i do with a 8-bit surface but i'm missing something i don't see.
in 8-bit surface this works fine (the pixel take the expected color among palette->colors):
SDL_Surface * surface = SDL_CreateRGBSurfaceWithFormat(0, width, height, 8, SDL_PIXELFORMAT_INDEX8);
SDL_Color colors[2] = {{0, 0, 0, 255}, {255, 255, 255, 255}};
SDL_SetPaletteColors(surface->format->palette, colors, 0, 2);
Uint8* pixels = (Uint8*)surface->pixels;
for(int i = 0; i < pixelsCount; i++){
if(*geometrical condition*){
pixels[i]=1;
}else{
pixels[i]=0;
}
}
with a 1-bit surface i get a non 0 exit because of assignment (last if):
SDL_Surface * surface = SDL_CreateRGBSurfaceWithFormat(0, width, height, 1, SDL_PIXELFORMAT_INDEX1MSB);
SDL_Color colors[2] = {{0, 0, 0, 255}, {255, 255, 255, 255}};
SDL_SetPaletteColors(surface->format->palette, colors, 0, 2);
Uint8* pixels = (Uint8*)surface->pixels;
for(int i = 0; i < pixelsCount; i++){
if(*geometrical condition*){
pixels[i]=1;
}else{
pixels[i]=0;
}
}
What's the right pixel assignment in 1-bit case?
well, this worked for me, maybe it is not the best solution but:
remind, 1-bitperpixel surface, SDL_PIXELFORMAT_INDEX1MSB.
//coordinates
int x, y;
//more pixels than in SCREEN_WIDTH, not used here, but useful for /*geom. cond.*/
int actualWidthPixels = surface1->pitch * CHAR_BIT;
//follow the pixel
int pixel{};
//iterate each byte
for( uint8_t* byte = pixels; byte - pixels < surface1->pitch * surface1->h; byte++)
{
//iterate each bit in byte
for(int bit = 0; bit < CHAR_BIT; bit++)
{
x = pixel % (surface1->pitch * CHAR_BIT);
y = (byte - pixels) / surface1->pitch;//pixels = (uint8_t*)s->pixels;
if (/*geometrical condition*/)
{
//set the bit
*byte |= (1 << 7-bit);
}
pixel++;
}
}