aboutsummaryrefslogtreecommitdiffstats
path: root/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'main.cpp')
-rw-r--r--main.cpp42
1 files changed, 14 insertions, 28 deletions
diff --git a/main.cpp b/main.cpp
index 0d3feeb..9f191a7 100644
--- 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;