最初在SDL中,以下代码可用于设置曲面:
SDL_Surface *screen = SDL_SetVideoMode(800, 600, 8, 0);
现在,在SDL2中必须使用以下代码:
SDL_Window *window = SDL_CreateWindow("Title", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, 0);
SDL_Surface *surface = SDL_GetWindowSurface(window);
我的问题是SDL_CreateWindow无法设置窗口或曲面的bbp。它似乎默认为32位。我尝试使用SDL_ConvertSurfaceFormat
,但surface->format->BitsPerPixel
保持在32。
如何在SDL 2中创建8位表面?我意识到我需要一个调色板。
答案 0 :(得分:1)
使用SDL_CreateRGBSurface指定位数。
示例:
SDL_Window *sdlWindow;
SDL_Renderer *sdlRenderer;
SDL_CreateWindowAndRenderer(0, 0, SDL_WINDOW_FULLSCREEN_DESKTOP, &sdlWindow, &sdlRenderer);
SDL_Surface *screen = SDL_CreateRGBSurface(0, 640, 480, 32,
0x00FF0000,
0x0000FF00,
0x000000FF,
0xFF000000);
SDL_Texture *sdlTexture = SDL_CreateTexture(sdlRenderer,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
640, 480);
SDL_UpdateTexture(sdlTexture, NULL, screen->pixels, screen->pitch);
SDL_RenderClear(sdlRenderer);
SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, NULL);
SDL_RenderPresent(sdlRenderer);