From 9e2c239e744c24483a81f2d076565757fc6ce44d Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sat, 28 Aug 2010 22:51:13 +0000 Subject: [PATCH] Support tilesets larger than 512x512 and switch the coordinates system to (0,0) at the bottom left. --- src/debugsprite.cpp | 6 +++--- src/font.cpp | 8 ++++---- src/gtk/editor.cpp | 2 +- src/gtk/glmapview.cpp | 3 ++- src/input.cpp | 4 ++-- src/map.cpp | 6 +++++- src/test-map.cpp | 2 +- src/tileset.cpp | 28 +++++++++++++++++----------- src/video.cpp | 2 +- 9 files changed, 36 insertions(+), 25 deletions(-) diff --git a/src/debugsprite.cpp b/src/debugsprite.cpp index 2531b007..7eea7679 100644 --- a/src/debugsprite.cpp +++ b/src/debugsprite.cpp @@ -64,10 +64,10 @@ void DebugSprite::TickDraw(float deltams) int y = data->y; int z = data->z; - data->game->GetScene()->AddTile((data->tiler << 16) | 15, - x - 16, y - 32, z + 32, 1); data->game->GetScene()->AddTile((data->tiler << 16) | 31, - x - 16, y - 32, z, 1); + x - 16, y, z + 32, 1); + data->game->GetScene()->AddTile((data->tiler << 16) | 15, + x - 16, y, z, 1); } DebugSprite::~DebugSprite() diff --git a/src/font.cpp b/src/font.cpp index f0879166..b4bfddee 100644 --- a/src/font.cpp +++ b/src/font.cpp @@ -120,13 +120,13 @@ void Font::Print(int x, int y, char const *str) if (ch != ' ') { - glTexCoord2f(tx, ty); + glTexCoord2f(tx, ty + .0625f); glVertex2f(x, y); - glTexCoord2f(tx + .0625f, ty); - glVertex2f(x + data->width, y); glTexCoord2f(tx + .0625f, ty + .0625f); + glVertex2f(x + data->width, y); + glTexCoord2f(tx + .0625f, ty); glVertex2f(x + data->width, y + data->height); - glTexCoord2f(tx, ty + .0625f); + glTexCoord2f(tx, ty); glVertex2f(x, y + data->height); } diff --git a/src/gtk/editor.cpp b/src/gtk/editor.cpp index d28c4639..383be83f 100644 --- a/src/gtk/editor.cpp +++ b/src/gtk/editor.cpp @@ -56,7 +56,7 @@ int main(int argc, char **argv) GTK_SIGNAL_FUNC(delayed_quit), NULL); g_object_unref(G_OBJECT(builder)); - glmapview->LoadMap("maps/testmap.tmx"); + glmapview->LoadMap("maps/testmap2.tmx"); new DebugFps(); gtk_main(); diff --git a/src/gtk/glmapview.cpp b/src/gtk/glmapview.cpp index 945ee95d..af722e29 100644 --- a/src/gtk/glmapview.cpp +++ b/src/gtk/glmapview.cpp @@ -104,7 +104,8 @@ gboolean GlMapView::IdleTick() if (mapviewer) mapviewer->SetPOV(gtk_adjustment_get_value(hadj), - gtk_adjustment_get_value(vadj)); + mapviewer->GetHeight() - glarea->allocation.height + - gtk_adjustment_get_value(vadj)); /* Tick the game */ Ticker::TickGame(); diff --git a/src/input.cpp b/src/input.cpp index de3a1f9b..590c2068 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -42,9 +42,9 @@ Float2 Input::GetAxis(int axis) /* Simulate a joystick using the keyboard. This SDL call is free. */ Uint8 *keystate = SDL_GetKeyState(NULL); int left = keystate[SDLK_d] - (keystate[SDLK_a] | keystate[SDLK_q]); - int up = keystate[SDLK_s] - (keystate[SDLK_w] | keystate[SDLK_z]); - f.y += up; + int up = (keystate[SDLK_w] | keystate[SDLK_z]) - keystate[SDLK_s] ; f.x += left; + f.y += up; if (left && up) { f.x *= invsqrt2; diff --git a/src/map.cpp b/src/map.cpp index 2836d118..96502fed 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -90,7 +90,11 @@ Map::Map(char const *path) } } - tiles[ntiles++] = code; + int x = ntiles % data->width; + int y = data->height - 1 - (ntiles / data->width); + tiles[y * data->width + x] = code; + ntiles++; + while (isdigit(*parser)) parser++; if (*parser == ',') diff --git a/src/test-map.cpp b/src/test-map.cpp index 79dc2357..b54f155e 100644 --- a/src/test-map.cpp +++ b/src/test-map.cpp @@ -45,7 +45,7 @@ int main(int argc, char **argv) Video::Setup(video->w, video->h); /* Create a game */ - Game *game = new Game("maps/testmap.tmx"); + Game *game = new Game("maps/testmap2.tmx"); /* Register an input driver and some debug stuff */ new SdlInput(); diff --git a/src/tileset.cpp b/src/tileset.cpp index b9e58342..8df059e6 100644 --- a/src/tileset.cpp +++ b/src/tileset.cpp @@ -37,7 +37,8 @@ class TileSetData private: char *name; int *tiles; - int ntiles; + int nw, nh, ntiles; + float tx, ty; SDL_Surface *img; GLuint texture; @@ -52,7 +53,6 @@ TileSet::TileSet(char const *path) data = new TileSetData(); data->name = strdup(path); data->tiles = NULL; - data->ntiles = 0; data->img = NULL; data->texture = 0; @@ -65,6 +65,12 @@ TileSet::TileSet(char const *path) SDL_Quit(); exit(1); } + + data->nw = data->img->w / 32; + data->nh = data->img->h / 32; + data->ntiles = data->nw * data->nh; + data->tx = 32.0f / data->img->w; + data->ty = 32.0f / data->img->h; } TileSet::~TileSet() @@ -113,8 +119,8 @@ char const *TileSet::GetName() void TileSet::BlitTile(uint32_t id, int x, int y, int z, int o) { - float tx = .0625f * (id & 0xf); - float ty = .0625f * ((id >> 4) & 0xf); + float tx = data->tx * ((id & 0xffff) % data->nw); + float ty = data->ty * ((id & 0xffff) / data->nw); float sqrt2 = sqrtf(2.0f); int off = o ? 32 : 0; @@ -123,14 +129,14 @@ void TileSet::BlitTile(uint32_t id, int x, int y, int z, int o) { glBindTexture(GL_TEXTURE_2D, data->texture); glBegin(GL_QUADS); + glTexCoord2f(tx, ty + data->ty); + glVertex3f(x, sqrt2 * (y - 70 + off), sqrt2 * (z + off)); + glTexCoord2f(tx + data->tx, ty + data->ty); + glVertex3f(x + 32, sqrt2 * (y - 70 + off), sqrt2 * (z + off)); + glTexCoord2f(tx + data->tx, ty); + glVertex3f(x + 32, sqrt2 * (y - 38), sqrt2 * z); glTexCoord2f(tx, ty); - glVertex3f(x, sqrt2 * (y + off), sqrt2 * (z + off)); - glTexCoord2f(tx + .0625f, ty); - glVertex3f(x + 32, sqrt2 * (y + off), sqrt2 * (z + off)); - glTexCoord2f(tx + .0625f, ty + .0625f); - glVertex3f(x + 32, sqrt2 * (y + 32), sqrt2 * z); - glTexCoord2f(tx, ty + .0625f); - glVertex3f(x, sqrt2 * (y + 32), sqrt2 * z); + glVertex3f(x, sqrt2 * (y - 38), sqrt2 * z); glEnd(); } } diff --git a/src/video.cpp b/src/video.cpp index 28b48522..39646c9a 100644 --- a/src/video.cpp +++ b/src/video.cpp @@ -43,7 +43,7 @@ void Video::Setup(int width, int height) /* Projection matrix: once and for all */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); - glOrtho(0, width, height, 0, -(width + height), width + height); + glOrtho(0, width, 0, height, -(width + height), width + height); } void Video::Clear()