【问题标题】:SDL texture array?SDL纹理数组?
【发布时间】:2016-04-10 10:07:11
【问题描述】:

我是 sdl 的新手。我正在制作二十一点游戏。我想制作一系列纹理。我想知道是否有人可以帮助我。这是我一直在尝试做的事情:

// array of textures for the extra player cards
    SDL_Texture *hitCardsText[] = { NULL };

// this does not give me errors but i dont know if it is right

    hitCardsText[0] = loadTexture(ren, cards[dynamicPlayerCards[0]]);
    hitCardsText[1] = loadTexture(ren, cards[dynamicPlayerCards[1]]);

// i get an error here

SDL_DestroyTexture(hitCardsText[0]);
SDL_DestroyTexture(hitCardsText[1]);

我在上面的代码中指出了这个错误(我的文件称为 introSDL.exe btw):

introSDL.exe 中 0x6C78CE9A (SDL2.dll) 处的未处理异常:0xC0000005:访问冲突读取位置 0x00000050。

【问题讨论】:

    标签: c++ arrays sdl


    【解决方案1】:

    您正在写入超出数组范围。

    SDL_Texture *hitCardsText[] = { NULL };
    

    只有 1 个元素。如果你想要更多,你要么需要向初始化列表中添加更多元素,要么在方括号中指定确切的数量。

    如果您想要一个动态大小的数组,请使用std::vector

    std::vector<SDL_Texture*> hitCardsText;
    hitCardsText.push_back(loadTexture(ren, cards[dynamicPlayerCards[0]]));
    hitCardsText.push_back(loadTexture(ren, cards[dynamicPlayerCards[1]]));
    

    【讨论】:

    • 感谢您的回复。你说的是有道理的,但现在我在 introSDL.exe 中的 0x779DDAD8 处得到一个未处理的异常:Microsoft C++ 异常:std::bad_alloc at memory location 0x0018EFE4 at hitCardsText.push_back(loadTexture(ren,cards[dynamicPlayerCards[0]] ));
    猜你喜欢
    • 1970-01-01
    • 2019-06-09
    • 2011-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多