diff options
author | P. J. McDermott <pj@pehjota.net> | 2021-08-06 19:20:13 (EDT) |
---|---|---|
committer | P. J. McDermott <pj@pehjota.net> | 2021-08-06 19:20:13 (EDT) |
commit | 05aac4b1dd03e95049cf134c8f64c31312f2f9e5 (patch) | |
tree | a554f07d639971e6f4f3ad023567064c71ffbf5f | |
parent | 8473a1dfde3193e989c4d1fd371db99cf5a104c6 (diff) | |
download | mazefight-05aac4b1dd03e95049cf134c8f64c31312f2f9e5.zip mazefight-05aac4b1dd03e95049cf134c8f64c31312f2f9e5.tar.gz mazefight-05aac4b1dd03e95049cf134c8f64c31312f2f9e5.tar.bz2 |
tk: Maintain static text input buffers
-rw-r--r-- | src/tk/text.c | 38 |
1 files changed, 24 insertions, 14 deletions
diff --git a/src/tk/text.c b/src/tk/text.c index dedad4b..e5ae0b1 100644 --- a/src/tk/text.c +++ b/src/tk/text.c @@ -35,6 +35,7 @@ struct mftk_text { int w; int h; char *val; + char *curval; TTF_Font *font; int line_skip; int ascent; @@ -164,7 +165,6 @@ _mftk_text_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y) { struct mftk_text *t = (struct mftk_text *) w; SDL_Rect rect; - char *val; int cur_x; if (t->texture == NULL && _mftk_text_render_val(t, renderer) < 0) { @@ -176,15 +176,9 @@ _mftk_text_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y) rect.w = t->w; rect.h = t->h; - val = mf_strdup(t->val); - if (val == NULL) { - SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, - "Couldn't render widget: %s", - strerror(errno)); - return -1; - } - val[t->cur] = '\0'; - TTF_SizeUTF8(t->font, val, &cur_x, NULL); + memcpy(t->curval, t->val, t->cur); + t->curval[t->cur] = '\0'; + TTF_SizeUTF8(t->font, t->curval, &cur_x, NULL); if (SDL_RenderCopy(renderer, t->texture, NULL, &rect) < 0 || SDL_SetRenderDrawColor(renderer, @@ -195,7 +189,6 @@ _mftk_text_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y) SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't render widget: %s", SDL_GetError()); - free(val); return -1; } if (w->focused == SDL_TRUE && SDL_RenderDrawLine(renderer, @@ -203,11 +196,9 @@ _mftk_text_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y) SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't render widget: %s", SDL_GetError()); - free(val); return -1; } - free(val); return 0; } @@ -217,6 +208,7 @@ _mftk_text_destroy(struct mftk_widget *w) struct mftk_text *t = (struct mftk_text *) w; free(t->val); + free(t->curval); SDL_DestroyTexture(t->texture); } @@ -235,13 +227,31 @@ mftk_text_new(char min_char, char max_char, size_t len, const char *val, t->max_char = max_char; t->len = len; t->cur = strlen(val); - t->val = mf_strdup(val); t->font = font; t->line_skip = TTF_FontLineSkip(font); t->ascent = TTF_FontAscent (font); t->color = color; t->texture = NULL; + t->val = calloc(len + 1, sizeof(*t->val)); + if (t->val == NULL) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "Couldn't create widget: %s", + strerror(errno)); + free(w); + return NULL; + } + t->curval = calloc(len + 1, sizeof(*t->curval)); + if (t->curval == NULL) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "Couldn't create widget: %s", + strerror(errno)); + free(t->curval); + free(w); + return NULL; + } + memcpy(t->val, val, t->cur); + w->w = 0; for (ch = min_char; ch <= max_char; ++ch) { if (TTF_GlyphMetrics(font, ch, NULL, NULL, NULL, NULL, &advance) |