77
88void Setting::importFile (const char * src, GameData& data)
99{
10- YAML::Node animGraph = YAML::LoadFile (src);
11- if (!animGraph )
10+ YAML::Node root = YAML::LoadFile (src);
11+ if (!root )
1212 {
1313 errorAndExit (std::string (" Could not find setting file here: " ) + src);
1414 }
1515
16- std::string section;
17- {
18- section = " Game" ;
19- YAML::Node nodesSection = animGraph[section];
20- if (!nodesSection)
21- errorAndExit (" Cannot find \" " + section + " \" in setting.yaml" );
22-
23- data.FPS = std::max (nodesSection[" FPS" ].as <int >(), 1 );
24- data.randomSeed = nodesSection[" RandomSeed" ].as <int >();
25- }
26-
27- {
28- section = " Physic" ;
29- YAML::Node nodesSection = animGraph[section];
30- if (!nodesSection)
31- errorAndExit (" Cannot find \" " + section + " \" in setting.yaml" );
32-
33- data.physicFrameRate = std::max (nodesSection[" PhysicFrameRate" ].as <int >(), 0 );
34- data.bounciness = std::clamp (nodesSection[" Bounciness" ].as <float >(), 0 .f , 1 .f );
35- data.gravity = Vec2{nodesSection[" GravityX" ].as <float >(), nodesSection[" GravityY" ].as <float >()};
36- data.gravityDir = data.gravity .normalized ();
37- data.friction = std::clamp (nodesSection[" Friction" ].as <float >(), 0 .f , 1 .f );
38- data.continuousCollisionMaxSqrVelocity =
39- std::max (nodesSection[" ContinuousCollisionMaxVelocity" ].as <float >(), 0 .f );
40- data.continuousCollisionMaxSqrVelocity *= data.continuousCollisionMaxSqrVelocity ;
41- data.footBasementWidth = std::max (nodesSection[" FootBasementWidth" ].as <int >(), 2 );
42- data.footBasementHeight = std::max (nodesSection[" FootBasementHeight" ].as <int >(), 2 );
43- data.collisionPixelRatioStopMovement =
44- std::clamp (nodesSection[" CollisionPixelRatioStopMovement" ].as <float >(), 0 .f , 1 .f );
45- data.isGroundedDetection = std::max (nodesSection[" IsGroundedDetection" ].as <float >(), 0 .f );
46- data.releaseImpulse = std::max (nodesSection[" InputReleaseImpulse" ].as <float >(), 0 .f );
47- }
48-
49- {
50- section = " GamePlay" ;
51- YAML::Node nodesSection = animGraph[section];
52- if (!nodesSection)
53- errorAndExit (" Cannot find \" " + section + " \" in setting.yaml" );
54-
55- data.coyoteTimeCursorPos = std::max (nodesSection[" CoyoteTimeCursorMovement" ].as <float >(), 0 .f );
56- }
57-
58- {
59- section = " Window" ;
60- YAML::Node nodesSection = animGraph[section];
61- if (!nodesSection)
62- errorAndExit (" Cannot find \" " + section + " \" in setting.yaml" );
63-
64- data.fullScreenWindow = nodesSection[" FullScreenWindow" ].as <bool >();
65- data.showWindow = nodesSection[" ShowWindow" ].as <bool >();
66- data.showFrameBufferBackground = nodesSection[" ShowFrameBufferBackground" ].as <bool >();
67- data.useForwardWindow = nodesSection[" UseForwardWindow" ].as <bool >();
68- data.useMousePassThoughWindow = nodesSection[" UseMousePassThoughWindow" ].as <bool >();
69- }
70-
71- {
72- section = " Style" ;
73- YAML::Node nodesSection = animGraph[section];
74- if (!nodesSection)
75- errorAndExit (" Cannot find \" " + section + " \" in setting.yaml" );
76-
77- data.styleName = nodesSection[" Theme" ].as <std::string>();
78- }
79-
80- {
81- section = " Accessibility" ;
82- YAML::Node nodesSection = animGraph[section];
83- if (!nodesSection)
84- errorAndExit (" Cannot find \" " + section + " \" in setting.yaml" );
85-
86- data.scale = std::max (nodesSection[" Scale" ].as <int >(), 1 );
87- data.textScale = std::max (nodesSection[" TextScale" ].as <float >(), 1 .f );
88- }
89-
90- {
91- section = " Debug" ;
92- YAML::Node nodesSection = animGraph[section];
93- if (!nodesSection)
94- errorAndExit (" Cannot find \" " + section + " \" in setting.yaml" );
95-
96- data.debugEdgeDetection = nodesSection[" ShowEdgeDetection" ].as <bool >();
16+ YAML::Node nodesSection;
17+ for (auto roleIter = root.begin (); roleIter != root.end (); roleIter++)
18+ {
19+ nodesSection = (*roleIter)[" Game" ];
20+ if (nodesSection)
21+ {
22+ data.FPS = std::max (nodesSection[" FPS" ].as <int >(), 1 );
23+ data.randomSeed = nodesSection[" RandomSeed" ].as <int >();
24+ continue ;
25+ }
26+
27+ nodesSection = (*roleIter)[" Physic" ];
28+ if (nodesSection)
29+ {
30+ data.physicFrameRate = std::max (nodesSection[" PhysicFrameRate" ].as <int >(), 0 );
31+ data.bounciness = std::clamp (nodesSection[" Bounciness" ].as <float >(), 0 .f , 1 .f );
32+ data.gravity = Vec2{nodesSection[" GravityX" ].as <float >(), nodesSection[" GravityY" ].as <float >()};
33+ data.gravityDir = data.gravity .normalized ();
34+ data.friction = std::clamp (nodesSection[" Friction" ].as <float >(), 0 .f , 1 .f );
35+ data.continuousCollisionMaxSqrVelocity =
36+ std::max (nodesSection[" ContinuousCollisionMaxVelocity" ].as <float >(), 0 .f );
37+ data.continuousCollisionMaxSqrVelocity *= data.continuousCollisionMaxSqrVelocity ;
38+ data.footBasementWidth = std::max (nodesSection[" FootBasementWidth" ].as <int >(), 2 );
39+ data.footBasementHeight = std::max (nodesSection[" FootBasementHeight" ].as <int >(), 2 );
40+ data.collisionPixelRatioStopMovement =
41+ std::clamp (nodesSection[" CollisionPixelRatioStopMovement" ].as <float >(), 0 .f , 1 .f );
42+ data.isGroundedDetection = std::max (nodesSection[" IsGroundedDetection" ].as <float >(), 0 .f );
43+ data.releaseImpulse = std::max (nodesSection[" InputReleaseImpulse" ].as <float >(), 0 .f );
44+ continue ;
45+ }
46+
47+ nodesSection = (*roleIter)[" GamePlay" ];
48+ if (nodesSection)
49+ {
50+ data.coyoteTimeCursorPos = std::max (nodesSection[" CoyoteTimeCursorMovement" ].as <float >(), 0 .f );
51+ continue ;
52+ }
53+
54+ nodesSection = (*roleIter)[" Window" ];
55+ if (nodesSection)
56+ {
57+ data.fullScreenWindow = nodesSection[" FullScreenWindow" ].as <bool >();
58+ data.showWindow = nodesSection[" ShowWindow" ].as <bool >();
59+ data.showFrameBufferBackground = nodesSection[" ShowFrameBufferBackground" ].as <bool >();
60+ data.useForwardWindow = nodesSection[" UseForwardWindow" ].as <bool >();
61+ data.useMousePassThoughWindow = nodesSection[" UseMousePassThoughWindow" ].as <bool >();
62+ continue ;
63+ }
64+
65+ nodesSection = (*roleIter)[" Style" ];
66+ if (nodesSection)
67+ {
68+ data.styleName = nodesSection[" Theme" ].as <std::string>();
69+ continue ;
70+ }
71+
72+ nodesSection = (*roleIter)[" Accessibility" ];
73+ if (nodesSection)
74+ {
75+ data.scale = std::max (nodesSection[" Scale" ].as <int >(), 1 );
76+ data.textScale = std::max (nodesSection[" TextScale" ].as <float >(), 1 .f );
77+ continue ;
78+ }
79+
80+ nodesSection = (*roleIter)[" Debug" ];
81+ if (nodesSection)
82+ {
83+ data.debugEdgeDetection = nodesSection[" ShowEdgeDetection" ].as <bool >();
84+ continue ;
85+ }
9786 }
9887}
9988
@@ -107,7 +96,7 @@ void Setting::exportFile(const char* dest, GameData& data)
10796 }
10897
10998 YAML::Emitter out;
110-
99+ out << YAML::BeginSeq;
111100 std::string section;
112101 {
113102 section = " Game" ;
@@ -130,11 +119,12 @@ void Setting::exportFile(const char* dest, GameData& data)
130119 out << YAML::Key << " GravityX" << YAML::Value << YAML::Precision (4 ) << data.gravity .x ;
131120 out << YAML::Key << " GravityY" << YAML::Value << YAML::Precision (4 ) << data.gravity .y ;
132121 out << YAML::Key << " Friction" << YAML::Value << YAML::Precision (4 ) << data.friction ;
133- out << YAML::Key << " ContinuousCollisionMaxVelocity" << YAML::Value << std::sqrt (data.continuousCollisionMaxSqrVelocity );
122+ out << YAML::Key << " ContinuousCollisionMaxVelocity" << YAML::Value
123+ << std::sqrt (data.continuousCollisionMaxSqrVelocity );
134124 out << YAML::Key << " FootBasementWidth" << YAML::Value << data.footBasementWidth ;
135125 out << YAML::Key << " FootBasementHeight" << YAML::Value << data.footBasementHeight ;
136126 out << YAML::Key << " CollisionPixelRatioStopMovement" << YAML::Value << YAML::Precision (4 )
137- << data.collisionPixelRatioStopMovement ;
127+ << data.collisionPixelRatioStopMovement ;
138128 out << YAML::Key << " IsGroundedDetection" << YAML::Value << data.isGroundedDetection ;
139129 out << YAML::Key << " InputReleaseImpulse" << YAML::Value << data.releaseImpulse ;
140130 out << YAML::EndMap;
@@ -195,7 +185,7 @@ void Setting::exportFile(const char* dest, GameData& data)
195185 out << YAML::EndMap;
196186 out << YAML::EndMap;
197187 }
198-
188+ out << YAML::EndSeq;
199189 fwrite (out.c_str (), sizeof (char ), out.size (), file);
200190 fclose (file);
201191}
0 commit comments