-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scene.cpp
87 lines (70 loc) · 1.79 KB
/
Scene.cpp
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
#include "Scene.h"
#include "IDrawable.h"
#include "Map.h"
Scene::Scene(QQuickWindow *window)
: m_window(window)
, m_dirty(false)
{
setFlag(QSGNode::UsePreprocess);
setCameraPosition(QPointF(0, 0));
}
QSGTexture *Scene::createTexture(const QImage &image)
{
return m_window->createTextureFromImage(image);
}
QSGTexture *Scene::createTexture(const QString &filename)
{
QImage image(filename);
QSGTexture *texture = createTexture(image);
return texture;
}
void Scene::setCameraPosition(const QPointF &position, Map *map)
{
QPointF windowCenter(m_window->width() / 2, m_window->height() / 2);
m_cameraPosition = position - windowCenter;
if(m_cameraPosition.x() < 0)
m_cameraPosition.setX(0);
if(m_cameraPosition.y() < 0)
m_cameraPosition.setY(0);
if(!map)
return;
if(m_cameraPosition.x() + m_window->width() > map->width())
m_cameraPosition.setX(map->width() - m_window->width());
if(m_cameraPosition.y() + m_window->height() > map->height())
m_cameraPosition.setY(map->height() - m_window->height());
}
void Scene::add(IDrawable *drawable)
{
m_drawables << drawable;
m_dirty = true;
appendChildNode(drawable);
}
void Scene::remove(IDrawable *drawable)
{
m_drawables.removeAll(drawable);
removeChildNode(drawable);
}
void Scene::preprocess()
{
if(m_dirty)
{
m_drawables.sort(&IDrawable::compare);
m_dirty = false;
removeAllChildNodes();
for(IDrawable *drawable : m_drawables)
{
appendChildNode(drawable);
}
}
for(IDrawable *drawable : m_drawables)
{
QSGTexture *texture = drawable->texture();
QPointF position(drawable->x(), drawable->y());
position -= m_cameraPosition;
QSize size = texture->textureSize();
QRectF rect(position, size);
QSGSimpleTextureNode *node = (QSGSimpleTextureNode *)drawable;
node->setRect(rect);
node->setTexture(texture);
}
}