Jump to content
  • Advertisement
Sign in to follow this  
kubapl

OpenGL Frame Rate issues / Memory leaks?

This topic is 3778 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey there long time reader first time poster. I'm writing my second game ever in OpenGL / C++ and I'm running into some problems. Maybe someone can spot them. My game is asteroid right now its really simple it just draws 1 cube in the middle for the player it just rotates left and right 360 degrees (thats it for now ) and fires only 1 missle (again just for now). Anyways for some reason when I fire up this program and I watch the memory thats being used by the program in the task window it gradualy grows in size. It usualy starts around using 7,000KB but over time it climbs up. Some computers that I have tested on it takes up 100% of the cpu. This is written in VC2005. Any pointers would be nice. Release .exe of the game controls are left arrow key to move left, right " to move right, and down " key to fire a missile. http://kubapl.ath.cx/projects/release.zip And also here is my main game loop:
#include <windows.h>
#include <GL/glut.h>
#include <list>
#include <cmath>
#include <iostream>
#include <cstdlib> 
#include <ctime>
#include <mmsystem.h>



#include "Object.h"
#include "Enemy.h"
#include "Vector.h"
#include "Camera.h"
#include "Font.h"
#include "FPS.h"

using std::list;
using std::cout;
using std::endl;

#define SCREENHEIGHT 600
#define SCREENWIDTH 1000

CVector shoot();
void gameLogic();
void gameLights();

CObject *Player;
CObject *Missile;
CObject *Field;
CCamera *Camera;
CEnemy  *Asteroid[ 5 ];
CFont *Text;

list<CObject> List;



void myInit(void)
{

	glClearColor( 0.0, 0.0, 0.0, 0.0 );
	glShadeModel( GL_FLAT );

	glEnable( GL_NORMALIZE );

	glEnable( GL_LIGHTING );

	int nDisplayList  = 0;
	int nDisplayList1 = 1;
	int nDisplayList2 = 2;

	nDisplayList = glGenLists( 1 );
	glNewList( nDisplayList, GL_COMPILE );
	{
		glutSolidCube( 2.0 );
	}
	glEndList();
	nDisplayList1 = glGenLists( 2 );
	glNewList( nDisplayList1, GL_COMPILE );
	{
		glutSolidCube( 3.0 );
	}
	glEndList();
	nDisplayList2 = glGenLists( 3 );
	glNewList( nDisplayList2, GL_COMPILE );
	{
		glutSolidCube( 4.0 );
	}
	glEndList();

	Player = new CObject( nDisplayList, true );
	Missile = new CObject( nDisplayList, false );

	Text = new CFont();


	for( int i = 0; i < 5; ++i )
	{
		Asteroid[ i ] = new CEnemy( nDisplayList2, true );
	
		Asteroid[ i ]->setLocation( ( rand() %26 ), 0.0f, ( rand() %26 ) );
	}
	
	
	Field = new CObject();
	Camera = new CCamera( Field );

	Player->setLocation( 0.0f, 0.0f, 0.0f );
	Missile->setLocation( 0.0f, 0.0f, 0.0f );
	Field->setLocation( 0.0f, 0.0f, 0.0f );

	gameLights();
}

void myReshape (int w, int h)
{
	
	glViewport (0, 0, ( GLsizei ) w, ( GLsizei ) h); 
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();
	//gluOrtho2D( 0, 850, 0, 750 );
	gluPerspective( 65.0, ( GLfloat ) w/( GLfloat ) h, 1.0, 60.0 );
}

void myDisplay()
{		
	glEnable( GL_POLYGON_SMOOTH );
	glClear( GL_COLOR_BUFFER_BIT );
	glClear( GL_DEPTH_BUFFER_BIT  );
	glMatrixMode( GL_MODELVIEW );
	glLoadIdentity();
	glColor3f( 1.0, 1.0, 1.0 );
	

	

	
	Camera->Update();
	Player->Render();
	Missile->Render();

	for( int i = 0; i < 5; ++i )
	{
		Asteroid[ i ]->Render();
	}

	glDisable(GL_TEXTURE_2D);
	glColor3f( 1.0, 0.5, 1.0 );

	glutSwapBuffers();
	glFlush();
	
}

void Idle()
{
	glutPostRedisplay();
}

void myKeyboard( unsigned char key, int x, int y )
{
	switch( key )
	{
		case 27:
			{
				delete Player;
				delete Missile;
				delete Field;
				delete Camera;
				for( int i = 0; i < 5; ++ i )
				{
					delete Asteroid[ i ];
				}
				exit(0); 
				break;
			}
		case 'w': ///Toggle wireframe mode.
			{
				glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
				break;
			}

		case 'W': // Toggle wireframe mode.
			{
				glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
				break;
			}

			
		default:
		break;
	}

	glutPostRedisplay();
}

void SpecialKey( int nKey, int x, int y )
{
	CVector vOri = Player->getOrientation();
	CVector vVel = Missile->getVelocity();

	switch( nKey )
	{
	case GLUT_KEY_RIGHT:					//up arrow key
		{
			vOri.fy += 0.1f;
			break;
		}

	case GLUT_KEY_LEFT:					//down arrow key
		{
			vOri.fy -= 0.1f;
			break;
		}
	case GLUT_KEY_DOWN:
		{
		
			vVel = vVel + ( shoot() * 0.005f );

		}
	default:
	 break;
		

	}

	Missile->setVelocity( vVel );
	Player->setOrientation( vOri );

}

int main( int argc,  char** argv )
{
	glutInit( &argc, argv );
	glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB );
	glutInitWindowSize( SCREENWIDTH, SCREENHEIGHT ); 
	glutInitWindowPosition ( 100, 100 );
	
	glutCreateWindow("Asteroids");
	

	myInit();

	
	glutDisplayFunc( myDisplay ); 
	glutReshapeFunc( myReshape );
	glutKeyboardFunc( myKeyboard );
	glutSpecialFunc( SpecialKey );
	glutIdleFunc( Idle );
	glutMainLoop();


	return 0;
}

CVector shoot()
{
	Missile->setStatus( true );
	Missile->setOrientation( Player->getOrientation() );
	CVector vForward;
	CVector vOri = Player->getOrientation();
	CVector vLoc = Player->getLocation();
	vForward.fx = (float)sin( vOri.fy );
	vForward.fz = (float)cos( vOri.fy );
	
	return vForward;	
}

/**	@fn		void gameLights()
	@brief	Function that creates and renders the lights in the scene. Also adds fog.
	*/
void gameLights()
{
	glEnable( GL_FOG ); 
	GLfloat fogColor[ 4 ] = { 1.0, 1.0, 1.0, 1.0 }; //Fog color set to black
	glFogi ( GL_FOG_MODE, GL_EXP2 );
	glFogfv( GL_FOG_COLOR, fogColor );
	glFogf ( GL_FOG_DENSITY, 0.019 );
	glHint ( GL_FOG_HINT, GL_DONT_CARE );
	glFogf ( GL_FOG_START, -20.0 );
	glFogf ( GL_FOG_END, 100.0 );

	glEnable( GL_BLEND ); 

	glEnable( GL_LIGHTING ); // turned the lights on
	//created a array of clorage
					 	   //R		G		B	ALPHA
	float vAmbient[]   = { 0.3f,   0.3f,  0.3f, 1.0f };
	float vDiffuse[]   = { 0.5f,   0.5f,  0.5f, 1.0f };
	float vSpecific[]  = { 0.5f,   0.5f,  1.0f, 1.0f };
	float vPosition[]  = { 0.0f, -50.0f,  0.0f, 0.0f };
	float vDirection[] = { 0.0f,   1.0f,  0.0f, 0.0f };

	glLightModelfv( GL_LIGHT_MODEL_AMBIENT, vAmbient );

	glEnable( GL_COLOR_MATERIAL );
	glColorMaterial( GL_FRONT, GL_AMBIENT_AND_DIFFUSE );

	glLightfv( GL_LIGHT0, GL_AMBIENT, vSpecific );
	glLightfv( GL_LIGHT0, GL_DIFFUSE, vDiffuse );

	glLightfv( GL_LIGHT0, GL_POSITION, vPosition );
	glLightfv( GL_LIGHT0, GL_SPOT_DIRECTION, vDirection );

	glLightf( GL_LIGHT0, GL_SPOT_CUTOFF, 45.0f );
	glLightf( GL_LIGHT0, GL_SPOT_EXPONENT, 2.0f );

	glEnable( GL_LIGHT0 );
}

Share this post


Link to post
Share on other sites
Advertisement
This taking 100% CPU time is something that some see as part of the definition of a game: in contrast to "normal" applications, a game almost always runs and thus eats what it can get from the CPU.

As for memory leaks: I didn't spot any (although I didn't look at your code for that long). Unless you always allocate new memory without freeing it (e.g. in some loop), memory consumption shouldn't get too high.

Share this post


Link to post
Share on other sites
Quote:
Original post by kubapl
This is written in VC2005. Any pointers would be nice.

Well, a good start is to actually make sure that there is a problem.

Tell me, why shouldn't you expect your game use 100% CPU? If your code is running 100% of the time, it uses 100% of the CPU. Is that wrong? If so, what have you done to prevent it?)

And next up, how high does memory usage go?
Task Manager is a highly inaccurate way to determine memory usage. Going from 7MB to 8MB is not a memory leak. Going from 7MB to 2GB may not be a memory leak either. And whatever numbers task manager shows don't really have any direct relation to how much memory your app is actually *using*.

Do yourself the favor of determining whether or not there actually is a problem with your application before you ask for help in solving it. It's so hard to solve problems that don't exist.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!