Skip to content
Snippets Groups Projects
Commit 2bf71a52 authored by Jonas Johan Solsvik's avatar Jonas Johan Solsvik :video_game:
Browse files

draw to players and ground

parent 98d6cb1b
No related branches found
No related tags found
1 merge request!1Fix #6, #7 and #8 - Open window, draw and move circle + collsion example
build/ build/
.vscode/
...@@ -3,31 +3,62 @@ ...@@ -3,31 +3,62 @@
#include <SFML/Audio.hpp> #include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
int main() auto eventsUpdate(
sf::RenderWindow &window,
sf::View &camera) -> void;
auto renderUpdate(
sf::RenderWindow &window,
const std::vector<sf::RectangleShape> &players,
const sf::RectangleShape &ground) -> void;
auto main() -> int
{ {
// # Create the main window and camera // # Create the main window and camera
// const int WIDTH = 600;
// ## TODO
// - [ ] Make framerate limit adjustable
// - [ ] Make video and camera size the same as hardware window size
// - [ ] Resize video and camera size when user resizes window.
//
const int WIDTH = 800;
const int HEIGHT = 600; const int HEIGHT = 600;
const int FRAMERATE_LIMIT = 90; const int FRAMERATE_LIMIT = 90;
sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "Double trouble"); sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "Double trouble");
window.setFramerateLimit(FRAMERATE_LIMIT); window.setFramerateLimit(FRAMERATE_LIMIT);
sf::View camera(sf::FloatRect(0, 0, WIDTH, HEIGHT)); sf::View camera(sf::FloatRect(0, 0, WIDTH, HEIGHT));
camera.setCenter(0, 0); camera.setCenter(0, -200);
camera.setSize(WIDTH, HEIGHT);
window.setView(camera); window.setView(camera);
// # Create things // # Create ground
sf::CircleShape circle; sf::RectangleShape ground{sf::Vector2f{4000, 20}};
circle.setPosition(0, 0); ground.setPosition(-2000, 0);
circle.setRadius(10); ground.setFillColor(sf::Color::White);
circle.setFillColor(sf::Color::Red);
// # Create players
std::vector<sf::Vector2f> playerPositions{
sf::Vector2f{-100, -50},
sf::Vector2f{100, -50},
};
std::vector<sf::Color> playerColors{
sf::Color::Red,
sf::Color::Blue,
};
std::vector<sf::RectangleShape> players(2);
for (auto i = 0; i < players.size(); ++i)
{
players[i].setFillColor(playerColors[i]);
players[i].setSize(sf::Vector2f{50, 50});
players[i].setPosition(playerPositions[i]);
}
while (window.isOpen()) while (window.isOpen())
{
eventsUpdate(window, camera);
renderUpdate(window, players, ground);
}
return 0;
}
auto eventsUpdate(
sf::RenderWindow &window,
sf::View &camera) -> void
{ {
// # Process events // # Process events
sf::Event event; sf::Event event;
...@@ -40,12 +71,26 @@ int main() ...@@ -40,12 +71,26 @@ int main()
{ {
window.close(); window.close();
} }
if (event.type == sf::Event::Resized)
{
sf::FloatRect visibleArea(0, 0, event.size.width, event.size.height);
camera.setSize(event.size.width, event.size.height);
window.setView(camera);
}
}
} }
auto renderUpdate(
sf::RenderWindow &window,
const std::vector<sf::RectangleShape> &players,
const sf::RectangleShape &ground) -> void
{
// # Render video // # Render video
window.clear(); window.clear();
window.draw(circle); for (auto player : players)
window.display(); {
window.draw(player);
} }
return 0; window.draw(ground);
window.display();
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment