A weird game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
turings-tarpit/sfmlbackend.cpp

176 lines
4.4 KiB

#define _USE_MATH_DEFINES
#include <math.h>
#include <fmt/core.h>
#include <fmt/chrono.h>
#include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/CircleShape.hpp>
#include <SFML/Graphics/Text.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Window/Event.hpp>
#include <nlohmann/json.hpp>
#include "sfmlbackend.hpp"
using namespace fmt;
using namespace nlohmann;
using std::string;
void SoundQuip::load(json &data, const char *file_key, bool loop) {
auto audio = data["audio"];
json::string_t file_name = audio[file_key].template get<string>();
if(!buffer.loadFromFile(file_name)) {
println("Failed to load sound: {} with file {}", file_key, file_name);
}
sound.setBuffer(buffer);
sound.setLoop(loop);
}
void SoundQuip::play() {
sound.play();
}
void SoundQuip::stop() {
sound.stop();
}
void SFMLBackend::Window_update() {
window.display();
}
void SFMLBackend::handle_events() {
sf::Event event;
// is this a main event loop
while (window.pollEvent(event)) {
switch(event.type) {
case sf::Event::Closed:
fmt::print("Exiting...\n");
window.close();
break;
case sf::Event::KeyPressed:
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
if(show_build_log) {
window_active_out = false;
} else {
window.close();
}
} else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
window_active_out = !window_active_out;
}
break;
default:
// do nothing
break;
}
}
}
sf::Vector2f translate(int x, int y) {
float step_x = X_DIM / TEXT_SIZE;
float step_y = (Y_DIM - 12) / TEXT_SIZE;
sf::Vector2f position{step_x * x, step_y * y * 2};
return position;
}
void SFMLBackend::write_text(int x, int y, string content, float size_mult) {
sf::Vector2f position = translate(x,y);
sf::Text text;
text.setFont(font);
text.setString(content);
text.setCharacterSize(TEXT_SIZE * size_mult);
text.setFillColor(sf::Color(100, 250, 50));
text.setPosition(position);
window.draw(text);
}
void SFMLBackend::update_entities() {
window.clear();
sf::RectangleShape face_box(translate(X_ROWS/4, Y_LINES/2));
face_box.setPosition(translate(2,2));
face_box.setOutlineColor(sf::Color(50, 200, 25));
face_box.setOutlineThickness(10);
face_box.setFillColor(sf::Color(200, 250, 200));
window.draw(face_box);
sf::RectangleShape stats_box(translate(X_ROWS - X_ROWS/4 - 5, Y_LINES/2));
stats_box.setPosition(translate(X_ROWS/4 + 4, 2));
stats_box.setOutlineColor(sf::Color(50, 200, 25));
stats_box.setOutlineThickness(10);
stats_box.setFillColor(sf::Color(0, 0, 0));
window.draw(stats_box);
constexpr int hp_box_len = 45;
int current_hp = (float(game.hit_points) / float(game.starting_hp)) * float(hp_box_len);
sf::RectangleShape hp_bar(translate(current_hp,2));
hp_bar.setPosition(translate(2,21));
hp_bar.setFillColor(sf::Color(100, 250, 50));
window.draw(hp_bar);
sf::RectangleShape hp_box(translate(hp_box_len,2));
hp_box.setPosition(translate(2,21));
hp_box.setOutlineColor(sf::Color(100, 200, 50));
hp_box.setOutlineThickness(10);
hp_box.setFillColor(sf::Color::Transparent);
window.draw(hp_box);
string status = fmt::format("HP {}\nRounds {}\nStreaks {}\nDeaths {}",
game.hit_points, game.rounds,
game.streak, game.deaths);
write_text(X_ROWS/4 + 5, 2, status);
std::time_t t = std::time(nullptr);
string time = fmt::format("{:%r}", fmt::localtime(t));
write_text(X_ROWS/4+1, 14, time, 2.0f);
Window_update();
show_build_log = window_active_out;
}
SFMLBackend::SFMLBackend(GameEngine &g) : window(sf::VideoMode(X_DIM, Y_DIM), "Turing's Tarpit", sf::Style::None, settings), game(g)
{
}
/*
* This makes my sould hurt. Make it stop.
*
* TODO: Make this more efficient, and don't display
* more than 10 or so errors since more than that is
* not very useful.
*/
void SFMLBackend::update_log(std::vector<string> &lines) {
log.clear();
for(string &line : lines) {
log.push_back(line);
}
}
void SFMLBackend::startup() {
fmt::print("Setting up a window for you...\n");
settings.antialiasingLevel = 8;
if(!font.loadFromFile("./assets/text.ttf")) {
fmt::println("Cannot load font.");
}
window.setPosition({.x=0,.y=0});
window.setFramerateLimit(FPS);
window.setVerticalSyncEnabled(true);
}
bool SFMLBackend::is_open() {
return window.isOpen();
}
void SFMLBackend::shutdown() {
}