forked from plastictown/Engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscene.cpp
More file actions
161 lines (138 loc) · 3.98 KB
/
Copy pathscene.cpp
File metadata and controls
161 lines (138 loc) · 3.98 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* https://github.com/plastictown/Engine
* Copyright (C) 2018 Mikhail Domchenkov
*/
#include <memory>
#include <cmath>
#include <image.h>
#include <scene.h>
#include <render.h>
#include <point-object.h>
using namespace std;
Scene::Scene() : m_visibility{false} {}
uint32_t Scene::AddObject(std::shared_ptr<GameObject>& obj) {
uint32_t id = gen_id.get_next();
objects.insert(std::make_pair(id, obj));
return id;
}
void Scene::RemoveObject(uint32_t key) {
auto ptr = objects.find(key);
if (ptr != objects.cend())
objects.erase(ptr);
}
std::shared_ptr<GameObject> Scene::GetObject(uint32_t key) {
auto ptr = objects.find(key);
if (ptr != objects.cend())
return objects[key];
return nullptr;
}
void Scene::Draw() {
/*
for(auto& o:objects)
{
if(!o.second->isVisible())
continue;
switch(o.second->getType())
{
case GameObjectType::TypeImage:
{
auto ptr = std::static_pointer_cast<Image>(o.second);
Render::DrawImage(ptr->getImage(), ptr->getPosition());
break;
}
case GameObjectType::TypeAnimation:
{
auto ptr = std::static_pointer_cast<Animation>(o.second);
Render::DrawImage(ptr->getImage(), ptr->getPosition());
break;
}
}
}
*/
/*
glBegin(GL_POINTS);
glColor4f(0.0f,0.0f,0.0f,1.0f);
glVertex2f(100.0f,100.0f);
glEnd();
*/
// тест - точки
// glPointSize(4.0f);
// static float delta_x = 0;
// for(float i= 0.0f;i<640;i+=1.0f){
// PointObject po(Point2f{i, 240+sin((i+delta_x)/100)*200},
// Color4{1,0,0,1});
// po.draw();
// delta_x += 0.01f;
// }
//тест - прямая
// TODO - написать в рендере функцию, которая вернет текущий размер окна
glLineWidth(16.0);
auto x = Render::getWindowSize().x;
auto y = Render::getWindowSize().y;
Color4 ColorBlue = Color4(0, 0, 1);
static float angle = 50;
LineObject lo1(Point2f(0 - x / 2, 0 - y / 2), Point2f(x - x / 2, y - y / 2),
ColorBlue);
LineObject lo2(Point2f(x - x / 2, 0 - y / 2), Point2f(0 - x / 2, y - y / 2),
ColorBlue);
glLoadIdentity();
glPushMatrix();
glTranslatef(x / 2, y / 2, 0);
glRotatef(angle, 0, 0, 1);
lo1.draw();
lo2.draw();
glPopMatrix();
angle += 1.1f;
}
size_t Scene::Count() const {
return objects.size();
}
bool Scene::FromFile(const std::string& filename) try {
auto rv = m_loader.ParseScene(filename);
if (!rv) {
cerr << __FUNCTION__ << ": "
<< "can't load scene from file" << endl;
return false;
}
auto& images = m_loader.getImages();
auto& animations = m_loader.getAnimations();
auto& description = m_loader.getDescription();
this->setVisible(description.visibility);
for (auto& img : images) {
std::shared_ptr<GameObject> im = make_shared<Image>();
im->setVisible(img.visibility);
auto pi = std::static_pointer_cast<Image>(im);
if (!pi->load(img.file))
throw runtime_error(string("can't load image: ") + img.file);
pi->setPosition(img.pos);
this->AddObject(im);
} // for all images
for (auto& ani : animations) {
std::shared_ptr<GameObject> a = make_shared<Animation>(ani.interval);
a->setVisible(ani.visibility);
auto pa = std::static_pointer_cast<Animation>(a);
for (auto& f : ani.files) {
Image im;
if (!im.load(f))
throw runtime_error(string("can't load image: ") + f);
pa->AddImage(im);
}
pa->setPosition(ani.pos);
this->AddObject(a);
} // for all animations
return true;
} // Scene::FromFile()
catch (const std::exception& e) {
cerr << "Scene::FromFile failed: " << e.what() << endl;
return false;
}
void Scene::print() const {
cout << __FUNCTION__ << ": there are " << objects.size()
<< " objects in scene" << endl;
}
void Scene::setVisible(bool state) noexcept {
m_visibility = state;
}
bool Scene::isVisible() const noexcept {
return m_visibility;
}