1 /* 2 Copyright (c) 2013 Andrey Penechko 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 6 Permission is hereby granted, free of charge, to any person or organization 7 obtaining a copy of the software and accompanying documentation covered by 8 this license the "Software" to use, reproduce, display, distribute, 9 execute, and transmit the Software, and to prepare derivative works of the 10 Software, and to permit third-parties to whom the Software is furnished to 11 do so, all subject to the following: 12 13 The copyright notices in the Software and this entire statement, including 14 the above license grant, this restriction and the following disclaimer, 15 must be included in all copies of the Software, in whole or in part, and 16 all derivative works of the Software, unless such copies or derivative 17 works are solely in the form of machine-executable object code generated by 18 a source language processor. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 DEALINGS IN THE SOFTWARE. 27 */ 28 29 module anchovy.graphics.font.textureatlas; 30 31 import std.stdio; 32 33 import anchovy.utils.rectbinpacker; 34 import anchovy.graphics.texture; 35 import anchovy.graphics.bitmap; 36 import anchovy.core.math; 37 import anchovy.core.types; 38 39 40 class InsertException : Exception 41 { 42 this(string msg, string file = __FILE__, size_t line = __LINE__) 43 { 44 super(msg, file, line); 45 } 46 } 47 48 /// Can be used to tightly store small images in big atlas, such as font glyps, or lightmaps etc. 49 class TextureAtlas 50 { 51 this(in uint size) 52 { 53 binPacker = new RectBinPacker(size, size); 54 bitmap = new Bitmap(size, size, 1); 55 } 56 57 this(in uint width, in uint height) 58 { 59 binPacker = new RectBinPacker(width, height); 60 bitmap = new Bitmap(width, height, 1); 61 } 62 63 /++ 64 + Returns: position of inserted node, or throws if not enough space 65 +/ 66 uvec2 insert(in uint width, in uint height) 67 { 68 Node* node = binPacker.insert(width, height); 69 70 if (node is null) // There is no place to put new item. 71 { 72 if (_autoGrow) // Atlas can grow. 73 { 74 if (bitmap.size.x >= bitmap.size.y) // Growing vertically. 75 { 76 if (bitmap.size.y >= _maxAtlasSize) 77 { 78 throw new InsertException("Texture atlas is full. Max atlas size reached"); 79 } 80 81 bitmap.data ~= new ubyte[bitmap.size.y*bitmap.size.x]; 82 delete binPacker; 83 binPacker = new RectBinPacker(bitmap.size.x, bitmap.size.y, 0, bitmap.size.y); 84 bitmap.size.y *= 2; 85 } 86 else // Growing horizontally. 87 { 88 ubyte[] newData = new ubyte[bitmap.size.y*bitmap.size.x*2]; 89 for(uint y = 0; y < bitmap.size.y; ++y) 90 { 91 newData[bitmap.size.x*y*2..bitmap.size.x*y*2+bitmap.size.x] = bitmap.data[bitmap.size.x*y..bitmap.size.x*(y+1)]; 92 } 93 94 delete bitmap.data; 95 bitmap.data = newData; 96 delete binPacker; 97 binPacker = new RectBinPacker(bitmap.size.x, bitmap.size.y, bitmap.size.x, 0); 98 bitmap.size.x *= 2; 99 } 100 101 node = binPacker.insert(width, height); 102 } 103 else 104 { 105 throw new InsertException("Texture atlas is full"); 106 } 107 } 108 109 return uvec2(node.rect.x, node.rect.y); 110 } 111 112 /// Returns internal bitmap. 113 ref Bitmap getBitmap() 114 { 115 return bitmap; 116 } 117 118 /// Sets grow policy. 119 bool autoGrow(in bool allowAutoGrow) @property 120 { 121 return _autoGrow = allowAutoGrow; 122 } 123 124 /// Returns grow policy. 125 bool autoGrow() @property 126 { 127 return _autoGrow; 128 } 129 130 private: 131 132 bool _autoGrow = true; 133 uint _maxAtlasSize = 8192; // 2^13 134 135 Bitmap bitmap; 136 RectBinPacker binPacker; 137 }