עבור לתוכן

תוכנית opengl פשוטה הולכת ונעשת איטית

Featured Replies

פורסם

התוכנית היא יישום פשוט של פיצוץ בלונים - אתה ממקם בלון מתנפח על המסך וכל בלון אחר שנוגע בו מתחיל גם להתנפח (מועבר מווקטור הבלונים הנעים לווקטור המתנפחים). התוכנית בסך הכל עובדת היטב, אולם ככל שזמן הריצה שלה מתמשך (בד"כ תוך כחצי דקה) היא נעשת יותר יותר איטית. מפרט המחשב שלי רשום מימין, והוא כמובן לא הגורם המגביל פה (ניסיתי גם על המחשב המשני שלי - אותו סינדרום). אין גם דליפת זיכרון. אז כנראה שהבעיה טמונה ביישום לא נכון של opengl ואשמח לקבל עצה בעניין (אם אפשר בדחיפות, הדד-ליין היום בחצות).

Controler* WindowGL::_maker=NULL;

int WindowGL::_size;

GLint pntX=0, pntY=0;

WindowGL::WindowGL(int size, Controler* maker)

{

_size = size;

glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);

//glutInitWindowPosition(WINDOW_POS_X,WINDOW_POS_Y);

glutInitWindowSize(_size, _size);

glutCreateWindow("Balloon Game 0.1a");

_maker = maker;

glutDisplayFunc(WindowGL::display);

glutReshapeFunc(WindowGL::reshapeWindow);

glutIdleFunc(WindowGL::iteration);

}

WindowGL::~WindowGL(void)

{

}

void WindowGL::iteration()

{

_maker->_mainLoop(0);

glutTimerFunc(20,WindowGL::timer,0);

}

void WindowGL::timer(int i)

{

glutPostRedisplay();

glutTimerFunc(20,WindowGL::timer,0);

}

void WindowGL::reshapeWindow(int width, int height)

{

glutReshapeWindow(_size, _size);

}

void WindowGL::display(void)

{

glClear (GL_COLOR_BUFFER_BIT);

_maker->_drawItems();

glutSwapBuffers();

}

//*********************************************************************//

/*

void WindowGL::winReshapeFnc(GLint newWidth,GLint newHeight)

{

glViewport(0,0,newWidth,newHeight);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(0.0,GLdouble(newWidth),0.0,GLdouble(newHeight));

winWidth = newWidth;

winHeight = newHeight;

}

*/

//get the mouse cor and pass them to the controler

void WindowGL::processMouse(int button, int state, int x, int y)

{

if(button == GLUT_LEFT_BUTTON && state==GLUT_DOWN)

{

pntX=(x*2-WINDOW_SIZE);

pntY=(y*2-WINDOW_SIZE)*-1;

_maker->_getMouseCor(pntX, pntY);

}

//glutPostRedisplay();

}

הפונקציה הקוראת לצייר

void Controler::_drawItems()

{

char message [256];

sprintf (message,"Currnet/Goal %d/%d Total Score %d", _levelscore, _level+1, _totalscore);

drawText(message, -0.95, -0.9);

manager._drawCircles();

}

הפונקציה המציירת את המעגל (אלגוריתם של חישוב מעגל במספרים שלמים)

Circle::Circle (int CX, int CY, int R, sRGB color)

{

_color = color;

_radius = R;

_centerY=CY;

_centerX=CX;

_dR = _vectorX = _vectorY = 0;

}

// cicrle drawing main routine

void Circle::_drawcircle()

{

int d = 1 -_radius;

int x=0;

int y=_radius;

glPointSize(1.0);

glBegin(GL_POLYGON);

glColor3f (_color._r, _color._g, _color._b);

while(y >= x)

{

if(d < 0)

{

d += 2 * x + 3;

}

else

{

d += 2 * (x - y) + 5;

y--;

}

x++;

_setPoints(x, y);

}

glEnd();

// glFlush();

}

Circle::~Circle(void)

{

}

// setting the points in the 8 octants

void Circle::_setPoints(int x, int y)

{

_draw8(_centerX+x, _centerY+y); //{point in octant 1}

_draw8(_centerX-x, _centerY+y); //{point in octant 4}

_draw8(_centerX-x, _centerY-y); //{point in octant 5}

_draw8(_centerX+x, _centerY-y); //{point in octant 8}

_draw8(_centerX+y, _centerY+x); //{point in octant 2}

_draw8(_centerX-y, _centerY+x); //{point in octant 3}

_draw8(_centerX-y, _centerY-x); //{point in octant 6}

_draw8(_centerX+y, _centerY-x); //{point in octant 7}

}

// setting the polygon points

void Circle::_draw8(int x, int y)

{

glVertex2f(x/700.0, y/700.0);

}

פורסם

תראה אני כתבתי פרוייקט בOPENGL בC ככה שהקוד שלך C++ קצת מוזר לי אבל אני אנסה לעזור כמה שאפשר - לדעתי יש לך בעייה פה :

glutIdleFunc(WindowGL::iteration);

אתה לא אמור להגדיר בתוך זה את הmainloop של glut, תוציא את הmainloop מהפונצקיה iteration)( לתוך הmain ותקרא לה אחרי glutIdleFunc

 glutIdleFunc(WindowGL::iteration);
_maker->_mainLoop(0);

עריכה :נראה לי שאתה יכול לוותר על הקריאה הזאת לחלוטין ופשוט לעשות glutTimerFunc(20,WindowGL::timer,0); בmain.

פורסם
  • מחבר

העברה של חלק מהפקודות ל-main פתרה את הבעיה. תודה.

כמובן שעכשיו צצות עוד בעיות של תזמון וכו', אבל אני אנסה להתמודד איתן לבד.

עריכה - זהו, הכל עובד!

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים