gpt4 book ai didi

c++ - SDL bmp图像背景

转载 作者:行者123 更新时间:2023-11-30 03:31:04 24 4
gpt4 key购买 nike

正如您在下面看到的,有一个 bmp 图像,但它的背景颜色是白色:

enter image description here

我如何将其更改为黑色(不编辑 bmp 图像本身)?

这里我有在窗口上显示 bmp 图像的代码:

#include <iostream>
#include "SDL.h"

using namespace std;

int main(int argc, char *args[])
{
SDL_Window *window = nullptr;
SDL_Renderer *renderer = nullptr;
SDL_Surface *surface = nullptr;
SDL_Texture *texture = nullptr;
SDL_Event event;

SDL_Rect positionRect;
SDL_Rect dimensionRect;

positionRect.x = 0;
positionRect.y = 0;
positionRect.w = 100;
positionRect.h = 100;

dimensionRect.x = 0;
dimensionRect.y = 0;
dimensionRect.w = 300;
dimensionRect.h = 300;


if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s", SDL_GetError());
return 1;
}

if (SDL_CreateWindowAndRenderer(500, 500, SDL_WINDOW_OPENGL, &window, &renderer)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window and renderer: %s", SDL_GetError());
return 1;
}

surface = SDL_LoadBMP("sample.bmp");
if (!surface) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create surface from image: %s", SDL_GetError());
return 1;
}

texture = SDL_CreateTextureFromSurface(renderer, surface);
if (!texture) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture from surface: %s", SDL_GetError());
return 1;
}

SDL_FreeSurface(surface);
surface = nullptr;

while (1) {
SDL_PollEvent(&event);
if (event.type == SDL_QUIT) {
break;
}
//SDL_SetRenderDrawColor(renderer, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, &positionRect, &dimensionRect);
SDL_RenderPresent(renderer);
}

SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);

SDL_Quit();

return 0;
}

最佳答案

你应该添加这两行,

Uint32 colorkey = SDL_MapRGB(surface->format, 255, 255, 255);
SDL_SetColorKey(surface, SDL_TRUE, colorkey);

在你的代码中的这一行之前

texture = SDL_CreateTextureFromSurface(renderer, surface);

关于c++ - SDL bmp图像背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44512619/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com