1 /** 2 * Copyright: Copyright (C) 2018 Gabriel Gheorghe, All Rights Reserved 3 * Authors: $(Gabriel Gheorghe) 4 * License: $(LINK2 https://www.gnu.org/licenses/gpl-3.0.txt, GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007) 5 * Source: $(LINK2 https://github.com/GabyForceQ/LibertyEngine/blob/master/source/liberty/framework/gui/tileset.d) 6 * Documentation: 7 * Coverage: 8 **/ 9 module liberty.framework.gui.tileset; 10 11 import liberty.image.format.bmp; 12 import liberty.image.io; 13 import liberty.material.impl; 14 import liberty.math.vector; 15 16 /** 17 * 18 **/ 19 final class TileSet { 20 private { 21 Material** materials; 22 Vector2I dimension; 23 } 24 25 /** 26 * 27 **/ 28 this(Material material, Vector2I tileSize) { 29 import core.stdc.stdlib : malloc; 30 31 dimension.x = material.getTexture().getWidth() / tileSize.x; 32 dimension.y = material.getTexture().getHeight() / tileSize.y; 33 34 auto image = cast(BMPImage)ImageIO.loadImage(material.getTexture().getRelativePath()); 35 36 BMPHeader header; 37 header.width = tileSize.x; 38 header.height = tileSize.y; 39 40 materials = cast(Material**)malloc(dimension.y * (Material*).sizeof); 41 foreach (i; 0..dimension.y) { 42 materials[i] = cast(Material*)malloc(dimension.x * (Material).sizeof); 43 foreach (j; 0..dimension.x) { 44 ubyte[] data = new ubyte[header.width * header.height * 4]; 45 BMPImage im = ImageIO.createImage(header, data); 46 47 int k; 48 foreach (m; (dimension.y - i - 1) * 128..header.width + (dimension.y - i - 1) * 128) 49 foreach (n; j * 128..header.height + j * 128) { 50 data[k++] = image.getRGBAPixel(n, m).b; 51 data[k++] = image.getRGBAPixel(n, m).g; 52 data[k++] = image.getRGBAPixel(n, m).r; 53 data[k++] = image.getRGBAPixel(n, m).a; 54 } 55 56 materials[i][j] = new Material(im); 57 } 58 } 59 } 60 61 ~this() { 62 import core.stdc.stdlib : free; 63 64 foreach (i; 0..dimension.y) 65 free(materials[i]); 66 67 free(materials); 68 } 69 70 /** 71 * 72 **/ 73 Material getTile(int x, int y) { 74 return getTile(Vector2I(x, y)); 75 } 76 77 /** 78 * 79 **/ 80 Material getTile(Vector2I index) { 81 return materials[index.x][index.y]; 82 } 83 }