This repository was archived by the owner on Oct 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.cpp
More file actions
121 lines (96 loc) · 3.19 KB
/
engine.cpp
File metadata and controls
121 lines (96 loc) · 3.19 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//
// Created by lepet on 8/2/2018.
//
#include <iostream>
#include <models/model.h>
#include <models/ms3d.h>
#include <objects/object.h>
#include <objects/camera.h>
#include <system/grid.h>
#include <system/engine.h>
using namespace std;
engine& engine::get_instance() {
static engine instance;
return instance;
}
model* engine::get_model(const string& name) {
auto iter = models.find(name);
if (iter == models.end()) {
cerr << "Cannot locate model " << name << endl;
return nullptr;
} else
return iter->second.get();
}
void engine::add_object(std::unique_ptr<object>&& child) {
world->add_child(std::move(child));
}
// Load models and textures.
void engine::load_resources() {
std::unique_ptr<model> mesh{nullptr};
// Load car.
mesh = std::make_unique<ms3d>("./data/car/car.ms3d");
mesh->load_texture("./data/car/car.bmp");
models.insert({"car.body", std::move(mesh)});
mesh = std::make_unique<ms3d>("./data/car/wheel.ms3d");
mesh->load_texture("./data/car/wheel.bmp");
models.insert({"car.wheel", std::move(mesh)});
// Load ufo body.
mesh = std::make_unique<ms3d>("./data/ufo/body.ms3d");
mesh->load_texture("./data/ufo/diffuse.bmp");
models.insert({"ufo.body", std::move(mesh)});
// Load ufo cockpit.
mesh = std::make_unique<ms3d>("./data/ufo/cockpit.ms3d");
mesh->load_texture("./data/ufo/diffuse_glow.bmp");
models.insert({"ufo.cockpit", std::move(mesh)});
// Load rocket.
mesh = std::make_unique<ms3d>("./data/rocket/rocket.ms3d");
mesh->set_color({0, 0, 0, 1.0f});
models.insert({"rocket", std::move(mesh)});
// Load tiles.
mesh = std::make_unique<ms3d>("./data/tiles/tile.ms3d");
mesh->load_texture("./data/tiles/block.bmp");
models.insert({"tiles.block", std::move(mesh)});
mesh = std::make_unique<ms3d>("./data/tiles/tile.ms3d");
mesh->load_texture("./data/tiles/park.bmp");
models.insert({"tiles.park", std::move(mesh)});
mesh = std::make_unique<ms3d>("./data/tiles/tile.ms3d");
mesh->load_texture("./data/tiles/path.bmp");
models.insert({"tiles.path", std::move(mesh)});
}
void engine::initialize() {
// Initialize world as the parent of all other objects.
park->create_scene();
park->find_empty();
park->generate_car();
}
void engine::update() {
world->update();
}
void engine::render() {
// Render for 3 passes.
// The first pass, render normal objects.
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
world->render(0);
// The second pass, render outlines.
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
world->render(1);
// Last pass, render objects with outlines.
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
world->render(2);
}
void engine::mouse(int button, int state, glm::vec<2, int>& pos) {
world->mouse(button, state, pos);
}
void engine::motion(glm::vec<2, int>& pos) {
world->motion(pos);
}
engine::engine() :
park{std::make_unique<grid>(8, 10)},
world{std::make_unique<camera>(glm::vec3{2.0f, 6.0f, 6.0f},
glm::vec3{3.5f, 0, -4.5f},
glm::vec3{0, 1, 0})} {
load_resources();
}