2012-03-30 2 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

당신이 표준 : : 목록 , 또는 표준을 사용하지 않는 이유가 있나요 : 벡터 및 표준 : : 벡터 ? 그냥 참고하시기 바랍니다. – Robinson

+0

특별한 이유없이 오히려 지금 당장이 방법을 사용하십시오 – user1280446

답변

1

그래, 알았어.

정적 배열 Lines []를 사용하고 있습니다. 당신이 "temp-> pointer = Lines;" 각 다각형에 대해이 정적 배열을 가리키고 있습니다. 따라서 첫 번째 다각형을 그릴 때 두 번째 다각형을 시작하려고하면 첫 번째 다각형의 선을 편집하고있는 것입니다.

이미 문제가있는 행 대신 "for"루프를 반복하는 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