我正在尝试加载我从 Flash CS3 导出的图像,它是一张非常可爱的脸,但它加载时非常奇怪,它以蓝色方式加载这是两个文件的代码:
//main.cpp
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "test.hpp"
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_VIDEO);
// Activamos modo de video
screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE | SDL_DOUBLEBUF);
image = IMG_Load("face.bmp");
dest.x = 200;
dest.y = 200;
//Main Loop
while(Abierto)
{
//We Draw
Draw();
//Events
while( SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
Abierto = false;
}
}
// We free the image
SDL_FreeSurface(image);
SDL_Quit();
return 0;
}
现在另一个;
//test.hpp
DL_Surface *image = NULL, *screen = NULL;
SDL_Rect dest;
SDL_Event event;
bool Abierto = true;
float PlaneX = 300, PlaneY = 200;
float velX = 0.1, velY = 0.1;
void Draw()
{
Uint32 color;
// Black Background is created
color = SDL_MapRGB (screen -> format, 0, 0, 0);
SDL_FillRect (screen, NULL, color);
SDL_DisplayFormatAlpha(image);
SDL_BlitSurface(image, NULL, screen, &dest);
// Flip the working image buffer with the screen buffer
SDL_Flip (screen);
}
我需要这方面的帮助,我在 SDL 方面没有那么丰富的经验哦,如果你想仔细看看,我上传了项目 here .
糟糕,我必须根据 Flash 导出选项添加图像为 32 像素和 alpha
最佳答案
根据文档,SDL_DisplayFormatAlpha
返回一个新图像并保持原始图像不变。
因此,在加载图像时尝试第一部分:
SDL_Surface *origImage = IMG_Load("face.bmp");
image = SDL_DisplayFormatAlpha(origImage);
SDL_FreeSurface(origImage)
因为不需要在每一帧调用 SDL_DisplayFormatAlpha
。
然后在第二部分,只是 blit image
,没有调用 SDL_DisplayFormatAlpha
。
更新
我刚刚查看了您的图片,它看起来像是一个奇怪 bmp。我以前见过:BMP 格式是如此困惑,如果您不掌握基本知识,不同的程序很可能会以不同的方式解释数据。
在你的情况下:
display face.bmp
正确显示。gthumb face.bmp
什么也没显示。eog face.bmp
表示“伪造的标题数据”。
我强烈建议您对所有游戏卡通图片使用 PNG 文件,对所有照片图片使用 JPG。
所以运行
$ convert face.bmp face.png
并使用 PNG 文件。我会做得更好,您将得到一个比原始文件大 20% 的文件。
关于c++ - SDL 加载我的图像搞砸了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8582619/