如何使用SDL2创建每像素1位的表面

6

我正在尝试使用C语言(带有SDL2用于图形和声音)编写一个具有内存映射图形的复古计算机模拟器。

这将是一台16位字长的机器,每个RAM位置容纳16位。屏幕分辨率为512 x 256像素,仅限黑白显示。图形以8 K块的内存进行内存映射(因此有8192个位置,每个位置容纳16位)。

该内存区域中的每一位都保存着单个像素的数据,可为黑色(0)或白色(1)。

据我所知,SDL2的像素格式最低只支持8位/像素,但我不确定。这会是一个256色调色板索引。

是否有人知道一种将原始位图数据直接发送到SDL2函数的方法,以便返回可以将其blit到显示器上的Surface?


自己解析位图数据并使用SDL相应地设置像素。我之前做过一个类似的模拟器,其中1个位表示1个像素,最简单的方法就是自己解析它。我认为你所要求的是不可能的。 - Fredrik
好的,谢谢。我可以问一下你映射到的屏幕有多大吗?你是每帧都解析每个像素值吗? - dvanaria
1
SDL支持两种1位单色图像格式:SDL_PIXELFORMAT_INDEX1MSB(首选)和SDL_PIXELFORMAT_INDEX1LSB。每像素的位数仍然至少为8,但应易于转换为真正的每像素1位自定义格式。 - Nelfeal
最简单的方法是使用带有2色(黑白)调色板的SDL_PIXELFORMAT_INDEX8格式。然后只需将图像中的每个像素映射为0或1即可。 - Mark Benningfield
1个回答

2

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

Links:
https://wiki.libsdl.org/SDL_Surface
https://wiki.libsdl.org/SDL_PixelFormatEnum


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接