Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/servers/rendering/renderer_canvas_render.cpp
10277 views
1
/**************************************************************************/
2
/* renderer_canvas_render.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "renderer_canvas_render.h"
32
#include "servers/rendering/rendering_server_globals.h"
33
34
RendererCanvasRender *RendererCanvasRender::singleton = nullptr;
35
36
const Rect2 &RendererCanvasRender::Item::get_rect() const {
37
if (custom_rect || (!rect_dirty && !update_when_visible && skeleton == RID())) {
38
return rect;
39
}
40
41
//must update rect
42
43
if (commands == nullptr) {
44
rect = Rect2();
45
rect_dirty = false;
46
return rect;
47
}
48
49
Transform2D xf;
50
bool found_xform = false;
51
bool first = true;
52
53
const Item::Command *c = commands;
54
55
while (c) {
56
Rect2 r;
57
58
switch (c->type) {
59
case Item::Command::TYPE_RECT: {
60
const Item::CommandRect *crect = static_cast<const Item::CommandRect *>(c);
61
r = crect->rect;
62
63
} break;
64
case Item::Command::TYPE_NINEPATCH: {
65
const Item::CommandNinePatch *style = static_cast<const Item::CommandNinePatch *>(c);
66
r = style->rect;
67
} break;
68
69
case Item::Command::TYPE_POLYGON: {
70
const Item::CommandPolygon *polygon = static_cast<const Item::CommandPolygon *>(c);
71
r = polygon->polygon.rect_cache;
72
} break;
73
case Item::Command::TYPE_PRIMITIVE: {
74
const Item::CommandPrimitive *primitive = static_cast<const Item::CommandPrimitive *>(c);
75
for (uint32_t j = 0; j < primitive->point_count; j++) {
76
if (j == 0) {
77
r.position = primitive->points[0];
78
} else {
79
r.expand_to(primitive->points[j]);
80
}
81
}
82
} break;
83
case Item::Command::TYPE_MESH: {
84
const Item::CommandMesh *mesh = static_cast<const Item::CommandMesh *>(c);
85
AABB aabb = RSG::mesh_storage->mesh_get_aabb(mesh->mesh, skeleton);
86
87
r = Rect2(aabb.position.x, aabb.position.y, aabb.size.x, aabb.size.y);
88
89
} break;
90
case Item::Command::TYPE_MULTIMESH: {
91
const Item::CommandMultiMesh *multimesh = static_cast<const Item::CommandMultiMesh *>(c);
92
AABB aabb = RSG::mesh_storage->multimesh_get_aabb(multimesh->multimesh);
93
94
r = Rect2(aabb.position.x, aabb.position.y, aabb.size.x, aabb.size.y);
95
96
} break;
97
case Item::Command::TYPE_PARTICLES: {
98
const Item::CommandParticles *particles_cmd = static_cast<const Item::CommandParticles *>(c);
99
if (particles_cmd->particles.is_valid()) {
100
AABB aabb = RSG::particles_storage->particles_get_aabb(particles_cmd->particles);
101
r = Rect2(aabb.position.x, aabb.position.y, aabb.size.x, aabb.size.y);
102
}
103
104
} break;
105
case Item::Command::TYPE_TRANSFORM: {
106
const Item::CommandTransform *transform = static_cast<const Item::CommandTransform *>(c);
107
xf = transform->xform;
108
found_xform = true;
109
[[fallthrough]];
110
}
111
default: {
112
c = c->next;
113
continue;
114
}
115
}
116
117
if (found_xform) {
118
r = xf.xform(r);
119
}
120
121
if (first) {
122
rect = r;
123
first = false;
124
} else {
125
rect = rect.merge(r);
126
}
127
c = c->next;
128
}
129
130
rect_dirty = false;
131
return rect;
132
}
133
134
RendererCanvasRender::Item::CommandMesh::~CommandMesh() {
135
if (mesh_instance.is_valid()) {
136
RSG::mesh_storage->mesh_instance_free(mesh_instance);
137
}
138
}
139
140