Mercurial > molko
changeset 391:9334b420c975
rpg: don't use fixed size messages
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 15 Feb 2022 20:35:23 +0100 |
parents | ae2dcf40c1eb |
children | e6d7ebad33cc |
files | examples/example-action/main.c examples/example-message/main.c src/libmlk-rpg/rpg/battle-state-lost.c src/libmlk-rpg/rpg/battle-state-lost.h src/libmlk-rpg/rpg/battle-state-victory.c src/libmlk-rpg/rpg/battle-state-victory.h src/libmlk-rpg/rpg/message.c src/libmlk-rpg/rpg/message.h |
diffstat | 8 files changed, 114 insertions(+), 72 deletions(-) [+] |
line wrap: on
line diff
--- a/examples/example-action/main.c Wed Feb 16 08:56:02 2022 +0100 +++ b/examples/example-action/main.c Tue Feb 15 20:35:23 2022 +0100 @@ -64,6 +64,7 @@ * Event object for the chest to click on. */ static struct { + const char *text[2]; struct message msg; struct action msg_act; int x; @@ -73,14 +74,16 @@ struct sprite sprite; struct action event; } chest = { + .text = { + "100000 pièces.", + "Te voilà riche sale file de crevette." + }, .msg = { .x = MX, .y = MY, .w = MW, - .text = { - "100000 pièces.", - "Te voilà riche sale file de crevette." - } + .lines = chest.text, + .linesz = 2 } }; @@ -103,6 +106,7 @@ */ static struct { struct { + const char *text[6]; struct message msg; struct action act; } msgs[5]; @@ -122,61 +126,71 @@ } guide = { .msgs = { { + .text = { + "Bienvenue dans ce monde Molko." + }, .msg = { .x = MX, .y = MY, .w = MW, .delay = MESSAGE_DELAY_DEFAULT, .flags = MESSAGE_FLAGS_FADEIN, - .text = { - "Bienvenue dans ce monde Molko." - } + .lines = guide.msgs[0].text, + .linesz = 1 }, }, { + .text = { + "Penses tu vraiment pouvoir me battre ?" + }, .msg = { .x = MX, .y = MY, .w = MW, - .text = { - "Penses tu vraiment pouvoir me battre ?" - } + .lines = guide.msgs[1].text, + .linesz = 1 } }, { + .text = { + "Non j'ai la trouille.", + "Bien sûr, je suis la légende." + }, .msg = { .x = MX, .y = MY, .w = MW, .flags = MESSAGE_FLAGS_QUESTION, - .text = { - "Non j'ai la trouille.", - "Bien sûr, je suis la légende." - } + .lines = guide.msgs[2].text, + .linesz = 2 } }, /* In case of NO. */ { + .text = { + "Poule mouillée." + }, .msg = { .x = MX, .y = MY, .w = MW, - .text = { - "Poule mouillée." - } + .lines = guide.msgs[3].text, + .linesz = 1 } }, /* In case of YES. */ { + .text = { + "Prépare toi à souffrir." + }, .msg = { .x = MX, .y = MY, .w = MW, - .text = { - "Prépare toi à souffrir." - } + .lines = guide.msgs[4].text, + .linesz = 1 } } }
--- a/examples/example-message/main.c Wed Feb 16 08:56:02 2022 +0100 +++ b/examples/example-message/main.c Tue Feb 15 20:35:23 2022 +0100 @@ -121,16 +121,18 @@ static void basic(void) { + const char * const text[] = { + "This is a basic message.", + "Vertical spacing is automatically computed.", + "You need to press <Enter> to close it.", + }; struct message msg = { .x = MX, .y = MY, .w = MW, .spacing = 12, - .text = { - "This is a basic message.", - "Vertical spacing is automatically computed.", - "You need to press <Enter> to close it.", - }, + .lines = text, + .linesz = 3 }; message_query(&msg, NULL, &msg.h); @@ -140,16 +142,18 @@ static void automatic(void) { + const char * const text[] = { + "This is a an automatic message.", + "It will disappear in a few seconds.", + "You can still press <Enter> to close it quicker." + }; struct message msg = { .x = MX, .y = MY, .w = MW, .timeout = MESSAGE_TIMEOUT_DEFAULT, - .text = { - "This is a an automatic message.", - "It will disappear in a few seconds.", - "You can still press <Enter> to close it quicker." - }, + .lines = text, + .linesz = 3, .flags = MESSAGE_FLAGS_AUTOMATIC }; @@ -160,14 +164,16 @@ static void fadein(void) { + const char * const text[] = { + "This message will fade in." + }; struct message msg = { .x = MX, .y = MY, .w = MW, .delay = MESSAGE_DELAY_DEFAULT, - .text = { - "This message will fade in." - }, + .lines = text, + .linesz = 1, .flags = MESSAGE_FLAGS_FADEIN }; @@ -178,14 +184,16 @@ static void fadeout(void) { + const char * const text[] = { + "This message will fade out." + }; struct message msg = { .x = MX, .y = MY, .w = MW, .delay = MESSAGE_DELAY_DEFAULT, - .text = { - "This message will fade out." - }, + .lines = text, + .linesz = 1, .flags = MESSAGE_FLAGS_FADEOUT }; @@ -196,14 +204,16 @@ static void fade(void) { + const char * const text[] = { + "This message will fade in and out." + }; struct message msg = { .x = MX, .y = MY, .w = MW, .delay = MESSAGE_DELAY_DEFAULT, - .text = { - "This message will fade in and out." - }, + .lines = text, + .linesz = 1, .flags = MESSAGE_FLAGS_FADEIN | MESSAGE_FLAGS_FADEOUT }; @@ -214,14 +224,16 @@ static void question(void) { + const char * const text[] = { + "Okay, I've understood.", + "Nevermind, I'll do it again." + }; struct message msg = { .x = MX, .y = MY, .w = MW, - .text = { - "Okay, I've understood.", - "Nevermind, I'll do it again." - }, + .lines = text, + .linesz = 2, .flags = MESSAGE_FLAGS_QUESTION }; @@ -236,6 +248,9 @@ const unsigned int h = MH; const int x = (window.w / 2) - (w / 2); const int y = (window.h - h - 10); + const char * const text[] = { + "This one is small here." + }; struct message msg = { .x = x, @@ -244,9 +259,8 @@ .h = h, .delay = MESSAGE_DELAY_DEFAULT, .flags = MESSAGE_FLAGS_FADEIN | MESSAGE_FLAGS_FADEOUT, - .text = { - "This one is small here." - } + .lines = text, + .linesz = 1 }; run(&msg); @@ -255,15 +269,17 @@ static void toosmallh(void) { + const char * const text[] = { + "This one is too small in height and will emit a warning.", + "Because this line will be incomplete." + }; struct message msg = { .x = MX, .y = MY, .w = MW, .h = 40, - .text = { - "This one is too small in height and will emit a warning.", - "Because this line will be incomplete." - }, + .lines = text, + .linesz = 2 }; run(&msg); @@ -272,14 +288,16 @@ static void toosmallw(void) { + const char * const text[] = { + "This one is too small in width." + }; struct message msg = { .x = MX, .y = MY, .w = 160, .h = MH, - .text = { - "This one is too small in width." - }, + .lines = text, + .linesz = 1 }; run(&msg); @@ -288,16 +306,18 @@ static void custom(void) { + const char * const text[] = { + "This one will destroy your eyes.", + "Because it use a terrible custom theme." + }; struct theme theme; struct message msg = { .x = MX, .y = MY, .w = MW, .h = MH, - .text = { - "This one will destroy your eyes.", - "Because it use a terrible custom theme." - }, + .lines = text, + .linesz = 2, .theme = &theme };
--- a/src/libmlk-rpg/rpg/battle-state-lost.c Wed Feb 16 08:56:02 2022 +0100 +++ b/src/libmlk-rpg/rpg/battle-state-lost.c Tue Feb 15 20:35:23 2022 +0100 @@ -62,7 +62,10 @@ assert(lost); assert(bt); - lost->msg.text[0] = _("You have been defeated..."); + lost->text = _("You have been defeated..."); + + lost->msg.lines = &lost->text; + lost->msg.linesz = 1; lost->msg.theme = bt->theme; lost->msg.flags = MESSAGE_FLAGS_AUTOMATIC | MESSAGE_FLAGS_FADEIN |
--- a/src/libmlk-rpg/rpg/battle-state-lost.h Wed Feb 16 08:56:02 2022 +0100 +++ b/src/libmlk-rpg/rpg/battle-state-lost.h Tue Feb 15 20:35:23 2022 +0100 @@ -26,6 +26,7 @@ struct battle; struct battle_state_lost { + const char *text; struct message msg; };
--- a/src/libmlk-rpg/rpg/battle-state-victory.c Wed Feb 16 08:56:02 2022 +0100 +++ b/src/libmlk-rpg/rpg/battle-state-victory.c Tue Feb 15 20:35:23 2022 +0100 @@ -63,7 +63,10 @@ { assert(bt); - vic->msg.text[0] = _("Victory!"); + vic->text = _("Victory!"); + + vic->msg.lines = &vic->text; + vic->msg.linesz = 1; vic->msg.theme = bt->theme; vic->msg.flags = MESSAGE_FLAGS_AUTOMATIC | MESSAGE_FLAGS_FADEIN |
--- a/src/libmlk-rpg/rpg/battle-state-victory.h Wed Feb 16 08:56:02 2022 +0100 +++ b/src/libmlk-rpg/rpg/battle-state-victory.h Tue Feb 15 20:35:23 2022 +0100 @@ -26,6 +26,7 @@ union event; struct battle_state_victory { + const char *text; struct message msg; };
--- a/src/libmlk-rpg/rpg/message.c Wed Feb 16 08:56:02 2022 +0100 +++ b/src/libmlk-rpg/rpg/message.c Tue Feb 15 20:35:23 2022 +0100 @@ -86,10 +86,10 @@ unsigned int maxw = 0, w = 0; - for (size_t i = 0; i < MESSAGE_LINES_MAX; ++i) { - if (!msg->text[i]) + for (size_t i = 0; i < msg->linesz; ++i) { + if (!msg->lines[i]) continue; - if (font_query(THEME(msg)->fonts[THEME_FONT_INTERFACE], msg->text[i], &w, NULL) < 0) + if (font_query(THEME(msg)->fonts[THEME_FONT_INTERFACE], msg->lines[i], &w, NULL) < 0) panic(); if (w > maxw) maxw = w; @@ -106,7 +106,7 @@ const struct theme *th = THEME(msg); const unsigned int lh = font_height(th->fonts[THEME_FONT_INTERFACE]); - return (th->padding * 2) + (MESSAGE_LINES_MAX * lh) + ((MESSAGE_LINES_MAX - 1) * msg->spacing); + return (th->padding * 2) + (msg->linesz * lh) + ((msg->linesz - 1) * msg->spacing); } static void @@ -122,16 +122,16 @@ */ theme_shallow(&theme, THEME(msg)); - for (int i = 0; i < MESSAGE_LINES_MAX; ++i) { - if (!msg->text[i]) + for (size_t i = 0; i < msg->linesz; ++i) { + if (!msg->lines[i]) continue; - if (font_query(theme.fonts[THEME_FONT_INTERFACE], msg->text[i], &lw, &lh) < 0) + if (font_query(theme.fonts[THEME_FONT_INTERFACE], msg->lines[i], &lw, &lh) < 0) panic(); label.theme = &theme; label.x = theme.padding; label.y = theme.padding + (i * (lh + msg->spacing)); - label.text = msg->text[i]; + label.text = msg->lines[i]; label.flags = LABEL_FLAGS_SHADOW; if (label.x + lw > msg->w) @@ -202,7 +202,7 @@ msg->index--; break; case KEY_DOWN: - if (msg->index < 5 && msg->text[msg->index + 1]) + if (msg->index + 1 < msg->linesz && msg->lines[msg->index + 1]) msg->index++; break; case KEY_ENTER:
--- a/src/libmlk-rpg/rpg/message.h Wed Feb 16 08:56:02 2022 +0100 +++ b/src/libmlk-rpg/rpg/message.h Tue Feb 15 20:35:23 2022 +0100 @@ -28,9 +28,8 @@ union event; -#define MESSAGE_DELAY_DEFAULT (150) -#define MESSAGE_TIMEOUT_DEFAULT (5000) -#define MESSAGE_LINES_MAX (3) +#define MESSAGE_DELAY_DEFAULT (150) +#define MESSAGE_TIMEOUT_DEFAULT (5000) enum message_flags { MESSAGE_FLAGS_AUTOMATIC = (1 << 0), @@ -54,7 +53,8 @@ unsigned int spacing; unsigned int delay; unsigned int timeout; - const char *text[MESSAGE_LINES_MAX]; + const char * const *lines; + size_t linesz; unsigned int index; enum message_flags flags; enum message_state state;