Help check my code, pls

Started by
5 comments, last by Fahrenheit451 18 years, 7 months ago
Hi, everybody :). I experience a problem with my code. And I don't know what causes this. The problem is my ship moves with occasional jerking and there are also some skipping frames or something. It looks very unpleasant. And I want to know what exactly is the cause of it. This is my protogame written in C++ with glut: the game. If somebody has time to check it and is willing to help, please do :). Thank you. P.S. I know the code is hilarious at best but it's my first try in game programming using OOP.
Advertisement
It's hard to tell when given the entire source code. I just checked your main.cpp and there seems to be a problem in main(...).
	glutDisplayFunc(myDisplay);	glutIdleFunc(myDisplay);should be:	glutDisplayFunc(myDisplay);	glutIdleFunc(myIdle);


Tom

EDIT:
I also have a comment on your layout, if you care:
Please use more spacing. I find snippets like these completely unreadable:
void Asteroid::draw(void) {	glDisable(GL_TEXTURE_2D);	Asteroid *n=first;	while(n!=NULL) {		glColor3f(0.0, 1.0, 0.0);		glBegin(GL_POLYGON);		for(int i=0; i<64; i++) glVertex2i(n->center.x+n->radius*(cos((2*PI/64)*i)), n->center.y+n->radius*(sin((2*PI/64)*i)));		glEnd();		glMatrixMode(GL_MODELVIEW);		glPushMatrix();		glTranslatef(0.0, 480, 0.0);		glBegin(GL_POLYGON);		for(int i=0; i<64; i++) glVertex2i(n->center.x+n->radius*(cos((2*PI/64)*i)), n->center.y+n->radius*(sin((2*PI/64)*i)));		glEnd();		glPopMatrix();		glPushMatrix();		glTranslatef(0.0, -480.0, 0.0);		glBegin(GL_POLYGON);		for(int i=0; i<64; i++) glVertex2i(n->center.x+n->radius*(cos((2*PI/64)*i)), n->center.y+n->radius*(sin((2*PI/64)*i)));		glEnd();		glPopMatrix();		glPushMatrix();		glTranslatef(640.0, 0.0, 0.0);		glBegin(GL_POLYGON);		for(int i=0; i<64; i++) glVertex2i(n->center.x+n->radius*(cos((2*PI/64)*i)), n->center.y+n->radius*(sin((2*PI/64)*i)));		glEnd();		glPopMatrix();		glPushMatrix();		glTranslatef(-640.0, 0.0, 0.0);		glBegin(GL_POLYGON);		for(int i=0; i<64; i++) glVertex2i(n->center.x+n->radius*(cos((2*PI/64)*i)), n->center.y+n->radius*(sin((2*PI/64)*i)));		glEnd();		glPopMatrix();		n=n->next;		}	}


I suggest something like this:

void Asteroid::draw(void) {	glDisable(GL_TEXTURE_2D);	Asteroid *n=first;	while(n!=NULL)         {		glColor3f(0.0, 1.0, 0.0);		glBegin(GL_POLYGON);		    for(int i=0; i<64; i++)                    {                          glVertex2i(n->center.x + n->radius * (cos((2 * PI / 64)  * i)),                                     n->center.y + n->radius * (sin((2  * PI / 64) * i)));                    }		glEnd();		glMatrixMode(GL_MODELVIEW);		glPushMatrix();		    glTranslatef(0.0, 480, 0.0);		    glBegin(GL_POLYGON);	  	        for(int i=0; i<64; i++)                        {                            glVertex2i(n->center.x  + n->radius  * (cos((2 *PI / 64) * i)),                                        n->center.y  + n->radius * (sin((2 * PI  / 64) * i)));		    glEnd();		glPopMatrix();		                glPushMatrix();		    glTranslatef(0.0, -480.0, 0.0);		    glBegin(GL_POLYGON);		        for(int i=0; i<64; i++)                                              glVertex2i(n->center.x + n->radius * (cos((2 * PI / 64) * i)),                                        n->center.y + n->radius * (sin((2 * PI  / 64) * i)));		    glEnd();		glPopMatrix();		glPushMatrix();		    glTranslatef(640.0, 0.0, 0.0);		    glBegin(GL_POLYGON);		        for(int i=0; i<64; i++)                        {                            glVertex2i(n->center.x + n->radius * (cos((2 * PI / 64) * i)),                                        n->center.y  + n->radius * (sin((2 * PI / 64) * i)));		    glEnd();		glPopMatrix();		glPushMatrix();		    glTranslatef(-640.0, 0.0, 0.0);		    glBegin(GL_POLYGON);		        for(int i=0; i<64; i++)                               glVertex2i(n->center.x + n->radius * (cos((2 * PI / 64) * i)),                                         n->center.y + n->radius * (sin((2 * PI / 64) * i)));		    glEnd();		glPopMatrix();		n = n->next;	}}


This may be many more lines, but that's good: now I can read it. Anyway, it's up to you...
Also try to replace all constant numbers (like 64, -640, etc) by constant variables with a proper name. That will make it much more readable as well...

Tom
Yes, I must acknowledge that the layout of some parts is not so good. But those parts are usually trivial.
And also separating the rendering and idle callbacks seems to not solve the problem...
Really would like to hear opinion of those few people who have downloaded my stuff. At least on how does it run on your system. And do you notice those flaws that I have noticed? How would you describe them? As I really can't figure out what the hell is wrong. Almost lost my sleep because of it...
I've always had the impression that glut is a little bulky itself. It's nice and all but I personally prefer to use my own win32 code which always seems to run smoothly.

I don't think glut was really written with game development in mind.

That said, you might want to look at using vsync or frame rate control to handle the problem...
Quote:Original post by Leo_E_49
I've always had the impression that glut is a little bulky itself. It's nice and all but I personally prefer to use my own win32 code which always seems to run smoothly.

I don't think glut was really written with game development in mind.

That said, you might want to look at using vsync or frame rate control to handle the problem...


Thanks for the answer. Vsync is on by default in the OpenGL properties of my graphic card. And it must be included in the project as well I assume (at least I'm sure I didn't turn it off). I'm going to implement frame rate control using the precision timer. Not sure if it will help though, but it is necessary nontheless for the rest of my application.
I know glut is inept in many ways, but I saw and I'm almost sure you saw too applications written with it that run smooth. I've read a lot of examples but still there are problems. It seems that I can't notice some small but important detail or something like that. And that's why I need another person's opinion.

I believe you are on track with Frame Rate Independent Movement. Implementing a timing routine that will move your sprites based on how long the timer says it took to execute the last frame will result in smooth movement on any system under any frame rate.

Clear explanation
gamedev.net link
gamedev.net link2

hth
F451

[Edited by - Fahrenheit451 on August 23, 2005 2:33:53 PM]

This topic is closed to new replies.

Advertisement