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



我的代码中的调整导致奇怪的事件,导致段错误。通过 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;
}

}
};

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

您将更改以下代码行:

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

对此:

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

与号返回变量wh的地址。

如果您想了解有关C++中的指针的更多信息,请访问以下链接:

与号 (&) 在 C++ 中如何工作?

相关内容

最新更新