Rendering Engine 0.2.0
Modular Graphics Rendering Engine | v0.2.0
Loading...
Searching...
No Matches
texture_atlas_maker.cpp
Go to the documentation of this file.
2#include <cmath>
3
4namespace rendering_engine
5{
6
7const char* const TextureAtlasMaker::mMetaDataFileName = "MetaData.txt";
9
10TextureAtlasMaker::TextureAtlasMaker(std::map<char, ImageData> images)
11{
12 mImageCollection = images;
13}
14
15bool TextureAtlasMaker::CreateTextureAtlas(std::map<char, std::pair<unsigned int, unsigned int>>& texAtlasData, ImageData& texAtlasImage)
16{
17 if( mImageCollection.size() == 0 )
18 {
19 return false;
20 }
21
22 //Find palette specification
23
24 //Find dimension of each cell
25 unsigned int cellWidth;
26 unsigned int cellHeight;
27 FindCellDimensions(cellWidth, cellHeight);
28
29 //Find number of rows and columns
30 unsigned int numberOfColumns;
31 unsigned int numberOfRows;
32 CalculateGridDimensions(numberOfColumns, numberOfRows);
33
34 //Create palette
35 ImageData textureAtlasPalette(static_cast<unsigned int>(cellWidth * numberOfColumns), static_cast<unsigned int>(cellHeight * numberOfRows));
36 textureAtlasPalette.Fill(sDefaultPaletteBackgroundColor);
37
38 auto imageIterator = mImageCollection.begin();
39
40 for( int y = 0; y < numberOfRows; y++ )
41 {
42 for( int x = 0; x < numberOfColumns; x++ )
43 {
44 if( imageIterator != mImageCollection.end() )
45 {
46 ImageData::DrawImageOnImageAtPos(x * cellWidth, y * cellHeight, textureAtlasPalette, imageIterator->second);
47
48 texAtlasData.emplace(imageIterator->first, std::make_pair<unsigned int, unsigned int>(x * cellWidth, y * cellHeight));
49
50 imageIterator++;
51 }
52 }
53 }
54 texAtlasImage = textureAtlasPalette;
55
56 return true;
57}
58
59void TextureAtlasMaker::FindCellDimensions(unsigned int& outputWidth, unsigned int& outputHeight)
60{
61 unsigned int height = 0U;
62 unsigned int width = 0U;
63 for( auto& it : mImageCollection )
64 {
65 if( it.second.GetHeight() >= height )
66 {
67 height = it.second.GetHeight();
68 }
69 if( it.second.GetWidth() >= width )
70 {
71 width = it.second.GetWidth();
72 }
73 }
74
75 outputWidth = width;
76 outputHeight = height;
77}
78
79void TextureAtlasMaker::CalculateGridDimensions(unsigned int& outputNumberOfColumns, unsigned int& outputNumberOfRows)
80{
81 unsigned int cellWidth;
82 unsigned int cellHeight;
83 FindCellDimensions(cellWidth, cellHeight);
84
85 unsigned int cellsNum = mImageCollection.size();
86 int totalSquare = cellWidth * cellHeight * cellsNum;
87 float rootSq = sqrtf(totalSquare);
88
89 outputNumberOfColumns = std::ceil(rootSq / cellWidth);
90 outputNumberOfRows = std::ceil((float)(cellsNum) / (float)(outputNumberOfColumns));
91}
92
93}// rendering_engine
Represents raw 2D image data stored in memory.
static void DrawImageOnImageAtPos(unsigned int const x, unsigned int const y, ImageData &toImage, ImageData &fromImage)
Overlays one image on top of another at a given position.
void Fill(Color color)
Fills the image with a solid color.
void FindCellDimensions(unsigned int &outputWidth, unsigned int &outputHeight)
bool CreateTextureAtlas(std::map< char, std::pair< unsigned int, unsigned int > > &texAtlasData, ImageData &texAtlasImage)
TextureAtlasMaker(std::map< char, ImageData > images)
std::map< char, ImageData > mImageCollection
void CalculateGridDimensions(unsigned int &outputNumberOfColumns, unsigned int &outputNumberOfRows)
Represents a color with red, green, blue, and alpha channels.