From 779fd068b6aadef909aefdd25d6ec4086300dde2 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Tue, 6 Aug 2024 10:32:26 -0400 Subject: [PATCH] easier initializaton --- include/components/point.hpp | 6 ++++- include/components/solid.hpp | 5 ++-- include/components/texture.hpp | 8 ++++--- include/components/velocity.hpp | 6 ++++- include/vec2.hpp | 12 +++++++++- main.cpp | 42 +++++++++++---------------------- 6 files changed, 43 insertions(+), 36 deletions(-) diff --git a/include/components/point.hpp b/include/components/point.hpp index b0d0f35..0abc081 100644 --- a/include/components/point.hpp +++ b/include/components/point.hpp @@ -3,7 +3,11 @@ #include "vec2.hpp" -struct Point : public Vec2 {}; +struct Point : public Vec2 { + constexpr Point() = default; + constexpr Point(float x_, float y_): Vec2(x_, y_) {} + Point(std::tuple tup): Vec2(tup) {} +}; #endif // COMPONENTS_POINT_HPP diff --git a/include/components/solid.hpp b/include/components/solid.hpp index d02d229..87d18e3 100644 --- a/include/components/solid.hpp +++ b/include/components/solid.hpp @@ -6,11 +6,12 @@ #include #include +#include class Solid { public: - Solid(const char *path) { - bitmap = sdl2LoadSolid(path); + Solid(std::string path) { + bitmap = sdl2LoadSolid(path.c_str()); } ~Solid() { diff --git a/include/components/texture.hpp b/include/components/texture.hpp index 14253f1..ea08c77 100644 --- a/include/components/texture.hpp +++ b/include/components/texture.hpp @@ -4,11 +4,13 @@ #include "components/point.hpp" #include "window.hpp" +#include + class Texture { public: - Texture(const char *path) { - tex = sdl2LoadTexture(path); + Texture(std::string path) { + tex = sdl2LoadTexture(path.c_str()); if (tex) SDL_QueryTexture(tex, nullptr, nullptr, &w, &h); } @@ -27,7 +29,7 @@ public: } Point dim() const noexcept { - return Point {static_cast(w), static_cast(h)}; + return Point(static_cast(w), static_cast(h)); } private: diff --git a/include/components/velocity.hpp b/include/components/velocity.hpp index 2337ebd..96b8673 100644 --- a/include/components/velocity.hpp +++ b/include/components/velocity.hpp @@ -3,7 +3,11 @@ #include "vec2.hpp" -struct Velocity : public Vec2 {}; +struct Velocity : public Vec2 { + constexpr Velocity() = default; + constexpr Velocity(float x_, float y_): Vec2(x_, y_) {} + Velocity(std::tuple tup): Vec2(tup) {} +}; #endif // COMPONENTS_VELOCITY_HPP diff --git a/include/vec2.hpp b/include/vec2.hpp index 6b3c30a..b0f559f 100644 --- a/include/vec2.hpp +++ b/include/vec2.hpp @@ -1,8 +1,18 @@ #ifndef VEC2_HPP #define VEC2_HPP +#include + struct Vec2 { - float x, y; + float x = 0.f, y = 0.f; + + constexpr Vec2() = default; + + constexpr Vec2(float x_, float y_): + x(x_), y(y_) {} + + Vec2(std::tuple tup): + x(std::get<0>(tup)), y(std::get<1>(tup)) {} auto operator+(const Vec2& o) const noexcept { return Vec2 {x + o.x, y + o.y}; diff --git a/main.cpp b/main.cpp index 0d3feeb..9f191a7 100644 --- a/main.cpp +++ b/main.cpp @@ -8,13 +8,14 @@ #include #include #include +#include #include #include #include #include -constexpr std::chrono::microseconds FRAME_TIME (1'000'000 / 60); +constexpr std::chrono::duration FRAME_TIME (1.0 / 60.0); static bool handleInputs(entt::registry& registry); @@ -26,43 +27,28 @@ int main(int argc, const char *argv[]) if (auto err = sdl2Initialize(); err) return err; - for (int i = 1; i < argc; ++i) - lua.script_file(argv[i]); + for (auto file : std::views::counted(argv + 1, argc - 1)) + lua.script_file(file); - sol::optional entities = lua["entities"]; - if (entities) { + if (auto entities = lua.get>("entities"); entities) { entities->for_each([®istry](sol::object _, sol::object val) { const auto ent = registry.create(); auto tbl = val.as(); - sol::optional solid = tbl["solid"]; - if (solid) { - registry.emplace(ent, solid->c_str()); - } + if (auto solid = tbl.get>("solid"); solid) + registry.emplace(ent, *solid); - sol::optional texture = tbl["texture"]; - if (texture) { - registry.emplace(ent, texture->c_str()); - } + if (auto tex = tbl.get>("texture"); tex) + registry.emplace(ent, *tex); - sol::optional point = tbl["point"]; - if (point) { - registry.emplace(ent, - static_cast((*point)["x"]), - static_cast((*point)["y"])); - } + if (auto point = tbl.get>("point"); point) + registry.emplace(ent, point->get("x", "y")); - sol::optional velocity = tbl["velocity"]; - if (velocity) { - registry.emplace(ent, - static_cast((*velocity)["x"]), - static_cast((*velocity)["y"])); - } + if (auto vel = tbl.get>("velocity"); vel) + registry.emplace(ent, vel->get("x", "y")); - sol::optional player = tbl["player"]; - if (player) { + if (auto player = tbl.get>("player"); player) registry.emplace(ent); - } }); } else { std::cout << "No entities defined..." << std::endl;