diff --git a/src/yaml/SConscript b/src/yaml/SConscript index 4205d69..ca3c5a9 100644 --- a/src/yaml/SConscript +++ b/src/yaml/SConscript @@ -7,6 +7,7 @@ Import('env') files = [ 'parsers.cc', + 'object_parser.cc', 'scene_parser.cc', ] diff --git a/src/yaml/object_parser.cc b/src/yaml/object_parser.cc index e69de29..8d2d6da 100644 --- a/src/yaml/object_parser.cc +++ b/src/yaml/object_parser.cc @@ -0,0 +1,112 @@ +/* object_parser.cc + * vim: set tw=80: + * Eryn Wells + */ +/** + * Implementation of ObjectParser. + */ + +#include +#include +#include + +#include "yaml/object_parser.hh" + +#include "object_sphere.h" +#include "yaml/vector_parser.hh" + + +namespace yaml { + +ObjectParser::ObjectParser(Scene& scene, + ParserStack& parsers) + : Parser(scene, parsers) +{ } + + +void +ObjectParser::HandleEvent(yaml_event_t& event) +{ + switch (mSection) { + case NoSection: + HandleTopLevelEvent(event); + break; + case OriginSection: + break; + case RadiusSection: + break; + default: + assert(false); + break; + } +} + + +void +ObjectParser::HandleTopLevelEvent(yaml_event_t& event) +{ + static const std::string ORIGIN = "origin"; + static const std::string RADIUS = "radius"; + + if (event.type == YAML_MAPPING_END_EVENT) { + SetDone(true); + return; + } + + if (event.type != YAML_SCALAR_EVENT) { + /* TODO: Clean this up. */ + assert(false); + } + + std::string value = (char *)event.data.scalar.value; + if (value == ORIGIN) { + mSection = OriginSection; + } + else if (value == RADIUS) { + mSection = RadiusSection; + } + else { + /* TODO: Clean this up. */ + assert(false); + } +} + + +void +ObjectParser::HandleOriginEvent(yaml_event_t& event) +{ + if (event.type != YAML_SEQUENCE_START_EVENT) { + /* TODO: Clean this up. */ + assert(false); + } + + auto onDone = [this](std::vector origin) { + if (origin.size() < 3) { + assert(origin.size() < 3); + } + mObject->set_origin(Vector3(origin[0], origin[1], origin[2])); + mSection = NoSection; + }; + + GetParsers().push(new VectorParser(GetScene(), GetParsers(), onDone)); +} + + +void +ObjectParser::HandleRadiusEvent(yaml_event_t& event) +{ + if (event.type != YAML_SCALAR_EVENT) { + /* TODO: Clean this up. */ + assert(false); + } + + double radius; + if (!ParseScalar((char *)event.data.scalar.value, radius)) { + /* TODO: Clean this up. */ + assert(false); + } + mObject->set_radius(radius); + mSection = NoSection; +} + +} /* namespace yaml */ diff --git a/src/yaml/object_parser.hh b/src/yaml/object_parser.hh index e69de29..19e2c81 100644 --- a/src/yaml/object_parser.hh +++ b/src/yaml/object_parser.hh @@ -0,0 +1,42 @@ +/* object_parser.hh + * vim: set tw=80: + * Eryn Wells + */ +/** + * An ObjectParser parses objects of various sorts. + */ + +#ifndef __YAML_OBJECTPARSER_HH__ +#define __YAML_OBJECTPARSER_HH__ + +#include "yaml/parsers.hh" + + +class Sphere; + + +namespace yaml { + +struct ObjectParser + : public Parser +{ + ObjectParser(Scene& scene, ParserStack& parsers); + + void HandleEvent(yaml_event_t& event); + void HandleTopLevelEvent(yaml_event_t& event); + void HandleOriginEvent(yaml_event_t& event); + void HandleRadiusEvent(yaml_event_t& event); + +private: + Sphere* mObject; + + enum { + NoSection, + OriginSection, + RadiusSection + } mSection; +}; + +} /* namespace yaml */ + +#endif /* __YAML_OBJECTPARSER_HH__ */