-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wang Tiles #1582
Wang Tiles #1582
Changes from 1 commit
904e8bc
cb8d9ae
e93cc74
c329a97
4757fb6
31689c6
e10151e
c7e67d0
0ef5add
74dbae3
400d47c
ff6c624
3916cd6
acae98a
68bd9af
52ae862
4398cea
7f58709
bb6b98a
0969d46
8b959a9
1df48f5
25f8494
888ea7b
825774a
0d3f353
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,12 +39,12 @@ WangSet::WangSet(Tileset *tileset, | |
int imageTileId): | ||
Object(Object::TerrainType), //for now, will add unique type soon | ||
mTileSet(tileset), | ||
mEdgeColors(edgeColors), | ||
mCornerColors(cornerColors), | ||
mName(std::move(name)), | ||
mImageTileId(imageTileId), | ||
mWangIdToTile(QMultiMap<WangId, Tile*>()), | ||
mTileIdToWangId(QMap<int, WangId>()) | ||
mEdgeColors(edgeColors), | ||
mCornerColors(cornerColors)/*, | ||
mWangIdToTile(QMultiHash<WangId, Tile*>()), | ||
mTileIdToWangId(QHash<int, WangId>())*/ | ||
{ | ||
} | ||
|
||
|
@@ -53,58 +53,69 @@ void WangSet::addTile(Tile *tile, WangId wangId) | |
Q_ASSERT(tile->tileset() == mTileSet); | ||
|
||
for (int i = 0; i < 4; ++i) { | ||
Q_ASSERT(wangId.getColor(i,true) <= mEdgeColors); | ||
Q_ASSERT(wangId.edgeColor(i) <= mEdgeColors); | ||
} | ||
|
||
for (int i = 0; i < 4; ++i) { | ||
Q_ASSERT(wangId.getColor(i,false) <= mCornerColors); | ||
Q_ASSERT(wangId.cornerColor(i) <= mCornerColors); | ||
} | ||
|
||
mWangIdToTile.insert(wangId, tile); | ||
mTileIdToWangId.insert(tile->id(), wangId); | ||
} | ||
|
||
Tile *WangSet::getMatchingTile(WangId wangId) const | ||
Tile *WangSet::findMatchingTile(WangId wangId) const | ||
{ | ||
auto potentials = getAllTiles(wangId); | ||
auto potentials = findMatchingTiles(wangId); | ||
|
||
if (potentials.length() > 0) | ||
return potentials[qrand() % potentials.length()]; | ||
else | ||
return NULL; | ||
} | ||
|
||
QList<Tile*> WangSet::getAllTiles(WangId wangId) const | ||
typedef struct WangWildCard | ||
{ | ||
int index, colorCount; | ||
} WangWildCard; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This way of defining a struct was necessary in C times, to avoid needing to repeat the struct WangWildCard
{
int index;
int colorCount;
}; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What a wonderful time we live in! Thanks for informing me |
||
|
||
QList<Tile*> WangSet::findMatchingTiles(WangId wangId) const | ||
{ | ||
QList<Tile*> list; | ||
|
||
//Stores the space of a wild card, followed by how many colors that space can have. | ||
QVector<QPoint> wildCards; | ||
QVector<WangWildCard> wildCards; | ||
|
||
if (mEdgeColors > 0) { | ||
for (int i = 0; i < 4; ++i) { | ||
if (!wangId.getColor(i,true)) { | ||
wildCards.append(QPoint(i * 8, mEdgeColors)); | ||
if (!wangId.edgeColor(i)) { | ||
WangWildCard w; | ||
w.index = i * 8; | ||
w.colorCount = mEdgeColors; | ||
|
||
wildCards.append(w); | ||
} | ||
} | ||
} | ||
|
||
if (mCornerColors > 0) { | ||
for (int i = 0; i < 4; ++i) { | ||
if (!wangId.getColor(i,false)) { | ||
wildCards.append(QPoint(i * 8 + 4, mCornerColors)); | ||
if (!wangId.cornerColor(i)) { | ||
WangWildCard w; | ||
w.index = i * 8 + 4; | ||
w.colorCount = mCornerColors; | ||
|
||
wildCards.append(w); | ||
} | ||
} | ||
} | ||
|
||
if (wildCards.isEmpty()) { | ||
list.append(mWangIdToTile.values(wangId)); | ||
} else { | ||
QStack<QPoint> stack; | ||
QStack<WangWildCard> stack; | ||
|
||
for (int i = 0; i < wildCards.size(); ++i) { | ||
stack.push(wildCards[i]); | ||
} | ||
stack.append(wildCards); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I checked the documentation, check the following link. http://doc.qt.io/qt-5/qvector.html#append-2 The method to append another qvector was introduced in Qt 5.5 so there is a difference in the Qt version that you are using and the one that the Travis CI uses, I would suggest you to get Qt 5.4.2 which is the officially minimal supported to build Tiled. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You might be able to use += or << operators instead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess we could also just raise the minimum Qt version to 5.5. I wanted to quickly look up if those operators would work as an alternative, but http://doc.qt.io/archives/index.html does not even host Qt 5.4 documentation anymore. In general, it does not matter which version is used for development but I'd recommend simply using the latest version, since that is likely the version the next Tiled release is going to be made against. We can check the minimum supported version using Travis CI. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So for the time being, should I change that part of the code to comply with the older version? Or just leave it as is? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bjorn has more a word on this than I, he is the product owner, but I would say that if += work in older versions and in newers aswell, then just use the operator. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, looking at the Qt 5.4 docs in Qt Creator, the operator+= does work with another QVector, so please just use that instead of append, so we can have it in a single line without raising the Qt dependency. |
||
|
||
int max = wildCards.size(); | ||
|
||
|
@@ -113,19 +124,19 @@ QList<Tile*> WangSet::getAllTiles(WangId wangId) const | |
int idVariation = 0; | ||
|
||
for (int i = 0; i < max; ++i) { | ||
idVariation |= stack[i].y() << stack[i].x(); | ||
idVariation |= stack[i].colorCount << stack[i].index; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Coding style: please leave out the braces for one-line bodies (also below). |
||
|
||
list.append(mWangIdToTile.values(idVariation | wangId.id())); | ||
list.append(mWangIdToTile.values(idVariation | wangId)); | ||
|
||
QPoint top = stack.pop(); | ||
top.setY(top.y() - 1); | ||
if (top.y() > 0) | ||
WangWildCard top = stack.pop(); | ||
top.colorCount -= 1; | ||
if (top.colorCount > 0) | ||
stack.push(top); | ||
} else { | ||
QPoint top = stack.pop(); | ||
top.setY(top.y() - 1); | ||
if (top.y() > 0) { | ||
WangWildCard top = stack.pop(); | ||
top.colorCount -= 1; | ||
if (top.colorCount > 0) { | ||
stack.push(top); | ||
|
||
for (int i = stack.size(); i < max; ++i) { | ||
|
@@ -139,10 +150,17 @@ QList<Tile*> WangSet::getAllTiles(WangId wangId) const | |
return list; | ||
} | ||
|
||
WangId WangSet::getWangIdOfTile(Tile *tile) const | ||
WangId WangSet::wangIdOfTile(Tile *tile) const | ||
{ | ||
if (tile->tileset() == mTileSet && mTileIdToWangId.contains(tile->id())) | ||
return mTileIdToWangId.value(tile->id()); | ||
else | ||
return WangId(0); | ||
return mTileIdToWangId.value(tile->id()); | ||
} | ||
|
||
WangSet *WangSet::clone(Tileset *tileset) const | ||
{ | ||
WangSet *c = new WangSet(tileset, mEdgeColors, mCornerColors, mName, mImageTileId); | ||
|
||
c->mWangIdToTile = mWangIdToTile; | ||
c->mTileIdToWangId = mTileIdToWangId; | ||
|
||
return c; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just remove these lines.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Knew I forgot something...