чување 43013189dfa79769b924d84f5349cc147a05255e
родитељ c838ef229143cf7528f6fb4efac46a75b573d741
Аутор: Страхиња Радић <contact@strahinja.org>
Датум: Wed, 16 Aug 2023 11:04:11 +0200
Add help dialog, change ter16 sprite sheet to have transparent background
Signed-off-by: Страхиња Радић <contact@strahinja.org>
Diffstat:
измењених датотека: 4, додавања: 101(+), брисања: 21(-)
diff --git a/README b/README
@@ -49,9 +49,12 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
---
-Sprite sheet ter16.png created in GIMP using Terminus font 4.49.1, Copyright (C)
-2020 Dimitar Toshkov Zhekov[1], licensed under SIL Open Font License 1.1. See
-file OFL.TXT for more information.
+Sprite sheet sprites.png (source file sprites.xcf.xz) hand drawn/crafted in GIMP
+by Strahinya Radich.
+
+Sprite sheet ter16.png (source file ter16.xcf.xz) created in GIMP using Terminus
+font 4.49.1. Terminus font Copyright (C) 2020 Dimitar Toshkov Zhekov[1],
+licensed under SIL Open Font License 1.1. See file OFL.TXT for more information.
[1]: http://terminus-font.sourceforge.net/
diff --git a/lav-sdl.c b/lav-sdl.c
@@ -21,6 +21,8 @@
#define CELL_SIZE 32
#define DEFAULT_WIDTH 1280
#define DEFAULT_HEIGHT 960
+#define HELP_DIALOG_WIDTH 640
+#define HELP_DIALOG_HEIGHT 300
#define LABYRINTH_MAXHEIGHT 20
#define LABYRINTH_MAXWIDTH 40
#define LOCATION_STACK_MAX (LABYRINTH_MAXWIDTH * LABYRINTH_MAXHEIGHT)
@@ -56,6 +58,23 @@ const char* state_descriptions[] = {
"Failed!",
"Success!"
};
+
+const char* help_text[] = {
+ "lav-sdl - SDL maze solver",
+ "=========================",
+ "",
+ "F1 - Show this help screen",
+ "Esc - Hide this help screen",
+ "Space - Pause/resume simulation",
+ "C-Q - Exit",
+ "",
+ "---",
+ "",
+ "This program is licensed under the terms of GNU GPL v3 or (at your",
+ "option) any later version. Copyright (C) 2023 Strahinya Radich.",
+ "See the file LICENSE for exact copyright and license details.",
+ NULL
+};
/* clang-format on */
struct AnimationState {
@@ -78,6 +97,7 @@ static SDL_Point hero, start, end;
static struct Labyrinth labyrinth;
static int location_pointer = 0;
static SDL_Point* location_stack = NULL;
+static int help_shown = 0;
static int redraw = 1;
static SDL_Renderer* renderer = NULL;
static int running = 1;
@@ -94,6 +114,7 @@ void assign_point_xyIF(const int x, const int y, SDL_FPoint* to);
void calculate_delta(void);
void cleanup(void);
void draw(void);
+void draw_help_dialog(void);
void draw_text(const int x, const int y, const int font_width,
const int font_height, SDL_Texture* font_tex, const char* text, ...);
void get_sprite_xy(const char cell, int* x, int* y);
@@ -218,10 +239,9 @@ draw(void)
}
draw_text(bg_rect.x, bg_rect.y + bg_rect.h, TER16_WIDTH, TER16_HEIGHT,
- ter16_texture, "Column: %2d, Row: %2d", hero.x + 1,
- hero.y + 1);
- draw_text(cx, bg_rect.y + bg_rect.h, TER16_WIDTH, TER16_HEIGHT,
- ter16_texture, "Status: %s", state_descriptions[state]);
+ ter16_texture,
+ "Column: %2d, Row: %2d F1 = Help Status: %s", hero.x + 1,
+ hero.y + 1, state_descriptions[state]);
if (animation.active)
{
@@ -241,11 +261,57 @@ draw(void)
}
}
+ if (help_shown)
+ draw_help_dialog();
+
SDL_RenderPresent(renderer);
// SDL_RenderFlush(renderer);
}
void
+draw_help_dialog(void)
+{
+ const char** phelp_text = help_text;
+ int sx = screen_width / 2 - HELP_DIALOG_WIDTH / 2 * scale_factor;
+ int sy = screen_height / 2 - HELP_DIALOG_HEIGHT / 2 * scale_factor;
+ SDL_Rect dialog_rect;
+
+ dialog_rect.x = sx;
+ dialog_rect.y = sy;
+ dialog_rect.w = HELP_DIALOG_WIDTH * scale_factor;
+ dialog_rect.h = HELP_DIALOG_HEIGHT * scale_factor;
+
+ SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
+ SDL_SetRenderDrawColor(renderer, 0, 0, 0, .75 * 255);
+ SDL_RenderFillRect(renderer, &dialog_rect);
+
+ SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
+ SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
+ SDL_RenderDrawRect(renderer, &dialog_rect);
+ SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
+ dialog_rect.x--;
+ dialog_rect.y--;
+ dialog_rect.w += 2;
+ dialog_rect.h += 2;
+ SDL_RenderDrawRect(renderer, &dialog_rect);
+ dialog_rect.x--;
+ dialog_rect.y--;
+ dialog_rect.w += 2;
+ dialog_rect.h += 2;
+ SDL_RenderDrawRect(renderer, &dialog_rect);
+
+ while (*phelp_text)
+ {
+ draw_text(sx + (2 * TER16_WIDTH) * scale_factor,
+ sy + (TER16_HEIGHT + 2) * scale_factor
+ + (phelp_text - help_text) * (TER16_HEIGHT + 2)
+ * scale_factor,
+ TER16_WIDTH, TER16_HEIGHT, ter16_texture, *phelp_text);
+ phelp_text++;
+ }
+}
+
+void
draw_text(const int x, const int y, const int font_width, const int font_height,
SDL_Texture* font_tex, const char* text, ...)
{
@@ -372,13 +438,20 @@ handle_event(SDL_Event* event)
case SDL_KEYDOWN:
keysym = event->key.keysym;
- /*print_debug("Got Keydown, sym = %X, mod = %X",
- keysym.sym, keysym.mod);*/
+ print_debug("Got Keydown, sym = %X, mod = %X", keysym.sym,
+ keysym.mod);
- if (keysym.sym == SDLK_ESCAPE)
- running = 0;
- else if (keysym.sym == SDLK_SPACE)
+ switch (keysym.sym)
{
+ case SDLK_F1:
+ help_shown = 1;
+ redraw = 1;
+ break;
+ case SDLK_ESCAPE:
+ help_shown = 0;
+ redraw = 1;
+ break;
+ case SDLK_SPACE:
if (state == STATE_SEARCHING)
{
print_debug("State: paused");
@@ -394,10 +467,14 @@ handle_event(SDL_Event* event)
print_debug("Resetting");
reset();
}
+ break;
+ case SDLK_q:
+ if (keysym.mod & KMOD_CTRL)
+ running = 0;
+ break;
+ default:
+ print_debug("switch fallthrough");
}
- else if (keysym.sym == SDLK_q && (keysym.mod & KMOD_CTRL))
- running = 0;
- break;
}
}
@@ -518,12 +595,12 @@ load_labyrinth(const char* pathname)
if (x == LABYRINTH_MAXWIDTH || y == LABYRINTH_MAXHEIGHT)
{
print_error("Labyrinth overflow at %d, %d"
- " (ch == %c)",
+ " (ch == %c)",
x, y, ch);
exit(1);
}
- labyrinth.cells[y][x] = (ch == '0' ? CELL_EMPTY :
- CELL_WALL);
+ labyrinth.cells[y][x]
+ = (ch == '0' ? CELL_EMPTY : CELL_WALL);
x++;
if (firstline)
labyrinth.width++;
@@ -721,11 +798,11 @@ update_size(void)
factor1 = (float)screen_width / DEFAULT_WIDTH;
factor2 = (float)screen_height / DEFAULT_HEIGHT;
-
+
scale_factor = MAX(factor1, factor2);
if (LABYRINTH_MAXWIDTH * CELL_SIZE * scale_factor > screen_width
- || LABYRINTH_MAXHEIGHT * CELL_SIZE * scale_factor >
- screen_height)
+ || LABYRINTH_MAXHEIGHT * CELL_SIZE * scale_factor
+ > screen_height)
scale_factor = MIN(factor1, factor2);
}
diff --git a/ter16.png b/ter16.png
Бинарне датотеке се разликују.
diff --git a/ter16.xcf.xz b/ter16.xcf.xz
Бинарне датотеке се разликују.