void Map::DrawPlayers(float time)
{
	// ======= Render models ======= 
	for (int i = 0; i < Players.size(); i++)
	{
		float x1, x2;

		CellToSpace(Players[i].GetXCell(), Players[i].GetYCell(), x1, x2);

		Players[i].SetSpacePosition(x1,
			(float) (Players[i].Model.GetSize().GetY() / 4.f) + 6.5f, 
			x2);

		HighlightSquare(Players[i].GetXCell(), Players[i].GetYCell(), Players[i].GetWRadius(), sf::Color(1, 1, 0), 1);
		HighlightSquare(Players[i].GetXCell(), Players[i].GetYCell(), Players[i].GetARadius(), sf::Color(1, 0, 0), 2);

		Players[i].Draw(time);
	}
}
Exemple #2
0
void 
BWDrawPoint(Widget w, Position x, Position y, bit value)
{
    BitmapWidget BW = (BitmapWidget) w;
    
    if (QueryInBitmap(BW, x, y)) {
	if (value == Highlight)
	    HighlightSquare(BW, x, y);
	else
	    DrawPoint(BW, x, y, value);
    }
}
int main()
{
	sf::RenderWindow App(sf::VideoMode(800, 600, 32), "MyDemo");
    App.PreserveOpenGLStates(true);

	sf::Clock Clock;
	sf::Vector3f CamPos;

	// ======= Create some players =======
	Player p1;

	p1.SetRace('b');
	p1.SetID((int) Players.size());
	p1.SetCellPosition(0, 10);

	Players.push_back(p1);
	
	Player p2;

	p2.SetRace('a');
	p2.SetID((int) Players.size());
	p2.SetCellPosition(10, 10);
	
	Players.push_back(p2);

	// ======== Calculate 1/2 of map size ==========
	HalfMapSpaceSize = (int) Players.size() * CellsPerPlayer * (CellWidth / 2);
	HalfMapCellSize = (int) Players.size() * (CellsPerPlayer / 2);

	PreapareGrid();

	// ======== Place players in random corners of the map ========
	for (unsigned int i = 0; i < Players.size(); i++)
    {
		int a = i % 3, b = i / 3;
        Players[i].SetCellPosition(((HalfMapCellSize * 2) - 1) * ((a > 1) ? 1 : a), ((HalfMapCellSize * 2) - 1) * ((b > 1) ? 1 : b));
    }

	// ======= OpenGL settings =======
	glEnable(GL_DEPTH_TEST);
	glDepthMask(GL_TRUE);
	glClearDepth(1.f);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(90.f, 1.f, 1.f, 500.f);

	glClearColor(0.0f, 0.5f, 1.0f, 0.5f);

	// ======= Set camera position =======
	CamPos = sf::Vector3f(0, 100, 75);

	App.ShowMouseCursor(false);

	while (App.IsOpened())
    {
        sf::Event Event;

		while (App.GetEvent(Event))
        {
	        if (Event.Type == sf::Event::Resized)
                glViewport(0, 0, Event.Size.Width, Event.Size.Height);

			if (Event.Type == sf::Event::Closed || App.GetInput().IsKeyDown(sf::Key::Escape))
				App.Close();

			if (App.GetInput().IsKeyDown(sf::Key::W))
				CamPos.z -= 3;

			if (App.GetInput().IsKeyDown(sf::Key::S))
				CamPos.z += 3;

			if (App.GetInput().IsKeyDown(sf::Key::A))
				CamPos.x -= 3;

			if (App.GetInput().IsKeyDown(sf::Key::D))
				CamPos.x += 3;

			if (Event.Type == sf::Event::MouseButtonReleased)
			{
				if (Event.MouseButton.Button == sf::Mouse::Left)
				{
					sf::Vector3f v = ScreenToSpace(App.GetInput().GetMouseX(), App.GetInput().GetMouseY());

					int x1, z1; // cell indexes
					float x2, z2; // cell normal basis

					SpaceToCell(v.x, v.z, x1, z1, x2, z2);

					PlayerMove(x1, z1, Players[0]);
				}
			}
		}
		
		//  ======= Clear OpenGL state ======= 
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();

		if (true)
		{
			//  ======= Update camera view ======= 
			/*gluLookAt(Players[0].Model.GetPosition().GetX(), Players[0].Model.GetPosition().GetY() + 100, Players[0].Model.GetPosition().GetZ() + 75, 
					  Players[0].Model.GetPosition().GetX(), Players[0].Model.GetPosition().GetY(), Players[0].Model.GetPosition().GetZ(), 
					  0, 1, 0);*/

			gluLookAt(CamPos.x, CamPos.y, CamPos.z,
					  CamPos.x, CamPos.y - 100, CamPos.z - 75, 
					  0, 1, 0);

			DrawGrid();

			//  ======= Pick terrain point at mouse cursor position ======= 
			{
				sf::Vector3f v = ScreenToSpace(App.GetInput().GetMouseX(), App.GetInput().GetMouseY());

				int x1, z1; // cell indexes
				float x2, z2; // cell normal basis

				SpaceToCell(v.x, v.z, x1, z1);
				CellToSpace(x1, z1, x2, z2);

				//  ======= Draw normal ======= 
				glBegin(GL_LINES);
					glVertex3f(x2, 0, z2);
					glVertex3f(x2, 150, z2);
				glEnd();
			}

			//  ======= Render models ======= 
			for (unsigned int i = 0; i < Players.size(); i++)
			{
				float x1, x2;
				float time = Clock.GetElapsedTime();

				CellToSpace(Players[i].GetXCell(), Players[i].GetYCell(), x1, x2);

				Players[i].SetSpacePosition(x1,
					(float) (Players[i].GetModel().GetSize().GetY() / 4.f) + 6.5f, 
					x2);

				HighlightSquare(Players[i].GetXCell(), Players[i].GetYCell(), Players[i].GetWRadius(), sf::Color(1, 1, 0), 1);
				HighlightSquare(Players[i].GetXCell(), Players[i].GetYCell(), Players[i].GetARadius(), sf::Color(1, 0, 0), 2);

				Players[i].Draw(time);
			}
		}

		App.Display();
    }

	return EXIT_SUCCESS;
}