]> code.bitgloo.com Git - clyne/game.git/commitdiff
easier initializaton
authorClyne Sullivan <clyne@bitgloo.com>
Tue, 6 Aug 2024 14:32:26 +0000 (10:32 -0400)
committerClyne Sullivan <clyne@bitgloo.com>
Tue, 6 Aug 2024 14:32:26 +0000 (10:32 -0400)
include/components/point.hpp
include/components/solid.hpp
include/components/texture.hpp
include/components/velocity.hpp
include/vec2.hpp
main.cpp

index b0d0f35f3624c95217a8415bf4a8bf24c596eae5..0abc081f5beff2ed974ec7df72de093c8188ada0 100644 (file)
@@ -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
 
index d02d229138253eaccf74232b7833bcf76545395d..87d18e3eada9a5ce50f80a7544d7b0d740757585 100644 (file)
@@ -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() {
index 14253f12141b094eac720bff39ae025c94b3d9c1..ea08c771c80d6ae5923ae4f9016f47c7eb783755 100644 (file)
@@ -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:
index 2337ebd3839c88eba1802d43cf3886d87237a9eb..96b8673a427570f6c8c3ffbdccc7bc1cb898fbe5 100644 (file)
@@ -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
 
index 6b3c30ad510ba75c4cf31b8885af7b9dadb4c4db..b0f559fdf4c4a4eed3d4deca52fa37ce1d97d3de 100644 (file)
@@ -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};
index 0d3feeb9682a85ab72e9e266ddaf1816f164f51a..9f191a7ce4d170ffe85d7fd013457a264e4b832a 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -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([&registry](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;