c++ - SDL 加载我的图像搞砸了

标签 c++ image-processing sdl

我正在尝试加载我从 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/

相关文章:

java - 从 C++ 在 Java 中重新组装两个字节,没有得到预期的结果

c++ - 预期类型说明符

image - 一个从图像库中检测图像的库?

rust - headless SDL 事件处理

linux - 在 Linux 上安装 lispbuilder-SDL 时出现问题。 - "Don' t 知道如何要求 ASDF-INSTALL"

c++ - SDL 窗口未出现

c++ - 编译期间 boost xml 序列化失败

C++ : Function overloading vs Variadic function vs Variadic template vs default parameter

python - Python 中的细胞分割和荧光计数

java - BufferedImage 的像素访问 - 内存泄漏?