2014-02-08 38 views
0
const int PIXEL_WIDTH = 10; 
const int PIXEL_HEIGHT = 10; 
const int WORLD_X = 64; //WORLD_X * PIXEL WIDTH = SCREEN_WIDTH if you want the world to be the same size as the screen 
const int WORLD_Y = 64; 

enum Pixel_Types { 
    AIR, 
    WALL, 
    DIRT, 
    STONE 
}; 

class Pixel 
{ 
    int x, y; 
    bool affected_by_gravity; 
    Pixel_Types type; 
    public: 
     Pixel() : affected_by_gravity(false), type(AIR), x(0), y(0) {} 
     Pixel(int temp_x, int temp_y) : affected_by_gravity(false), type(AIR), x(temp_x), y(temp_y) {} 

     int getX() { return x; } //x is 0-63, scales up in the rendering code 
     int getY() { return y; } //y is 0-63, scales up in the rendering code 

     int getScreenX() { return x*PIXEL_WIDTH; } //x is 0-63, scales up in the rendering code 
     int getScreenY() { return y*PIXEL_HEIGHT; } //y is 0-63, scales up in the rendering code 

     bool setDeltaX(int temp_delta_x); 
     bool setDeltaY(int temp_delta_y); 

     void setAffectedByGravity(bool yesorno) { affected_by_gravity = yesorno; } 
     bool getAffectedByGravity() { return affected_by_gravity; } 

     Pixel_Types getType() { return type; } 
     void setType(Pixel_Types what_type) { type = what_type; }//if (type == DIRT or type == STONE) { affected_by_gravity = true; } } 
}; 

std::vector<Pixel> world; //the world is a dynamically allocated thing 

Pixel* getPixelFromCoordinates(int x, int y) 
{ 
    if (x > 63) x = 63; 
    else if (x < 0) x = 0; 
    if (y > 63) y = 63; 
    else if (y < 0) y = 0; 

    for (int pixel_index = 0; pixel_index < world.size(); pixel_index++) { 
     if (world.at(pixel_index).getX() == x && world.at(pixel_index).getY() == y) { 
      return &world.at(pixel_index); 
     } 
    } 
    return NULL; 
} 

bool Pixel::setDeltaX(int temp_delta_x) { 
    if (x+temp_delta_x > SCREEN_WIDTH/PIXEL_WIDTH or x+temp_delta_x < 0) { 
     return false; 
    } 
    if (getPixelFromCoordinates(x+temp_delta_x, y)->type == AIR) { 
     x += temp_delta_x; 
     return true; 
    } 
    return false; 
} 

bool Pixel::setDeltaY(int temp_delta_y) { 
    if (y+temp_delta_y > SCREEN_HEIGHT/PIXEL_HEIGHT or y+temp_delta_y < 0) { 
     return false; 
    } 
    if (getPixelFromCoordinates(x, y+temp_delta_y)->type == AIR) { 
     y += temp_delta_y; 
     return true; 
    } 
    return false; 
} 

void generateWorld() 
{ 
    for (int world_generation_index = 0; world_generation_index < 4096; world_generation_index++) { 
     int x = world_generation_index % WORLD_X; //the world is 64 pixels left and right, and 64 up and down. this math is pretty easy and just extrapolates that. also each pixel is 10 pixels across, times 64 pixels = 640 (the resolution) 
     int y = floor(world_generation_index/WORLD_Y); //both x and y start at 0 
     world.push_back(Pixel(x, y)); 


     if (x == 0 || x == 63) { 
      world.at(world_generation_index).setType(WALL); 
     } 

     if (y == 1) { 
      world.at(world_generation_index).setType(WALL); 
     }  

    } 
    std::cout << "World size: " << world.size() << std::endl; 
} 

void createPixel(int x, int y, Pixel_Types type) 
{ 
    std::cout << x << std::endl; 
    std::cout << y << std::endl << std::endl; 
    y = (SCREEN_HEIGHT/PIXEL_HEIGHT) - y; //compensate for the stupid inverted y in opengl 
    //if (getPixelFromCoordinates(x, y)->getType() == AIR) { 
     getPixelFromCoordinates(x, y)->setType(type); 
    //} 
} 

void physicsOneStep() 
{ 
    for (int pixel_index = 0; pixel_index < world.size(); pixel_index++) { 
     if (world.at(pixel_index).getType() == DIRT or world.at(pixel_index).getType() == STONE) {//if (world.at(pixel_index).getAffectedByGravity()) { 
      world.at(pixel_index).setDeltaY(-1); 
      //std::cout << world.at(pixel_index).getX() << std::endl; 
      //std::cout << world.at(pixel_index).getY() << std::endl << std::endl; 
     } 
    } 
} 

內的布爾時,所以,當我嘗試運行這段代碼(一個更大的項目的一部分),它偶爾給我一個段錯誤從createPixel()中調用setType(DIRT)。我知道提供給createPixel()的值在允許的範圍內(0到64)。如果您在同一地點點擊兩次(它呼叫createPixel()),似乎會出現段錯誤。調試器說段錯誤的線是段錯誤試圖訪問類

void setType(Pixel_Types what_type) { type = what_type; }

不過,我驗證過,我提供給此值是正確的。

回答

1

由於在類中沒有動態分配,因此this指針本身是不正確的(NULL或分配不當),因此在此類分配上出現段錯誤肯定會發生。您應該在分段查看分配給您的名爲setType的對象時應該找到回溯。例如,不應該行

world.push_back(Pixel(x, y)); 

world.push_back(new Pixel(x,y)); 

+0

好的,所以我嘗試了這一點,並且改變了世界變量以使用像素指針,並且我仍然在同一行上出現了段錯誤。我研究了這些變量,並注意到'this'被設置爲'0x0',並且像素類中的變量未定義,即使當我使用'new Pixel(x,y)'時,如下所示:http:// i.imgur.com/kFZ8QBL.png,這就是它應該看起來像,從'generateWorld()'調用http://i.imgur.com/QlJ5Wx9.png – Aearnus

+0

我的診斷是正確的。你在空指針上調用'setType'。請注意,我沒有說我修正了所有的分配問題。因此,在調試器中調用堆棧框架以查找何時該空指針來自何處。 – hivert

+0

正常的堆棧:http://i.imgur.com/JAV7SSH.png和堆棧當我得到一個段錯誤:http://i.imgur.com/YoLOhFd.png正如你可以看到像素的指針是第二張圖像中的0x0。只是傳遞我想要使用的像素的指針是我可以做的事情(比如,'setType()'不是'Pixel'中的函數)? – Aearnus

相關問題