2012-03-30 70 views
0

我目前有一個問題,將鼠標座標存儲到一個數組中,該數組的指針被傳遞給一個函數,該函數將使用這些座標在屏幕上顯示折線。C++/OpenGL - 數組的指針

以下是我的代碼。請注意,我談到那裏的問題似乎是,但我想我會貼的大部分代碼反正:

struct mousePoint{ 
    int x, y; 
}; 

struct Node{ 
    mousePoint *pointer; 
    int corner; 
    Node *next; 
}; 

Node *Top; 
Node *Bottom; 


void init(void){ // doesnt need to be shown, but initialises linked list 

// Adds the mouse coords array to the top of the linked list 
void AddLines(mousePoint Lines[], int corner){ 
    Node *temp; 
    temp = new Node; 
    cout << "(AddLines func) array x1: "; 
cout << Lines[0].x << endl; 
cout << "(AddLines func) array y1: "; 
cout << Lines[0].y << endl; 
cout << "(AddLines func) array x2: "; 
cout << Lines[1].x << endl; 
cout << "(AddLines func) array y1: "; 
cout << Lines[1].y << endl; 
    temp->pointer = Lines; // <--- I believe this is the error 
    temp->corner = corner; 
    temp->next = NULL; 
    if(Top == NULL){ 
     Top = temp; 
    }else{ 
     temp->next = Top; 
     Top = temp; 
     if(Bottom == NULL){ 
      Bottom = Top; 
     } 
    } 
} 

// Draws the polyline based on the coords in the array 
void DrawLines(mousePoint Lines[], int corner){ 
    cout << "(DrawLines func) array x1: "; 
cout << Lines[0].x << endl; 
cout << "(DrawLines func) array y1: "; 
cout << Lines[0].y << endl; 
cout << "(DrawLines func) array x2: "; 
cout << Lines[1].x << endl; 
cout << "(DrawLines func) array y1: "; 
cout << Lines[1].y << endl; 
    glBegin(GL_LINE_STRIP); 
     for(int i = 0; i < corner; i++){ 
      glVertex2i(Lines[i].x, Lines[i].y); 
     } 
    glEnd(); 

} 

void display(void){ 
    Node *current; 
     current = Top; 
       // cycle through all polylines in linked list 
     for(; current != NULL; current = current->next){ 
      DrawLines(current->pointer, current->corner); 
     } 
    glFlush(); 
} 

void mouse(int button, int state, int x, int y) 
{ 
    static mousePoint Lines[100]; // array to store mouse coords 
    static int NumCorner = 0; // counter for mouse click 
    if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) 
    { 
     Lines[NumCorner].x = x; 
     Lines[NumCorner].y = 480 - y; 
       // draw individual points 
     glBegin(GL_POINTS); 
     glVertex2i(Lines[NumCorner].x, Lines[NumCorner].y); 
     glEnd(); 
     NumCorner++; 
    }else if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN){ 
     AddLines(Lines, NumCorner); // add mouse coords to linked list 
     NumCorner = 0; // reset counter back to 0 
    } 
} 

int main(int argc, char** argv) // doesnt need to be shown 

基本上,當我用鼠標點擊屏幕上的一個點應當制定同時,這些座標被保存到一個數組中。用戶可以通過點擊鼠標左鍵來繪製點,同時保存這些座標。直到按下鼠標右鍵,這些線條纔會被繪製,其中數組存儲在鏈接列表中。鏈表可以存儲所有不同的折線形狀。然而,問題在於指針未正確指向數組,並且drawlines函數未正確繪製線條。例如,如果點擊顯示屏上的兩個點(注意代碼中的cout語句),然後右鍵單擊,使用這兩點繪製一條線。但是,如果我點擊另一個點,則不需要按下鼠標右鍵,就可以從先前的座標系中劃出一條線。

(AddLines func) array x1: 338 
(AddLines func) array y1: 395 
(AddLines func) array x2: 325 
(AddLines func) array y1: 308 
(DrawLines func) array x1: 338 
(DrawLines func) array y1: 395 
(DrawLines func) array x2: 325 
(DrawLines func) array y1: 308 
(DrawLines func) array x1: 383 
(DrawLines func) array y1: 224 
(DrawLines func) array x2: 325 
(DrawLines func) array y1: 308 

注意如何在不將指針添加到指針的情況下繪製額外的行?

我試過使用memcpy - 線條繪製完美如果少於5-6點使用,任何更多和應用程序崩潰。因此,爲什麼我相信這是一個指針問題

+0

有沒有使用的std ::列表,或std理由:: vector的和std :: vector的?只是供參考,它確實使事情變得更容易看到。 – Robinson 2012-03-30 08:28:41

+0

沒有特別的理由,寧願現在就這樣做 – user1280446 2012-03-30 08:36:36

回答

1

好吧,我想我明白了。

您正在使用靜態數組Lines []。當你做「temp-> pointer = Lines」時你正在爲每個多邊形指向這個靜態數組。因此,當繪製您的第一個多邊形,然後當您嘗試開始第二個多邊形時,您正在編輯第一個多邊形的行。

而不是你已經發現有問題的行,你可以複製每個點在for循環迭代「角」時間。

代替:

temp->pointer = Lines; // <--- I believe this is the error 

嘗試:

temp->pointer = new mousePoint[corner]; 
for(int a = 0; a < corner; a++) 
{ 
    temp->pointer[a] = Lines[a]; 
} 
+0

這是行得通的!非常感謝 – user1280446 2012-03-30 10:42:52