C++ C++;/OpenGL-数组的指针
我目前在将鼠标坐标存储到数组中时遇到了一个问题,在该数组中,该数组的指针被传递给一个函数,然后该函数将使用这些坐标在屏幕上显示一条多段线 下面是我的代码。请注意,我已经对问题所在进行了评论,但我认为我还是要粘贴大部分代码:C++ C++;/OpenGL-数组的指针,c++,opengl,C++,Opengl,我目前在将鼠标坐标存储到数组中时遇到了一个问题,在该数组中,该数组的指针被传递给一个函数,然后该函数将使用这些坐标在屏幕上显示一条多段线 下面是我的代码。请注意,我已经对问题所在进行了评论,但我认为我还是要粘贴大部分代码: struct mousePoint{ int x, y; }; struct Node{ mousePoint *pointer; int corner; Node *next; }; Node *Top; Node *Bottom;
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
注意它如何在不将其添加到指针的情况下绘制一条额外的线
我曾经尝试过使用memcpy——如果使用的点数少于5-6点,那么这些线就画得非常完美,再多的话,应用程序就会崩溃。因此,我相信这是指针问题,好吧,我想我明白了
您正在使用静态数组行[]。当您执行“temp->pointer=Lines”时,您将为每个多边形指向此静态数组。因此,当绘制第一个多边形,然后尝试开始第二个多边形时,您正在编辑第一个多边形的线
您可以复制for循环中的每个点,迭代“角点”次,而不是您已经发现有问题的线
相反:
temp->pointer = Lines; // <--- I believe this is the error
temp->pointer=行;//指针=新鼠标点[角点];
对于(int a=0;a指针[a]=行[a];
}
您不使用std::list或std::vector和std::vector有什么原因吗?仅供参考,这确实让事情更容易看。没有特别的原因,现在就这样做吧
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];
}