SDL加载我的图像搞砸了

SDL loading my image messed up

本文关键字:图像 加载 我的 SDL      更新时间:2023-10-16

我要加载一张从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的东西哦,如果你想仔细看看我上传的项目在这里。

哦,不好意思,我必须根据flash导出选项添加32像素的alpha图像

根据docs, 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%