Skip to content

Commit

Permalink
added a CLI argument to enable capturing frames
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdcvlsc committed Oct 28, 2023
1 parent 040c638 commit 17127a4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ src/tmp
build
*.nn
*.tmp
docs/*.mp4
src/*.mp4
src/*.jpg
src/*.png
src/*.jpeg

# Prerequisites
*.d
Expand Down
41 changes: 38 additions & 3 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@
#include <chrono>
#include <random>
#include <memory>
#include <string>
#include <sstream>
#include <iomanip>

#include <cmath>

#include <SFML/Graphics.hpp>
#include <SFML/Graphics/Image.hpp>
#include <SFML/Graphics/RenderTexture.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/Font.hpp>
#include <SFML/Graphics/Text.hpp>
#include <SFML/System/Clock.hpp>
#include <SFML/System/Time.hpp>
#include <SFML/Graphics/Color.hpp>
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Window/Window.hpp>
#include <SFML/Window/VideoMode.hpp>
#include <SFML/System/Clock.hpp>
#include <SFML/System/Time.hpp>

#include "bird.hpp"
#include "config.hpp"
Expand All @@ -23,7 +30,17 @@
#include "collision.hpp"
#include "genetic_algo.hpp"

int main()
std::string lpad(std::string const &raw, char fillchar, size_t width)
{
static std::ostringstream ss;
ss << std::right << std::setfill(fillchar) << std::setw(width) << raw;
std::string padded = ss.str();
ss.str(std::string());
ss.clear();
return padded;
}

int main(int argc, char *args[])
{
auto game_statistics = std::make_shared<GameStats>();

Expand All @@ -39,6 +56,17 @@ int main()
window.setView(game_view);
window.setFramerateLimit(GameStats::FRAME_LIMIT);

sf::Texture capture_texture;
size_t captured_frames = 0;
bool capture_frames = false;
if (argc == 2) {
if (std::string(args[1]) == "capture") {
std::cout << "frame capture flag enabled\n";
capture_frames = true;
capture_texture.create(WINDOW_WIDTH, WINDOW_HEIGHT);
}
}

sf::Color background(19, 235, 220);

Bird player_bird;
Expand Down Expand Up @@ -136,6 +164,13 @@ int main()

window.display();

if (capture_frames) {
capture_texture.update(window);
capture_texture.copyToImage().saveToFile(
"flappy-ffnn-ga-frame-" + lpad(std::to_string(captured_frames++), '0', 6) + ".jpg"
);
}

if (birds.population == 0ULL && player_bird.dead) {
genetic_algorithm.rank_fitness(birds);

Expand Down

0 comments on commit 17127a4

Please sign in to comment.