Example #1
0
void DisplayHandler () // display callback function
{
	if (bRotate) 
	{
		fCameraAngle[0] += 1.0 ;
		fCameraAngle[2] += 1.0 ;
	}
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ;
	glLoadIdentity() ;

	SetCamera() ;

	if (!bPause)
	{
		GLfloat fTempX = GLfloat(iLastMouseX) / GLfloat(iWinWidth) ;
		GLfloat fTempY = GLfloat(iLastMouseY) / GLfloat(iWinHeight) ;
		GLfloat fFixedX = (Player.ptLocation.dX - 8.65) / -17.3 ;
		GLfloat fFixedZ = (Player.ptLocation.dZ - 8.65) / -17.3 ;
		if (bRightMouseDown)
		{
			if (!(fFixedX + 0.02 > fTempX && fFixedX - 0.02 < fTempX))
			{
				if (fFixedX > fTempX)
				{
					Player.ptLocation.dX += 0.1 ;
					if (Player.ptAngle.dZ > -20) 
						Player.ptAngle.dZ -= 5.0 ;
				} else
				{
					Player.ptLocation.dX -= 0.1 ;
					if (Player.ptAngle.dZ < 20) 
						Player.ptAngle.dZ += 5.0 ;
				}
			} else
			{
				if (Player.ptAngle.dZ < 0) Player.ptAngle.dZ += 5.0 ;
				else if (Player.ptAngle.dZ > 0)	Player.ptAngle.dZ -= 5.0 ;
			}
			if (!(fFixedZ + 0.1 > fTempY && fFixedZ - 0.02 < fTempY))
			{
				if (fFixedZ > fTempY)
				{
					Player.ptLocation.dZ += 0.1 ;
				} else 
				{
					Player.ptLocation.dZ -= 0.1 ;
				}
			}
		} else Player.ptAngle.dZ = 0 ;

		if (randp() > 0.98) AddPlanet() ;
		AddStar() ;
		AddStar() ;
		AddStar() ;
		AddStar() ;
		AddStar() ;
		AddStar() ;

		if (/*!bExplode && */iCount % 15 == 0) SpawnEnemy() ;

		if (!bExplode && bAutofire && iCount % 4 == 0) FireShot(&Player, pPlayerShots) ;

		iCount ++ ;

	}
	
	DisplayStars() ;
	DisplayPlanets() ;
	DisplayEnemies() ;

	DisplayShots(pPlayerShots) ;
	DisplayShots(pEnemyShots) ;

	if (bExplode && !bPause)
	{
		if (iCount == 0) 
		{
			printf("Game over\n") ;
			MakeExplosion(Player.ptLocation, 1000) ;
		}
	} else 
	{
		if (iCount % 4)
		{
			Particle *pNew = new Particle ;
			pNew->ptLocation.dX = Player.ptLocation.dX + randp() * 0.5 - 0.25;
			pNew->ptLocation.dY = Player.ptLocation.dY + 0.3 * randp() ;
			pNew->ptLocation.dZ = Player.ptLocation.dZ - 1.5 + 0.2 * randp() ;
			pNew->ptVelocity.dX = 0 ;
			pNew->ptVelocity.dY = 0 ;
			pNew->ptVelocity.dZ = -0.2 ;
			pNew->ptColor.dX = 0.98 ;
			pNew->ptColor.dY = 0.59 + 0.3 * randp() - 0.15 ;
			pNew->ptColor.dZ = 0.01 ;
			pNew->dAlpha = 0.7 ;
			pNew->dMass = 1 ;
			pNew->dLife = 7 * randp() + 3 ;
			pNew->ptAcceleration = 0 ;
			Particles.AddBack(pNew) ;
		}

		Player.Display() ;
	}

	LinkedListNode<Particle> *pTravParticles = Particles.pHead ;
	LinkedListNode<Particle> *pTempParticle ;
	
	while (pTravParticles != 0)
	{
		pTravParticles->pValue->dLife -- ;
		if (pTravParticles->pValue->dLife <= 0)
		{
			pTempParticle = pTravParticles ;
			pTravParticles = pTravParticles->pNext ;
			Particles.Remove(pTempParticle, true) ;
		} else
		{
			pTravParticles->pValue->Apply(0.9) ;
			
			DisplayParticle(pTravParticles->pValue) ;
			
			pTravParticles = pTravParticles->pNext ;
		}
	}
	
		
	glFinish() ;
	glutSwapBuffers() ;
	glutPostRedisplay() ;
}
Example #2
0
int PlanetWars::ParseGameState(const string& s, bool bInverted)
{
	m_aPlanets.clear();
	m_aFleets.clear();
	m_fWidth = 0.f;
	m_fHeight = 0.f;

	vector<string> lines = StringUtil::Tokenize(s, "\n");

	int planet_id = 0;
	for (unsigned int i = 0; i < lines.size(); ++i)
	{
		string& line = lines[i];

		size_t comment_begin = line.find_first_of('#');
		if (comment_begin != string::npos)
		{
			line = line.substr(0, comment_begin);
		}
		vector<string> tokens = StringUtil::Tokenize(line);
		if (tokens.size() == 0)
		{
			continue;
		}

		if (tokens[0] == "P")
		{
			if (tokens.size() != 6)
			{
				return 0;
			}

			int iOwner = atoi(tokens[3].c_str());
			if (bInverted && iOwner > 0)
				iOwner = 3 - iOwner;

			AddPlanet(planet_id++,        // The ID of this planet
				iOwner,  // Owner
				atoi(tokens[4].c_str()),  // Num ships
				atoi(tokens[5].c_str()),  // Growth rate
				(float)atof(tokens[1].c_str()),  // X
				(float)atof(tokens[2].c_str())); // Y
		}
		else if (tokens[0] == "F")
		{
			if (tokens.size() != 7)
			{
				return 0;
			}

			AddFleet((bInverted ? (uint16)(3-atoi(tokens[1].c_str())) : (uint16)atoi(tokens[1].c_str())),  // Owner
				(uint16)atoi(tokens[2].c_str()),       // Num ships
				(uint16)atoi(tokens[3].c_str()),       // Source
				(uint16)atoi(tokens[4].c_str()),       // Destination
				(uint16)atoi(tokens[5].c_str()),       // Total trip length
				(uint16)atoi(tokens[6].c_str()));      // Turns remaining
		}
		else
		{
			return 0;
		}
	}
	return 1;
}