Now working on the new SFML with better audio.
This commit is contained in:
parent
2d81f900be
commit
c47e688b0b
15 changed files with 189 additions and 169 deletions
|
@ -13,9 +13,10 @@
|
|||
#include "sfmlbackend.hpp"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include "dbc.hpp"
|
||||
|
||||
using namespace nlohmann;
|
||||
using std::string;
|
||||
using std::string, std::make_shared;
|
||||
|
||||
std::array<sf::Color, 10> VALUES{
|
||||
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) {
|
||||
buffer = make_shared<sf::SoundBuffer>();
|
||||
|
||||
auto audio = data["audio"];
|
||||
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);
|
||||
}
|
||||
|
||||
sound.setBuffer(buffer);
|
||||
sound.setLoop(loop);
|
||||
sound = make_shared<sf::Sound>(*buffer);
|
||||
sound->setLooping(loop);
|
||||
}
|
||||
|
||||
void SoundQuip::play() {
|
||||
sound.play();
|
||||
sound->play();
|
||||
}
|
||||
|
||||
void SoundQuip::stop() {
|
||||
sound.stop();
|
||||
sound->stop();
|
||||
}
|
||||
|
||||
void SFMLBackend::Window_update() {
|
||||
|
@ -59,35 +62,11 @@ sf::Color SFMLBackend::value(Value level) {
|
|||
}
|
||||
|
||||
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)) {
|
||||
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;
|
||||
while(const auto ev = window.pollEvent()) {
|
||||
if(ev->is<sf::Event::Closed>()) {
|
||||
fmt::print("Exiting...\n");
|
||||
window.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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) {
|
||||
sf::Vector2f position = translate(x,y);
|
||||
sf::Text text;
|
||||
text.setFont(font);
|
||||
sf::Text text(font);
|
||||
text.setString(content);
|
||||
text.setCharacterSize(TEXT_SIZE * size_mult);
|
||||
text.setFillColor(value(color));
|
||||
|
@ -129,8 +107,8 @@ void SFMLBackend::update_entities() {
|
|||
window.clear();
|
||||
|
||||
sf::RectangleShape face_box = box(2, 2, X_ROWS/4, Y_LINES/2, Value::DARK_DARK);
|
||||
face_sprite.setPosition(translate(2,2));
|
||||
window.draw(face_sprite);
|
||||
face_sprite->setPosition(translate(2,2));
|
||||
window.draw(*face_sprite);
|
||||
|
||||
sf::RectangleShape stats_box = box(X_ROWS/4 + 4, 2,
|
||||
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);
|
||||
|
||||
|
||||
stop_button.setPosition(translate(27, 15));
|
||||
window.draw(start_button);
|
||||
start_button.setPosition(translate(37, 15));
|
||||
window.draw(stop_button);
|
||||
stop_button->setPosition(translate(27, 15));
|
||||
window.draw(*start_button);
|
||||
start_button->setPosition(translate(37, 15));
|
||||
window.draw(*stop_button);
|
||||
|
||||
Window_update();
|
||||
}
|
||||
|
@ -174,24 +152,27 @@ void SFMLBackend::change_face(const string name) {
|
|||
auto images = data["images"];
|
||||
json::string_t file_name = images[name].template get<string>();
|
||||
|
||||
face_texture.loadFromFile(file_name);
|
||||
face_sprite.setTexture(face_texture);
|
||||
bool good = face_texture->loadFromFile(file_name);
|
||||
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)
|
||||
{
|
||||
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");
|
||||
|
||||
face_sprite = make_shared<sf::Sprite>(*face_texture);
|
||||
stop_button = make_shared<sf::Sprite>(*stop_texture);
|
||||
start_button = make_shared<sf::Sprite>(*start_texture);
|
||||
|
||||
change_face("building");
|
||||
|
||||
stop_texture.loadFromFile("./assets/stop_button.png");
|
||||
stop_button.setTexture(stop_texture);
|
||||
|
||||
start_texture.loadFromFile("./assets/start_button.png");
|
||||
start_button.setTexture(start_texture);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* This makes my soul hurt. Make it stop.
|
||||
*
|
||||
|
@ -208,9 +189,8 @@ void SFMLBackend::update_log(std::vector<string> &lines) {
|
|||
|
||||
void SFMLBackend::startup() {
|
||||
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.");
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue