From 28a4cda6fd60f9a821f89ee7d369f49b58fbd95b Mon Sep 17 00:00:00 2001 From: Steve Date: Sun, 6 Dec 2015 15:03:44 +0000 Subject: [PATCH] Set size of message box, based on amount of text. --- src/battle/messageBox.c | 20 ++++++++++++++++++-- src/battle/messageBox.h | 1 + src/draw/text.c | 32 ++++++++++++++++++++++++++++++++ src/structs.h | 1 + 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/battle/messageBox.c b/src/battle/messageBox.c index c6f93d5..60de4e7 100644 --- a/src/battle/messageBox.c +++ b/src/battle/messageBox.c @@ -20,6 +20,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "messageBox.h" +static void calculateMessageBoxHeight(MessageBox *msg); + static MessageBox head; static MessageBox *tail; @@ -79,6 +81,15 @@ void doMessageBox(void) } } +static void calculateMessageBoxHeight(MessageBox *msg) +{ + limitTextWidth(575); + + msg->height = getWrappedTextHeight(msg->body, 18); + + limitTextWidth(0); +} + int showingMessageBoxes(void) { return head.next != NULL; @@ -91,9 +102,14 @@ void drawMessageBox(void) if (msg && msg->time > 0) { + if (!msg->height) + { + calculateMessageBoxHeight(msg); + } + r.y = 50; r.w = 650; - r.h = 110; + r.h = msg->height + 40; r.x = (SCREEN_WIDTH - r.w) / 2; SDL_SetRenderDrawBlendMode(app.renderer, SDL_BLENDMODE_BLEND); @@ -105,7 +121,7 @@ void drawMessageBox(void) drawText(r.x + 10, r.y + 5, 18, TA_LEFT, colors.cyan, msg->title); - limitTextWidth(550); + limitTextWidth(575); drawText(r.x + 10, r.y + 30, 18, TA_LEFT, colors.white, msg->body); diff --git a/src/battle/messageBox.h b/src/battle/messageBox.h index 08b0e4d..c863522 100644 --- a/src/battle/messageBox.h +++ b/src/battle/messageBox.h @@ -21,6 +21,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "../common.h" extern void drawText(int x, int y, int size, int align, SDL_Color c, const char *format, ...); +extern int getWrappedTextHeight(char *text, int size); extern void limitTextWidth(int width); extern void playSound(int sound); diff --git a/src/draw/text.c b/src/draw/text.c index 8fa5bf5..c6ead8f 100644 --- a/src/draw/text.c +++ b/src/draw/text.c @@ -142,6 +142,38 @@ static void drawTextSplit(int x, int y, int size, int align, SDL_Color c, char * drawTextNormal(x, y, size, align, c, drawTextBuffer); } +int getWrappedTextHeight(char *text, int size) +{ + char textBuffer[MAX_DESCRIPTION_LENGTH]; + char *token; + int w, h, currentWidth; + int y; + + STRNCPY(textBuffer, text, MAX_DESCRIPTION_LENGTH); + + token = strtok(textBuffer, " "); + + y = 0; + currentWidth = 0; + + while (token) + { + textSize(token, size, &w, &h); + + if (currentWidth + w > maxWidth) + { + currentWidth = 0; + y += h; + } + + currentWidth += w; + + token = strtok(NULL, " "); + } + + return y + h; +} + static void textSize(char *text, int size, int *w, int *h) { if (!font[size]) diff --git a/src/structs.h b/src/structs.h index ea7cdc6..499ecd9 100644 --- a/src/structs.h +++ b/src/structs.h @@ -296,6 +296,7 @@ struct MessageBox { char title[MAX_NAME_LENGTH]; char body[MAX_DESCRIPTION_LENGTH]; int time; + int height; MessageBox *next; };