On Thu, Jun 12, 2014, at 09:47 AM, SyntheticPrime GOW wrote:
> I got it!! thanks for all the help guys!!! Love Tiled keep up the great
> work!
That's great!
> I used this code for anyone else to use!!!
I've tried to improve the code a little:
const unsigned TILES_PER_ROW = 10;
const float TILES_PER_ROW_F = static_cast<float>(TILES_PER_ROW);
const float TILES_PER_COLUMN_F = TILES_PER_ROW_F;
auto *map =
static_cast<NX::ResourceTMX*>(NX::ResourceManager::GetResourceManager()->findResourcebyID(6))->getMap();
glBegin(GL_QUADS);
for (int y = 0; y < HEIGHT; ++y) {
for (int x = 0; x < WIDTH; ++x) {
//assuming firstgid = 1
unsigned tile = map->getGridData(x, y)->getGridId() - 1;
unsigned row = tile / TILES_PER_ROW;
unsigned col = tile % TILES_PER_ROW;
float x1 = col / TILES_PER_ROW_F;
float x2 = x1 + 1 / TILES_PER_ROW_F;
float y2 = row / TILES_PER_COLUMN_F;
float y1 = y2 + 1 / TILES_PER_COLUMN_F;
glTexCoord2f(x1, y1); glVertex3f(float(x*OFFSETWIDTH),
float(y*OFFSETHEIGHT), 0.0f);
glTexCoord2f(x2, y1); glVertex3f(float(x*OFFSETWIDTH +
OFFSETWIDTH), float(y*OFFSETHEIGHT), 0.0f);
glTexCoord2f(x2, y2); glVertex3f(float(x*OFFSETWIDTH +
OFFSETWIDTH), float(y*OFFSETHEIGHT + OFFSETHEIGHT), 0.0f);
glTexCoord2f(x1, y2); glVertex3f(float(x*OFFSETWIDTH),
float(y*OFFSETHEIGHT + OFFSETHEIGHT), 0.0f);
}
}
glEnd();
Changes I made:
* Retrieve the pointer to the map once and not for each tile.
* Calculate in integer the part that can be done with integers.
* Avoid the need for dxTOglTexcoord by calculating the right values
directly.
* Moved glBegin/glEnd out of the loop for performance reasons (though
for real performance gains one should not use this immediate rendering
mode but rather vertex arrays or FBOs).
Hopefully it still works because I haven't tested it!
For a more generic bit of code there's still quite a bit to improve of
course.
Regards,
Bjørn