From eeeee41ef1709a5c68f4a816f2296ada2e6099ed Mon Sep 17 00:00:00 2001 From: Steve Date: Wed, 31 Jan 2018 08:08:41 +0000 Subject: [PATCH] Start of map rendering. --- src/system/atlas.c | 3 +- src/test/atlasTest.c | 38 +++++++--- src/test/atlasTest.h | 6 +- src/world/map.c | 156 +++++++++++++++++++++++++++++++++++++++- src/world/map.h | 4 ++ src/world/worldLoader.c | 2 + 6 files changed, 195 insertions(+), 14 deletions(-) diff --git a/src/system/atlas.c b/src/system/atlas.c index 01b89ad..e1fb47e 100644 --- a/src/system/atlas.c +++ b/src/system/atlas.c @@ -50,8 +50,7 @@ Atlas *getImageFromAtlas(char *filename) } } - SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "No such atlas image '%s'", filename); - exit(1); + return NULL; } static void loadAtlasTexture(void) diff --git a/src/test/atlasTest.c b/src/test/atlasTest.c index 067664b..9182346 100644 --- a/src/test/atlasTest.c +++ b/src/test/atlasTest.c @@ -22,9 +22,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. static void logic(void); static void draw(void); - -static Atlas *testImage; -static Texture *atlasTexture; +static int timeout; +static void trackRandomEntity(void); void initAtlasTest(void) { @@ -34,20 +33,39 @@ void initAtlasTest(void) app.delegate.logic = &logic; app.delegate.draw = &draw; - testImage = getImageFromAtlas("gfx/sprites/evilblobs/machineGunBlobRight1.png"); - - atlasTexture = getTexture("gfx/atlas/atlas.png"); - - loadMapData("data/maps/raw/beachApproach.raw"); - loadWorld("data/maps/beachApproach.json"); + + initMap(); + + timeout = FPS * 2; + + cameraTrack((Entity*)world.bob); } static void logic(void) { + if (--timeout <= 0) + { + trackRandomEntity(); + } } static void draw(void) { - blitRect(atlasTexture->texture, 0, 0, &testImage->rect, 0); + drawMap(); +} + +static void trackRandomEntity(void) +{ + Entity *e; + + for (e = world.entityHead.next ; e != NULL ; e = e->next) + { + if (rand() % 4 == 0) + { + cameraTrack(e); + timeout = FPS * 2; + return; + } + } } diff --git a/src/test/atlasTest.h b/src/test/atlasTest.h index 0c89f18..e9d6917 100644 --- a/src/test/atlasTest.h +++ b/src/test/atlasTest.h @@ -24,8 +24,12 @@ extern Atlas *getImageFromAtlas(char *filename); extern Texture *getTexture(const char *filename); extern void blitRect(SDL_Texture *texture, int x, int y, SDL_Rect *srcRect, int center); extern void loadMapData(char *filename); -extern void initGame(void); +extern void initMap(void); extern void initHub(void); +extern void initGame(void); extern void loadWorld(char *filename); +extern void drawMap(void); +extern void cameraTrack(Entity *e); extern App app; +extern World world; diff --git a/src/world/map.c b/src/world/map.c index 7da29c5..58f1c04 100644 --- a/src/world/map.c +++ b/src/world/map.c @@ -20,7 +20,17 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "map.h" +static void loadMapData(void); +static void loadCommonTiles(void); +static void loadTileset(void); +static void loadDecals(void); +static SDL_Rect *loadTile(char *filename); + static int animTimer; +static int MAX_Y; +static Texture *atlasTexture; +static SDL_Rect *tiles[MAP_TILE_MAX]; +static SDL_Rect *decals[8]; void initMap(void) { @@ -32,6 +42,82 @@ void initMap(void) world.map.bounds.h = 0; animTimer = 0; + + atlasTexture = getTexture("gfx/atlas/atlas.png"); + + loadMapData(); + + loadCommonTiles(); + + loadTileset(); + + loadDecals(); +} + +void drawMap(void) +{ + int mx, x1, x2, my, y1, y2, tile, decal, x, y; + + mx = camera.x / MAP_TILE_SIZE; + x1 = (camera.x % MAP_TILE_SIZE) * -1; + x2 = x1 + RENDER_WIDTH * MAP_TILE_SIZE + (x1 == 0 ? 0 : MAP_TILE_SIZE); + + my = camera.y / MAP_TILE_SIZE; + y1 = (camera.y % MAP_TILE_SIZE) * -1; + y2 = y1 + RENDER_HEIGHT * MAP_TILE_SIZE + (y1 == 0 ? 0 : MAP_TILE_SIZE); + + tile = 0; + decal = 0; + + for (x = x1; x < x2; x += MAP_TILE_SIZE) + { + for (y = y1; y < y2; y += MAP_TILE_SIZE) + { + if (mx < MAP_WIDTH && my < MAP_HEIGHT && my <= MAX_Y) + { + tile = world.map.data[mx][my]; + + if (animTimer == 0) + { + if (tile >= MAP_TILE_ANIMATED_WATER && tile < MAP_TILE_ANIMATED_SLIME) + { + world.map.data[mx][my] = tile = rrnd(MAP_TILE_ANIMATED_WATER, MAP_TILE_ANIMATED_SLIME - 1); + } + + if (tile >= MAP_TILE_ANIMATED_SLIME && tile < MAP_TILE_ANIMATED_LAVA) + { + world.map.data[mx][my] = tile = rrnd(MAP_TILE_ANIMATED_SLIME, MAP_TILE_ANIMATED_LAVA - 1); + } + + if (tile >= MAP_TILE_ANIMATED_LAVA && tile < MAP_TILE_OUTSIDE) + { + world.map.data[mx][my] = tile = rrnd(MAP_TILE_ANIMATED_LAVA, MAP_TILE_OUTSIDE - 1); + } + } + + if (tile != MAP_TILE_AIR) + { + blitRect(atlasTexture->texture, x, y, tiles[tile], 0); + + decal = world.map.decal[mx][my]; + + if (decal != 0) + { + blitRect(atlasTexture->texture, x, y, decals[decal - 1], 0); + } + } + } + else + { + blitRect(atlasTexture->texture, x, y, tiles[MAP_TILE_OUTSIDE], 0); + } + + my++; + } + + my = camera.y / MAP_TILE_SIZE; + mx++; + } } void animateMap(void) @@ -146,13 +232,32 @@ static void calculateMapBounds(void) world.map.bounds.h += MAP_TILE_SIZE; SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG, "Map bounds [%d, %d, %d, %d]", world.map.bounds.x, world.map.bounds.y, world.map.bounds.w, world.map.bounds.h); + + MAX_Y = 0; + + for (y = 0; y < MAP_HEIGHT; y++) + { + for (x = 0; x < MAP_WIDTH; x++) + { + if (world.map.data[x][y] != 0) + { + if (y > MAX_Y) + { + MAX_Y = y; + } + } + } + } } -void loadMapData(char *filename) +static void loadMapData(void) { + char filename[MAX_FILENAME_LENGTH]; char *data, *p; int i, x, y; + sprintf(filename, "data/maps/raw/%s.raw", world.id); + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Loading %s", filename); data = readFile(filename); @@ -195,3 +300,52 @@ void loadMapData(char *filename) calculateMapBounds(); } + +static void loadCommonTiles(void) +{ + int i; + char filename[MAX_FILENAME_LENGTH]; + + tiles[1] = loadTile("gfx/tiles/common/1.png"); + tiles[2] = loadTile("gfx/tiles/common/2.png"); + tiles[3] = loadTile("gfx/tiles/common/3.png"); + + for (i = MAP_TILE_ANIMATED_WATER ; i < MAP_TILE_MAX ; i++) + { + sprintf(filename, "gfx/tiles/common/%d.png", i); + tiles[i] = loadTile(filename); + } +} + +static void loadTileset(void) +{ + int i; + char filename[MAX_FILENAME_LENGTH]; + + for (i = MAP_TILE_SOLID ; i < MAP_TILE_ANIMATED_WATER ; i++) + { + sprintf(filename, "gfx/tiles/%s/%d.png", world.tileset, i); + + tiles[i] = loadTile(filename); + } +} + +static SDL_Rect *loadTile(char *filename) +{ + return &getImageFromAtlas(filename)->rect; +} + +static void loadDecals(void) +{ + int i; + char filename[MAX_FILENAME_LENGTH]; + + for (i = 0 ; i < 4 ; i++) + { + sprintf(filename, "gfx/decals/blood%d.png", (i + 1)); + decals[i] = loadTile(filename); + + sprintf(filename, "gfx/decals/scorch%d.png", (i + 1)); + decals[i + 4] = loadTile(filename); + } +} diff --git a/src/world/map.h b/src/world/map.h index c36a3b5..ef6579c 100644 --- a/src/world/map.h +++ b/src/world/map.h @@ -26,5 +26,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. extern float limit(float i, float a, float b); extern int rrnd(int low, int high); extern char *readFile(const char *filename); +extern Atlas *getImageFromAtlas(char *filename); +extern Texture *getTexture(const char *filename); +extern void blitRect(SDL_Texture *texture, int x, int y, SDL_Rect *srcRect, int center); +extern Camera camera; extern World world; diff --git a/src/world/worldLoader.c b/src/world/worldLoader.c index 1f76a25..614cc26 100644 --- a/src/world/worldLoader.c +++ b/src/world/worldLoader.c @@ -134,6 +134,8 @@ static void loadBob(cJSON *root) b->facing = lookup(cJSON_GetObjectItem(root, "facing")->valuestring); initBob(b); + + world.bob = b; } static void loadEntities(cJSON *root)