-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBatchRenderer.cpp
187 lines (147 loc) · 4.91 KB
/
BatchRenderer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "BatchRenderer.h"
#include <QFontMetrics>
#include <QPainter>
#include <QRect>
#include <QTime>
#include <QCoreApplication>
#include <QDir>
BatchRenderer::BatchRenderer(QObject *parent) : QObject(parent)
{
bStop = false;
}
void BatchRenderer::run()
{
//Periodically check if we should stop
QCoreApplication::processEvents();
if(bStop) return;
renderingStart(folder + ".png");
//Load anim names from subfolder names, sheet frames from those
QDir dir(folder);
QStringList filters;
filters << "*";
QStringList animationDirs = dir.entryList(filters, QDir::Dirs | QDir::AllDirs | QDir::NoSymLinks | QDir::NoDotAndDotDot, QDir::Name | QDir::IgnoreCase);
foreach(QString f, animationDirs)
{
QList<QImage> animImages;
mAnimNames.append(f);
QString animFolder = folder + '/' + f;
QDir animDir(animFolder);
QStringList frameFilenames = animDir.entryList(filters, QDir::Files | QDir::NoSymLinks, QDir::Name | QDir::IgnoreCase);
foreach(QString frameImgFilename, frameFilenames)
{
QString frameFilename = animFolder + '/' + frameImgFilename;
QImage frameImg(frameFilename);
if(!frameImg.isNull())
animImages.append(frameImg);
}
if(animImages.size())
mSheetFrames.append(animImages);
//Periodically check if we should stop
QCoreApplication::processEvents();
if(bStop) return;
}
//Draw!
QFontMetrics fm(sheetFont);
float textHeight = fm.height() + 3;
//First pass: Figure out dimensions of final image
int iSizeX = offsetX;
int iSizeY = offsetY;
QStringList::iterator sName = mAnimNames.begin();
foreach(QList<QImage> ql, mSheetFrames)
{
int ySize = 0;
int iCurSizeX = offsetX;
foreach(QImage img, ql)
{
//Test to see if we should start next line
if(iCurSizeX + img.width() + offsetX > maxSheetWidth)
{
iSizeY += offsetY + ySize;
ySize = 0;
if(iCurSizeX > iSizeX)
iSizeX = iCurSizeX;
iCurSizeX = offsetX;
}
if(img.height() > ySize)
ySize = img.height();
iCurSizeX += img.width() + offsetX;
}
iSizeY += offsetY + ySize;
if(sName->length() && animNameEnabled)
iSizeY += textHeight;
sName++;
if(iCurSizeX > iSizeX)
iSizeX = iCurSizeX;
}
//Periodically check if we should stop
QCoreApplication::processEvents();
if(bStop)
return;
//Create sheet
mCurSheet = QImage(iSizeX, iSizeY, QImage::Format_ARGB32);
//Create image of the proper size and fill it with a good bg color
QPainter painter(&mCurSheet);
painter.setFont(sheetFont);
painter.setCompositionMode(QPainter::CompositionMode_Source);
if(sheetBgTransparent)
mCurSheet.fill(QColor(0,0,0,0));
else
mCurSheet.fill(sheetBgCol);
//Second pass: Print each frame into the final image
int curX = offsetX;
int curY = offsetY;
sName = mAnimNames.begin();
for(QList<QList<QImage> >::iterator ql = mSheetFrames.begin(); ql != mSheetFrames.end(); ql++)
{
QRect r;
r.setRect(0,curY-offsetY,iSizeX,0);
int ySize = 0;
//Draw label for animation
if(sName->length() && animNameEnabled)
{
painter.setPen(fontColor);
painter.drawText(QRectF(offsetX,curY,1000,textHeight), Qt::AlignLeft|Qt::AlignVCenter, *sName);
curY += textHeight;
}
sName++;
for(QList<QImage>::iterator img = ql->begin(); img != ql->end(); img++)
{
//Test to see if we should start next line
if(curX + img->width() + offsetX > maxSheetWidth)
{
curY += offsetY + ySize;
ySize = 0;
curX = offsetX;
}
//Erase this portion of the image
if(!frameBgTransparent)
{
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
painter.fillRect(curX, curY, img->width(), img->height(), QBrush(frameBgCol));
}
painter.drawImage(curX, curY, *img);
if(img->height() > ySize)
ySize = img->height();
curX += img->width() + offsetX;
//Periodically check if we should stop
QCoreApplication::processEvents();
if(bStop)
{
painter.end();
return;
}
}
curY += offsetY + ySize;
curX = offsetX;
r.setBottom(curY-offsetY);
}
painter.end();
//Save image as PNG
mCurSheet.save(folder + ".png", "PNG");
//Emit a signal saying we're done bro
renderingDone();
}
void BatchRenderer::stop()
{
bStop = true;
}