From c4915c5cf04246b9e776ada61f6c917f363a78b7 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Tue, 8 Jan 2019 19:52:42 -0500 Subject: isolate wall access --- src/libsok/sok-ctx.c | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/src/libsok/sok-ctx.c b/src/libsok/sok-ctx.c index d19682a..460c7ad 100644 --- a/src/libsok/sok-ctx.c +++ b/src/libsok/sok-ctx.c @@ -14,6 +14,29 @@ ((a).y == (b).y) \ ) +static bool +ctx_is_wall( + const sok_ctx_t * const ctx, + const sok_pos_t pos +) { + return ctx->level.walls[pos.y * ctx->level.size.x + pos.x]; +} + +static void +ctx_set_wall( + sok_ctx_t * const ctx, + const sok_pos_t pos +) { + ctx->level.walls[pos.y * ctx->level.size.x + pos.x] = true; +} + +static void +ctx_clear_walls( + sok_ctx_t * const ctx +) { + memset(ctx->level.walls, 0, SOK_LEVEL_MAX_WIDTH * SOK_LEVEL_MAX_HEIGHT); +} + static bool on_size( const sok_level_parser_t * const parser, @@ -65,7 +88,7 @@ on_wall( } // save wall - ctx->level.walls[pos.y * ctx->level.size.x + pos.x] = true; + ctx_set_wall(ctx, pos); // return sucess return true; @@ -194,7 +217,7 @@ sok_ctx_set_level( // clear level ctx->level.num_goals = 0; ctx->level.num_boxes = 0; - memset(ctx->level.walls, 0, SOK_LEVEL_MAX_WIDTH * SOK_LEVEL_MAX_HEIGHT); + ctx_clear_walls(ctx); // parse level sok_level_parser_t parser; @@ -224,7 +247,7 @@ tile_is_floor( return ( (pos.x < SOK_LEVEL_MAX_WIDTH - 1) && (pos.y < SOK_LEVEL_MAX_HEIGHT - 1) && - !(ctx->level.walls[pos.y * ctx->level.size.x + pos.x]) + !ctx_is_wall(ctx, pos) ); } @@ -449,12 +472,12 @@ bool sok_ctx_walk( if (cbs->on_wall) { // walk walls for (size_t i = 0; i < (ctx->level.size.x * ctx->level.size.y); i++) { - if (ctx->level.walls[i]) { - const sok_pos_t pos = { - .y = i / ctx->level.size.x, - .x = i % ctx->level.size.x, - }; + const sok_pos_t pos = { + .y = i / ctx->level.size.x, + .x = i % ctx->level.size.x, + }; + if (ctx_is_wall(ctx, pos)) { // emit wall if (!cbs->on_wall(ctx, pos, data)) { return false; -- cgit v1.2.3