如何合成 SDL2 纹理?

问题描述

通过绘制两个纹理来创建。 一个是 20x20 尺寸,另一个是 12x12 尺寸,所以我想把小尺寸放在大尺寸中合成。

以下是生成纹理的来源。

SDL_Texture* Background_Tx = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_RGBA8888,SDL_TEXTUREACCESS_STREAMING,20,20);
unsigned char* bytes = nullptr;
int pitch = 0;
SDL_LockTexture(Background_Tx,nullptr,reinterpret_cast<void**>(&bytes),&pitch);
unsigned char pix[4] = { 255,255,255};
for (int y = 1; y < 19; ++y) {
    for (int x = 1; x < 19; ++x) {
        memcpy(&bytes[(y * 20 + x) * sizeof(pix)],pix,sizeof(pix));
    }
}
SDL_UnlockTexture(Background_Tx);

SDL_Texture* Pixels_Tx = SDL_CreateTexture(renderer,12,12);
unsigned char* pixels_bytes = nullptr;
int pixels_pitch = 0;
SDL_LockTexture(Pixels_Tx,reinterpret_cast<void**>(&pixels_bytes),&pixels_pitch);
unsigned char rgba[4] = { 255,255 };
for (int y = 0; y < 12; ++y) {
    for (int x = 0; x < 12; ++x) {
        memcpy(&pixels_bytes[(y * 12 + x) * sizeof(rgba)],rgba,sizeof(rgba));
    }
}
SDL_UnlockTexture(Pixels_Tx);

此外,我计划在背景中平铺 20x20 纹理,但我使用 for 循环放置它。如果有更好的方法,请告诉我。

SDL_SetRenderDrawColor(renderer,255);
SDL_RenderClear(renderer);

for (int i = 0; i < SCREEN_WIDTH; i++) {
        for (int j = 0; j < SCREEN_HEIGHT; j++) {
            SDL_Rect destination = { i * pixel_size,j * pixel_size,pixel_size,pixel_size };
            SDL_RenderCopy(renderer,Background_Tx,NULL,&destination);
        }
}

SDL_RenderPresent(renderer);

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...