00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef FIFE_VIDEO_ATLASBOOK_H
00023 #define FIFE_VIDEO_ATLASBOOK_H
00024
00025
00026 #include <vector>
00027 #include <cassert>
00028 #include <cmath>
00029
00030
00031
00032
00033
00034
00035
00036 #include "util/structures/rect.h"
00037
00038 namespace FIFE {
00039
00040 class AtlasBlock {
00041 public:
00042 uint32_t page;
00043 uint32_t left, right, top, bottom;
00044
00045 AtlasBlock(const Rect& rect, uint32_t page)
00046 : page(page),
00047 left(rect.x), right(rect.right()),
00048 top(rect.y), bottom(rect.bottom()){
00049 }
00050
00051 AtlasBlock() {
00052 }
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 void setTrivial() {
00069 left = right = top = bottom = 0;
00070 }
00071
00072 bool isTrivial() const {
00073 return getWidth() == 0 || getHeight() == 0;
00074 }
00075
00076 uint32_t getWidth() const { return right - left; }
00077 uint32_t getHeight() const { return bottom - top; }
00078
00079 AtlasBlock intersects(AtlasBlock const& rect) const;
00080 void merge(AtlasBlock const& rect);
00081 };
00082
00083 class AtlasPage {
00084 public:
00085 AtlasPage(uint32_t width, uint32_t height,
00086 uint32_t pixelSize, uint32_t page)
00087 : width(width), height(height), pixelSize(pixelSize),
00088 page(page), freePixels(width*height*pixelSize){
00089 }
00090
00091 AtlasBlock* getBlock(uint32_t width, uint32_t height);
00092 void shrink(bool pot);
00093
00094 uint32_t getWidth() const {
00095 return width;
00096 }
00097
00098 uint32_t getHeight() const {
00099 return height;
00100 }
00101
00102 private:
00103 AtlasBlock const* intersects(AtlasBlock const* block) const;
00104
00105 uint32_t width, height;
00106 uint32_t pixelSize;
00107 uint32_t page;
00108 int32_t freePixels;
00109
00110 typedef std::vector<AtlasBlock> Blocks;
00111 Blocks blocks;
00112 };
00113
00114 class AtlasBook {
00115 public:
00116
00117 AtlasBook(uint32_t pageWidth, uint32_t pageHeight,
00118 uint32_t pixelSize = 4)
00119 : pageWidth(pageWidth), pageHeight(pageHeight),
00120 pixelSize(pixelSize) {
00121 }
00122
00123 AtlasBlock* getBlock(uint32_t width, uint32_t height);
00124
00125
00126 void shrink(bool pot);
00127
00128 AtlasPage& getPage(size_t index) {
00129 return pages[index];
00130 }
00131
00132 private:
00133
00134 AtlasPage* extendCache(uint32_t minPageWidth, uint32_t minPageHeight);
00135
00136
00137 uint32_t pageWidth, pageHeight;
00138 uint32_t pixelSize;
00139
00140 typedef std::vector<AtlasPage> Pages;
00141 Pages pages;
00142 };
00143 }
00144
00145 #endif
00146