在SDL_blitsurface中使用 SDL_Rect 向量时出现 c++ 运行时错误

Getting c++ runtime error when using a vector of SDL_Rect in SDL_blitsurface

本文关键字:SDL 运行时错误 向量 c++ blitsurface Rect      更新时间:2023-10-16

l SDL_BlitSurface(tileSheets.at(sheet), &clip[tile], screen, &tileBox);

工作得很好,我像这样启动剪辑:

clip[ 0 ].x = x;
clip[ 0 ].y = y;
clip[ 0 ].w = 48;
clip[ 0 ].h = 48;
x += 48;
clip[ 1 ].x = x;
clip[ 1 ].y = y;
clip[ 1 ].w = 48;
clip[ 1 ].h = 48;

但是这根本不起作用

SDL_BlitSurface(tileSheets.at(sheet), &clip.at(tile), screen, &tileBox);

我像这样启动它们:

for(int i = 0; i < number; i++)
    {
        SDL_Rect clipBox = {x,y,48,48};
        clip.push_back(clipBox);
    }

这是我得到的错误:http://imageshack.us/photo/my-images/836/83468944.png/

有什么线索吗?

不知道变量剪辑的类型,但可能是您将堆栈中的变量推送到向量,因此一旦退出 for 循环的范围,它们就会变为无效。

尝试以下方法之一:

 /* 1) */  SDL_Rect clipBox(x,y,48,48);
 /* 2) */  clip.push_back(SDL_Rect(x,y,48,48));