Move the GUI to its own file for the next stage.
This commit is contained in:
parent
58a5a415ec
commit
70d1389c54
3 changed files with 174 additions and 170 deletions
|
@ -52,7 +52,10 @@ executable('audiotest', 'audiotest.cpp',
|
|||
executable('jsontest', 'jsontest.cpp',
|
||||
dependencies: dependencies)
|
||||
|
||||
executable('sfmltest', 'sfmltest.cpp',
|
||||
executable('sfmltest', [
|
||||
'sfmltest.cpp',
|
||||
'sfmlgui.cpp',
|
||||
],
|
||||
dependencies: dependencies)
|
||||
|
||||
runtests = executable('runtests', [
|
||||
|
|
170
sfmlgui.cpp
Normal file
170
sfmlgui.cpp
Normal file
|
@ -0,0 +1,170 @@
|
|||
#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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 SFMLGui::write_text(int x, int y, const char *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 SFMLGui::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);
|
||||
|
||||
sf::RectangleShape hp_bar(translate(32,2));
|
||||
hp_bar.setPosition(translate(2,21));
|
||||
hp_bar.setFillColor(sf::Color(100, 250, 50));
|
||||
window.draw(hp_bar);
|
||||
|
||||
sf::RectangleShape hp_box(translate(44,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);
|
||||
|
||||
write_text(2, 18, "HP 222");
|
||||
// rounds text
|
||||
write_text(9, 18, "Rounds 333");
|
||||
// // streaks text
|
||||
write_text(21, 18, "Streaks 444");
|
||||
// // deaths text
|
||||
write_text(33, 18, "Deaths 555");
|
||||
|
||||
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();
|
||||
}
|
169
sfmltest.cpp
169
sfmltest.cpp
|
@ -1,174 +1,5 @@
|
|||
#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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 SFMLGui::write_text(int x, int y, const char *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 SFMLGui::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);
|
||||
|
||||
sf::RectangleShape hp_bar(translate(32,2));
|
||||
hp_bar.setPosition(translate(2,21));
|
||||
hp_bar.setFillColor(sf::Color(100, 250, 50));
|
||||
window.draw(hp_bar);
|
||||
|
||||
sf::RectangleShape hp_box(translate(44,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);
|
||||
|
||||
write_text(2, 18, "HP 222");
|
||||
// rounds text
|
||||
write_text(9, 18, "Rounds 333");
|
||||
// // streaks text
|
||||
write_text(21, 18, "Streaks 444");
|
||||
// // deaths text
|
||||
write_text(33, 18, "Deaths 555");
|
||||
|
||||
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();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue