Skip to content

Commit

Permalink
add spdlog and changed console output
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldomanek authored and michaeldomanek committed Mar 8, 2021
1 parent 72d4dbe commit 1daf0d6
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ builddir/
!builddir/.gitkeep
.vscode/
old/
logs/

2 changes: 1 addition & 1 deletion include/bullet.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Bullet {
{
const unsigned int size{config.getSize()};

sf::Texture tex{};
sf::Texture tex;
tex.create(size, size);

sprite.setTexture(tex);
Expand Down
2 changes: 1 addition & 1 deletion include/robot.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Robot {
config(config),
window(Window::getInstance())
{
robotTexture.loadFromFile("../src/resources/body-border.png");
robotTexture.loadFromFile("../src/resources/body-grey.png");
turretTexture.loadFromFile("../src/resources/turret.png");

robot.setTexture(robotTexture);
Expand Down
1 change: 0 additions & 1 deletion include/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#include <SFML/Graphics.hpp>
#include <vector>
#include <iostream>

class Window {
private:
Expand Down
11 changes: 10 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

#include "CLI11.hpp"
#include <SFML/Graphics.hpp>
#include <iostream>
#include "spdlog/spdlog.h"
#include "spdlog/sinks/rotating_file_sink.h"

#include <vector>
#include <math.h>
#include <random>
Expand Down Expand Up @@ -43,6 +45,11 @@ int main(int argc, char* argv[]) {

CLI11_PARSE(app, argc, argv);

auto file_logger = spdlog::rotating_logger_mt("file_logger", "../logs/server.log", 1048576 * 5, 3);
spdlog::set_default_logger(file_logger);
spdlog::set_pattern("[%Y %m %d %H:%M:%S,%e] [%l] %v");
spdlog::set_level(spdlog::level::debug);

RobotConfiguration config{robotSpeed, health, robotRotation, turretRotation};
BulletConfiguration bulletConfig{bulletSpeed, bulletDamage, bulletSize};

Expand Down Expand Up @@ -71,6 +78,8 @@ int main(int argc, char* argv[]) {
robo3.startShooting();
robo3.moveForward();

spdlog::info("Game started");

while (window.isOpen()) {
window.clear();

Expand Down
1 change: 0 additions & 1 deletion src/robot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <string>
#include <math.h>
#include <algorithm>
#include <iostream>

using namespace std;

Expand Down
35 changes: 26 additions & 9 deletions src/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
#include <SFML/Graphics.hpp>
#include "spdlog/fmt/fmt.h"
#include "spdlog/fmt/bundled/color.h"
#include "spdlog/spdlog.h"

#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;
Expand All @@ -20,9 +20,14 @@ void Window::addBullet(sf::Sprite turret, Robot* attacker) {

void Window::addRobot(Robot *robot) {
robots.push_back(robot);
fmt::print("Robot: ");
fmt::print(fg(fmt::color::cyan) | fmt::emphasis::bold, robot->getName());
fmt::print(" started playing! [{} / 4] player\n", robots.size());

string name{robot->getName()};
size_t players{robots.size()};
string message{"Robot: {0} started playing! [{1} / 4] player{2}"};

fmt::print(message, fmt::format(fmt::fg(fmt::color::royal_blue), name), players, "\n");

spdlog::info(message, name, players, "");
}

void Window::removeRobot(Robot *robot) {
Expand All @@ -32,12 +37,24 @@ void Window::removeRobot(Robot *robot) {
robots.erase(remove(robots.begin(), robots.end(), robot), robots.end());
deadBodies.push_back(deadRobot);

string name{robot->getName()};
size_t players{robots.size()};
string message_dead{"Robot: {0} is dead! [{1} / 4] player left{2}"};

fmt::print(message_dead, fmt::format(fmt::fg(fmt::color::lime), name), players, "\n");

spdlog::info(message_dead, name, players, "");

if (robots.size() == 1) {
window.close();
fmt::print("GAME OVER!\n");
fmt::print("Robot: ");
fmt::print(fg(fmt::color::crimson) | fmt::emphasis::bold, robots.back()->getName());
fmt::print(" won!\n");
string message_gameover{"Robot: {0} won!{1}"};
name = robots.back()->getName();

fmt::print(fmt::fg(fmt::color::crimson), "===========GAME OVER!===========\n");
fmt::print(message_gameover, fmt::format(fmt::fg(fmt::color::orange), name), "\n");

spdlog::info(message_gameover, name, "");

window.close();
}
}

Expand Down

0 comments on commit 1daf0d6

Please sign in to comment.