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