diff --git a/Makefile b/Makefile index 8b977f5..5e1b2b8 100644 --- a/Makefile +++ b/Makefile @@ -30,20 +30,20 @@ test: build run: build test ifeq '$(OS)' 'Windows_NT' - powershell "cp ./builddir/calc.exe ." - ./calc + powershell "cp ./builddir/clicker_game.exe ." + ./clicker_game else - ./builddir/calc + ./builddir/clicker_game endif debug: build - gdb --nx -x .gdbinit --ex run --args builddir/calc + gdb --nx -x .gdbinit --ex run --args builddir/clicker_game debug_run: build - gdb --nx -x .gdbinit --batch --ex run --ex bt --ex q --args builddir/calc + gdb --nx -x .gdbinit --batch --ex run --ex bt --ex q --args builddir/clicker_game debug_walk: build test - gdb --nx -x .gdbinit --batch --ex run --ex bt --ex q --args builddir/calc t + gdb --nx -x .gdbinit --batch --ex run --ex bt --ex q --args builddir/clicker_game t clean: meson compile --clean -C builddir diff --git a/assets/clicker_the_dog-1024.png b/assets/clicker_the_dog-1024.png new file mode 100644 index 0000000..e25e06b Binary files /dev/null and b/assets/clicker_the_dog-1024.png differ diff --git a/assets/config.json b/assets/config.json index e49536f..f4c8121 100644 --- a/assets/config.json +++ b/assets/config.json @@ -9,6 +9,11 @@ {"path": "assets/textures_test.png", "frame_width": 53, "frame_height": 34 + }, + "clicker_the_dog": + {"path": "assets/clicker_the_dog-1024.png", + "frame_width": 1024, + "frame_height": 1024 } }, "graphics": { diff --git a/demos/clicker_game.cpp b/demos/clicker_game.cpp new file mode 100644 index 0000000..7fbadf3 --- /dev/null +++ b/demos/clicker_game.cpp @@ -0,0 +1,110 @@ +#include "guecs/sfml/backend.hpp" +#include "guecs/sfml/components.hpp" +#include "guecs/ui.hpp" +#include +#include + +constexpr const int WINDOW_WIDTH=1280; +constexpr const int WINDOW_HEIGHT=720; +constexpr const int FRAME_LIMIT=60; +constexpr const bool VSYNC=true; + +using std::string, std::wstring; + +enum class Event { + CLICKER, A_BUTTON +}; + + +struct ClickerUI { + guecs::UI $gui; + + ClickerUI() { + $gui.position(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); + $gui.layout( + "[_|*%(300,400)clicker|_|_|_]" + "[_|_ |_|_|_]" + "[_|_ |_|_|_]" + "[_|_ |_|_|_]" + "[a1|a2|a3|a4|a5]"); + } + + void init() { + $gui.set($gui.MAIN, {}); + + for(auto& [name, cell] : $gui.cells()) { + auto id = $gui.entity(name); + if(name != "clicker") { + $gui.set(id, {}); + $gui.set(id, {}); + $gui.set(id, { guecs::to_wstring(name) }); + $gui.set(id, { + [&](auto, auto) { handle_button(Event::A_BUTTON); } + }); + } else { + $gui.set(id, {"clicker_the_dog"}); + $gui.set(id, {0.1f, "ui_shader"}); + $gui.set(id, { + [&](auto, auto) { handle_button(Event::CLICKER); } + }); + } + } + + $gui.init(); + } + + void render(sf::RenderWindow& window) { + $gui.render(window); + // $gui.debug_layout(window); + } + + void mouse(float x, float y, bool hover) { + $gui.mouse(x, y, hover); + } + + void handle_button(Event ev) { + using enum Event; + switch(ev) { + case CLICKER: + fmt::println("CLICKER LOVES YOU!"); + break; + + case A_BUTTON: + fmt::println("a button clicked"); + break; + + default: + assert(false && "invalid event"); + } + } +}; + +int main() { + sfml::Backend backend; + guecs::init(&backend); + + sf::RenderWindow window(sf::VideoMode({WINDOW_WIDTH, WINDOW_HEIGHT}), "LEL-GUECS Calculator"); + window.setFramerateLimit(FRAME_LIMIT); + window.setVerticalSyncEnabled(VSYNC); + + ClickerUI clicker; + clicker.init(); + + while(window.isOpen()) { + while (const auto event = window.pollEvent()) { + if(event->is()) { + window.close(); + } + + if(const auto* mouse = event->getIf()) { + if(mouse->button == sf::Mouse::Button::Left) { + sf::Vector2f pos = window.mapPixelToCoords(mouse->position); + clicker.mouse(pos.x, pos.y, false); + } + } + } + + clicker.render(window); + window.display(); + } +} diff --git a/meson.build b/meson.build index 96d175a..a98d1ee 100644 --- a/meson.build +++ b/meson.build @@ -112,6 +112,16 @@ executable('runtests', sfml_impl + [ link_with: [lel_guecs_lib], dependencies: dependencies + [catch2]) +executable('clicker_game', sfml_impl + [ + 'demos/clicker_game.cpp', + ], + cpp_args: cpp_args, + link_args: link_args, + override_options: exe_defaults, + include_directories: lel_guecs_inc, + link_with: [lel_guecs_lib], + dependencies: dependencies) + executable('calc', sfml_impl + [ 'demos/calc.cpp', ],