Reputation: 139
I'm currently having an issue storing mouse coordinates into an array in which a pointer of that array is passed to a function which will then use those coordinates to display a polyline on the screen.
Below is my code. Note that I've commented on where the problem seems to be but I thought I'd paste most of the code anyway:
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
Basically, when I click on the screen with the mouse, a point should be drawn and at the same time, those coords are saved to an array. The user can keep clicking with the left mouse button to draw points while those coords are saved. The lines won't draw until the right mouse button is pressed in which the array is stored in a linked list. The linked list is there to store all the different polyline shapes. The issue however, is that the pointer is not correctly pointing to the array and the drawlines function is not correctly drawing the lines.
So for example, If clicked on two points on the display (note the cout statements in the code) then right click, a line is drawn using those two points. However, if I click on another point, a line is drawn from the previous coords WITHOUT me pressing the right mouse button.
(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
Notice how it draws an extra line without adding it to the pointer?
I've tried using memcpy - the lines are drawn perfectly IF less that 5-6 points were used, any more and the application crashes. Hence why I believe it to a pointer issue
Upvotes: 0
Views: 996
Reputation: 616
Ok, I guess I got it.
You are using a static array Lines[]. When you do "temp->pointer = Lines;" you are pointing to this static array for each of your polygons. Thus, when draw your first polygon, and then when you try to start a second polygon, you are editing lines of the first one.
Instead of the line that you already found problematic, you can copy each point in a for loop iterating "corner" times.
instead:
temp->pointer = Lines; // <--- I believe this is the error
try:
temp->pointer = new mousePoint[corner];
for(int a = 0; a < corner; a++)
{
temp->pointer[a] = Lines[a];
}
Upvotes: 1