summaryrefslogtreecommitdiff
path: root/src/objects/scene.cc
blob: 21a244993cd2ec6ab221e53dfe2b398040d07164 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#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<vec3>(camera, "center", &scene->cam.center, vec3(0.f));
    pr.map_value<vec3>(camera, "eye", &scene->cam.eye, vec3(0.f));
    pr.map_value<vec3>(camera, "up", &scene->cam.up, vec3(0.f));

    const json &general = root["general"];
    pr.map_value<bool>(general, "clearenabled", &scene->clear_enabled, true);
    pr.map_value<vec4>(general, "clearcolor", &scene->clear_color, vec4(1.f));
    pr.map_value<f32>(general, "nearz", &scene->nearz, -1.f);
    pr.map_value<f32>(general, "farz", &scene->farz, 1.f);

    const json &ortho = general["orthogonalprojection"];
    pr.map_value<s32>(ortho, "width", &scene->width, 0);
    pr.map_value<s32>(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);
    }

    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
{
    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->draw(engine);
    }
}

Scene::~Scene()
{
    for (Object *object : this->objects)
    {
        delete object;
    }
}