我正在尝试用 C 语言编写一个具有内存映射图形的复古计算机模拟器(使用 SDL2 用于图形和声音)。
这将是一个 16 位 WORD 机器,每个 RAM 位置保存 16 位。屏幕尺寸为 512 x 256 像素,只有黑白。图形被映射到 8 K 内存块中(因此有 8192 个位置,每个位置有 16 位)。
该内存区域中的每个位保存单个像素的数据,黑色 (0) 或白色 (1)。
根据我对 SDL2 的了解,我相信它的最小像素格式限于每像素 8 位,但我不确定。这将是 256 种颜色调色板的索引。
有谁知道如何将这些原始位图数据直接发送到 SDL2 函数,该函数将返回一个 Surface,然后我可以将其 blit 到显示器?
使用创建的表面SDL_PIXELFORMAT_INDEX1MSB是每像素一位。1 和 0 索引到您可以设置的颜色。
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);
}
Run Code Online (Sandbox Code Playgroud)
链接:
https://wiki.libsdl.org/SDL_Surface
https://wiki.libsdl.org/SDL_PixelFormatEnum