-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprite.cpp.orig
322 lines (289 loc) · 10.4 KB
/
sprite.cpp.orig
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include "sprite.h"
#include "gif.h"
#include <QMessageBox>
#include <QRegularExpression>
#include <QFileDialog>
#include <QTextStream>
#include <QPainter>
#include <QDebug>
/**
* Constructor.
*/
sprite::sprite()
{
activeFrame = 0;
QImage firstImage(32, 32, QImage::Format_ARGB32);
firstImage.fill(QColor(0,0,0,0));
sprites.push_back(firstImage);
spriteIndexLoaded = -1;
pixMapIndex = 0;
}
/*******************************************************
****************** PRIVATE FUNCTIONS ******************
*******************************************************/
/**
* Updates the active image in the sprites list.
* @param store - The image to store/update
*/
void sprite::updateActiveImage(QImage &store) {
sprites[activeFrame] = store;
}
/*******************************************************
************************ SLOTS ************************
*******************************************************/
//void sprite::zoomIn(){
//}
//void sprite::zoomOut(){
//}
/**
* Load a saved project form a location.
* @param filepath - Location of the file
*/
void sprite::load(QString filepath) {
sprites.clear();
//Tell the preview list to be empty
QString filetype = filepath.split('.')[filepath.split('.').length() -1 ];
if (filetype.toLower() == "png" ) {
QImage loaded;
QString filename = filepath.split('/')[filepath.split('/').size()-1];
filename = filename.remove(filename.size()-4, 4);
filename = filename.split('_')[filename.split('_').size()-1];
int frameWidth = filename.split('x')[0].toInt();
if (loaded.load(filepath, "PNG")) {
for (int i = 0; i < loaded.width()/frameWidth; i++) {
QImage frame = loaded.copy(i*frameWidth, 0, frameWidth, loaded.height());
sprites.push_back(frame);
emit updateList(sprites, i+1);
}
<<<<<<< HEAD
QImage blank(sprites[0].width(), sprites[0].height(), QImage::Format_ARGB32);
blank.fill(QColor(0,0,0,0));
sprites.push_back(blank);
activeFrame = sprites.length() - 1;
=======
emit updateFrame(sprites[0]);
emit updateList(sprites, activeFrame = sprites.length()-1);
>>>>>>> 8d89cdf39c9ba13dfdafd21954274fd194d28d52
}
// emit error message?
}
else if (filetype.toLower() == "ssp") {
QFile file(filepath);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream reader(&file);
//Read height and width
int height;
int width;
if (!reader.atEnd()) {
QString dimensions = reader.readLine();
QRegularExpression dimensionCheck("^\\d+ \\d+$");
if (!dimensionCheck.match(dimensions).hasMatch())
return;
height = dimensions.split(' ')[0].toInt();
width = dimensions.split(' ')[1].toInt();
}
//Retrieve the number of sprites
if (reader.atEnd())
return;
QString spriteNum = reader.readLine();
QRegularExpression spriteNumCheck("^\\d+$");
if (!spriteNumCheck.match(spriteNum).hasMatch())
return;
int numSprites = spriteNum.toInt();
//Initialize QImages
for (int frame = 0; frame < numSprites; frame++) {
QImage newImg(width, height, QImage::Format_ARGB32);
newImg.fill(QColor(0,0,0,0));
sprites.push_back(newImg);
}
QRegularExpression pixelCheck("^\\d+ \\d+ \\d+ \\d+$");
//Fill images
for (int frame = 0; frame < numSprites; frame++) {
if (reader.atEnd())
return;
reader.readLine();
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++) {
QString pixelDefinition = reader.readLine();
if (!pixelCheck.match(pixelDefinition).hasMatch())
return;
int R = pixelDefinition.split(' ')[0].toInt();
int G = pixelDefinition.split(' ')[1].toInt();
int B = pixelDefinition.split(' ')[2].toInt();
int A = pixelDefinition.split(' ')[3].toInt();
sprites[frame].setPixelColor(j, i, QColor(R, G, B, A));
}
emit updateList(sprites, frame+1);
}
<<<<<<< HEAD
QImage blank(sprites[0].width(), sprites[0].height(), QImage::Format_ARGB32);
blank.fill(QColor(0,0,0,0));
sprites.push_back(blank);
activeFrame = sprites.length() - 1;
=======
emit updateFrame(sprites[0]);
emit updateList(sprites, activeFrame = sprites.length()-1);
>>>>>>> 8d89cdf39c9ba13dfdafd21954274fd194d28d52
}
}
}
/**
* Save the current project
* @param fullFilename - File name/path
* @param activeImage - Current image to save.
*/
void sprite::save(QString fullFilename, QImage activeImage) {
//add active frame to list
//updateActiveImage(activeImage);
QFile file(fullFilename);
//open the file if it does not exist. If it does it will be overwritten.
if(file.open(QIODevice::WriteOnly | QIODevice::Text)){
QTextStream out(&file);
//all ascii is human readable.
out << sprites[0].height() << " " << sprites[0].width() << "\n"
<< QString::number(sprites.length()) << "\n";
for (int i = 0; i < sprites.length() - 1; i++){
//frame number
out << QString::number(i) << "\n";
//for loops for every frame.
for (int n = 0; n < sprites[i].height(); n++){ //row
for (int m = 0; m < sprites[i].width(); m++){ //colmn
QColor pixColor = sprites[i].pixelColor(m, n);
out << QString::number(pixColor.red()) << " "
<< QString::number(pixColor.green()) << " "
<< QString::number(pixColor.blue()) << " "
<< QString::number(pixColor.alpha()) << "\n";
}
}
}
}
}
/**
* Exports the frames to PNG
* @param fullFilename - File name/path
* @param activeImage - The active image to save to PNG
*/
void sprite::export_To_PNG(QString fullFilename, QImage activeImage){
updateActiveImage(activeImage);
//The file size needed for load is set staticly for now.
QImage framesToPNG(sprites.length() * sprites[0].width(), sprites[0].height(), QImage::Format_ARGB32);
QPainter painter;
painter.begin(&framesToPNG);
painter.setCompositionMode(QPainter::CompositionMode_Source);
//variables for image positioning.
int imageWidth = 0;
int imageHeight = 0;
for(int i = 0; i < sprites.length(); i++){
qDebug() << "derp";
painter.drawImage(imageWidth,imageHeight, sprites[i],0,0,0,0,Qt::NoFormatConversion);
imageWidth += sprites[i].width();
//imageHeight += sprites[i].height();
}
painter.end();
if(!framesToPNG.save(fullFilename, 0, -1)){
//qDebug() << “derp”;
//emit error message.
}
}
/**
* Adds image to canvas.
* @param oldImg - Old active image that needs to be stored
*/
void sprite::add_Frame(QImage oldImg) {
qDebug() << "Active Frame: " << activeFrame;
updateActiveImage(oldImg);
QImage newImg(sprites[0].width(), sprites[0].height(), QImage::Format_ARGB32);
newImg.fill(QColor(0,0,0,0));
sprites.push_back(newImg);
activeFrame = sprites.length()-1;
emit updateList(sprites, activeFrame);
//Updates canvas
emit updateFrame(newImg);
}
/**
* Update the current active frame
* @param image - Image to set as active
* @param newIndex - New active index
*/
void sprite::update_Active_Frame(QImage image, int newIndex) {
updateActiveImage(image);
activeFrame = newIndex;
emit(updateFrame(sprites[activeFrame]));
emit updateList(sprites, activeFrame);
}
/**
* Exports the file to GIF
* @param fullFilename - File name/path
* @param activeImage - The active image to save to GIF
*/
void sprite::export_To_GIF(QString fullFilename, QImage activeImage){
updateActiveImage(activeImage);
GifWriter writer;
//convert QString to char array.
QByteArray toCharArray = fullFilename.toLatin1();
const char* filePath = toCharArray.data();
GifBegin(&writer, filePath, activeImage.width(), activeImage.height(), 1, 8, false);
for(int i = 0; i < sprites.length(); i++){
//Invert RGB to BGR
QImage imageEightBit = sprites[i].rgbSwapped();
//Create a char* point to the first pixel in memory.
const uint8_t* image = imageEightBit.bits();
GifWriteFrame(&writer, image, activeImage.width(), activeImage.height(),1,8, false);
}
GifEnd(&writer);
}
//void sprite::changeActiveFrame(QListWidgetItem* item) {
// QString result = item->text();
// int frame = result.split(' ')[1].toInt() - 1;
// activeFrame = frame;
// emit updateFrame(sprites[frame]);
//}
//void sprite::on_deleteKey_pressed(QKeyEvent *event)
//{
// if(spriteIndexLoaded == -1)
// {
// ui->canvas->clear_Image();
// }
// else
// {
// QWidget* widget = ui->horizontalLayout->itemAt(spriteIndexLoaded)->widget();
// ui->horizontalLayout->removeWidget(widget);
// delete widget;
// --pixMapIndex;
// ui->canvas->clear_Image();
// pixmapPreviews.remove(spriteIndexLoaded);
// renamePanels(spriteIndexLoaded);
// spriteIndexLoaded = -1;
// }
//}
/**
* Replaces (edits) a frame that was already created.
* @param pixmap - The updates that need to be added to a frame
* @param index - The index of where the frame is at in the list
*/
void sprite::on_addEditFrame(QPixmap pixmap, int index)
{
sprites.replace(index,pixmap.toImage());
}
/**
* Update the preview box with the latest image.
* @param indx - Index of the frame in the list
*/
void sprite::on_preview_label_clicked(int indx)
{
QImage image = sprites[indx];
emit updateFrame(image);
}
/**
*
* @param indx
*/
void sprite::on_deleteIndex(int indx)
{
--activeFrame;
sprites.remove(indx);
}
void sprite::on_playButtonClicked()
{
emit animationVector(sprites);
}