使用 SDL_SetWindowIcon 无法正常工作
Using SDL_SetWindowIcon is not working properley
我正在尝试使用 SDL 编写游戏引擎,并使用 picoPNG 作为图像加载器。我试图创建一个系统来为 Window class 中的 window 设置图标,但发生了一些奇怪的事情。看起来该图标适用于某些图像,但不适用于其他图像。我对 SDL_Surface 的工作原理几乎一无所知,所以我使用了一些网站来查找一些信息。 (我无法 post 链接到它们,因为我只有十分之八的所需声誉)
我的代码:
void Window::setWindowIcon(const std::string& filePath) {
//read file
std::vector<unsigned char> in;
std::vector<unsigned char> out;
unsigned long width, height;
//Use my file loading class to read the image file
if (DPE::IOManager::readFileToBuffer(filePath, in) == false) {
fatalError("Failed to open " + filePath);
}
int errorCode = DPE::decodePNG(out, width, height, &(in[0]), in.size());
if (errorCode != 0) {
fatalError("Failed to decode png file!");
}
Uint32 rmask = 0x000000ff;
Uint32 gmask = 0x0000ff00;
Uint32 bmask = 0x00ff0000;
Uint32 amask = 0xff000000;
_sdlSurface = SDL_CreateRGBSurfaceFrom((void*)&out[0], width, height, 32, width * 4, rmask, gmask, bmask, amask);
if (_sdlSurface == NULL) {
std::cout << SDL_GetError() << std::endl;
fatalError("Failed to create surface!");
}
SDL_SetWindowIcon(_sdlWindow, _sdlSurface);
SDL_FreeSurface(_sdlSurface);
}
最后,这是两个png文件
This one Worked.
This one didn't.
代码迭代显示一切正常,唯一的错误通知是图标没有改变。
编辑:我已将颜色遮罩更改为跨字节序兼容
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
int shift = 0;
rmask = 0xff000000 >> shift;
gmask = 0x00ff0000 >> shift;
bmask = 0x0000ff00 >> shift;
amask = 0x000000ff >> shift;
#else // little endian, like x86
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif
我想我找到了答案。看起来当我使用 alpha 像素时,它占用了 8 个 bpp,所以我将文件大小减小到 75x75,并且图像有效。
我正在尝试使用 SDL 编写游戏引擎,并使用 picoPNG 作为图像加载器。我试图创建一个系统来为 Window class 中的 window 设置图标,但发生了一些奇怪的事情。看起来该图标适用于某些图像,但不适用于其他图像。我对 SDL_Surface 的工作原理几乎一无所知,所以我使用了一些网站来查找一些信息。 (我无法 post 链接到它们,因为我只有十分之八的所需声誉)
我的代码:
void Window::setWindowIcon(const std::string& filePath) {
//read file
std::vector<unsigned char> in;
std::vector<unsigned char> out;
unsigned long width, height;
//Use my file loading class to read the image file
if (DPE::IOManager::readFileToBuffer(filePath, in) == false) {
fatalError("Failed to open " + filePath);
}
int errorCode = DPE::decodePNG(out, width, height, &(in[0]), in.size());
if (errorCode != 0) {
fatalError("Failed to decode png file!");
}
Uint32 rmask = 0x000000ff;
Uint32 gmask = 0x0000ff00;
Uint32 bmask = 0x00ff0000;
Uint32 amask = 0xff000000;
_sdlSurface = SDL_CreateRGBSurfaceFrom((void*)&out[0], width, height, 32, width * 4, rmask, gmask, bmask, amask);
if (_sdlSurface == NULL) {
std::cout << SDL_GetError() << std::endl;
fatalError("Failed to create surface!");
}
SDL_SetWindowIcon(_sdlWindow, _sdlSurface);
SDL_FreeSurface(_sdlSurface);
}
最后,这是两个png文件 This one Worked. This one didn't.
代码迭代显示一切正常,唯一的错误通知是图标没有改变。
编辑:我已将颜色遮罩更改为跨字节序兼容
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
int shift = 0;
rmask = 0xff000000 >> shift;
gmask = 0x00ff0000 >> shift;
bmask = 0x0000ff00 >> shift;
amask = 0x000000ff >> shift;
#else // little endian, like x86
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif
我想我找到了答案。看起来当我使用 alpha 像素时,它占用了 8 个 bpp,所以我将文件大小减小到 75x75,并且图像有效。