diff options
author | Clyne Sullivan <clyne@bitgloo.com> | 2024-08-06 10:32:26 -0400 |
---|---|---|
committer | Clyne Sullivan <clyne@bitgloo.com> | 2024-08-06 10:32:26 -0400 |
commit | 779fd068b6aadef909aefdd25d6ec4086300dde2 (patch) | |
tree | bdfe50ccc58fb2995968f8191935eec25bc072f2 | |
parent | 189afb447e2c76e5dd5e4550e786c5175a828cbc (diff) |
easier initializaton
-rw-r--r-- | include/components/point.hpp | 6 | ||||
-rw-r--r-- | include/components/solid.hpp | 5 | ||||
-rw-r--r-- | include/components/texture.hpp | 8 | ||||
-rw-r--r-- | include/components/velocity.hpp | 6 | ||||
-rw-r--r-- | include/vec2.hpp | 12 | ||||
-rw-r--r-- | 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<float, float> 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 <algorithm> #include <cstdint> +#include <string> 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 <string> + 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<float>(w), static_cast<float>(h)}; + return Point(static_cast<float>(w), static_cast<float>(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<float, float> 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 <tuple> + 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<float, float> 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}; @@ -8,13 +8,14 @@ #include <chrono> #include <fstream> #include <iostream> +#include <ranges> #include <thread> #include <entt/entt.hpp> #include <sol/sol.hpp> #include <SDL2/SDL.h> -constexpr std::chrono::microseconds FRAME_TIME (1'000'000 / 60); +constexpr std::chrono::duration<double> 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<sol::table> entities = lua["entities"]; - if (entities) { + if (auto entities = lua.get<sol::optional<sol::table>>("entities"); entities) { entities->for_each([®istry](sol::object _, sol::object val) { const auto ent = registry.create(); auto tbl = val.as<sol::table>(); - sol::optional<std::string> solid = tbl["solid"]; - if (solid) { - registry.emplace<Solid>(ent, solid->c_str()); - } + if (auto solid = tbl.get<sol::optional<std::string>>("solid"); solid) + registry.emplace<Solid>(ent, *solid); - sol::optional<std::string> texture = tbl["texture"]; - if (texture) { - registry.emplace<Texture>(ent, texture->c_str()); - } + if (auto tex = tbl.get<sol::optional<std::string>>("texture"); tex) + registry.emplace<Texture>(ent, *tex); - sol::optional<sol::table> point = tbl["point"]; - if (point) { - registry.emplace<Point>(ent, - static_cast<float>((*point)["x"]), - static_cast<float>((*point)["y"])); - } + if (auto point = tbl.get<sol::optional<sol::table>>("point"); point) + registry.emplace<Point>(ent, point->get<float, float>("x", "y")); - sol::optional<sol::table> velocity = tbl["velocity"]; - if (velocity) { - registry.emplace<Velocity>(ent, - static_cast<float>((*velocity)["x"]), - static_cast<float>((*velocity)["y"])); - } + if (auto vel = tbl.get<sol::optional<sol::table>>("velocity"); vel) + registry.emplace<Velocity>(ent, vel->get<float, float>("x", "y")); - sol::optional<sol::table> player = tbl["player"]; - if (player) { + if (auto player = tbl.get<sol::optional<sol::table>>("player"); player) registry.emplace<Player>(ent); - } }); } else { std::cout << "No entities defined..." << std::endl; |