Fixed up before doing the upgrade to SFML 3.0

main
Zed A. Shaw 10 months ago
parent 0766a2aacb
commit 2fa68351fc
  1. 27
      Makefile
  2. 47
      dbc.cpp
  3. 50
      dbc.hpp
  4. 31
      main.cpp
  5. 11
      meson.build
  6. 4
      scripts/reset_build.ps1
  7. 10
      scripts/reset_build.sh

@ -0,0 +1,27 @@
all: build
reset:
powershell -executionpolicy bypass .\scripts\reset_build.ps1
build:
meson compile -j 4 -C builddir
release_build:
meson --wipe builddir -Db_ndebug=true --buildtype release
meson compile -j 4 -C builddir
debug_build:
meson setup --wipe builddir -Db_ndebug=true --buildtype debugoptimized
meson compile -j 4 -C builddir
run: build
./builddir/sfmldemo.exe
debug: build
gdb --nx -x .gdbinit --ex run --args builddir/runtests.exe
debug_run: build
gdb --nx -x .gdbinit --batch --ex run --ex bt --ex q --args builddir/sfmldemo.exe
clean:
meson compile --clean -C builddir

@ -0,0 +1,47 @@
#include "dbc.hpp"
#include <iostream>
void dbc::log(const string &message, const std::source_location location) {
std::cout << '[' << location.file_name() << ':'
<< location.line() << "|"
<< location.function_name() << "] "
<< message << std::endl;
}
void dbc::sentinel(const string &message, const std::source_location location) {
string err = fmt::format("[SENTINEL!] {}", message);
dbc::log(err, location);
throw dbc::SentinelError{err};
}
void dbc::pre(const string &message, bool test, const std::source_location location) {
if(!test) {
string err = fmt::format("[PRE!] {}", message);
dbc::log(err, location);
throw dbc::PreCondError{err};
}
}
void dbc::pre(const string &message, std::function<bool()> tester, const std::source_location location) {
dbc::pre(message, tester(), location);
}
void dbc::post(const string &message, bool test, const std::source_location location) {
if(!test) {
string err = fmt::format("[POST!] {}", message);
dbc::log(err, location);
throw dbc::PostCondError{err};
}
}
void dbc::post(const string &message, std::function<bool()> tester, const std::source_location location) {
dbc::post(message, tester(), location);
}
void dbc::check(bool test, const string &message, const std::source_location location) {
if(!test) {
string err = fmt::format("[CHECK!] {}\n", message);
dbc::log(err, location);
throw dbc::CheckError{err};
}
}

@ -0,0 +1,50 @@
#pragma once
#include <string>
#include <fmt/core.h>
#include <functional>
#include <source_location>
using std::string;
namespace dbc {
class Error {
public:
const string message;
Error(string m) : message{m} {}
Error(const char *m) : message{m} {}
};
class CheckError : public Error {};
class SentinelError : public Error {};
class PreCondError : public Error {};
class PostCondError : public Error {};
void log(const string &message,
const std::source_location location =
std::source_location::current());
[[noreturn]] void sentinel(const string &message,
const std::source_location location =
std::source_location::current());
void pre(const string &message, bool test,
const std::source_location location =
std::source_location::current());
void pre(const string &message, std::function<bool()> tester,
const std::source_location location =
std::source_location::current());
void post(const string &message, bool test,
const std::source_location location =
std::source_location::current());
void post(const string &message, std::function<bool()> tester,
const std::source_location location =
std::source_location::current());
void check(bool test, const string &message,
const std::source_location location =
std::source_location::current());
}

@ -1,5 +1,3 @@
#include "imgui.h"
#include "imgui-SFML.h"
#define _USE_MATH_DEFINES #define _USE_MATH_DEFINES
#include <math.h> #include <math.h>
#include <fmt/core.h> #include <fmt/core.h>
@ -10,27 +8,11 @@
#include <SFML/System.hpp> #include <SFML/System.hpp>
#include <SFML/Audio.hpp> #include <SFML/Audio.hpp>
#include <SFML/Window/Event.hpp> #include <SFML/Window/Event.hpp>
#include "dbc.hpp"
void ImGui_setup(sf::RenderWindow &window) {
ImGui::SFML::Init(window);
}
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::Sprite &player) { void Window_update(sf::RenderWindow &window, sf::Sprite &player) {
window.clear(); window.clear();
window.draw(player); window.draw(player);
ImGui::SFML::Render(window);
window.display(); window.display();
} }
@ -73,10 +55,7 @@ void Handle_events(sf::RenderWindow &window, BoxTest &box, sf::Sound &click) {
// is this a main event loop // is this a main event loop
while (window.pollEvent(event)) { while (window.pollEvent(event)) {
ImGui::SFML::ProcessEvent(window, event);
switch(event.type) { switch(event.type) {
case sf::Event::Closed: case sf::Event::Closed:
fmt::print("Exiting...\n"); fmt::print("Exiting...\n");
window.close(); window.close();
@ -107,6 +86,8 @@ void Handle_events(sf::RenderWindow &window, BoxTest &box, sf::Sound &click) {
click.play(); click.play();
} }
break; break;
default:
return; // just here to shutup warnings
} }
} }
} }
@ -126,7 +107,6 @@ sf::Time Update_entities(sf::RenderWindow &window, b2World &world, sf::Clock &cl
player.setPosition(position.x * 100.0f, winSize.y - position.y * 100.0f); player.setPosition(position.x * 100.0f, winSize.y - position.y * 100.0f);
player.setRotation(angle * 180.0f / M_PI); player.setRotation(angle * 180.0f / M_PI);
ImGui_update(window, deltaClock, tick);
Window_update(window, player); Window_update(window, player);
return nextTick; return nextTick;
@ -154,10 +134,9 @@ int main() {
sf::ContextSettings settings; sf::ContextSettings settings;
settings.antialiasingLevel = 8; settings.antialiasingLevel = 8;
sf::RenderWindow window(sf::VideoMode(1920, 1080), "Simple Game Demo", sf::Style::Default, settings); sf::RenderWindow window(sf::VideoMode(1280, 720), "Simple Game Demo", sf::Style::Default, settings);
window.setFramerateLimit(60); window.setFramerateLimit(60);
window.setVerticalSyncEnabled(true); window.setVerticalSyncEnabled(true);
ImGui_setup(window);
sf::SoundBuffer buffer; sf::SoundBuffer buffer;
if(!buffer.loadFromFile("click.mp3")) { if(!buffer.loadFromFile("click.mp3")) {
@ -183,6 +162,4 @@ int main() {
// preparing for refactoring this into a class or struct for everything // preparing for refactoring this into a class or struct for everything
tick = Update_entities(window, world, clock, deltaClock, tick, box, player); tick = Update_entities(window, world, clock, deltaClock, tick, box, player);
} }
ImGui::SFML::Shutdown();
} }

@ -3,12 +3,17 @@ project('sfmldemo', 'cpp',
dependencies = [ dependencies = [
dependency('sfml'), dependency('sfml'),
dependency('imgui-sfml'), subproject('fmt').get_variable('fmt_dep'),
dependency('fmt'),
dependency('box2d'), dependency('box2d'),
] ]
executable('sfmldemo', 'main.cpp', source = [
'dbc.cpp',
'main.cpp'
]
executable('sfmldemo',
source,
win_subsystem: 'windows', win_subsystem: 'windows',
cpp_args: '-DFMT_HEADER_ONLY', cpp_args: '-DFMT_HEADER_ONLY',
dependencies: dependencies) dependencies: dependencies)

@ -3,5 +3,5 @@ rm -recurse -force .\subprojects\,.\builddir\
mkdir subprojects mkdir subprojects
mv .\packagecache .\subprojects\ mv .\packagecache .\subprojects\
mkdir builddir mkdir builddir
cp wraps\*.wrap subprojects cp wraps\*.wrap subprojects\
meson setup -Ddefault_library=static builddir meson setup --default-library=static --prefer-static builddir

@ -1,10 +1,10 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -ex
mv ./subprojects/packagecache . mv -f ./subprojects/packagecache .
rm -rf ./subprojects ./builddir rm -rf subprojects builddir
mkdir subprojects mkdir subprojects
mv ./packagecache ./subprojects mv -f packagecache ./subprojects/ && true
mkdir builddir mkdir builddir
cp wraps/*.wrap subprojects/ cp wraps/*.wrap subprojects/
meson setup builddir # on OSX you can't do this with static
meson setup --default-library=static --prefer-static builddir