为什么SDL_QueryTexture没有为其int *参数赋值?

时间:2017-07-16 03:21:34

标签: c++ sdl-2

我的代码中的调整导致奇怪的事件,导致段错误。在通过gdb运行之后,我发现对SDL_QueryTexture的调用并没有将SDL_Texture的宽度和高度值分配给传递给该方法的int指针。我调用了SDL_GetError()并将其打印出来,并说:"加载SHCORE.DLL失败:找不到指定的模块。"在进行搜索时,我听说这可能与旧版本的Windows有关。我有Windows 7,但代码以前工作,所以我怀疑Windows是这里的问题,但代码。我认为导致问题的代码(包括SDL_QueryTexture调用)如下:

struct TextTexture {

private:
    SDL_Texture* texture;
    SDL_Rect destinationrect;
    int* w;
    int* h;

void loadText(string s) {
        SDL_Color color = {255,255,255};
        SDL_Surface* textsurface = TTF_RenderText_Solid(font, s.c_str(), color);
        if(textsurface == NULL) {
            cout << "Could not rendertext onto surface" << endl;
        }
        texture = SDL_CreateTextureFromSurface(r,textsurface);
        if(texture == NULL) {
            cout << "Could not make texture" << SDL_GetError() << endl;
        }
        SDL_QueryTexture(texture, NULL, NULL, w, h);
        cout << SDL_GetError() << endl;
        SDL_FreeSurface(textsurface);
        textsurface = NULL;



    }

    void textRender(int x, int y) {

        destinationrect = {x,y,*w,*h};
        if (SDL_RenderCopy(r,texture,NULL,&destinationrect) < 0) {
            cout << "Rendercopy error" << SDL_GetError() << endl;
        }


    }
};

1 个答案:

答案 0 :(得分:-1)

您的代码的问题在于您只是按值而不是通过指针传递int变量。 SDL是用C语言编写的,因此它不像C ++那样通过引用传递。因此,要允许SDL将值输入到变量中,您需要将指针传递给那些变量

你会改变这行代码:

SDL_QueryTexture(texture, NULL, NULL, w, h);

对此:

SDL_QueryTexture(texture, NULL, NULL, &w, &h);

&符号返回变量wh的地址。

如果您想了解更多关于C ++指针的信息,可以点击链接:

how does the ampersand(&) sign work in c++?

相关问题