diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4023f87 --- /dev/null +++ b/Makefile @@ -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 diff --git a/dbc.cpp b/dbc.cpp new file mode 100644 index 0000000..6b17faf --- /dev/null +++ b/dbc.cpp @@ -0,0 +1,47 @@ +#include "dbc.hpp" +#include + +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 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 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}; + } +} diff --git a/dbc.hpp b/dbc.hpp new file mode 100644 index 0000000..87e4fb0 --- /dev/null +++ b/dbc.hpp @@ -0,0 +1,50 @@ +#pragma once + +#include +#include +#include +#include + +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 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 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()); +} diff --git a/main.cpp b/main.cpp index e5d7276..e47cadd 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,3 @@ -#include "imgui.h" -#include "imgui-SFML.h" #define _USE_MATH_DEFINES #include #include @@ -10,27 +8,11 @@ #include #include #include - -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(); -} +#include "dbc.hpp" void Window_update(sf::RenderWindow &window, sf::Sprite &player) { window.clear(); window.draw(player); - ImGui::SFML::Render(window); window.display(); } @@ -73,10 +55,7 @@ void Handle_events(sf::RenderWindow &window, BoxTest &box, sf::Sound &click) { // 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(); @@ -107,6 +86,8 @@ void Handle_events(sf::RenderWindow &window, BoxTest &box, sf::Sound &click) { click.play(); } 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.setRotation(angle * 180.0f / M_PI); - ImGui_update(window, deltaClock, tick); Window_update(window, player); return nextTick; @@ -154,10 +134,9 @@ int main() { sf::ContextSettings settings; 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.setVerticalSyncEnabled(true); - ImGui_setup(window); sf::SoundBuffer buffer; if(!buffer.loadFromFile("click.mp3")) { @@ -183,6 +162,4 @@ int main() { // preparing for refactoring this into a class or struct for everything tick = Update_entities(window, world, clock, deltaClock, tick, box, player); } - - ImGui::SFML::Shutdown(); } diff --git a/meson.build b/meson.build index 88dd7a2..cdd8bb1 100644 --- a/meson.build +++ b/meson.build @@ -3,12 +3,17 @@ project('sfmldemo', 'cpp', dependencies = [ dependency('sfml'), - dependency('imgui-sfml'), - dependency('fmt'), + subproject('fmt').get_variable('fmt_dep'), dependency('box2d'), ] -executable('sfmldemo', 'main.cpp', +source = [ + 'dbc.cpp', + 'main.cpp' +] + +executable('sfmldemo', + source, win_subsystem: 'windows', cpp_args: '-DFMT_HEADER_ONLY', dependencies: dependencies) diff --git a/scripts/reset_build.ps1 b/scripts/reset_build.ps1 index 1405a09..975852d 100644 --- a/scripts/reset_build.ps1 +++ b/scripts/reset_build.ps1 @@ -3,5 +3,5 @@ rm -recurse -force .\subprojects\,.\builddir\ mkdir subprojects mv .\packagecache .\subprojects\ mkdir builddir -cp wraps\*.wrap subprojects -meson setup -Ddefault_library=static builddir +cp wraps\*.wrap subprojects\ +meson setup --default-library=static --prefer-static builddir diff --git a/scripts/reset_build.sh b/scripts/reset_build.sh index 22ff10a..89931e7 100755 --- a/scripts/reset_build.sh +++ b/scripts/reset_build.sh @@ -1,10 +1,10 @@ #!/usr/bin/env bash -set -ex -mv ./subprojects/packagecache . -rm -rf ./subprojects ./builddir +mv -f ./subprojects/packagecache . +rm -rf subprojects builddir mkdir subprojects -mv ./packagecache ./subprojects +mv -f packagecache ./subprojects/ && true mkdir builddir cp wraps/*.wrap subprojects/ -meson setup builddir +# on OSX you can't do this with static +meson setup --default-library=static --prefer-static builddir