A game framework written with osu! in mind.
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2// See the LICENCE file in the repository root for full licence text.
3
4using NUnit.Framework;
5using osu.Framework.Graphics.OpenGL.Textures;
6using osu.Framework.Graphics.Primitives;
7using osu.Framework.Graphics.Textures;
8
9namespace osu.Framework.Tests.Graphics
10{
11 [TestFixture]
12 public class TextureAtlasTest
13 {
14 [TestCase(0, 0)]
15 [TestCase(1, 1)]
16 [TestCase(984, 20)]
17 [TestCase(985, 20)]
18 [TestCase(1008, 20)]
19 [TestCase(1020, 20)]
20 [TestCase(1024, 20)]
21 [TestCase(1025, 20)]
22 [TestCase(20, 984)]
23 [TestCase(20, 985)]
24 [TestCase(20, 1008)]
25 [TestCase(20, 1020)]
26 [TestCase(20, 1024)]
27 [TestCase(20, 1500)]
28 [TestCase(984, 984)]
29 [TestCase(985, 985)]
30 [TestCase(1020, 985)]
31 [TestCase(1500, 985)]
32 [TestCase(1008, 1008)]
33 [TestCase(1020, 1020)]
34 [TestCase(1500, 1500)]
35 public void TestAtlasAdd(int width, int height)
36 {
37 Assert.DoesNotThrow(() => testWithSize(width, height));
38 }
39
40 private void testWithSize(int width, int height)
41 {
42 TextureAtlas atlas = new TextureAtlas(1024, 1024);
43 TextureGL texture = atlas.Add(width, height);
44
45 if (texture != null)
46 {
47 Assert.AreEqual(texture.Width, width, message: $"Width: {texture.Width} != {width} for texture {width}x{height}");
48 Assert.AreEqual(texture.Height, height, message: $"Height: {texture.Height} != {height} for texture {width}x{height}");
49
50 RectangleF rect = texture.GetTextureRect(null);
51 Assert.LessOrEqual(rect.X + rect.Width, 1, message: $"Returned texture is wider than TextureAtlas for texture {width}x{height}");
52 Assert.LessOrEqual(rect.Y + rect.Height, 1, message: $"Returned texture is taller than TextureAtlas for texture {width}x{height}");
53 }
54 else
55 {
56 Assert.True(width > 1024 - TextureAtlas.PADDING * 2 || height > 1024 - TextureAtlas.PADDING * 2 ||
57 (width > 1024 - TextureAtlas.PADDING * 2 - TextureAtlas.WHITE_PIXEL_SIZE
58 && height > 1024 - TextureAtlas.PADDING * 2 - TextureAtlas.WHITE_PIXEL_SIZE),
59 message: $"Returned texture is null, but should have fit: {width}x{height}");
60 }
61 }
62
63 [Test]
64 public void TestAtlasFirstRowAddRespectsWhitePixelSize()
65 {
66 const int atlas_size = 1024;
67
68 var atlas = new TextureAtlas(atlas_size, atlas_size);
69
70 TextureGL texture = atlas.Add(64, 64);
71
72 RectangleF rect = texture.GetTextureRect(null);
73 Assert.GreaterOrEqual(atlas_size * rect.X, TextureAtlas.WHITE_PIXEL_SIZE + TextureAtlas.PADDING, message: "Texture is placed on top of the white pixel");
74 Assert.GreaterOrEqual(atlas_size * rect.Y, TextureAtlas.PADDING, message: "Texture has insufficient padding");
75 }
76
77 [Test]
78 public void TestAtlasSecondRowAddRespectsWhitePixelSize()
79 {
80 const int atlas_size = 1024;
81
82 var atlas = new TextureAtlas(atlas_size, atlas_size);
83
84 TextureGL texture = atlas.Add(atlas_size - 2 * TextureAtlas.PADDING, 64);
85
86 RectangleF rect = texture.GetTextureRect(null);
87 Assert.GreaterOrEqual(atlas_size * rect.X, TextureAtlas.PADDING, message: "Texture has insufficient padding");
88 Assert.GreaterOrEqual(atlas_size * rect.Y, TextureAtlas.WHITE_PIXEL_SIZE + TextureAtlas.PADDING, message: "Texture is placed on top of the white pixel");
89 }
90 }
91}