summaryrefslogtreecommitdiff
path: root/meme/text.c
diff options
context:
space:
mode:
authorWerner Almesberger <werner@almesberger.net>2014-09-14 05:39:48 (GMT)
committerWerner Almesberger <werner@almesberger.net>2014-09-14 05:39:48 (GMT)
commite2dc06ebc066399e7274a6627127f725e1f07b17 (patch)
treef901ec1c8f177a1bb1c5c5e08427b62c0636d345 /meme/text.c
parentd29d97b74306e97353414023d768775c9593de81 (diff)
downloadmisc-e2dc06ebc066399e7274a6627127f725e1f07b17.zip
misc-e2dc06ebc066399e7274a6627127f725e1f07b17.tar.gz
misc-e2dc06ebc066399e7274a6627127f725e1f07b17.tar.bz2
meme/: show measurement results on screen
Diffstat (limited to 'meme/text.c')
-rw-r--r--meme/text.c94
1 files changed, 94 insertions, 0 deletions
diff --git a/meme/text.c b/meme/text.c
new file mode 100644
index 0000000..4a4f33c
--- /dev/null
+++ b/meme/text.c
@@ -0,0 +1,94 @@
+/*
+ * text.c - Text rendering
+ *
+ * Written 2014 by Werner Almesberger
+ * Copyright 2014 by Werner Almesberger
+ *
+ * This program 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 2 of the License, or
+ * (at your option) any later version.
+ */
+
+
+#include <stdarg.h>
+#include <stdio.h>
+
+#include "SDL.h"
+#include "SDL_Pango.h"
+
+#include "text.h"
+
+
+const char *text_font = "Courier Bold 10";
+
+
+static SDLPango_Matrix matrix = {
+ .m = {
+ { 255, 0, 0, 0 },
+ { 255, 0, 0, 0 },
+ { 255, 0, 0, 0 },
+ { 0, 255, 0, 0 }
+ }
+};
+
+
+void text_color(Uint32 rgba)
+{
+ matrix.m[0][1] = rgba >> 24;
+ matrix.m[1][1] = rgba >> 16;
+ matrix.m[2][1] = rgba >> 8;
+ matrix.m[3][1] = rgba;
+}
+
+
+SDL_Surface *text(const char *s)
+{
+ SDLPango_Context *pango;
+ SDL_Surface *tmp, *surf;
+ int w, h;
+
+ pango = SDLPango_CreateContext_GivenFontDesc(text_font);
+ SDLPango_SetDefaultColor(pango, &matrix);
+ SDLPango_SetMinimumSize(pango, 0, 0);
+ SDLPango_SetMarkup(pango, s, -1);
+
+ // surf = SDLPango_CreateSurfaceDrawWithLayout(pango, NULL);
+
+ w = SDLPango_GetLayoutWidth(pango);
+ h = SDLPango_GetLayoutHeight(pango);
+
+ tmp = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 0, 0, 0, 0, 0);
+ surf = SDL_DisplayFormat(tmp);
+ SDL_FreeSurface(tmp);
+
+ SDLPango_Draw(pango, surf, 0, 0);
+
+ SDLPango_FreeContext(pango);
+
+ return surf;
+}
+
+
+SDL_Surface *vtextf(const char *fmt, va_list ap)
+{
+ char *tmp;
+ int ignore;
+
+ ignore = vasprintf(&tmp, fmt, ap);
+ (void) ignore;
+
+ return text(tmp);
+}
+
+SDL_Surface *textf(const char *fmt, ...)
+{
+ va_list ap;
+ SDL_Surface *surf;
+
+ va_start(ap, fmt);
+ surf = vtextf(fmt, ap);
+ va_end(ap);
+
+ return surf;
+}