Fx2D
A C++20 2D rigid-body physics engine
Loading...
Searching...
No Matches
YamlUtils.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <filesystem>
5#include <fstream>
6#include <iostream>
7#include <sstream>
8#include <string>
9#include <yaml-cpp/yaml.h>
10
11#include "Fx2D/Entity.h"
12#include "Fx2D/Math.h"
13#include "Fx2D/Scene.h"
14
15namespace fs = std::filesystem;
16
17namespace FxYAML {
18
19// Remove the indentation (leading spaces/tabs) from each line of a multi-line string.
20std::string deIndent(const std::string& raw);
21
22// Parse a YAML sequence of length N into a FxArray<float>.
23template<int N>
24inline FxArray<float> parseArray(const YAML::Node& node) {
25 FxArray<float> arr(N);
26 for (std::size_t i = 0; i < static_cast<std::size_t>(N); ++i)
27 arr[i] = node[i].as<float>();
28 return arr;
29}
30
31YAML::Node LoadSmart(const std::string& textOrPath);
32// Constructers from a YAML nodes.
33FxScene buildScene(const YAML::Node& config);
34FxShape buildShape(const YAML::Node& config);
35FxVisualShape buildVisualShape(const YAML::Node& config);
36FxCollisionShape buildCollisionShape(const YAML::Node& config);
37std::shared_ptr<FxEntity> buildEntity(const std::string& entity_name, const YAML::Node& config);
38// Build a joint from a YAML node; looks up parent/child entities by name from scene.
39std::shared_ptr<FxJoint> buildJoint(const std::string& joint_name, const YAML::Node& config,
40 FxScene& scene);
41
42inline FxScene buildScene(const std::string& textOrPath) {
43 YAML::Node config = LoadSmart(textOrPath);
44 return buildScene(config);
45}
46inline std::shared_ptr<FxEntity> buildEntity(const std::string& entity_name,
47 const std::string& textOrPath) {
48 YAML::Node config = LoadSmart(textOrPath);
49 return buildEntity(entity_name, config);
50}
51inline FxShape buildShape(const std::string& textOrPath) {
52 YAML::Node config = LoadSmart(textOrPath);
53 return buildShape(config);
54}
55inline FxVisualShape buildVisualShape(const std::string& textOrPath) {
56 YAML::Node config = LoadSmart(textOrPath);
57 return buildVisualShape(config);
58}
59
60inline FxVisualShape buildCollisionShape(const std::string& textOrPath) {
61 YAML::Node config = LoadSmart(textOrPath);
62 return buildCollisionShape(config);
63}
64} // namespace FxYAML
Fixed-size, aligned value array.
Definition Math.h:694
Owns a simulated world and advances it with step().
Definition Scene.h:56
YAML parsing and scene-builder functions.
Definition YamlUtils.h:17
FxShape buildShape(const YAML::Node &config)
std::shared_ptr< FxJoint > buildJoint(const std::string &joint_name, const YAML::Node &config, FxScene &scene)
FxScene buildScene(const YAML::Node &config)
FxArray< float > parseArray(const YAML::Node &node)
Definition YamlUtils.h:24
std::string deIndent(const std::string &raw)
YAML::Node LoadSmart(const std::string &textOrPath)
FxCollisionShape buildCollisionShape(const YAML::Node &config)
std::shared_ptr< FxEntity > buildEntity(const std::string &entity_name, const YAML::Node &config)
FxVisualShape buildVisualShape(const YAML::Node &config)
Unified circle, capsule, polygon, edge, and chain representation.
Definition Geometry.h:46