#include "imgui.h" #include "imgui-SFML.h" #define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include "sfmlbackend.hpp" using namespace ImGui; using std::string; void SFMLBackend::ImGui_setup() { bool res = SFML::Init(window); fmt::println("IMGUI returned {}", res); } void SFMLBackend::ImGui_update() { sf::Vector2u size = window.getSize(); SFML::Update(window, deltaClock.restart()); SetNextWindowPos(ImVec2(0, 0)); SetNextWindowSize(ImVec2(size.x, size.y)); Begin("Build Status", &window_active_out); TextColored(ImVec4(1,1,0,1), "Build Log"); BeginChild("Scrolling"); for(string &line : log) { TextWrapped(line.c_str()); } EndChild(); End(); } void SFMLBackend::Window_update() { if(show_build_log) { SFML::Render(window); } window.display(); } void SFMLBackend::handle_events() { sf::Event event; // is this a main event loop while (window.pollEvent(event)) { if(show_build_log) { SFML::ProcessEvent(window, 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) { sf::Vector2f position = translate(x,y); sf::Text text; text.setFont(font); text.setString(content); text.setCharacterSize(TEXT_SIZE); 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(X_ROWS/3+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); constexpr int hp_box_len = 44; 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 {} Rounds {} Streaks {} Deaths XXX", game.hit_points, game.rounds, game.streak); write_text(2, 18, status); if(show_build_log) { ImGui_update(); } 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) { } void SFMLBackend::update_log(std::vector &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); ImGui_setup(); } bool SFMLBackend::is_open() { return window.isOpen(); } void SFMLBackend::shutdown() { SFML::Shutdown(); }