Refactored into a class.

master
Zed A. Shaw 3 months ago
parent 846d5964fe
commit fda281be1d
  1. 39
      sfmlgui.hpp
  2. 90
      sfmltest.cpp

@ -0,0 +1,39 @@
#pragma once
#include <SFML/System.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/Font.hpp>
constexpr int FPS=30;
constexpr int X_DIM = 1920 / 2;
constexpr int Y_DIM = 1080 / 2;
class SFMLGui {
sf::ContextSettings settings;
sf::RenderWindow window;
sf::Clock clock;
sf::Clock deltaClock;
sf::Sprite background;
sf::Texture texture;
bool window_active_out = false;
bool show_build_log = false;
int hit_points = 50;
sf::Font font;
public:
SFMLGui();
void startup();
bool is_open();
void shutdown();
void handle_events();
void update_entities();
void write_text(sf::Vector2f position, const char *content);
void ImGui_setup();
void ImGui_update();
void Window_update();
};

@ -3,33 +3,22 @@
#define _USE_MATH_DEFINES #define _USE_MATH_DEFINES
#include <math.h> #include <math.h>
#include <fmt/core.h> #include <fmt/core.h>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/Texture.hpp> #include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/RectangleShape.hpp> #include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/CircleShape.hpp> #include <SFML/Graphics/CircleShape.hpp>
#include <SFML/Graphics/Font.hpp>
#include <SFML/Graphics/Text.hpp> #include <SFML/Graphics/Text.hpp>
#include <SFML/System.hpp>
#include <SFML/Audio.hpp> #include <SFML/Audio.hpp>
#include <SFML/Window/Event.hpp> #include <SFML/Window/Event.hpp>
#include "sfmlgui.hpp"
using namespace ImGui; using namespace ImGui;
constexpr int FPS=30; void SFMLGui::ImGui_setup() {
constexpr int X_DIM = 1920 / 2;
constexpr int Y_DIM = 1080 / 2;
bool window_active_out = false;
bool show_build_log = false;
int hit_points = 50;
sf::Font font;
void ImGui_setup(sf::RenderWindow &window) {
bool res = SFML::Init(window); bool res = SFML::Init(window);
fmt::println("IMGUI returned {}", res); fmt::println("IMGUI returned {}", res);
} }
void ImGui_update(sf::RenderWindow &window, sf::Clock &deltaClock) { void SFMLGui::ImGui_update() {
sf::Vector2u size = window.getSize(); sf::Vector2u size = window.getSize();
SFML::Update(window, deltaClock.restart()); SFML::Update(window, deltaClock.restart());
@ -48,14 +37,14 @@ void ImGui_update(sf::RenderWindow &window, sf::Clock &deltaClock) {
End(); End();
} }
void Window_update(sf::RenderWindow &window, sf::Sprite &background) { void SFMLGui::Window_update() {
if(show_build_log) { if(show_build_log) {
SFML::Render(window); SFML::Render(window);
} }
window.display(); window.display();
} }
void Handle_events(sf::RenderWindow &window) { void SFMLGui::handle_events() {
sf::Event event; sf::Event event;
// is this a main event loop // is this a main event loop
@ -87,7 +76,8 @@ void Handle_events(sf::RenderWindow &window) {
} }
} }
void write_text(sf::RenderWindow &window, const char *content, sf::Vector2f position) {
void SFMLGui::write_text(sf::Vector2f position, const char *content) {
// hp text // hp text
sf::Text text; sf::Text text;
text.setFont(font); text.setFont(font);
@ -98,18 +88,9 @@ void write_text(sf::RenderWindow &window, const char *content, sf::Vector2f posi
window.draw(text); window.draw(text);
} }
void Update_entities(sf::RenderWindow &window, sf::Clock &clock, sf::Clock &deltaClock) { void SFMLGui::update_entities() {
window.clear(); window.clear();
sf::Texture texture;
sf::Sprite background;
// fake image here
if(!texture.loadFromFile("./assets/turing_tarpit_main_screen.png")) {
fmt::println("Error loading sprite!");
}
texture.setSmooth(true);
background.setTexture(texture);
background.setPosition(0, 0); background.setPosition(0, 0);
window.draw(background); window.draw(background);
@ -119,48 +100,65 @@ void Update_entities(sf::RenderWindow &window, sf::Clock &clock, sf::Clock &delt
window.draw(hp_bar); window.draw(hp_bar);
// hp text // hp text
write_text(window, "100", {60, 363}); write_text({60, 363}, "100");
// rounds text // rounds text
write_text(window, "222", {305, 363}); write_text({305, 363}, "222");
// streaks text // streaks text
write_text(window, "333", {540, 363}); write_text({540, 363}, "333");
// deaths text // deaths text
write_text(window, "444", {757, 363}); write_text({757, 363}, "444");
if(show_build_log) { if(show_build_log) {
ImGui_update(window, deltaClock); ImGui_update();
} }
Window_update(window, background); Window_update();
show_build_log = window_active_out;
} }
SFMLGui::SFMLGui() : window(sf::VideoMode(X_DIM, Y_DIM), "Turing's Tarpit", sf::Style::None, settings) {
int main() { }
fmt::print("Setting up a window for you...\n");
sf::ContextSettings settings; void SFMLGui::startup() {
fmt::print("Setting up a window for you...\n");
settings.antialiasingLevel = 8; settings.antialiasingLevel = 8;
if(!font.loadFromFile("./assets/text.ttf")) { if(!font.loadFromFile("./assets/text.ttf")) {
fmt::println("Cannot load font."); fmt::println("Cannot load font.");
return 0;
} }
sf::RenderWindow window(sf::VideoMode(X_DIM, Y_DIM), "Turing's Tarpit", sf::Style::None, settings);
window.setPosition({.x=0,.y=0}); window.setPosition({.x=0,.y=0});
window.setFramerateLimit(FPS); window.setFramerateLimit(FPS);
window.setVerticalSyncEnabled(true); window.setVerticalSyncEnabled(true);
ImGui_setup(window); ImGui_setup();
sf::Clock deltaClock; // fake image here
sf::Clock clock; if(!texture.loadFromFile("./assets/turing_tarpit_main_screen.png")) {
fmt::println("Error loading sprite!");
}
texture.setSmooth(true);
background.setTexture(texture);
}
while (window.isOpen()) { bool SFMLGui::is_open() {
Handle_events(window); return window.isOpen();
// preparing for refactoring this into a class or struct for everything
Update_entities(window, clock, deltaClock);
show_build_log = window_active_out;
} }
void SFMLGui::shutdown() {
SFML::Shutdown(); SFML::Shutdown();
} }
int main() {
auto gui = SFMLGui();
gui.startup();
while (gui.is_open()) {
gui.handle_events();
gui.update_entities();
}
gui.shutdown();
}

Loading…
Cancel
Save