Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ / OpenGL - Pointer of an array
    primarykey
    data
    text
    <p>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.</p> <p>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:</p> <pre><code>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 &lt;&lt; "(AddLines func) array x1: "; cout &lt;&lt; Lines[0].x &lt;&lt; endl; cout &lt;&lt; "(AddLines func) array y1: "; cout &lt;&lt; Lines[0].y &lt;&lt; endl; cout &lt;&lt; "(AddLines func) array x2: "; cout &lt;&lt; Lines[1].x &lt;&lt; endl; cout &lt;&lt; "(AddLines func) array y1: "; cout &lt;&lt; Lines[1].y &lt;&lt; endl; temp-&gt;pointer = Lines; // &lt;--- I believe this is the error temp-&gt;corner = corner; temp-&gt;next = NULL; if(Top == NULL){ Top = temp; }else{ temp-&gt;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 &lt;&lt; "(DrawLines func) array x1: "; cout &lt;&lt; Lines[0].x &lt;&lt; endl; cout &lt;&lt; "(DrawLines func) array y1: "; cout &lt;&lt; Lines[0].y &lt;&lt; endl; cout &lt;&lt; "(DrawLines func) array x2: "; cout &lt;&lt; Lines[1].x &lt;&lt; endl; cout &lt;&lt; "(DrawLines func) array y1: "; cout &lt;&lt; Lines[1].y &lt;&lt; endl; glBegin(GL_LINE_STRIP); for(int i = 0; i &lt; 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-&gt;next){ DrawLines(current-&gt;pointer, current-&gt;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 &amp;&amp; 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 &amp;&amp; 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 </code></pre> <p>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.</p> <p>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. </p> <pre><code>(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 </code></pre> <p>Notice how it draws an extra line without adding it to the pointer?</p> <p>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</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload