Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/scene/test_sprite_2d.cpp
45991 views
1
/**************************************************************************/
2
/* test_sprite_2d.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 "tests/test_macros.h"
32
33
TEST_FORCE_LINK(test_sprite_2d)
34
35
#include "scene/2d/sprite_2d.h"
36
37
namespace TestSprite2D {
38
39
TEST_CASE("[SceneTree][Sprite2D] Constructor") {
40
Sprite2D *sprite_2d = memnew(Sprite2D);
41
42
CHECK(sprite_2d->get_texture().is_null());
43
CHECK_EQ(sprite_2d->get_offset(), Point2(0, 0));
44
CHECK(sprite_2d->is_centered());
45
CHECK_FALSE(sprite_2d->is_flipped_h());
46
CHECK_FALSE(sprite_2d->is_flipped_v());
47
CHECK_EQ(sprite_2d->get_hframes(), 1);
48
CHECK_EQ(sprite_2d->get_vframes(), 1);
49
CHECK_EQ(sprite_2d->get_frame(), 0);
50
CHECK_EQ(sprite_2d->get_frame_coords(), Vector2i(0, 0));
51
CHECK_FALSE(sprite_2d->is_region_enabled());
52
53
memdelete(sprite_2d);
54
}
55
56
TEST_CASE("[SceneTree][Sprite2D] Frames") {
57
Sprite2D *sprite_2d = memnew(Sprite2D);
58
59
SUBCASE("Invalid range") {
60
ERR_PRINT_OFF;
61
sprite_2d->set_frame(30);
62
sprite_2d->set_frame(-1);
63
ERR_PRINT_ON;
64
CHECK(sprite_2d->get_frame() == 0);
65
}
66
67
SUBCASE("Base value") {
68
sprite_2d->set_hframes(1);
69
sprite_2d->set_vframes(1);
70
CHECK(sprite_2d->get_frame() == 0);
71
}
72
73
SUBCASE("2x2 frames") {
74
sprite_2d->set_hframes(2);
75
sprite_2d->set_vframes(2);
76
77
CHECK(sprite_2d->get_hframes() == 2);
78
CHECK(sprite_2d->get_vframes() == 2);
79
80
sprite_2d->set_frame(0);
81
CHECK(sprite_2d->get_frame() == 0);
82
CHECK_EQ(sprite_2d->get_frame_coords(), Vector2i(0, 0));
83
84
sprite_2d->set_frame(1);
85
CHECK(sprite_2d->get_frame() == 1);
86
CHECK_EQ(sprite_2d->get_frame_coords(), Vector2i(1, 0));
87
88
sprite_2d->set_frame(2);
89
CHECK(sprite_2d->get_frame() == 2);
90
CHECK_EQ(sprite_2d->get_frame_coords(), Vector2i(0, 1));
91
92
sprite_2d->set_frame(3);
93
CHECK(sprite_2d->get_frame() == 3);
94
CHECK_EQ(sprite_2d->get_frame_coords(), Vector2i(1, 1));
95
}
96
97
SUBCASE("4x4 frames") {
98
sprite_2d->set_hframes(4);
99
sprite_2d->set_vframes(4);
100
101
CHECK(sprite_2d->get_hframes() == 4);
102
CHECK(sprite_2d->get_vframes() == 4);
103
104
sprite_2d->set_frame(0);
105
CHECK(sprite_2d->get_frame() == 0);
106
CHECK_EQ(sprite_2d->get_frame_coords(), Vector2i(0, 0));
107
108
sprite_2d->set_frame(2);
109
CHECK(sprite_2d->get_frame() == 2);
110
CHECK_EQ(sprite_2d->get_frame_coords(), Vector2i(2, 0));
111
112
sprite_2d->set_frame(4);
113
CHECK(sprite_2d->get_frame() == 4);
114
CHECK_EQ(sprite_2d->get_frame_coords(), Vector2i(0, 1));
115
116
sprite_2d->set_frame(6);
117
CHECK(sprite_2d->get_frame() == 6);
118
CHECK_EQ(sprite_2d->get_frame_coords(), Vector2i(2, 1));
119
120
sprite_2d->set_frame(8);
121
CHECK(sprite_2d->get_frame() == 8);
122
CHECK_EQ(sprite_2d->get_frame_coords(), Vector2i(0, 2));
123
124
sprite_2d->set_frame(10);
125
CHECK(sprite_2d->get_frame() == 10);
126
CHECK_EQ(sprite_2d->get_frame_coords(), Vector2i(2, 2));
127
128
sprite_2d->set_frame(12);
129
CHECK(sprite_2d->get_frame() == 12);
130
CHECK_EQ(sprite_2d->get_frame_coords(), Vector2i(0, 3));
131
132
sprite_2d->set_frame(14);
133
CHECK(sprite_2d->get_frame() == 14);
134
CHECK_EQ(sprite_2d->get_frame_coords(), Vector2i(2, 3));
135
}
136
137
SUBCASE("8x4 frames") {
138
sprite_2d->set_hframes(8);
139
sprite_2d->set_vframes(4);
140
141
CHECK(sprite_2d->get_hframes() == 8);
142
CHECK(sprite_2d->get_vframes() == 4);
143
144
sprite_2d->set_frame(0);
145
CHECK(sprite_2d->get_frame() == 0);
146
CHECK_EQ(sprite_2d->get_frame_coords(), Vector2i(0, 0));
147
148
sprite_2d->set_frame(4);
149
CHECK(sprite_2d->get_frame() == 4);
150
CHECK_EQ(sprite_2d->get_frame_coords(), Vector2i(4, 0));
151
152
sprite_2d->set_frame(8);
153
CHECK(sprite_2d->get_frame() == 8);
154
CHECK_EQ(sprite_2d->get_frame_coords(), Vector2i(0, 1));
155
156
sprite_2d->set_frame(16);
157
CHECK(sprite_2d->get_frame() == 16);
158
CHECK_EQ(sprite_2d->get_frame_coords(), Vector2i(0, 2));
159
160
sprite_2d->set_frame(31);
161
CHECK(sprite_2d->get_frame() == 31);
162
CHECK_EQ(sprite_2d->get_frame_coords(), Vector2i(7, 3));
163
}
164
165
SUBCASE("100x100 frames") {
166
sprite_2d->set_hframes(100);
167
sprite_2d->set_vframes(100);
168
169
CHECK(sprite_2d->get_hframes() == 100);
170
CHECK(sprite_2d->get_vframes() == 100);
171
172
sprite_2d->set_frame(0);
173
CHECK(sprite_2d->get_frame() == 0);
174
CHECK_EQ(sprite_2d->get_frame_coords(), Vector2i(0, 0));
175
176
sprite_2d->set_frame(60);
177
CHECK(sprite_2d->get_frame() == 60);
178
CHECK_EQ(sprite_2d->get_frame_coords(), Vector2i(60, 0));
179
180
sprite_2d->set_frame(120);
181
CHECK(sprite_2d->get_frame() == 120);
182
CHECK_EQ(sprite_2d->get_frame_coords(), Vector2i(20, 1));
183
184
sprite_2d->set_frame(240);
185
CHECK(sprite_2d->get_frame() == 240);
186
CHECK_EQ(sprite_2d->get_frame_coords(), Vector2i(40, 2));
187
188
sprite_2d->set_frame(360);
189
CHECK(sprite_2d->get_frame() == 360);
190
CHECK_EQ(sprite_2d->get_frame_coords(), Vector2i(60, 3));
191
192
sprite_2d->set_frame(2048);
193
CHECK(sprite_2d->get_frame() == 2048);
194
CHECK_EQ(sprite_2d->get_frame_coords(), Vector2i(48, 20));
195
196
sprite_2d->set_frame(8192);
197
CHECK(sprite_2d->get_frame() == 8192);
198
CHECK_EQ(sprite_2d->get_frame_coords(), Vector2i(92, 81));
199
}
200
201
memdelete(sprite_2d);
202
}
203
204
TEST_CASE("[SceneTree][Sprite2D] Flipping") {
205
Sprite2D *sprite_2d = memnew(Sprite2D);
206
207
SUBCASE("Both False") {
208
CHECK_FALSE(sprite_2d->is_flipped_h());
209
CHECK_FALSE(sprite_2d->is_flipped_v());
210
}
211
212
SUBCASE("Both True") {
213
sprite_2d->set_flip_h(true);
214
sprite_2d->set_flip_v(true);
215
216
CHECK(sprite_2d->is_flipped_h());
217
CHECK(sprite_2d->is_flipped_v());
218
}
219
220
SUBCASE("True False") {
221
sprite_2d->set_flip_h(true);
222
sprite_2d->set_flip_v(false);
223
224
CHECK(sprite_2d->is_flipped_h());
225
CHECK_FALSE(sprite_2d->is_flipped_v());
226
}
227
228
SUBCASE("False True") {
229
sprite_2d->set_flip_h(false);
230
sprite_2d->set_flip_v(true);
231
232
CHECK_FALSE(sprite_2d->is_flipped_h());
233
CHECK(sprite_2d->is_flipped_v());
234
}
235
236
memdelete(sprite_2d);
237
}
238
239
TEST_CASE("[SceneTree][Sprite2D] Offset") {
240
Sprite2D *sprite_2d = memnew(Sprite2D);
241
242
sprite_2d->set_offset(Point2(0.0, 0.0));
243
CHECK(sprite_2d->get_offset() == Point2(0.0, 0.0));
244
245
sprite_2d->set_offset(Point2(8.0, 8.0));
246
CHECK(sprite_2d->get_offset() == Point2(8.0, 8.0));
247
248
sprite_2d->set_offset(Point2(25.0, 50.0));
249
CHECK(sprite_2d->get_offset() == Point2(25.0, 50.0));
250
251
sprite_2d->set_offset(Point2(500.0, 250.0));
252
CHECK(sprite_2d->get_offset() == Point2(500.0, 250.0));
253
254
sprite_2d->set_offset(Point2(-8.0, -8.0));
255
CHECK(sprite_2d->get_offset() == Point2(-8.0, -8.0));
256
257
sprite_2d->set_offset(Point2(-25.0, -50.0));
258
CHECK(sprite_2d->get_offset() == Point2(-25.0, -50.0));
259
260
sprite_2d->set_offset(Point2(-500.0, -250.0));
261
CHECK(sprite_2d->get_offset() == Point2(-500.0, -250.0));
262
263
memdelete(sprite_2d);
264
}
265
266
} // namespace TestSprite2D
267
268