Voila mon code :
bool multiplayer(sf::RenderWindow & window, sf::Font font)
{
sf::Texture redCarTex, blueCarTex, grassTex, roadTex;
sf::Sprite redCarSprite, blueCarSprite, grassSprite, roadSprite;
bool overview = false;
int mapX = 0;
int mapY = 0;
int map[WIDTH_MAP_TILE][HEIGHT_MAP_TILE] = {0};
// Loading the map
if(!loadLevel(map))
{
return false;
}
// Loading Textures
// Grass
if(grassTex.loadFromFile("ressources/grass.png"))
{
grassSprite.setTexture(grassTex);
}
else
{
return false;
}
// Road
if(roadTex.loadFromFile("ressources/road.png"))
{
roadSprite.setTexture(roadTex);
}
else
{
return false;
}
// Red car
if(!redCarTex.loadFromFile("ressources/car red.png"))
{
return false;
}
else
{
redCarTex.setSmooth(true);
redCarSprite.setTexture(redCarTex);
redCarSprite.setScale(0.5f, 0.5f);
redCarSprite.setOrigin(CAR_WIDTH / 2, CAR_HEIGHT / 2);
redCarSprite.setRotation(270);
}
// Blue car
if(!blueCarTex.loadFromFile("ressources/car blue.png"))
{
return false;
}
else
{
blueCarTex.setSmooth(true);
blueCarSprite.setTexture(blueCarTex);
blueCarSprite.setScale(0.5f, 0.5f);
blueCarSprite.setOrigin(CAR_WIDTH / 2, CAR_HEIGHT / 2);
blueCarSprite.setRotation(270);
}
Car redCar(35 * WIDTH_TILE, 15 * HEIGHT_TILE, 90, 0);
Car blueCar(36 * WIDTH_TILE, 15 * HEIGHT_TILE, 90, 0);
// VIEWS
sf::Vector2f quartSize(SCREEN_WIDTH / 4, SCREEN_HEIGHT / 2);
// Red car view
sf::Vector2f redCarCenter(redCar.getX(), redCar.getY());
sf::View redCarView(redCarCenter, quartSize);
redCarView.setViewport(sf::FloatRect(0, 0, 0.5f, 1));
window.setView(redCarView);
// Blue car view
sf::Vector2f blueCarCenter(blueCar.getX(), blueCar.getY());
sf::View blueCarView(blueCarCenter, quartSize);
blueCarView.setViewport(sf::FloatRect(0.5f, 0, 0.5f, 1));
window.setView(blueCarView);
// Overview
sf::Vector2f centerMap((WIDTH_MAP_TILE * WIDTH_TILE) / 2, (HEIGHT_MAP_TILE * HEIGHT_TILE) / 2);
sf::Vector2f sizeMap( WIDTH_MAP_TILE * WIDTH_TILE, HEIGHT_MAP_TILE * HEIGHT_TILE);
sf::View overviewCircuit(centerMap, sizeMap);
window.setFramerateLimit(60);
// Main loop
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
{
return true;
}
redCar.move();
blueCar.move();
redCarView.setCenter(redCar.getX(), redCar.getY());
blueCarView.setCenter(blueCar.getX(), blueCar.getY());
if(redCar.getX() < SCREEN_WIDTH / 2)
{
redCarView.setCenter(SCREEN_WIDTH / 2, redCar.getY());
}
if(redCar.getY() < SCREEN_HEIGHT / 2)
{
redCarView.setCenter(redCar.getX(), SCREEN_HEIGHT / 2);
}
if(redCar.getX() < SCREEN_WIDTH / 2 && redCar.getY() < SCREEN_HEIGHT / 2)
{
redCarView.setCenter(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
}
// Red car speep up
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
{
redCar.speedUp();
}
// Red car speed down
else
{
redCar.speedDown();
}
// Red car slow down
if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
redCar.slowDown();
}
// Red car turn right
if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
redCar.turnRight();
redCarSprite.setRotation(redCar.getAngle());
}
// Red car turn left
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Q))
{
redCar.turnLeft();
redCarSprite.setRotation(redCar.getAngle());
}
// Blue car speep up
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
blueCar.speedUp();
}
// Blue car speed down
else
{
blueCar.speedDown();
}
// Blue car slow down
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
blueCar.slowDown();
}
// Blue car turn right
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
blueCar.turnRight();
blueCarSprite.setRotation(blueCar.getAngle());
}
// Blue car turn left
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
blueCar.turnLeft();
blueCarSprite.setRotation(blueCar.getAngle());
}
// Switch view
if(sf::Keyboard::isKeyPressed(sf::Keyboard::V))
{
if(!overview)
{
overview = true;
}
}
else
{
overview = false;
}
window.clear();
for(mapY = 0; mapY < HEIGHT_MAP_TILE; mapY++)
{
for(mapX = 0; mapX < WIDTH_MAP_TILE; mapX++)
{
switch(map[mapX][mapY])
{
// Grass
case '0':
grassSprite.setPosition(mapX * WIDTH_TILE, mapY * HEIGHT_TILE);
window.draw(grassSprite);
break;
// Road
case '1':
roadSprite.setPosition(mapX * WIDTH_TILE, mapY * HEIGHT_TILE);
window.draw(roadSprite);
break;
}
}
}
redCarSprite.setPosition(redCar.getX(), redCar.getY());
window.draw(redCarSprite);
blueCarSprite.setPosition(blueCar.getX(), blueCar.getY());
window.draw(blueCarSprite);
if(!overview)
{
window.setView(blueCarView);
window.setView(redCarView);
window.display();
}
else if(overview)
{
window.setView(overviewCircuit);
window.display();
}
}
return true;
}