L4C/main.cpp

92 lines
2.1 KiB
C++
Raw Permalink Normal View History

2024-11-06 16:56:40 -07:00
/**
* @author Tyler Beckman (tyler_beckman@mines.edu)
* @brief A program that randomly generates an interesting looking canvas using
* SFML and a whole bunch of uniform int distributions. Also, I added some sound
* effects to make it more enjoyable to watch :)
* @version 1
* @date 2024-11-1
*
*/
#include <chrono>
#include <random>
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/Color.hpp>
const unsigned int WINDOW_SIZE = 640;
int main() {
sf::RenderWindow window( sf::VideoMode(WINDOW_SIZE, WINDOW_SIZE), ":3" );
// Limit the FPS to a very low value so the shapes are
// actually displayed for a meaningful amount of time
window.setFramerateLimit(2);
sf::Event event;
std::mt19937 mt(std::chrono::steady_clock::now().time_since_epoch().count());
std::uniform_int_distribution<unsigned int> sidesDist(0, 8);
std::uniform_int_distribution<unsigned int> colorDist(0, 255);
std::uniform_int_distribution<unsigned int> positionDist(0, WINDOW_SIZE);
std::uniform_int_distribution<unsigned int> soundDist(0, 10);
sf::Music pipe;
if (!pipe.openFromFile("data/pipe.mp3")) {
return -1;
}
sf::Music running;
if (!running.openFromFile("data/running.mp3")) {
return -1;
}
sf::Music shimmer;
if (!shimmer.openFromFile("data/shimmer.mp3")) {
return -1;
}
while (window.isOpen()) {
window.clear(sf::Color(
colorDist(mt),
colorDist(mt),
colorDist(mt)
));
sf::CircleShape circle;
for (auto i = 0; i < 100; i++) {
circle.setPosition(
positionDist(mt),
positionDist(mt)
);
circle.setRadius(30);
circle.setFillColor(sf::Color(
colorDist(mt),
colorDist(mt),
colorDist(mt)
));
circle.setPointCount(sidesDist(mt));
window.draw(circle);
}
const unsigned int RANDOM_SOUND_NUMBER = soundDist(mt);
if (RANDOM_SOUND_NUMBER == 0) {
pipe.play();
}
if (RANDOM_SOUND_NUMBER == 1) {
running.play();
}
if (RANDOM_SOUND_NUMBER == 2) {
shimmer.play();
}
window.display();
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
}
return 0;
}