summaryrefslogtreecommitdiff
path: root/meme
diff options
context:
space:
mode:
authorWerner Almesberger <werner@almesberger.net>2014-09-14 02:39:48 -0300
committerWerner Almesberger <werner@almesberger.net>2014-09-14 02:39:48 -0300
commite2dc06ebc066399e7274a6627127f725e1f07b17 (patch)
treef901ec1c8f177a1bb1c5c5e08427b62c0636d345 /meme
parentd29d97b74306e97353414023d768775c9593de81 (diff)
downloadmisc-e2dc06ebc066399e7274a6627127f725e1f07b17.tar.gz
misc-e2dc06ebc066399e7274a6627127f725e1f07b17.tar.bz2
misc-e2dc06ebc066399e7274a6627127f725e1f07b17.zip
meme/: show measurement results on screen
Diffstat (limited to 'meme')
-rw-r--r--meme/Makefile4
-rw-r--r--meme/gui.c76
-rw-r--r--meme/text.c94
-rw-r--r--meme/text.h30
4 files changed, 202 insertions, 2 deletions
diff --git a/meme/Makefile b/meme/Makefile
index 644d3c0..abbd6a8 100644
--- a/meme/Makefile
+++ b/meme/Makefile
@@ -13,9 +13,9 @@
include Makefile.c-common
CFLAGS = -Wall -Wshadow -g -O9
-OBJS = main.o stl.o mesh.o gui.o time.o plane.o
+OBJS = main.o stl.o mesh.o gui.o time.o plane.o text.o
CFLAGS += $(shell sdl-config --cflags)
-LDLIBS = $(shell sdl-config --libs) -lSDL_gfx -lm
+LDLIBS = $(shell sdl-config --libs) -lSDL_gfx -lSDL_Pango -lm
NAME = meme
diff --git a/meme/gui.c b/meme/gui.c
index 7c9ddf1..2e562b4 100644
--- a/meme/gui.c
+++ b/meme/gui.c
@@ -21,6 +21,7 @@
#include "time.h"
#include "mesh.h"
#include "plane.h"
+#include "text.h"
#define XRES 640
@@ -462,6 +463,35 @@ static const struct vertex *find_nearest(int x, int y)
}
+/* ----- Add text change --------------------------------------------------- */
+
+
+#include <stdarg.h>
+
+
+static void text_change(SDL_Surface *s, int x, int y, const char *fmt, ...)
+{
+ SDL_Surface *t;
+ SDL_Rect r;
+ va_list ap;
+
+ va_start(ap, fmt);
+ t = vtextf(fmt, ap);
+ va_end(ap);
+
+ change(s, x, y, t->w, t->h);
+ r.x = x;
+ r.y = y;
+ r.w = t->w;
+ r.h = t->h;
+ if (SDL_BlitSurface(t, NULL, s, &r)) {
+ fprintf(stderr, "SDL_BlitSurface failed\n");
+ exit(1);
+ }
+ SDL_FreeSurface(t);
+}
+
+
/* ----- Markers ----------------------------------------------------------- */
@@ -481,12 +511,58 @@ static void show_marker(SDL_Surface *s, const struct vertex *v, Uint32 color)
}
+static void show_meas(SDL_Surface *s, int x, int y,
+ const struct vertex *a, const struct vertex *b,
+ Uint32 color)
+{
+ float dx, dy, dz;
+
+ dx = abs(a->x - b->x) / 1000.0;
+ dy = abs(a->y - b->y) / 1000.0;
+ dz = abs(a->z - b->z) / 1000.0;
+
+#define LS 12
+
+ text_color(color);
+ if (dx) {
+ text_change(s, x, y, "X %7.3f", dx);
+ y += LS;
+ }
+ if (dy) {
+ text_change(s, x, y, "Y %7.3f", dy);
+ y += LS;
+ }
+ if (dz) {
+ text_change(s, x, y, "Z %7.3f", dz);
+ y += LS;
+ }
+ if (dx && dy) {
+ text_change(s, x, y, "XY %7.3f", hypotf(dx, dy));
+ y += LS;
+ }
+ if (dx && dz) {
+ text_change(s, x, y, "XZ %7.3f", hypotf(dx, dz));
+ y += LS;
+ }
+ if (dy && dz) {
+ text_change(s, x, y, "YZ %7.3f", hypotf(dy, dz));
+ y += LS;
+ }
+ if (dx && dy && dz) {
+ text_change(s, x, y, "3D %7.3f", hypotf(hypotf(dx, dy), dz));
+ y += LS;
+ }
+}
+
+
static void show_markers(SDL_Surface *s)
{
if (mark_a)
show_marker(s, mark_a, MARK_A_RGBA);
if (mark_b)
show_marker(s, mark_b, MARK_B_RGBA);
+ if (mark_a && mark_b)
+ show_meas(s, 10, 10, mark_a, mark_b, 0x000000ff);
}
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;
+}
diff --git a/meme/text.h b/meme/text.h
new file mode 100644
index 0000000..f9e84de
--- /dev/null
+++ b/meme/text.h
@@ -0,0 +1,30 @@
+/*
+ * text.h - 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.
+ */
+
+
+#ifndef TEXT_H
+#define TEXT_H
+
+#include <stdarg.h>
+
+#include "SDL.h"
+
+
+extern const char *text_font;
+
+
+void text_color(Uint32 rgba);
+SDL_Surface *text(const char *s);
+SDL_Surface *textf(const char *fmt, ...);
+SDL_Surface *vtextf(const char *fmt, va_list ap);
+
+#endif /* !TEXT_H */