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"
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

@ -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() {

@ -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:

@ -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

@ -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([&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;

Loading…
Cancel
Save