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/sfmltest.cpp

165 lines
3.5 KiB

#include "imgui.h"
#include "imgui-SFML.h"
#define _USE_MATH_DEFINES
#include <math.h>
#include <fmt/core.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 "sfmlgui.hpp"
using namespace ImGui;
void SFMLGui::ImGui_setup() {
bool res = SFML::Init(window);
fmt::println("IMGUI returned {}", res);
}
void SFMLGui::ImGui_update() {
sf::Vector2u size = window.getSize();
SFML::Update(window, deltaClock.restart());
SetNextWindowPos(ImVec2(0, 0));
SetNextWindowSize(ImVec2(size.x, size.y / 2));
Begin("Build Status", &window_active_out);
TextColored(ImVec4(1,1,0,1), "Build Log");
BeginChild("Scrolling");
for(int n = 0; n < 50; n++) {
TextWrapped("%04d: Some Text", n);
}
EndChild();
End();
}
void SFMLGui::Window_update() {
if(show_build_log) {
SFML::Render(window);
}
window.display();
}
void SFMLGui::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;
}
}
}
void SFMLGui::write_text(sf::Vector2f position, const char *content) {
// hp text
sf::Text text;
text.setFont(font);
text.setString(content);
text.setCharacterSize(48);
text.setFillColor(sf::Color(100, 250, 50));
text.setPosition(position);
window.draw(text);
}
void SFMLGui::update_entities() {
window.clear();
background.setPosition(0, 0);
window.draw(background);
sf::RectangleShape hp_bar({1920/2 - 18*2, 45.0f});
hp_bar.setPosition({18, 434});
hp_bar.setFillColor(sf::Color(100, 250, 50));
window.draw(hp_bar);
// hp text
write_text({60, 363}, "100");
// rounds text
write_text({305, 363}, "222");
// streaks text
write_text({540, 363}, "333");
// deaths text
write_text({757, 363}, "444");
if(show_build_log) {
ImGui_update();
}
Window_update();
show_build_log = window_active_out;
}
SFMLGui::SFMLGui() : window(sf::VideoMode(X_DIM, Y_DIM), "Turing's Tarpit", sf::Style::None, settings) {
}
void SFMLGui::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();
// fake image here
if(!texture.loadFromFile("./assets/turing_tarpit_main_screen.png")) {
fmt::println("Error loading sprite!");
}
texture.setSmooth(true);
background.setTexture(texture);
}
bool SFMLGui::is_open() {
return window.isOpen();
}
void SFMLGui::shutdown() {
SFML::Shutdown();
}
int main() {
auto gui = SFMLGui();
gui.startup();
while (gui.is_open()) {
gui.handle_events();
gui.update_entities();
}
gui.shutdown();
}