/* Copyright (c) 2018 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #include "gamemath/Scalar.h" #include "gamemath/TextureAtlas.h" #include #include #include #define stemobject_implementation TextureAtlas stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_end(); TextureAtlas * TextureAtlas_create(unsigned int pixelWidth, unsigned int pixelHeight) { stemobject_create_implementation(init, pixelWidth, pixelHeight) } bool TextureAtlas_init(TextureAtlas * self, unsigned int pixelWidth, unsigned int pixelHeight) { call_super(init, self); assert(pixelWidth > 0 && pixelHeight > 0); self->pixelWidth = pixelWidth; self->pixelHeight = pixelHeight; self->private_ivar(hashTable) = HashTable_create(sizeof(Rect4f)); return true; } void TextureAtlas_dispose(TextureAtlas * self) { HashTable_dispose(self->private_ivar(hashTable)); call_super(dispose, self); } void TextureAtlas_setPixelDimensions(TextureAtlas * self, unsigned int pixelWidth, unsigned int pixelHeight) { assert(pixelWidth > 0 && pixelHeight > 0); self->pixelWidth = pixelWidth; self->pixelHeight = pixelHeight; } HashTable_key * TextureAtlas_listKeys(TextureAtlas * self, size_t * outCount) { return HashTable_listKeys(self->private_ivar(hashTable), outCount); } size_t TextureAtlas_getKeyCount(TextureAtlas * self) { return self->private_ivar(hashTable)->count; } HashTable_key * TextureAtlas_keyAtIndex(TextureAtlas * self, size_t keyIndex) { return HashTable_keyAtIndex(self->private_ivar(hashTable), keyIndex); } bool TextureAtlas_hasKey(TextureAtlas * self, HashTable_key key) { return HashTable_get(self->private_ivar(hashTable), key) != NULL; } bool TextureAtlas_removeEntry(TextureAtlas * self, HashTable_key key) { return HashTable_delete(self->private_ivar(hashTable), key); } void TextureAtlas_removeAllEntries(TextureAtlas * self) { HashTable_deleteAll(self->private_ivar(hashTable)); } void TextureAtlas_setEntry(TextureAtlas * self, HashTable_key key, Rect4f entry) { HashTable_set(self->private_ivar(hashTable), key, &entry); } Rect4f TextureAtlas_lookup(TextureAtlas * self, HashTable_key key) { Rect4f * entry = HashTable_get(self->private_ivar(hashTable), key); if (entry != NULL) { return *entry; } return RECT4f_EMPTY; } Vector2f TextureAtlas_getEntryPixelDimensions(TextureAtlas * self, Rect4f entry) { return VECTOR2f((entry.xMax - entry.xMin) * self->pixelWidth, (entry.yMax - entry.yMin) * self->pixelHeight); } Rect4f TextureAtlas_entryToPixelCoordinates(TextureAtlas * self, Rect4f entry) { entry.xMin *= self->pixelWidth; entry.xMax *= self->pixelWidth; entry.yMin *= self->pixelHeight; entry.yMax *= self->pixelHeight; return entry; } Rect4f TextureAtlas_pixelToEntryCoordinates(TextureAtlas * self, Rect4f entry) { entry.xMin /= self->pixelWidth; entry.xMax /= self->pixelWidth; entry.yMin /= self->pixelHeight; entry.yMax /= self->pixelHeight; return entry; }