summaryrefslogtreecommitdiffstats
path: root/src/tk
diff options
context:
space:
mode:
Diffstat (limited to 'src/tk')
-rw-r--r--src/tk/label.c137
-rw-r--r--src/tk/local.mk1
2 files changed, 138 insertions, 0 deletions
diff --git a/src/tk/label.c b/src/tk/label.c
new file mode 100644
index 0000000..e9ae38c
--- /dev/null
+++ b/src/tk/label.c
@@ -0,0 +1,137 @@
+/*
+ * Copyright (C) 2021 P. J. McDermott
+ *
+ * This file is part of Maze Fight
+ *
+ * Maze Fight is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Maze Fight is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Maze Fight. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <SDL.h>
+#include <SDL_ttf.h>
+#include "../tk.h"
+#include "widget.h"
+
+struct mftk_label {
+ struct mftk_widget parent;
+ SDL_Texture *texture;
+ int y;
+ int h;
+};
+
+static void
+_mftk_label_layout(struct mftk_widget *w __attribute__((__unused__)))
+{
+ /* Size set in constructor */
+}
+
+static int
+_mftk_label_event(struct mftk_widget *w __attribute__((__unused__)),
+ SDL_Event *e __attribute__((__unused__)))
+{
+ /* No events */
+ return 0;
+}
+
+static int
+_mftk_label_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y)
+{
+ struct mftk_label *l = (struct mftk_label *) w;
+ SDL_Rect rect;
+
+ rect.x = x;
+ rect.y = l->y + y;
+ rect.w = w->w;
+ rect.h = l->h;
+
+ if (SDL_RenderCopy(renderer, l->texture, NULL, &rect) < 0) {
+ SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
+ "Couldn't render widget: %s",
+ SDL_GetError());
+ return -1;
+ }
+
+ return 0;
+}
+
+static void
+_mftk_label_destroy(struct mftk_widget *w)
+{
+ struct mftk_label *l = (struct mftk_label *) w;
+
+ SDL_DestroyTexture(l->texture);
+}
+
+struct mftk_widget *
+mftk_label_new(TTF_Font *font, const char *text, SDL_Color *color,
+ SDL_Renderer *renderer)
+{
+ struct mftk_widget *w;
+ struct mftk_label *l;
+ SDL_Surface *surface;
+ int line_skip;
+ int ascent;
+ int max_y;
+ const char *c;
+ int glyph_max_y;
+
+ if ((w = mftk_widget_new(sizeof(*l))) == NULL) return NULL;
+ l = (struct mftk_label *) w;
+ w->layout = &_mftk_label_layout;
+ w->event = &_mftk_label_event;
+ w->render = &_mftk_label_render;
+ w->destroy = &_mftk_label_destroy;
+
+ surface = TTF_RenderUTF8_Blended(font, text, *color);
+ if (surface == NULL) {
+ SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
+ "Couldn't create surface: %s",
+ TTF_GetError());
+ free(l);
+ return NULL;
+ }
+
+ l->texture = SDL_CreateTextureFromSurface(renderer, surface);
+ if (l->texture == NULL) {
+ SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
+ "Couldn't create texture: %s",
+ SDL_GetError());
+ SDL_FreeSurface(surface);
+ free(l);
+ return NULL;
+ }
+
+ line_skip = TTF_FontLineSkip(font);
+ ascent = TTF_FontAscent (font);
+ max_y = 0;
+ for (c = text; *c != '\0'; ++c) {
+ if (TTF_GlyphMetrics(font, *c, NULL, NULL, NULL, &glyph_max_y,
+ NULL) < 0) {
+ SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
+ "Couldn't get glyph metrics: %s",
+ SDL_GetError());
+ continue;
+ }
+ if (glyph_max_y > max_y) {
+ max_y = glyph_max_y;
+ }
+ }
+ w->w = surface->w;
+ w->h = line_skip;
+ l->y = (line_skip - ascent) / 2 + (ascent - max_y);
+ l->h = surface->h;
+
+ SDL_FreeSurface(surface);
+
+ return w;
+}
diff --git a/src/tk/local.mk b/src/tk/local.mk
index 39b1226..0c37299 100644
--- a/src/tk/local.mk
+++ b/src/tk/local.mk
@@ -1,3 +1,4 @@
mazefight_SOURCES += \
+ %reldir%/label.c \
%reldir%/widget.c \
%reldir%/widget.h