2

I am getting a 3 error code from an SDL executable, and it seems to be in a place where I pass a SDL color by value and I don't understand the reason.

void Map::draw(SDL_Surface *surface, int level){
//the surface is locked
if ( SDL_MUSTLOCK(surface) )
    SDL_LockSurface(surface);

long start= (long)level * this->xmax * this->ymax;
long end= (long)(level+1) * this->xmax * this->ymax;

for(long n=start; n<end; ++n){
    Node *pn= this->nodes+n;
    //exit(18); //exit code is 18
    draw_pixel_nolock(surface, pn->location.x, pn->location.y, colors[pn->content]);
}

//the surface is unlocked
if ( SDL_MUSTLOCK(surface) )
    SDL_UnlockSurface(surface);

}

And the graphics function called is:

SDL_Color colors[]= { {0,0,0},  {0xFF,0,0},  {0,0xFF,0},  {0,0,0xFF} };

void PutPixel32_nolock(SDL_Surface * surface, int x, int y, Uint32 color)
{
    Uint8 * pixel = (Uint8*)surface->pixels;
    pixel += (y * surface->pitch) + (x * sizeof(Uint32));
    *((Uint32*)pixel) = color;
}

void PutPixel24_nolock(SDL_Surface * surface, int x, int y, Uint32 color)
{
    Uint8 * pixel = (Uint8*)surface->pixels;
    pixel += (y * surface->pitch) + (x * sizeof(Uint8) * 3);
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
    pixel[0] = (color >> 24) & 0xFF;
    pixel[1] = (color >> 16) & 0xFF;
    pixel[2] = (color >> 8) & 0xFF;
#else
    pixel[0] = color & 0xFF;
    pixel[1] = (color >> 8) & 0xFF;
    pixel[2] = (color >> 16) & 0xFF;
#endif
}

void PutPixel16_nolock(SDL_Surface * surface, int x, int y, Uint32 color)
{
    Uint8 * pixel = (Uint8*)surface->pixels;
    pixel += (y * surface->pitch) + (x * sizeof(Uint16));
    *((Uint16*)pixel) = color & 0xFFFF;
}

void PutPixel8_nolock(SDL_Surface * surface, int x, int y, Uint32 color)
{
    Uint8 * pixel = (Uint8*)surface->pixels;
    pixel += (y * surface->pitch) + (x * sizeof(Uint8));
    *pixel = color & 0xFF;
}

//this function draws a pixel of wanted color on a surface at (x,y) coordinate
void draw_pixel_nolock(SDL_Surface *surface, int x, int y, SDL_Color s_color)
{  exit(19);//exit code is 3
    //SDL_MapRGB return a color map depending on bpp (definition)
    Uint32 color = SDL_MapRGB(surface->format, s_color.r, s_color.g, s_color.b);

    //byte per pixel
    int bpp = surface->format->BytesPerPixel;

    //here is checked the number of byte used by our surface
    switch (bpp)
    {
        case 1: // 1 byte =>  8-bpp
            PutPixel8_nolock(surface, x, y, color);
            break;
        case 2: // 2 byte => 16-bpp
            PutPixel16_nolock(surface, x, y, color);
            break;
       case 3: // 3 byte => 24-bpp
            PutPixel24_nolock(surface, x, y, color);
            break;
      case 4: // 4 byte => 32-bpp
            PutPixel32_nolock(surface, x, y, color);
            break;
    }
}

The code returns error code 18 when I exit there, but never returns error code 19, and gives errror code 3 instead. What could possibly go wrong?

4

1 回答 1

2

没有看到整个代码很难说,但作为一般做法:

验证

long start= (long)level * this->xmax * this->ymax;
long end= (long)(level+1) * this->xmax * this->ymax;

start并且endnode数组的有效偏移量,否则this->node + n将返回垃圾指针。

验证

Node *pn= this->nodes+n;

不是指向对象null有效指针Node

验证

pn->content

在您的colors数组范围内

于 2012-11-28T14:44:13.830 回答