Commit 2bf71a52 authored by Jonas Johan Solsvik's avatar Jonas Johan Solsvik 🎮
Browse files

draw to players and ground

parent 98d6cb1b
......@@ -3,49 +3,94 @@
#include <SFML/Audio.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
//
// ## 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 WIDTH = 600;
const int HEIGHT = 600;
const int FRAMERATE_LIMIT = 90;
sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "Double trouble");
window.setFramerateLimit(FRAMERATE_LIMIT);
sf::View camera(sf::FloatRect(0, 0, WIDTH, HEIGHT));
camera.setCenter(0, 0);
camera.setCenter(0, -200);
camera.setSize(WIDTH, HEIGHT);
window.setView(camera);
// # Create things
sf::CircleShape circle;
circle.setPosition(0, 0);
circle.setRadius(10);
circle.setFillColor(sf::Color::Red);
// # Create ground
sf::RectangleShape ground{sf::Vector2f{4000, 20}};
ground.setPosition(-2000, 0);
ground.setFillColor(sf::Color::White);
// # 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())
{
// # Process events
sf::Event event;
while (window.pollEvent(event))
eventsUpdate(window, camera);
renderUpdate(window, players, ground);
}
return 0;
}
auto eventsUpdate(
sf::RenderWindow &window,
sf::View &camera) -> void
{
// # Process events
sf::Event event;
while (window.pollEvent(event))
{
// ## Close window on exit
if (event.type == sf::Event::Closed ||
event.type == sf::Event::KeyPressed &&
event.key.code == sf::Keyboard::Escape)
{
// ## Close window on exit
if (event.type == sf::Event::Closed ||
event.type == sf::Event::KeyPressed &&
event.key.code == sf::Keyboard::Escape)
{
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);
}
}
}
// # Render video
window.clear();
window.draw(circle);
window.display();
auto renderUpdate(
sf::RenderWindow &window,
const std::vector<sf::RectangleShape> &players,
const sf::RectangleShape &ground) -> void
{
// # Render video
window.clear();
for (auto player : players)
{
window.draw(player);
}
return 0;
window.draw(ground);
window.display();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment