|
|
@ -13,9 +13,10 @@ |
|
|
|
#include "sfmlbackend.hpp" |
|
|
|
#include "sfmlbackend.hpp" |
|
|
|
#include <fstream> |
|
|
|
#include <fstream> |
|
|
|
#include <iostream> |
|
|
|
#include <iostream> |
|
|
|
|
|
|
|
#include "dbc.hpp" |
|
|
|
|
|
|
|
|
|
|
|
using namespace nlohmann; |
|
|
|
using namespace nlohmann; |
|
|
|
using std::string; |
|
|
|
using std::string, std::make_shared; |
|
|
|
|
|
|
|
|
|
|
|
std::array<sf::Color, 10> VALUES{ |
|
|
|
std::array<sf::Color, 10> VALUES{ |
|
|
|
sf::Color{1, 4, 2}, // black
|
|
|
|
sf::Color{1, 4, 2}, // black
|
|
|
@ -31,23 +32,25 @@ std::array<sf::Color, 10> VALUES{ |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
void SoundQuip::load(json &data, const char *file_key, bool loop) { |
|
|
|
void SoundQuip::load(json &data, const char *file_key, bool loop) { |
|
|
|
|
|
|
|
buffer = make_shared<sf::SoundBuffer>(); |
|
|
|
|
|
|
|
|
|
|
|
auto audio = data["audio"]; |
|
|
|
auto audio = data["audio"]; |
|
|
|
json::string_t file_name = audio[file_key].template get<string>(); |
|
|
|
json::string_t file_name = audio[file_key].template get<string>(); |
|
|
|
|
|
|
|
|
|
|
|
if(!buffer.loadFromFile(file_name)) { |
|
|
|
if(!buffer->loadFromFile(file_name)) { |
|
|
|
fmt::println("Failed to load sound: {} with file {}", file_key, file_name); |
|
|
|
fmt::println("Failed to load sound: {} with file {}", file_key, file_name); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
sound.setBuffer(buffer); |
|
|
|
sound = make_shared<sf::Sound>(*buffer); |
|
|
|
sound.setLoop(loop); |
|
|
|
sound->setLooping(loop); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void SoundQuip::play() { |
|
|
|
void SoundQuip::play() { |
|
|
|
sound.play(); |
|
|
|
sound->play(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void SoundQuip::stop() { |
|
|
|
void SoundQuip::stop() { |
|
|
|
sound.stop(); |
|
|
|
sound->stop(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void SFMLBackend::Window_update() { |
|
|
|
void SFMLBackend::Window_update() { |
|
|
@ -59,35 +62,11 @@ sf::Color SFMLBackend::value(Value level) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void SFMLBackend::handle_events() { |
|
|
|
void SFMLBackend::handle_events() { |
|
|
|
sf::Event event; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// is this a main event loop
|
|
|
|
// is this a main event loop
|
|
|
|
while (window.pollEvent(event)) { |
|
|
|
while(const auto ev = window.pollEvent()) { |
|
|
|
switch(event.type) { |
|
|
|
if(ev->is<sf::Event::Closed>()) { |
|
|
|
case sf::Event::Closed: |
|
|
|
fmt::print("Exiting...\n"); |
|
|
|
fmt::print("Exiting...\n"); |
|
|
|
window.close(); |
|
|
|
window.close(); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case sf::Event::KeyPressed: |
|
|
|
|
|
|
|
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) { |
|
|
|
|
|
|
|
window.close(); |
|
|
|
|
|
|
|
} else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) { |
|
|
|
|
|
|
|
fmt::println("STOP THE CLOCK"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case sf::Event::MouseButtonPressed: { |
|
|
|
|
|
|
|
// rect::contains(x,y) for if mouse is in the button rect
|
|
|
|
|
|
|
|
sf::Event::MouseButtonEvent btn = event.mouseButton; |
|
|
|
|
|
|
|
if(stop_button.getGlobalBounds().contains(btn.x, btn.y)) { |
|
|
|
|
|
|
|
buttons = Button::STOP; |
|
|
|
|
|
|
|
} else if(start_button.getGlobalBounds().contains(btn.x, btn.y)) { |
|
|
|
|
|
|
|
buttons = Button::START; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
// do nothing
|
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -104,8 +83,7 @@ sf::Vector2f translate(int x, int y) { |
|
|
|
|
|
|
|
|
|
|
|
void SFMLBackend::write_text(int x, int y, string content, float size_mult, Value color) { |
|
|
|
void SFMLBackend::write_text(int x, int y, string content, float size_mult, Value color) { |
|
|
|
sf::Vector2f position = translate(x,y); |
|
|
|
sf::Vector2f position = translate(x,y); |
|
|
|
sf::Text text; |
|
|
|
sf::Text text(font); |
|
|
|
text.setFont(font); |
|
|
|
|
|
|
|
text.setString(content); |
|
|
|
text.setString(content); |
|
|
|
text.setCharacterSize(TEXT_SIZE * size_mult); |
|
|
|
text.setCharacterSize(TEXT_SIZE * size_mult); |
|
|
|
text.setFillColor(value(color)); |
|
|
|
text.setFillColor(value(color)); |
|
|
@ -129,8 +107,8 @@ void SFMLBackend::update_entities() { |
|
|
|
window.clear(); |
|
|
|
window.clear(); |
|
|
|
|
|
|
|
|
|
|
|
sf::RectangleShape face_box = box(2, 2, X_ROWS/4, Y_LINES/2, Value::DARK_DARK); |
|
|
|
sf::RectangleShape face_box = box(2, 2, X_ROWS/4, Y_LINES/2, Value::DARK_DARK); |
|
|
|
face_sprite.setPosition(translate(2,2)); |
|
|
|
face_sprite->setPosition(translate(2,2)); |
|
|
|
window.draw(face_sprite); |
|
|
|
window.draw(*face_sprite); |
|
|
|
|
|
|
|
|
|
|
|
sf::RectangleShape stats_box = box(X_ROWS/4 + 4, 2, |
|
|
|
sf::RectangleShape stats_box = box(X_ROWS/4 + 4, 2, |
|
|
|
X_ROWS - X_ROWS/4 - 5, Y_LINES/2, Value::DARK_DARK); |
|
|
|
X_ROWS - X_ROWS/4 - 5, Y_LINES/2, Value::DARK_DARK); |
|
|
@ -160,10 +138,10 @@ void SFMLBackend::update_entities() { |
|
|
|
write_text(7, 14, time, 2.0f); |
|
|
|
write_text(7, 14, time, 2.0f); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
stop_button.setPosition(translate(27, 15)); |
|
|
|
stop_button->setPosition(translate(27, 15)); |
|
|
|
window.draw(start_button); |
|
|
|
window.draw(*start_button); |
|
|
|
start_button.setPosition(translate(37, 15)); |
|
|
|
start_button->setPosition(translate(37, 15)); |
|
|
|
window.draw(stop_button); |
|
|
|
window.draw(*stop_button); |
|
|
|
|
|
|
|
|
|
|
|
Window_update(); |
|
|
|
Window_update(); |
|
|
|
} |
|
|
|
} |
|
|
@ -174,24 +152,27 @@ void SFMLBackend::change_face(const string name) { |
|
|
|
auto images = data["images"]; |
|
|
|
auto images = data["images"]; |
|
|
|
json::string_t file_name = images[name].template get<string>(); |
|
|
|
json::string_t file_name = images[name].template get<string>(); |
|
|
|
|
|
|
|
|
|
|
|
face_texture.loadFromFile(file_name); |
|
|
|
bool good = face_texture->loadFromFile(file_name); |
|
|
|
face_sprite.setTexture(face_texture); |
|
|
|
dbc::check(good, fmt::format("failed to load texture {}", file_name)); |
|
|
|
|
|
|
|
face_sprite->setTexture(*face_texture); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
SFMLBackend::SFMLBackend(GameEngine &g) : window(sf::VideoMode(X_DIM, Y_DIM), "Turing's Tarpit", sf::Style::None, settings), game(g) |
|
|
|
SFMLBackend::SFMLBackend(GameEngine &g) |
|
|
|
|
|
|
|
: window(sf::VideoMode({X_DIM, Y_DIM}), "Turing's Tarpit"), |
|
|
|
|
|
|
|
game(g) |
|
|
|
{ |
|
|
|
{ |
|
|
|
change_face("building"); |
|
|
|
face_texture = make_shared<sf::Texture>("./assets/building.png"); |
|
|
|
|
|
|
|
stop_texture = make_shared<sf::Texture>("./assets/stop_button.png"); |
|
|
|
|
|
|
|
start_texture = make_shared<sf::Texture>("./assets/start_button.png"); |
|
|
|
|
|
|
|
|
|
|
|
stop_texture.loadFromFile("./assets/stop_button.png"); |
|
|
|
face_sprite = make_shared<sf::Sprite>(*face_texture); |
|
|
|
stop_button.setTexture(stop_texture); |
|
|
|
stop_button = make_shared<sf::Sprite>(*stop_texture); |
|
|
|
|
|
|
|
start_button = make_shared<sf::Sprite>(*start_texture); |
|
|
|
|
|
|
|
|
|
|
|
start_texture.loadFromFile("./assets/start_button.png"); |
|
|
|
change_face("building"); |
|
|
|
start_button.setTexture(start_texture); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* This makes my soul hurt. Make it stop. |
|
|
|
* This makes my soul hurt. Make it stop. |
|
|
|
* |
|
|
|
* |
|
|
@ -208,9 +189,8 @@ void SFMLBackend::update_log(std::vector<string> &lines) { |
|
|
|
|
|
|
|
|
|
|
|
void SFMLBackend::startup() { |
|
|
|
void SFMLBackend::startup() { |
|
|
|
fmt::print("Setting up a window for you...\n"); |
|
|
|
fmt::print("Setting up a window for you...\n"); |
|
|
|
settings.antialiasingLevel = 8; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(!font.loadFromFile("./assets/text.ttf")) { |
|
|
|
if(!font.openFromFile("./assets/text.ttf")) { |
|
|
|
fmt::println("Cannot load font."); |
|
|
|
fmt::println("Cannot load font."); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|