Switches the old MapTile texture system to use sprites instead

This is hardly tested at the moment.
This commit is contained in:
Linus Probert 2018-10-12 08:36:00 +02:00
parent 53151b6ee0
commit 2e0a88e5e3
3 changed files with 27 additions and 62 deletions

View File

@ -58,7 +58,6 @@ map_create(void)
map->artifacts = linkedlist_create(); map->artifacts = linkedlist_create();
map->objects = linkedlist_create(); map->objects = linkedlist_create();
map->currentRoom = (Position) { 0, 0 }; map->currentRoom = (Position) { 0, 0 };
map->renderTimer = timer_create();
map->monsterMoveTimer = timer_create(); map->monsterMoveTimer = timer_create();
map->level = 1; map->level = 1;
@ -75,9 +74,9 @@ MapTile*
map_create_tile(void) map_create_tile(void)
{ {
MapTile *tile = ec_malloc(sizeof(MapTile)); MapTile *tile = ec_malloc(sizeof(MapTile));
tile->textureIndex0 = -1; tile->sprite = sprite_create();
tile->textureIndex1 = -1; tile->sprite->clip = CLIP16(0, 0);
tile->clip = CLIP16(0, 0); tile->sprite->dim = DIM(32, 32);
tile->collider = false; tile->collider = false;
tile->lethal = false; tile->lethal = false;
tile->lightsource = false; tile->lightsource = false;
@ -92,9 +91,13 @@ map_add_tile(Map *map, Position *tile_pos, MapTile *tile)
Room *room = map->rooms[cr->x][cr->y]; Room *room = map->rooms[cr->x][cr->y];
MapTile **oldTile = &room->tiles[tile_pos->x][tile_pos->y]; MapTile **oldTile = &room->tiles[tile_pos->x][tile_pos->y];
// Clear possible decoration // Set the tile sprites position to match tile pos
if (tile->levelExit && room->tiles[tile_pos->x][tile_pos->y]) { tile->sprite->pos = POS(tile_pos->x * TILE_DIMENSION + (map->currentRoom.x * GAME_VIEW_WIDTH),
MapTile **decoration = &room->tiles[tile_pos->x][tile_pos->y]; tile_pos->y * TILE_DIMENSION + (map->currentRoom.y * GAME_VIEW_HEIGHT));
// If this is the level exit then clear the decoration if one exists
if (tile->levelExit && room->decorations[tile_pos->x][tile_pos->y]) {
MapTile **decoration = &room->decorations[tile_pos->x][tile_pos->y];
free(*decoration); free(*decoration);
*decoration = NULL; *decoration = NULL;
} }
@ -111,6 +114,11 @@ void map_add_decoration(Map *map, Position *tile_pos, MapTile *tile)
{ {
const Position *cr = &map->currentRoom; const Position *cr = &map->currentRoom;
MapTile **oldTile = &map->rooms[cr->x][cr->y]->decorations[tile_pos->x][tile_pos->y]; MapTile **oldTile = &map->rooms[cr->x][cr->y]->decorations[tile_pos->x][tile_pos->y];
// Set the decoration sprites position to match tile pos
tile->sprite->pos = POS(tile_pos->x * TILE_DIMENSION + (map->currentRoom.x * GAME_VIEW_WIDTH),
tile_pos->y * TILE_DIMENSION + (map->currentRoom.y * GAME_VIEW_HEIGHT));
if (*oldTile != NULL) { if (*oldTile != NULL) {
free(*oldTile); free(*oldTile);
*oldTile = NULL; *oldTile = NULL;
@ -241,39 +249,12 @@ int map_add_texture(Map *map, const char *path, SDL_Renderer *renderer)
} }
static static
void map_tile_render(Map *map, MapTile *tile, Position *pos, Camera *cam) void map_tile_render(MapTile *tile, Camera *cam)
{ {
static bool second_texture = false;
if (tile == NULL) if (tile == NULL)
return; return;
if (timer_get_ticks(map->renderTimer) > 300) { sprite_render(tile->sprite, cam);
timer_start(map->renderTimer);
second_texture = !second_texture;
}
Position camPos = camera_to_camera_position(cam, pos);
SDL_Rect draw_box = (SDL_Rect) {
camPos.x,
camPos.y,
TILE_DIMENSION,
TILE_DIMENSION
};
Texture *texture;
if (tile->textureIndex1 >= 0 && second_texture) {
texture = linkedlist_get(&map->textures, tile->textureIndex1);
} else {
texture = linkedlist_get(&map->textures, tile->textureIndex0);
}
SDL_RenderCopy(cam->renderer,
texture->texture,
&tile->clip,
&draw_box
);
} }
void void
@ -304,28 +285,13 @@ void map_render(Map *map, Camera *cam)
unsigned int i, j; unsigned int i, j;
Room *room; Room *room;
if (!timer_started(map->renderTimer)) {
timer_start(map->renderTimer);
}
Position roomPos = { map->currentRoom.x, map->currentRoom.y }; Position roomPos = { map->currentRoom.x, map->currentRoom.y };
Position roomCords = {
roomPos.x * MAP_ROOM_WIDTH * TILE_DIMENSION,
roomPos.y * MAP_ROOM_HEIGHT * TILE_DIMENSION
};
room = map->rooms[roomPos.x][roomPos.y]; room = map->rooms[roomPos.x][roomPos.y];
for (i=0; i < MAP_ROOM_WIDTH; ++i) { for (i=0; i < MAP_ROOM_WIDTH; ++i) {
for (j=0; j < MAP_ROOM_HEIGHT; ++j) { for (j=0; j < MAP_ROOM_HEIGHT; ++j) {
Position tilePos = (Position) { map_tile_render(room->tiles[i][j], cam);
roomCords.x + i*TILE_DIMENSION, map_tile_render(room->decorations[i][j], cam);
roomCords.y + j*TILE_DIMENSION
};
map_tile_render(map, room->tiles[i][j], &tilePos, cam);
map_tile_render(map,
room->decorations[i][j],
&tilePos,
cam);
if (room->traps[i][j]) if (room->traps[i][j])
trap_render(room->traps[i][j], cam); trap_render(room->traps[i][j], cam);
} }
@ -451,7 +417,6 @@ void map_destroy(Map *map)
while (map->objects != NULL) while (map->objects != NULL)
artifact_destroy(linkedlist_pop(&map->objects)); artifact_destroy(linkedlist_pop(&map->objects));
timer_destroy(map->renderTimer);
timer_destroy(map->monsterMoveTimer); timer_destroy(map->monsterMoveTimer);
free(map); free(map);
} }

View File

@ -37,9 +37,7 @@ typedef struct UpdateData UpdateData;
typedef struct Trap Trap; typedef struct Trap Trap;
typedef struct MapTile_t { typedef struct MapTile_t {
int textureIndex0; Sprite *sprite;
int textureIndex1;
SDL_Rect clip;
bool collider; bool collider;
bool lethal; bool lethal;
bool lightsource; bool lightsource;
@ -62,7 +60,6 @@ typedef struct Map_t {
LinkedList *artifacts; LinkedList *artifacts;
LinkedList *objects; LinkedList *objects;
Position currentRoom; Position currentRoom;
Timer *renderTimer;
Timer *monsterMoveTimer; Timer *monsterMoveTimer;
int level; int level;
} Map; } Map;

View File

@ -200,9 +200,12 @@ extract_tile_data(lua_State *L,
SDL_Rect clip = (SDL_Rect) { tile_clip_x, tile_clip_y, 16, 16 }; SDL_Rect clip = (SDL_Rect) { tile_clip_x, tile_clip_y, 16, 16 };
MapTile *tile = map_create_tile(); MapTile *tile = map_create_tile();
tile->textureIndex0 = t_index0; if (t_index0 >= 0)
tile->textureIndex1 = t_index1; sprite_set_texture(tile->sprite, linkedlist_get(&map->textures, t_index0), 0);
tile->clip = clip; if (t_index1 >= 0)
sprite_set_texture(tile->sprite, linkedlist_get(&map->textures, t_index1), 1);
tile->sprite->clip = clip;
tile->collider = collider; tile->collider = collider;
tile->lightsource = lightsource; tile->lightsource = lightsource;
tile->levelExit = levelExit; tile->levelExit = levelExit;