A surface created with SDL_PIXELFORMAT_INDEX1MSB
is one-bit-per-pixel. The 1 and 0 are indexed to colors you can set.
SDL_Init(SDL_INIT_VIDEO);
/* scale here to make the window larger than the surface itself.
Integer scalings only as set by function below. */
SDL_Window * window = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, 0);
SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
const int width = SCREEN_WIDTH; //
const int height = SCREEN_HEIGHT;
/* Since we are going to display a low resolution buffer,
it is best to limit the window size so that it cannot be
smaller than our internal buffer size. */
SDL_SetWindowMinimumSize(window, width, height);
SDL_RenderSetLogicalSize(renderer, width, height);
SDL_RenderSetIntegerScale(renderer, 1);
/* A one-bit-per-pixel Surface, indexed to these colors */
SDL_Surface * surface = SDL_CreateRGBSurfaceWithFormat(SDL_SWSURFACE,
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);
while (!done) {
/* draw things into the byte array at surface->pixels. bit-math stuff. */
/* docs are here: https://wiki.libsdl.org/SDL_Surface */
/* draw the Surface to the screen */
SDL_RenderClear(renderer);
SDL_Texture * screen_texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_RenderCopy(renderer, screen_texture, NULL, NULL);
SDL_RenderPresent(renderer);
SDL_DestroyTexture(screen_texture);
}
Links:
https://wiki.libsdl.org/SDL_Surface
https://wiki.libsdl.org/SDL_PixelFormatEnum
SDL_PIXELFORMAT_INDEX1MSB
(preferred) andSDL_PIXELFORMAT_INDEX1LSB
. The number of bits per pixel is still at least 8, but it should be easily convertible to a true 1-bit per pixel custom format. – BeachSDL_PIXELFORMAT_INDEX8
format with a 2-color (black & white) palette. Then just map each pixel in the image to 0 or 1. – Tautomerism