-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtile.cpp
More file actions
292 lines (251 loc) · 8.16 KB
/
tile.cpp
File metadata and controls
292 lines (251 loc) · 8.16 KB
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
#include "tile.h"
#include "mainwindow.h"
#include "project.h"
#include <QMessageBox>
extern Project project;
Tile::Tile(int offs, bool vflip, bool hflip, int palid)
{
this->tileset_offset= offs;
this->hflip= hflip;
this->vflip= vflip;
this->palette_index= palid;
}
QImage Tile::RenderImage(Tileset* tileset, bool bpp4)
{
QImage timg= tileset->tiles[tileset_offset];
QImage oimg= timg;
for (int iy=0; iy<TILE_H; iy++)
{
for (int ix=0; ix<TILE_W; ix++)
{
unsigned char pixel;
if (bpp4)
pixel= (timg.scanLine(iy)[ix]&0x0F)|((palette_index%16)<<4);
else
pixel= timg.scanLine(iy)[ix];
oimg.scanLine(vflip?TILE_H-iy-1:iy)[hflip?TILE_W-ix-1:ix]= pixel;
}
}
return oimg;
}
QImage Tile::TransformImage(QImage img, bool vflip, bool hflip, int palid)
{
QImage oimg;
QTransform ttrans;
ttrans.scale(hflip?-1:1, vflip?-1:1);
oimg= img.transformed(ttrans);
if (palid >= 0)
{
for (int iy=0; iy<img.height(); iy++) for (int ix=0; ix<img.width(); ix++)
oimg.scanLine(iy)[ix]= ((palid%16)<<4)|(oimg.scanLine(iy)[ix]&0x0F);
}
return oimg;
}
///// Tileset operations /////
bool Tileset::FromImage(int count)
{
//Returns true on success
if (!image)
{
printf("DEBUG: tileset_image is null!\n");
return false;
}
if (image->colorTable().isEmpty())
{
QMessageBox* msgb= new QMessageBox(QMessageBox::Critical, "Error! - Import tileset from image", "Image does not have a color palette");
msgb->show();
return false;
}
if (image->width()%TILE_W || image->height()%TILE_H)
{
QMessageBox* msgb= new QMessageBox(QMessageBox::Critical, "Error! - Import tileset from image",
"Image size should be a multiple of "+QString::number(TILE_W)+"x"+QString::number(TILE_H));
msgb->show();
return false;
}
//Populate the pixmap array with 8x8 tiles from the image
tiles.clear();
if (count<0)
count= (image->width()/TILE_W) * (image->height()/TILE_H);
for (int ity=0; ity<image->height()/TILE_H; ity++)
{
for (int itx=0; itx<image->width()/TILE_W; itx++)
{
if (itx+ity*(image->width()/TILE_W) >= count)
break;
tiles+= image->copy(itx*TILE_W, ity*TILE_H, TILE_W, TILE_H);
}
}
//Populate the palette array with the one embedded in the image
palette.clear();
for (int ic=0; ic<PALETTE_W*PALETTE_H; ic++)
{
if (ic < image->colorCount())
palette+= image->colorTable()[ic];
else
{
int randval= rand()%256;
palette+= QColor::fromRgb(randval, randval, randval).rgb();
}
}
return true;
}
bool Tileset::FromImage(QString fname, bool load_new, int count)
{
if (image)
{
delete image;
image= nullptr;
}
image= new QImage(fname);
if (!image || image->format() != QImage::Format_Indexed8)
{
QMessageBox::critical(project.tab_widget->currentWidget(), "Error", "Image is invalid!");
return false;
}
if (load_new)
{
QMessageBox::StandardButton dial_result= QMessageBox::question(project.tab_widget->currentWidget(), "Question - Import tileset from image",
"It is reccomended that you create a copy of the image for the project.\r\nDo you wish to create a copy now?\r\nChoosing \"no\" will overwrite the original file");
if (dial_result == QMessageBox::Yes)
image_fpath= "//clone//";
else
image_fpath= fname;
}
else
image_fpath= fname;
return FromImage(count);
}
QVector<QImage> Tileset::Optimized(QList<Tile>* tilemap, Tileset::optimize_flags_t optiflags)
{
QVector<QImage> new_tileset;
bool tileset_matchstatus[this->tiles.count()];
QList<Tile> new_tilemap;
new_tileset.clear();
new_tilemap.clear();
for (int i=0; i<this->tiles.count(); i++)
tileset_matchstatus[i]= false;
for (int im=0; im<tilemap->count(); im++)
{
Tile* tmtile= &(*tilemap)[im];
QImage tmtile_img= tmtile->RenderImage(this,false);
bool hflipped=false, vflipped=false;
int ind_found= -2;
//Search the new tilemap by all the possible versions of this tile
for (int ipal=0; ipal<PALETTE_H && ipal>=0; ipal++)
{
if (!(optiflags&Tileset::OptimizeWithPalette))
if (ipal != tmtile->palette_index) continue;
if (!isSubPalettedFormat()) ipal= -1;
hflipped=false, vflipped=false;
ind_found= new_tileset.indexOf(Tile::TransformImage(tmtile_img,vflipped,hflipped,ipal));
if (ind_found >= 0) break;
if (!(optiflags&Tileset::OptimizeWithFlip))
{
if (isSubPalettedFormat())
continue;
else
break;
}
hflipped=false, vflipped=true;
ind_found= new_tileset.indexOf(Tile::TransformImage(tmtile_img,vflipped,hflipped,ipal));
if (ind_found >= 0) break;
hflipped=true, vflipped=false;
ind_found= new_tileset.indexOf(Tile::TransformImage(tmtile_img,vflipped,hflipped,ipal));
if (ind_found >= 0) break;
hflipped=true, vflipped=true;
ind_found= new_tileset.indexOf(Tile::TransformImage(tmtile_img,vflipped,hflipped,ipal));
if (ind_found >= 0) break;
if (!isSubPalettedFormat())
break;
}
if (ind_found >= 0)
{
new_tilemap+= Tile(ind_found,vflipped,hflipped,tmtile->palette_index);
}
else
{
new_tileset+= tmtile->RenderImage(this,isSubPalettedFormat());;
new_tilemap+= Tile(new_tileset.count()-1,false,false,new_tileset.last().scanLine(0)[0]>>4);
}
tileset_matchstatus[tmtile->tileset_offset]= true;
(*tilemap)[im]= new_tilemap[im];
}
//Restore tiles that were not matched (aka. unused)
if (optiflags&Tileset::OptimizeKeepUnused)
for (int it=0; it<this->tiles.count(); it++)
if (!tileset_matchstatus[it])
new_tileset+= this->tiles[it];
return new_tileset;
}
void Tileset::RebuildTilesetImage(int columns)
{
if (!tiles.count())
return;
QImage timg= QImage(QSize(columns*TILE_W,tiles.count()/columns*TILE_H+TILE_H), QImage::Format_Indexed8);
timg.setColorTable(palette);
timg.fill(240);
for (int it=0; it<tiles.count(); it++)
{
QImage* ttile= &tiles[it];
int final_tx= (it*TILE_W)%(columns*TILE_W);
int final_ty= (it*TILE_W)/(columns*TILE_W)*TILE_H;
for (int ity=0; ity<TILE_H; ity++)
{
unsigned char* slptr= ttile->scanLine(ity);
for (int itx=0; itx<TILE_W; itx++)
{
timg.setPixel(final_tx+itx, final_ty+ity, slptr[itx]);
}
}
}
// if (image)
// delete image;
image= new QImage(timg);
}
void Tileset::UpdatePalettes()
{
for (int it=0; it<tiles.count(); it++)
{
tiles[it].setColorTable(palette);
}
}
void Tileset::RemoveTile(int id, QList<Tile>* tilemap)
{
if (id >= tiles.count() || id < 0)
return;
int tab_id= project.GetTileCanvasIndex(id);
//Close the TileEdit tab of this tile if it was open ...
if (tab_id >= 0)
{
project.tab_widget->removeTab(tab_id);
}
// ... and then update the other tabs
for (int i= id; i<tiles.count(); i++)
{
TileCanvas* next_tab= project.GetTileCanvasWidget(i);
if (!next_tab)
continue;
next_tab->UpdateTileId(next_tab->TileId()-1);
}
//Optionally update the tilemap
if (tilemap)
{
for(int it=0; it<tilemap->count(); it++)
{
if ((*tilemap)[it].tileset_offset >= id)
(*tilemap)[it].tileset_offset--;
}
}
tiles.remove(id);
}
bool Tileset::isSubPalettedFormat()
{
switch (format)
{
case Tileset::GBA_4bpp:
return true;
default:
return false;
}
}