From 95ce2c5f032e740ea2e3c44f8055cb3c1f1ce8c5 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Wed, 26 Jan 2022 17:00:00 -0500 Subject: maze: Factor out wall line drawing --- (limited to 'src/maze.c') diff --git a/src/maze.c b/src/maze.c index 4655405..05703a7 100644 --- a/src/maze.c +++ b/src/maze.c @@ -233,6 +233,24 @@ mf_maze_new(int s, int w, int h, int show_all) return m; } +static int +_mf_maze_render_line(SDL_Renderer *renderer, int x, int y, int w, int h, int cw) +{ + SDL_Rect rect; + + rect.x = x * cw - 1; + rect.y = y * cw - 1; + rect.w = w * cw + 2; + rect.h = h * cw + 2; + if (SDL_RenderFillRect(renderer, &rect) < 0) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "Couldn't draw line: %s", SDL_GetError()); + return -1; + } + + return 0; +} + int mf_maze_render(struct mf_maze *m, SDL_Renderer *renderer, SDL_Color *color, int cw) @@ -268,36 +286,16 @@ mf_maze_render(struct mf_maze *m, SDL_Renderer *renderer, SDL_Color *color, if (y < m->h - 1 && mf_maze_is_wall(m, x, y, 0, 1) && _mf_maze_revealed_wall(m, x, y, 0, 1)) { /* Draw h line */ - if (SDL_RenderDrawLine(renderer, - (x ) * cw - 1, (y+1) * cw - 1, - (x+1) * cw , (y+1) * cw - 1) - < 0 || - SDL_RenderDrawLine(renderer, - (x ) * cw - 1, (y+1) * cw , - (x+1) * cw , (y+1) * cw ) - < 0) { - SDL_LogError( - SDL_LOG_CATEGORY_APPLICATION, - "Couldn't draw line: %s", - SDL_GetError()); + if (_mf_maze_render_line(renderer, x , y+1, + 1, 0, cw) < 0) { e = -1; } } if (x < m->w - 1 && mf_maze_is_wall(m, x, y, 1, 0) && _mf_maze_revealed_wall(m, x, y, 1, 0)) { /* Draw v line */ - if (SDL_RenderDrawLine(renderer, - (x+1) * cw - 1, (y ) * cw - 1, - (x+1) * cw - 1, (y+1) * cw ) - < 0 || - SDL_RenderDrawLine(renderer, - (x+1) * cw , (y ) * cw - 1, - (x+1) * cw , (y+1) * cw ) - < 0) { - SDL_LogError( - SDL_LOG_CATEGORY_APPLICATION, - "Couldn't draw line: %s", - SDL_GetError()); + if (_mf_maze_render_line(renderer, x+1, y , + 0, 1, cw) < 0) { e = -1; } } -- cgit v0.9.1