easier initializaton

main
Clyne 4 months ago
parent 189afb447e
commit 779fd068b6
Signed by: clyne
GPG Key ID: 3267C8EBF3F9AFC7

@ -3,7 +3,11 @@
#include "vec2.hpp" #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 #endif // COMPONENTS_POINT_HPP

@ -6,11 +6,12 @@
#include <algorithm> #include <algorithm>
#include <cstdint> #include <cstdint>
#include <string>
class Solid { class Solid {
public: public:
Solid(const char *path) { Solid(std::string path) {
bitmap = sdl2LoadSolid(path); bitmap = sdl2LoadSolid(path.c_str());
} }
~Solid() { ~Solid() {

@ -4,11 +4,13 @@
#include "components/point.hpp" #include "components/point.hpp"
#include "window.hpp" #include "window.hpp"
#include <string>
class Texture class Texture
{ {
public: public:
Texture(const char *path) { Texture(std::string path) {
tex = sdl2LoadTexture(path); tex = sdl2LoadTexture(path.c_str());
if (tex) if (tex)
SDL_QueryTexture(tex, nullptr, nullptr, &w, &h); SDL_QueryTexture(tex, nullptr, nullptr, &w, &h);
} }
@ -27,7 +29,7 @@ public:
} }
Point dim() const noexcept { 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: private:

@ -3,7 +3,11 @@
#include "vec2.hpp" #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 #endif // COMPONENTS_VELOCITY_HPP

@ -1,8 +1,18 @@
#ifndef VEC2_HPP #ifndef VEC2_HPP
#define VEC2_HPP #define VEC2_HPP
#include <tuple>
struct Vec2 { 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 { auto operator+(const Vec2& o) const noexcept {
return Vec2 {x + o.x, y + o.y}; return Vec2 {x + o.x, y + o.y};

@ -8,13 +8,14 @@
#include <chrono> #include <chrono>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <ranges>
#include <thread> #include <thread>
#include <entt/entt.hpp> #include <entt/entt.hpp>
#include <sol/sol.hpp> #include <sol/sol.hpp>
#include <SDL2/SDL.h> #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); static bool handleInputs(entt::registry& registry);
@ -26,43 +27,28 @@ int main(int argc, const char *argv[])
if (auto err = sdl2Initialize(); err) if (auto err = sdl2Initialize(); err)
return err; return err;
for (int i = 1; i < argc; ++i) for (auto file : std::views::counted(argv + 1, argc - 1))
lua.script_file(argv[i]); lua.script_file(file);
sol::optional<sol::table> entities = lua["entities"]; if (auto entities = lua.get<sol::optional<sol::table>>("entities"); entities) {
if (entities) {
entities->for_each([&registry](sol::object _, sol::object val) { entities->for_each([&registry](sol::object _, sol::object val) {
const auto ent = registry.create(); const auto ent = registry.create();
auto tbl = val.as<sol::table>(); auto tbl = val.as<sol::table>();
sol::optional<std::string> solid = tbl["solid"]; if (auto solid = tbl.get<sol::optional<std::string>>("solid"); solid)
if (solid) { registry.emplace<Solid>(ent, *solid);
registry.emplace<Solid>(ent, solid->c_str());
}
sol::optional<std::string> texture = tbl["texture"]; if (auto tex = tbl.get<sol::optional<std::string>>("texture"); tex)
if (texture) { registry.emplace<Texture>(ent, *tex);
registry.emplace<Texture>(ent, texture->c_str());
}
sol::optional<sol::table> point = tbl["point"]; if (auto point = tbl.get<sol::optional<sol::table>>("point"); point)
if (point) { registry.emplace<Point>(ent, point->get<float, float>("x", "y"));
registry.emplace<Point>(ent,
static_cast<float>((*point)["x"]),
static_cast<float>((*point)["y"]));
}
sol::optional<sol::table> velocity = tbl["velocity"]; if (auto vel = tbl.get<sol::optional<sol::table>>("velocity"); vel)
if (velocity) { registry.emplace<Velocity>(ent, vel->get<float, float>("x", "y"));
registry.emplace<Velocity>(ent,
static_cast<float>((*velocity)["x"]),
static_cast<float>((*velocity)["y"]));
}
sol::optional<sol::table> player = tbl["player"]; if (auto player = tbl.get<sol::optional<sol::table>>("player"); player)
if (player) {
registry.emplace<Player>(ent); registry.emplace<Player>(ent);
}
}); });
} else { } else {
std::cout << "No entities defined..." << std::endl; std::cout << "No entities defined..." << std::endl;

Loading…
Cancel
Save