Exemple #1
0
int main()
{
   if (!glfwInit())
   {
      std::cout << "GLFW failed to initialize!\n";
      return 1;
   }
   glfwOpenWindow(Width,Height,0,0,0,0,0,0,GLFW_WINDOW);
   glfwSetWindowTitle("Pong");
   glViewport(0,0,Width,Height);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(0,Width,Height,0,0,1);
   glMatrixMode(GL_MODELVIEW);
   glClearColor(0,0,1,1);

   NPad::SetScreen(Width,Height);
   NBall::SetScreen(Width,Height);
   NBall::Ball MyBall;
   NPad::SetCollisions(&MyBall);
   NPad::Paddle MyPaddle(Height-20,60);
   NPad::Paddle EnemyPaddle(20,60);
   MyPaddle.SetControl();
   float LastTime = glfwGetTime();
   while(true)
   {
       float DeltaTime = glfwGetTime()-LastTime;
       LastTime = glfwGetTime();
       if (!glfwGetWindowParam(GLFW_OPENED))
       {
           std::cout << "Window was closed! Exiting...\n";
           break;
       }
       MyBall.Update(DeltaTime);
       MyPaddle.Update(DeltaTime);
       EnemyPaddle.Update(DeltaTime);
       glClear(GL_COLOR_BUFFER_BIT);
       MyBall.Draw();
       MyPaddle.Draw();
       EnemyPaddle.Draw();
       glfwSwapBuffers();
   }
   return 0;
}
Exemple #2
0
int main()
{
	if (!glfwInit())
	{
		std::cout << "GLFW Couldn't initialize! \n";
		exit(1);
	}

	glfwOpenWindow(Width,Height,0,0,0,0,0,0,GLFW_WINDOW);
	glfwSetWindowTitle("Shazbot Pong");
	glViewport(0,0,Width,Height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0,Width,Height,0,0,1);
	glMatrixMode(GL_MODELVIEW);

	glClearColor(0,0,1,1);


	NMenu::CMenu Menu(Width , Height);

	CScore score;

	Npad::SetScreen(Width , Height);
	Nball::SetScreen(Width, Height);

	Nball::Ball ball;
	Npad::SetCollisions(&ball);
	ball.SetScore(&score);

	Npad::Paddle Player(Height - 20 , 60);
	Npad::Paddle Enemy(20 , 60);

	Player.SetControl();

	
	float LastTime = glfwGetTime(); // (for deltatime)
	
	
	

	bool EscPressed = false; // thanks naelstrof
	bool EscReleased = true;
	while (true)
	{
		

		float DeltaTime = glfwGetTime() - LastTime; // Deltatime init
		LastTime = glfwGetTime(); // update for deltatime

		if (glfwGetKey(GLFW_KEY_ESC) && EscReleased  && !EscPressed)
		{
			EscReleased = false;
			EscPressed = true;
		}
		

		if (EscPressed && !EscReleased) // if they press escape
		{
			if (Menu.State == "Playing") //if the state is playing
			{
				Menu.State = "Paused"; // change it to paused
				std::cout <<"\n\n-----PAUSED-----\n\n";
			}

			else if (Menu.State == "Paused") // else if the state is paused
				Menu.State = "Playing"; // change it to playing
	
			EscPressed = false;
		}

		if (!glfwGetKey(GLFW_KEY_ESC))
		{
			EscPressed = false;
			EscReleased = true;
		}
		

		if (!glfwGetWindowParam(GLFW_OPENED))
		{
			std::cout << "Window was closed! Exiting...\n";
			exit(0);
		}

		if (Menu.State == "Menu")
		{	
			Menu.Update();

			if (Menu.State == "Playing")
			{
				NSound::PlaySound("sounds/song.ogg" , true);
			}
		}

		if (Menu.State == "Playing") // if the state is playing update everything
		{
		ball.Update (DeltaTime);
			


		Player.Update (DeltaTime); // update everything
		Enemy.Update (DeltaTime);
		}
		glClear(GL_COLOR_BUFFER_BIT);

		if (Menu.State == "Menu")
		{
			Menu.Draw();
		}

		if (Menu.State == "Playing" || Menu.State == "Paused")
		{
		Menu.BgDraw();
		ball.Draw();
		Player.Draw(); // draws
		Enemy.Draw();
		}
		glfwSwapBuffers();
	}

	NSound::sound->drop();
	return 0;
}