可执行程​​序内嵌入SDL形象 [英] SDL embed image inside program executable

查看:113
本文介绍了可执行程​​序内嵌入SDL形象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用SDL一个程序,它可以在运行时使用中嵌入的图像。

Is it possible to embed an image within a program using SDL which can be used at run time.

例如,我有一个计划,带来了含有标志和版权信息启动闪屏。而不是在一个位图文件有这个映像,并使用SDL_LoadBMP将其加载到SDL_Surface。我想有嵌入程序二进制图像,以阻止别人可能改变开机画面和版权的名字。

For example, I have a program which brings up a splash screen on startup containing the logo and copyright information. Rather than having this image in a bitmap file and using SDL_LoadBMP to load it to a SDL_Surface. I would like to have the image embedded in the program binary, to stop someone potentially changing the splash image and copyright name.

有没有人有办法做到这一点有什么建议?例如code将是巨大的。

Does anyone have any suggestions on ways to do this? Example code would be great.

推荐答案

在一个可执行嵌入的文件是容易的,但也有一些陷阱,有几种方法可以做到这一点,包括一些便携式和非便携式的方式。

Embedding a file in an executable is easy but there are some gotchas, there are several ways to do it including some portable and non-portable ways.

写一个脚本,以将图像转换为一个常量数组中的C.该脚本会看在Python是这样的:

Write a script to convert the image to a constant array in C. The script would look something like this in Python:

#!/usr/bin/env python3
print("static const unsigned char IMAGE_DATA[] = {{{}}};".format(
        ",".join(str(b) for b in open("myimage.bmp", "rb").read())))

只是管输出到 *的.h 文件,包括来自另一个文件中的文件。你可以用获取文件的大小的sizeof(IMAGE_DATA)

Just pipe the output to a *.h file and include that file from one other file. You can get the size of the file with sizeof(IMAGE_DATA).

优点:便携

缺点:需要Python进行安装,如果数组是编译器的过大无法正常工作,需要增加一个自定义步骤来构建系统

Disadvantages: requires Python to be installed, does not work if array is too large for compiler, requires adding a custom step to the build system

这是更依赖于平台的。与GNU binutils的工具链(如Linux)的平台,您可以用 objcopy把,我觉得 bin2obj 适用于微软的工具链。

This is more platform-dependent. On platforms with GNU binutils toolchains (e.g. Linux) you can use objcopy, I think bin2obj works on Microsoft toolchains.

优点:作品无处不在

缺点:不可移植,需要添加一个自定义步骤来构建系统,自定义步骤可能会非常棘手得到正确

Disadvantages: non-portable, requires adding a custom step to the build system, the custom step might be tricky to get right

objcopy把程序允许您指定二进制作为输入格式,但你需要明确指定架构...所以你将不得不修改命令为可执行的i386和64位版本。

The objcopy program lets you specify binary as the input format, but then you need to specify the architecture explicitly... so you will have to modify the command for i386 and x64 versions of your executable.

$ objcopy --input binary --output elf32-i386 --binary-architecture i386 \
    myimage.bmp myimage.o

您可以通过使用下面的声明得到C中的数据:

You can get the data from C by using the following declarations:

// Ignore the fact that these are char...
extern char _binary_myimage_bmp_start, _binary_myimage_bmp_end;

#define MYIMAGE_DATA ((void *) &_binary_myimage_bmp_start)
#define MYIMAGE_SIZE \
    ((size_t) (&_binary_myimage_bmp_end - &_binary_myimage_bmp_start))

使用汇编指令

奇怪的是,嵌入一个静态文件汇编相当容易。汇编经常有像指令.incbin伪(与GAS和YASM作品)。

Use an assembler directive

Paradoxically, embedding a static file is fairly easy in assembler. Assemblers often have directives like .incbin (which works with GAS and YASM).

优点:作品无处不在

缺点:不可移植,汇编语法是平台之间的不同

Disadvantages: non-portable, assembler syntax is different between platforms

在Windows中,可以嵌入资源的EXE文件,然后使用库调用获取的资源。

On Windows, you can embed resources in an EXE and then get the resources using library calls.

优点:可能比较容易,如果你是在Windows

Advantages: probably easiest if you are on Windows

缺点:仅适用于Windows

Disadvantages: only works on Windows

这篇关于可执行程​​序内嵌入SDL形象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆