Mercurial > molko
changeset 556:7eebac09fcb7
examples: make map funnier
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 08 Mar 2023 12:51:18 +0100 |
parents | 6c911cbc1fd7 |
children | 944798a59b8a |
files | CMakeLists.txt examples/example-map/example-map.c libmlk-rpg/mlk/rpg/map-loader-file.c libmlk-rpg/mlk/rpg/map-loader-file.h |
diffstat | 4 files changed, 83 insertions(+), 21 deletions(-) [+] |
line wrap: on
line diff
--- a/CMakeLists.txt Tue Mar 07 22:35:43 2023 +0100 +++ b/CMakeLists.txt Wed Mar 08 12:51:18 2023 +0100 @@ -46,9 +46,9 @@ endif () option(MLK_WITH_DOXYGEN "Enable doxygen documentation" On) -option(MLK_WITH_EXAMPLES "Enable examples" Off) -option(MLK_WITH_NLS "Enable NLS support" On) -option(MLK_WITH_TESTS "Enable unit tests" Off) +option(MLK_WITH_EXAMPLES "Enable examples" On) +option(MLK_WITH_NLS "Enable NLS support" Off) +option(MLK_WITH_TESTS "Enable unit tests" On) include(cmake/MlkBcc.cmake) include(cmake/MlkExecutable.cmake)
--- a/examples/example-map/example-map.c Tue Mar 07 22:35:43 2023 +0100 +++ b/examples/example-map/example-map.c Wed Mar 08 12:51:18 2023 +0100 @@ -27,6 +27,7 @@ #include <mlk/core/game.h> #include <mlk/core/image.h> #include <mlk/core/key.h> +#include <mlk/core/maths.h> #include <mlk/core/painter.h> #include <mlk/core/panic.h> #include <mlk/core/state.h> @@ -35,12 +36,14 @@ #include <mlk/core/util.h> #include <mlk/core/window.h> +#include <mlk/ui/align.h> #include <mlk/ui/label.h> #include <mlk/ui/ui.h> #include <mlk/rpg/map-loader-file.h> #include <mlk/rpg/map-loader.h> #include <mlk/rpg/map.h> +#include <mlk/rpg/message.h> #include <mlk/rpg/tileset-loader-file.h> #include <mlk/rpg/tileset-loader.h> #include <mlk/rpg/tileset.h> @@ -59,6 +62,27 @@ static struct mlk_map_loader map_loader; static struct mlk_map map; +static struct { + int x; + int y; + unsigned int w; + unsigned int h; + struct mlk_message message; +} tomb = { + .x = 620, + .y = 280, + .w = 60, + .h = 100, + .message = { + .flags = MLK_MESSAGE_FLAGS_FADEIN | MLK_MESSAGE_FLAGS_FADEOUT, + .lines = (const char *[]) { + "Welcome to your tomb.", + "What did you expect on this island?" + }, + .linesz = 2 + } +}; + static const struct { const char *basename; struct mlk_texture *texture; @@ -144,8 +168,24 @@ case MLK_EVENT_QUIT: mlk_game_quit(); break; + case MLK_EVENT_KEYDOWN: + /* Open tomb message if we're near the location. */ + if (tomb.message.state) + mlk_message_handle(&tomb.message, ev); + else { + if (ev->key.key == MLK_KEY_ENTER && mlk_maths_is_boxed(map.player_x, map.player_y, tomb.x, tomb.y, tomb.w, tomb.h)) { + mlk_message_query(&tomb.message, &tomb.message.w, &tomb.message.h); + mlk_message_start(&tomb.message); + mlk_align(MLK_ALIGN_TOP, &tomb.message.x, &tomb.message.y, tomb.message.w, tomb.message.h, + 0, 50, MLK_EXAMPLE_W, MLK_EXAMPLE_H); + map.player_movement = 0; + } else + mlk_map_handle(&map, ev); + } + break; default: - mlk_map_handle(&map, ev); + if (!tomb.message.state) + mlk_map_handle(&map, ev); break; } } @@ -156,6 +196,10 @@ (void)st; mlk_map_update(&map, ticks); + + if (tomb.message.state && mlk_message_update(&tomb.message, ticks)) { + + } } static void @@ -166,6 +210,10 @@ mlk_painter_set_color(MLK_EXAMPLE_BG); mlk_painter_clear(); mlk_map_draw(&map); + + if (tomb.message.state) + mlk_message_draw(&tomb.message); + mlk_painter_present(); }
--- a/libmlk-rpg/mlk/rpg/map-loader-file.c Tue Mar 07 22:35:43 2023 +0100 +++ b/libmlk-rpg/mlk/rpg/map-loader-file.c Wed Mar 08 12:51:18 2023 +0100 @@ -33,11 +33,19 @@ (void)map; struct mlk_map_loader_file *file = self->data; + struct mlk_tileset_loader tileset_loader; char path[MLK_PATH_MAX]; snprintf(path, sizeof (path), "%s/%s", file->directory, ident); - if (mlk_tileset_loader_open(file->tileset_loader, &file->tileset, path) < 0) + /* + * Just make sure that we don't leak in case tileset directory is listed + * more than once. + */ + mlk_tileset_loader_file_finish(&file->tileset_loader_file); + mlk_tileset_loader_file_init(&file->tileset_loader_file, &tileset_loader, path); + + if (mlk_tileset_loader_open(&tileset_loader, &file->tileset, path) < 0) return NULL; return &file->tileset; @@ -88,14 +96,19 @@ char filepath[MLK_PATH_MAX]; - /* Determine base filename base directory. */ - mlk_util_strlcpy(filepath, filename, sizeof (filepath)); - mlk_util_strlcpy(file->directory, mlk_util_dirname(filepath), sizeof (file->directory)); - - loader->data = file; - loader->init_tileset = init_tileset; - loader->alloc_tiles = alloc_tiles; - loader->expand_blocks = expand_blocks; + if (!file->directory[0]) { + /* Determine base filename base directory. */ + mlk_util_strlcpy(filepath, filename, sizeof (filepath)); + mlk_util_strlcpy(file->directory, mlk_util_dirname(filepath), sizeof (file->directory)); + } + if (!loader->data) + loader->data = file; + if (!loader->init_tileset) + loader->init_tileset = init_tileset; + if (!loader->alloc_tiles) + loader->alloc_tiles = alloc_tiles; + if (!loader->expand_blocks) + loader->expand_blocks = expand_blocks; } void @@ -108,6 +121,7 @@ file->tiles[i] = NULL; } + mlk_tileset_loader_file_finish(&file->tileset_loader_file); mlk_alloc_free(file->blocks); file->blocks = NULL; }
--- a/libmlk-rpg/mlk/rpg/map-loader-file.h Tue Mar 07 22:35:43 2023 +0100 +++ b/libmlk-rpg/mlk/rpg/map-loader-file.h Wed Mar 08 12:51:18 2023 +0100 @@ -27,10 +27,10 @@ #include <mlk/util/util.h> #include "map.h" +#include "tileset-loader-file.h" #include "tileset.h" struct mlk_map_loader; -struct mlk_tileset_loader; /** * \struct mlk_map_loader_file @@ -44,16 +44,16 @@ */ char directory[MLK_PATH_MAX]; - /** - * (read-write, borrowed) - * - * The tileset loader to use when finding tilesets in maps. - */ - struct mlk_tileset_loader *tileset_loader; - /** \cond MLK_PRIVATE_DECLS */ unsigned int *tiles[MLK_MAP_LAYER_TYPE_LAST]; + + /* + * We use a tileset file loader if init_tileset function isn't present + * in this map loader. + */ + struct mlk_tileset_loader_file tileset_loader_file; struct mlk_tileset tileset; + struct mlk_map_block *blocks; /** \endcond MLK_PRIVATE_DECLS */ };