Start of map rendering.

This commit is contained in:
Steve 2018-01-31 08:08:41 +00:00
parent 0705225253
commit eeeee41ef1
6 changed files with 195 additions and 14 deletions

View File

@ -50,8 +50,7 @@ Atlas *getImageFromAtlas(char *filename)
} }
} }
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "No such atlas image '%s'", filename); return NULL;
exit(1);
} }
static void loadAtlasTexture(void) static void loadAtlasTexture(void)

View File

@ -22,9 +22,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
static void logic(void); static void logic(void);
static void draw(void); static void draw(void);
static int timeout;
static Atlas *testImage; static void trackRandomEntity(void);
static Texture *atlasTexture;
void initAtlasTest(void) void initAtlasTest(void)
{ {
@ -34,20 +33,39 @@ void initAtlasTest(void)
app.delegate.logic = &logic; app.delegate.logic = &logic;
app.delegate.draw = &draw; 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"); loadWorld("data/maps/beachApproach.json");
initMap();
timeout = FPS * 2;
cameraTrack((Entity*)world.bob);
} }
static void logic(void) static void logic(void)
{ {
if (--timeout <= 0)
{
trackRandomEntity();
}
} }
static void draw(void) 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;
}
}
} }

View File

@ -24,8 +24,12 @@ extern Atlas *getImageFromAtlas(char *filename);
extern Texture *getTexture(const 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 blitRect(SDL_Texture *texture, int x, int y, SDL_Rect *srcRect, int center);
extern void loadMapData(char *filename); extern void loadMapData(char *filename);
extern void initGame(void); extern void initMap(void);
extern void initHub(void); extern void initHub(void);
extern void initGame(void);
extern void loadWorld(char *filename); extern void loadWorld(char *filename);
extern void drawMap(void);
extern void cameraTrack(Entity *e);
extern App app; extern App app;
extern World world;

View File

@ -20,7 +20,17 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "map.h" #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 animTimer;
static int MAX_Y;
static Texture *atlasTexture;
static SDL_Rect *tiles[MAP_TILE_MAX];
static SDL_Rect *decals[8];
void initMap(void) void initMap(void)
{ {
@ -32,6 +42,82 @@ void initMap(void)
world.map.bounds.h = 0; world.map.bounds.h = 0;
animTimer = 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) void animateMap(void)
@ -146,13 +232,32 @@ static void calculateMapBounds(void)
world.map.bounds.h += MAP_TILE_SIZE; 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); 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; char *data, *p;
int i, x, y; 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); SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Loading %s", filename);
data = readFile(filename); data = readFile(filename);
@ -195,3 +300,52 @@ void loadMapData(char *filename)
calculateMapBounds(); 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);
}
}

View File

@ -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 float limit(float i, float a, float b);
extern int rrnd(int low, int high); extern int rrnd(int low, int high);
extern char *readFile(const char *filename); 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; extern World world;

View File

@ -134,6 +134,8 @@ static void loadBob(cJSON *root)
b->facing = lookup(cJSON_GetObjectItem(root, "facing")->valuestring); b->facing = lookup(cJSON_GetObjectItem(root, "facing")->valuestring);
initBob(b); initBob(b);
world.bob = b;
} }
static void loadEntities(cJSON *root) static void loadEntities(cJSON *root)