This is a simple game I'm writing to test the idea of using games to teach C++. https://learncodethehardway.com/
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.

122 lines
3.3 KiB

#include "imgui.h"
#include "imgui-SFML.h"
#include <fmt/core.h>
#include <SFML/Graphics/CircleShape.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/System.hpp>
#include <SFML/Window/Event.hpp>
void ImGui_setup(sf::RenderWindow &window) {
int res = ImGui::SFML::Init(window);
if(res == 1) {
fmt::print("ImGui returned result {}\n", res);
} else {
fmt::print("ImGui returned an error code={}\n", res);
}
}
void ImGui_update(sf::RenderWindow &window, sf::Clock &deltaClock, sf::Time &tick) {
ImGui::SFML::Update(window, deltaClock.restart());
// ImGui::ShowDemoWindow();
ImGui::Begin("Clock");
sf::Vector2u size = window.getSize();
ImGui::SetWindowPos(ImVec2(size.x - 150, 0));
ImGui::SetWindowSize(ImVec2(150, 50));
std::string msg = fmt::format("Time: {}\n", tick.asSeconds());
ImGui::Button(msg.c_str());
ImGui::End();
}
void Window_update(sf::RenderWindow &window, sf::Shape &shape) {
window.clear();
window.draw(shape);
ImGui::SFML::Render(window);
window.display();
}
int main() {
fmt::print("Setting up a window for you...\n");
sf::ContextSettings settings;
settings.antialiasingLevel = 8;
sf::RenderWindow window(sf::VideoMode(1920, 1080), "Simple Game Demo", sf::Style::Default, settings);
// window.setFramerateLimit(60);
window.setVerticalSyncEnabled(true);
ImGui_setup(window);
sf::CircleShape shape(100.f, 4);
sf::Vector2u size = window.getSize();
shape.setPosition(size.x / 2, size.y / 2);
shape.setOrigin(100.f, 100.f);
shape.setFillColor(sf::Color(150, 50, 250));
shape.setOutlineThickness(10.f);
shape.setOutlineColor(sf::Color(250, 150, 100));
sf::Clock deltaClock;
sf::Clock clock;
sf::Time tick = clock.getElapsedTime();
// very cool, c++11 lambdas doing a 1 second slept thread
sf::Thread thread([](){
for(int i = 0; i < 10; i++) {
fmt::print("I'm a thread. {}\n", i);
sf::sleep(sf::seconds(1));
}
});
thread.launch();
while (window.isOpen()) {
sf::Event event;
// is this a main event loop
while (window.pollEvent(event)) {
ImGui::SFML::ProcessEvent(window, event);
switch(event.type) {
case sf::Event::Closed:
fmt::print("Exiting...\n");
window.close();
thread.terminate();
break;
case sf::Event::KeyPressed:
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
shape.move(-20, 0);
} else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
shape.move(20, 0);
} else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
shape.move(0, -20);
} else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
shape.move(0, 20);
}
break;
case sf::Event::MouseButtonPressed:
if(sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
sf::Vector2i clickAt = sf::Mouse::getPosition(window);
shape.setPosition(clickAt.x, clickAt.y);
}
break;
}
}
// should this move up to the pollEvent loop?
sf::Time since = clock.getElapsedTime();
if(since - tick > sf::seconds(1)) {
tick = since;
}
shape.rotate(1);
ImGui_update(window, deltaClock, tick);
Window_update(window, shape);
}
ImGui::SFML::Shutdown();
}