如何合成 SDL2 纹理?

时间:2021-03-04 01:34:20

标签: c mfc sdl-2

通过绘制两个纹理来创建。 一个是 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, 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, SDL_PIXELFORMAT_RGBA8888, 
SDL_TEXTUREACCESS_STREAMING, 12, 12);
unsigned char* pixels_bytes = nullptr;
int pixels_pitch = 0;
SDL_LockTexture(Pixels_Tx, nullptr, reinterpret_cast<void**>(&pixels_bytes), &pixels_pitch);
unsigned char rgba[4] = { 255, 0, 0, 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, 255, 255, 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

0 个答案:

没有答案
相关问题