#include "scene.h" using namespace Mauri; auto Scene::parse(const std::string &path) -> Scene * { Scene *scene = new Scene(); Asset *asset = asset_manager()->get(path); if (asset->type == ASSET_VOID) parse_fail(scene); const json &root = json::parse(asset->as_string(), nullptr, false); if (root.is_discarded()) parse_fail(scene); Parser &pr = scene->parser; const json &camera = root["camera"]; pr.map_value(camera, "center", &scene->cam.center, vec3(0.f)); pr.map_value(camera, "eye", &scene->cam.eye, vec3(0.f)); pr.map_value(camera, "up", &scene->cam.up, vec3(0.f)); const json &general = root["general"]; pr.map_value(general, "clearenabled", &scene->clear_enabled, true); pr.map_value(general, "clearcolor", &scene->clear_color, vec4(1.f)); pr.map_value(general, "nearz", &scene->nearz, -1.f); pr.map_value(general, "farz", &scene->farz, 1.f); const json &ortho = general["orthogonalprojection"]; pr.map_value(ortho, "width", &scene->width, 0); pr.map_value(ortho, "height", &scene->height, 0); for (const json &object : root["objects"]) { if (!object.contains("image") || object["image"].is_null()) continue; if (object["name"] == "ICUE") continue; Object *obj = Object::parse(scene->parser, object); if (obj) scene->objects.emplace_back(obj); } if ((scene->width == 0 || scene->height == 0) && scene->objects.size() > 0) { scene->width = scene->objects[0]->size[0]; scene->height = scene->objects[0]->size[1]; } return scene; } auto Scene::offset_camera(s32 x, s32 y) -> void { this->cam.offset[0] = x; this->cam.offset[1] = y; } auto Scene::get_object_by_id(u32 id) -> Object * { for (Object *object : this->objects) { if (object->id == id) return object; } return nullptr; } auto Scene::load(Engine *engine) -> void { /* NOTE: Removing objects here is known to cause issues. if (this->objects.size() > 0) { for (auto it = this->objects.begin(); it != this->objects.end();) { if (!(*it)->visible) it = this->objects.erase(it); else it++; } } */ for (Object *object : this->objects) { object->load(engine); } } auto Scene::draw(Engine *engine) -> void { for (Object *object : this->objects) { if (!object->loaded_as_dep && object->visible) object->draw(engine); } } Scene::~Scene() { for (Object *object : this->objects) { delete object; } }