using System; using System.Collections.Generic; using System.Text; using System.Drawing; namespace OpenRa.FileFormats { // T is probably going to be BluntDirectX.Direct3D.Texture public delegate T Provider(); public class TileSheetBuilder where T : class { readonly Size pageSize; readonly Provider pageProvider; public TileSheetBuilder(Size pageSize, Provider pageProvider) { this.pageSize = pageSize; this.pageProvider = pageProvider; } T current = null; int x = 0, y = 0, rowHeight = 0; public SheetRectangle AddImage(Size imageSize) { if (imageSize.Width > pageSize.Width || imageSize.Height > pageSize.Height) return null; if (current == null) current = pageProvider(); if (rowHeight == 0 || imageSize.Width + x > pageSize.Width) { y += rowHeight; rowHeight = imageSize.Height; x = 0; } if (imageSize.Height > rowHeight) rowHeight = imageSize.Height; if (y + imageSize.Height > pageSize.Height) { current = pageProvider(); x = y = rowHeight = 0; } SheetRectangle rect = new SheetRectangle(current, new Point(x, y), imageSize); x += imageSize.Width; return rect; } } public class SheetRectangle where T : class { public readonly Point origin; public readonly Size size; public readonly T sheet; internal SheetRectangle(T sheet, Point origin, Size size) { this.origin = origin; this.size = size; this.sheet = sheet; } } }