C++ SDL_GetPixel指针问题

C++ SDL_GetPixel指针问题,c++,pointers,sdl,getpixel,C++,Pointers,Sdl,Getpixel,这是我的第一个问题: 下面这两个函数中的第一个在某种程度上可以正常工作: Uint32 AWSprite::get_pixelColor_location(SDL_Surface * surface, int x, int y) { int bpp = surface->format->BytesPerPixel; /* Here p is the address to the pixel we want to retrieve */ Uint8 *p = (Uint8 *)surf

这是我的第一个问题:

下面这两个函数中的第一个在某种程度上可以正常工作:

Uint32 AWSprite::get_pixelColor_location(SDL_Surface * surface, int x, int y) {
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to retrieve */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;

switch (bpp) {
case 1:
    return *p;
case 2:
    return *(Uint16 *)p;
case 3:
    if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
        return p[0] << 16 | p[1] << 8 | p[2];
    else
        return p[0] | p[1] << 8 | p[2] << 16;
case 4:
    return *(Uint32 *)p;
default:
    return 0;       
}
Uint32 AWSprite::获取像素颜色位置(SDL\u Surface*Surface,int x,int y){
int bpp=表面->格式->字节/像素;
/*这里p是我们要检索的像素的地址*/
Uint8*p=(Uint8*)表面->像素+y*表面->基音+x*bpp;
交换机(bpp){
案例1:
返回*p;
案例2:
返回*(Uint16*)p;
案例3:
if(SDL_字节顺序==SDL_大字节)
返回p[0]w){
如果(ih/2){
每像素透明像素计数=1;
top_gap++;
}否则{
每像素透明像素计数=1;
底部间隙++;
}
}
}
}
int realHeight=帧[f]->h-(顶部间隙+底部间隙);
absolute_sprite[f]=新帧_图像_absolute();
绝对雪碧[f]->偏移量y=顶部间隙;
绝对精灵[f]->高度=真实高度;
}
}

当我运行此程序时,我得到: SE Game.exe中0x00173746处的未处理异常:0xC0000005:访问冲突读取位置0x03acc0b8

在调试过程中,我发现它在以下位置崩溃: 当迭代器变量f==31,i==38,j=139 并在“return*(Uint32*)p行中的AWSprite::get_pixelColor_location()处停止

我发现如果我再次运行它并逐行调试,那么我有时会工作,有时不会!!!所以我的意思是“当f>30,I,j迭代器值时,它会随机崩溃”


发生了什么…

我还不能对这个问题发表评论,但这里有一些问题:

j来自哪里?基于
get\u pixelColor\u location
函数,我假设您正在迭代曲面的宽度。您发布的代码中似乎缺少这一部分

你确认i和j在你的曲面范围内了吗

此外,您似乎没有解锁曲面


在这里运行函数似乎可以充分发挥作用,因此我怀疑您正在使用无效参数读取缓冲区外的数据。

是的,我现在刚刚检查了它,您是对的,我正在使用无效参数读取缓冲区外的数据。我没有包括j,因为代码是I Thank emartel的副本
void AWSprite::set_all_frame_image_actual_size() {
/* This function finds an entire rows that has transparency
   then stores the amount of rows to a Frame_image_absolute structure
*/
absolute_sprite = new Frame_image_absolute*[howManyFrames];
for (int f = 0; f < howManyFrames; f++) {
    SDL_LockSurface(frames[f]);
    int top_gap = 0; int bottom_gap = 0;
    int per_transparent_px_count = 1;
    for (int i = 0; i < frames[f]->h; i++) {
        int per_transparent_px_count = 1;
            if (this->get_pixelColor_location(frames[f], j, i) == transparentColour) per_transparent_px_count++;
            if (per_transparent_px_count >= frames[f]->w) {
                if (i < frames[f]->h / 2) {
                    per_transparent_px_count = 1;
                    top_gap++; 
                } else {
                    per_transparent_px_count = 1;
                    bottom_gap++;
                }
            }
        }
    }
    int realHeight = frames[f]->h - (top_gap + bottom_gap);
    absolute_sprite[f] = new Frame_image_absolute();
    absolute_sprite[f]->offset_y = top_gap;
    absolute_sprite[f]->height = realHeight;
}