Jump to content
  • Advertisement
Sign in to follow this  
kubapl

std::list

This topic is 3802 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

Hi. I'm makeing an asteroid game in openGL and am trying to store my missiles in a list. But I'm not 100% sure how to do that any help on this would be nice. So I have no problems initializing the list and storing the projectile object in it but when it comes to calling member functions of the projectile such as shoot() and render() I'm a little lost.
 
#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 "Projectile.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

//Game Functions
CVector shoot( );//int j );
void gameLogic();
void gameLights();
short int Sprite_Collide( CObject *object1, CObject *object2 );

//Pointers to game objects
CObject *Player;
CObject *Field;
CCamera *Camera;
CEnemy  *Asteroid[ 5 ];
CFont   *Text;
CProjectile *Missile;

//List 
list<CObject*>lstMissile;


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();

	//Creating the objects
	Player = new CObject( nDisplayList, true );

	if( !lstMissile.empty() )
	{
		lstMissile.clear();
	}

	lstMissile.push_back( Missile );

	Missile = new CProjectile( 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 );

	gameLights();
}

void myReshape (int w, int h)
{
	
	glViewport (0, 0, ( GLsizei ) w, ( GLsizei ) h); 
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();
	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 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();

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

	case GLUT_KEY_LEFT:					   //left arrow key
		{
			vOri.fy -= 0.1f;
			break;
		}
	case GLUT_KEY_DOWN:
		{
			Missile->shoot( Player );
		}
	default:
	 break;
		

	}

	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;
}


Share this post


Link to post
Share on other sites
Advertisement
lstMissile.push_back( Missile );

Missile = new CProjectile( nDisplayList, false );


When you add Missle to the list, it is not initialized to anything. The above two lines should be reversed.

There are various C++ references online that show how to use std::list (see here for an example).

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!