从 GIMP 导出的 C 源文件加载图像

发布于 2024-12-13 17:02:08 字数 1144 浏览 4 评论 0 原文

我正在尝试加载游戏的 openGL 纹理。纹理是已从 GIMP 导出为 .C 源文件 的图像。当我在项目中#include此文件时(使用 Visual C++ 2010 Ultimate),出现编译器错误,显示致命错误 C1091:编译器限制:字符串长度超过 65535 字节

任何解决方法吗?

我想将图像导出为 C 头文件 的原因是,程序可以与图像一起编译,并且我不必提供 原始 图像文件以及可执行文件。

代码:

#include <iostream>
#include <Windows.h>
#include <glfw.h>
#include "X.c"

#define X 1
#define O 2

#pragma comment(lib, "glfw.lib")
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "gdi32.lib")

using namespace std;

float render();
void stepGame(float);
void keyboard(int, int);

int main(int argv, int *argc[])
{
    glfwInit();
    glfwOpenWindow(480, 480, 16, 16, 16, 16, 16, 16, GLFW_WINDOW);
    glfwSetKeyCallback(keyboard);
    glfwSetWindowTitle("Tic Tac Toe!");
    glClearColor(1.0, 1.0, 1.0, 1.0);
    float dT;

    while(glfwGetWindowParam(GLFW_OPENED) > 0)
    {
        glfwPollEvents();
        dT = render();
        stepGame(dT);
    }
    return 0;
}

图片文件: Xc

I'm trying to load an openGL texture for a game. The texture is an image that has been exported as a .C source file from GIMP. When I #include this file in my project (using Visual C++ 2010 Ultimate), I get a compiler error saying fatal error C1091: compiler limit: string exceeds 65535 bytes in length

Is there any workaround ?

The reason I wanted to export the image as a C header file was so that the program compiles with the image, and I dont have to provide raw image files along with the executable.

Code:

#include <iostream>
#include <Windows.h>
#include <glfw.h>
#include "X.c"

#define X 1
#define O 2

#pragma comment(lib, "glfw.lib")
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "gdi32.lib")

using namespace std;

float render();
void stepGame(float);
void keyboard(int, int);

int main(int argv, int *argc[])
{
    glfwInit();
    glfwOpenWindow(480, 480, 16, 16, 16, 16, 16, 16, GLFW_WINDOW);
    glfwSetKeyCallback(keyboard);
    glfwSetWindowTitle("Tic Tac Toe!");
    glClearColor(1.0, 1.0, 1.0, 1.0);
    float dT;

    while(glfwGetWindowParam(GLFW_OPENED) > 0)
    {
        glfwPollEvents();
        dT = render();
        stepGame(dT);
    }
    return 0;
}

Image file:
X.c

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

数理化全能战士 2024-12-20 17:02:08

这是一个替代解决方案。将图像导出为原始数据文件,然后使用 bin2hex 将其转换为C/C++ 数组。这会很好地工作,因为该脚本生成的不是一个巨大的字符串,而是一个字符数组。这是一个例子:

$ bin2hex.pl
bin2hex.pl by Chami.com

usage:
  perl bin2hex.pl <binary file> <language id>

  <binary file> : path to the binary file
  <language id> : 0 = Perl, 1 = C/C++/Java, 2 = Pascal/Delphi

$ bin2hex.pl x.bin 1

/* begin binary data: */
char bin_data[] = /* 112 */
{0x23,0x69,0x6E,0x63,0x6C,0x75,0x64,0x65,0x20,0x3C,0x73,0x74,0x64,0x69,0x6F
,0x2E,0x68,0x3E,0x0A,0x23,0x69,0x6E,0x63,0x6C,0x75,0x64,0x65,0x20,0x3C,0x74
,0x69,0x6D,0x65,0x2E,0x68,0x3E,0x0A,0x0A,0x69,0x6E,0x74,0x20,0x6D,0x61,0x69
,0x6E,0x28,0x69,0x6E,0x74,0x20,0x61,0x72,0x67,0x63,0x2C,0x20,0x63,0x68,0x61
,0x72,0x2A,0x20,0x61,0x72,0x67,0x76,0x5B,0x5D,0x29,0x0A,0x7B,0x0A,0x20,0x20
,0x20,0x20,0x70,0x72,0x69,0x6E,0x74,0x66,0x28,0x22,0x25,0x64,0x5C,0x6E,0x22
,0x2C,0x20,0x73,0x69,0x7A,0x65,0x6F,0x66,0x28,0x74,0x69,0x6D,0x65,0x5F,0x74
,0x29,0x29,0x3B,0x0A,0x7D,0x0A,0x0A};
/* end binary data. size = 112 bytes */

Here is an alternative solution. Export the image as a raw data file, then use bin2hex to convert it to a C/C++ array. This will work fine because instead of a huge string this script generates a char array. Here is an example:

$ bin2hex.pl
bin2hex.pl by Chami.com

usage:
  perl bin2hex.pl <binary file> <language id>

  <binary file> : path to the binary file
  <language id> : 0 = Perl, 1 = C/C++/Java, 2 = Pascal/Delphi

$ bin2hex.pl x.bin 1

/* begin binary data: */
char bin_data[] = /* 112 */
{0x23,0x69,0x6E,0x63,0x6C,0x75,0x64,0x65,0x20,0x3C,0x73,0x74,0x64,0x69,0x6F
,0x2E,0x68,0x3E,0x0A,0x23,0x69,0x6E,0x63,0x6C,0x75,0x64,0x65,0x20,0x3C,0x74
,0x69,0x6D,0x65,0x2E,0x68,0x3E,0x0A,0x0A,0x69,0x6E,0x74,0x20,0x6D,0x61,0x69
,0x6E,0x28,0x69,0x6E,0x74,0x20,0x61,0x72,0x67,0x63,0x2C,0x20,0x63,0x68,0x61
,0x72,0x2A,0x20,0x61,0x72,0x67,0x76,0x5B,0x5D,0x29,0x0A,0x7B,0x0A,0x20,0x20
,0x20,0x20,0x70,0x72,0x69,0x6E,0x74,0x66,0x28,0x22,0x25,0x64,0x5C,0x6E,0x22
,0x2C,0x20,0x73,0x69,0x7A,0x65,0x6F,0x66,0x28,0x74,0x69,0x6D,0x65,0x5F,0x74
,0x29,0x29,0x3B,0x0A,0x7D,0x0A,0x0A};
/* end binary data. size = 112 bytes */
铜锣湾横着走 2024-12-20 17:02:08

我做了一个更好的 github 存储库 上找到。希望有帮助。

I made a better gimp plug-in in python to export image data as C code. Feel free to use it or make it better for custom use. The scripts are available on a github repository. Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文