2012-11-27 52 views
2

我從SDL可執行文件中獲取3錯誤代碼,並且它似乎位於按值傳遞SDL顏色的位置,但我不明白原因。SDL從代碼中奇怪的位置返回代碼3代碼

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); 

}

並稱爲圖形功能是:

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; 
    } 
} 

代碼返回錯誤代碼18,當我離開那裏,但不會返回錯誤代碼19,並給出errror代碼3代替。什麼可能會出錯?

回答

2

沒有看到全部的代碼,這很難說,但作爲一般的做法:

驗證

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

startend是您node陣列的有效補償,否則this->node + n將返回一個垃圾指針。

驗證

Node *pn= this->nodes+n; 

是不是null有效的指針Node對象

驗證

pn->content 

是您colors數組的邊界內

+0

你是對的,我沒有初始化「級別」,雖然我確實有代碼來驗證該級別是否可以隨時更改。 – Adder