简体   繁体   English

resource.rc文件中返回多个资源

[英]Multiple resources returned in resource.rc file

Ok, I think I know what is wrong, but I don't know how to fix this. 好吧,我想我知道出了什么问题,但我不知道如何解决这个问题。

LoadResource is returning text from multiple resources. LoadResource从多个资源返回文本。

(These are resources compiled into my EXE by MINGW's 'winres' utility.) (This is NOT an OpenGL question.) (这些是由MINGW的'winres'实用程序编译到我的EXE中的资源。)(这不是一个OpenGL问题。)

I am trying to load the text of a shader into memory from a resource stored in the EXE. 我试图从存储在EXE中的资源将着色器的文本加载到内存中。

Here are the relivant pieces of code: 以下是重要的代码片段:

Where I am calling my function from: 我从哪里调用我的函数:

void SetupDisplay() {
    UINT vShader = LoadShaderResource (VERTEX1, GL_VERTEX_SHADER);
    UINT fShader = LoadShaderResource (FRAGMENT1, GL_FRAGMENT_SHADER);

.... ....

The top half where it's starting the process of loading the shader.... (I put a bunch of stuff in here trying to locate the problem, I walked back until I found this: 它开始加载着色器的过程中的上半部分....(我在这里放了一堆东西试图找到问题,我走回去,直到我发现这个:

UINT LoadShaderResource (int index, int type) {
    std::vector<std::string> Lines;
    std::string tShader = LoadTextFileResource(index);
    std::cerr << "-------BEGIN " << index << "\n";
    std::cerr <<  tShader;
    std::cerr << "-------END\n";
    std::istringstream iss(tShader);

... the rest isn't relivant, the shader complie crashes, at the bottom is the diagnostics this produces as to why. ...其余部分不是重复,着色器组合崩溃,底部是诊断产生的原因。 I use istringstream because I get all the text back in one big glob, but that's not the issue, (I have code that follows this that splits it.) the issue is both files get included at the same time when they are not supposed to be, at least for one of them, my guess is how they are stored, but I don't know how to fix this. 我使用istringstream是因为我将所有文本都放回到一个大的glob中,但这不是问题,(我有跟随它的代码将它分开。)问题是两个文件在它们不应该同时包含在同一时间至少对于他们中的一个,我的猜测是它们是如何存储的,但我不知道如何解决这个问题。

Ok, Maybe something needs to be done here? 好吧,也许有些事需要在这里完成?

std::string LoadTextFileResource(int name) {
    HMODULE handle = GetModuleHandleA(NULL);
    HRSRC rc = FindResourceA(handle, MAKEINTRESOURCE(name),  RT_RCDATA);
    HGLOBAL rcData = LoadResource(handle, rc);
    std::string result = (const char*) LockResource(rcData);
    return result;
 }

Ok, that's all the C++ code, now for the resouce stuff: 好的,这就是所有的C ++代码,现在用于资源:

resource.rc RESOURCE.RC

// Generated by ResEdit 1.5.11
// Copyright (C) 2006-2012
// http://www.resedit.net

#include <windows.h>
#include <commctrl.h>
#include <richedit.h>
#include "resource.h"


LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
VERTEX1        RCDATA         "..\\Data\\shader.vert"


LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
FRAGMENT1      RCDATA         "..\\Data\\shader.frag"


//
// Icon resources
//
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDI_ICON1          ICON           "..\\Data\\Generic.ico"

.... I show the icon only so you can see what is following, the rest is version info and such. ....我只显示图标,以便您可以看到正在关注的内容,其余的是版本信息等。

My resource.h file to link my code to the resource.rc file: 我的resource.h文件将我的代码链接到resource.rc文件:

#ifndef IDC_STATIC
#define IDC_STATIC (-1)
#endif

#define IDI_ICON1                               100
#define VERTEX1                                 110
#define FRAGMENT1                               120

Now, the text of the shaders. 现在,着色器的文本。 (remember, this is not an OpenGL question.) (记住,这不是一个OpenGL问题。)

shader.frag shader.frag

#version 330
#pragma optimize(off)
#pragma debug(on)

smooth in vec3 theColor;
out vec4 outputColor;

void main()
{
    outputColor = vec4(theColor, 1.0);
}

shader.vert shader.vert

#version 330
#pragma optimize(off)
#pragma debug(on)

layout (location = 0) in vec3 inPosition;
layout (location = 1) in vec3 inColor;

smooth out vec3 theColor;

void main()
{
    gl_Position = vec4(inPosition, 1.0);
    theColor = inColor;
}

Ok, with all that out of the way, here is what it produces. 好吧,尽管如此,这就是它产生的东西。 (including the double spacing.) (包括双倍间距。)

-------BEGIN 110
#version 330

#pragma optimize(off)

#pragma debug(on)



layout (location = 0) in vec3 inPosition;

layout (location = 1) in vec3 inColor;



smooth out vec3 theColor;



void main()

{

    gl_Position = vec4(inPosition, 1.0);

    theColor = inColor;

}

#version 330

#pragma optimize(off)

#pragma debug(on)



smooth in vec3 theColor;

out vec4 outputColor;



void main()

{

    outputColor = vec4(theColor, 1.0);

}

-------END
-------BEGIN 120
#version 330

#pragma optimize(off)

#pragma debug(on)



smooth in vec3 theColor;

out vec4 outputColor;



void main()

{

    outputColor = vec4(theColor, 1.0);

}

-------END

Ok, now if you notice, BEGIN 110 contains both shaders, where BEGIN 120 does not. 好的,现在如果你注意到,BEGIN 110包含两个着色器,BEGIN 120没有。 Of course, both shaders in the 110 is what is blowing things up. 当然,110中的两个着色器都是吹嘘的东西。 What I can not understand is where is it getting both shaders from in 110? 我无法理解的是它在110中获得两个着色器的位置是什么?

There is something going on here I do not understand, but I am lost as what to look at next. 这里有些事情我不明白,但我迷失了,接下来要看什么。

In LoadTextFileResource() you copy the resource data into a std::string using std::string::operator=(const char*) . LoadTextFileResource() ,使用std::string::operator=(const char*)将资源数据复制到std::string This operator assumes that your string is null -terminated. 此运算符假定您的字符串为null -terminated。 But the pointer you give it is not necessarily pointing to a null -terminated string. 但是你给它的指针不一定指向一个null -terminated字符串。 To fix this, you need to call SizeofResource() to determine the actual lenght of the string: 要解决此问题,您需要调用SizeofResource()来确定字符串的实际长度:

std::string LoadTextFileResource(int name) {
    HMODULE handle = GetModuleHandleA(NULL);
    HRSRC rc = FindResourceA(handle, MAKEINTRESOURCE(name),  RT_RCDATA);
    HGLOBAL rcData = LoadResource(handle, rc);
    DWORD data_size = ::SizeofResource(handle, rc);

    std::string result;

    if (data_size != 0) {
        const char* data = (const char*)::LockResource(rcData);
        result.assign(data, data_size);
    }

    return result;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM