lnvis

nanovg lightning network visualizer
git clone git://jb55.com/lnvis
Log | Files | Refs | README | LICENSE

commit 2578bf3ce0ff67bef9712aed45b924d3acb1c7c9
parent e94e3954873743b96ae7daf14511de1a5132559f
Author: William Casarin <jb55@jb55.com>
Date:   Fri,  3 Aug 2018 15:30:59 -0700

progress

Diffstat:
M.gitignore | 1+
MMakefile | 7++++++-
Mdefs.h | 2++
Aln.h | 25+++++++++++++++++++++++++
Mmain.c | 33+++++++++++++++++++++++----------
Arender.c | 37+++++++++++++++++++++++++++++++++++++
Arender.h | 9+++++++++
7 files changed, 103 insertions(+), 11 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,3 +1,4 @@ *.o *.d /lnvis +/TAGS diff --git a/Makefile b/Makefile @@ -8,8 +8,10 @@ LDFLAGS = -lglfw -lGL SRCS = main.c -SRCS += demo.c + +SRCS += render.c SRCS += perf.c +SRCS += demo.c SRCS += $(wildcard deps/*/*.c) OBJS = $(SRCS:.c=.o) @@ -24,6 +26,9 @@ include $(OBJS:.o=.d) $(BIN): $(OBJS) $(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@ +TAGS: fake + etags $(SRCS) + format: fake clang-format -i *.c diff --git a/defs.h b/defs.h @@ -2,6 +2,8 @@ #ifndef LNVIS_DEFS_H #define LNVIS_DEFS_H +#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) + typedef unsigned char u8; typedef signed char s8; diff --git a/ln.h b/ln.h @@ -0,0 +1,25 @@ + +#ifndef LNVIS_LN_H +#define LNVIS_LN_H + +#include "nanovg/nanovg.h" + +struct node { + const char *alias; + const char *nodeid; + + union { + float rgba[4]; + struct { + float r, g, b, a; + }; + } color; +}; + +struct ln { + NVGcontext *vg; + struct node *nodes; + int node_count; +}; + +#endif /* LNVIS_LN_H */ diff --git a/main.c b/main.c @@ -1,5 +1,6 @@ -#include <stdio.h> #include "gl.h" + +#include <stdio.h> #ifdef NANOVG_GLEW #include <GL/glew.h> #endif @@ -8,16 +9,18 @@ #include "nanovg/nanovg.h" #define NANOVG_GL2_IMPLEMENTATION #include "nanovg/nanovg_gl.h" -#include "demo.h" + +#include "defs.h" #include "perf.h" +#include "demo.h" +#include "render.h" +#include "ln.h" void errorcb(int error, const char *desc) { printf("GLFW error %d: %s\n", error, desc); } -int blowup = 0; -int screenshot = 0; int premult = 0; static void key(GLFWwindow *window, int key, int scancode, int action, int mods) @@ -26,10 +29,8 @@ static void key(GLFWwindow *window, int key, int scancode, int action, int mods) NVG_NOTUSED(mods); if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) glfwSetWindowShouldClose(window, GL_TRUE); - if (key == GLFW_KEY_SPACE && action == GLFW_PRESS) - blowup = !blowup; - if (key == GLFW_KEY_S && action == GLFW_PRESS) - screenshot = 1; + /* if (key == GLFW_KEY_SPACE && action == GLFW_PRESS) */ + /* blowup = !blowup; */ if (key == GLFW_KEY_P && action == GLFW_PRESS) premult = !premult; } @@ -41,6 +42,15 @@ int main() NVGcontext *vg = NULL; PerfGraph fps; double prevt = 0; + struct ln ln; + + struct node test_nodes[] = { { + .alias = "@jb55", + .color = { { 255, 0, 0, 0 } }, + } }; + + ln.nodes = test_nodes; + ln.node_count = ARRAY_SIZE(test_nodes); if (!glfwInit()) { printf("Failed to init GLFW."); @@ -84,10 +94,12 @@ int main() return -1; } + ln.vg = vg; + if (loadDemoData(vg, &data) == -1) return -1; - glfwSwapInterval(0); + glfwSwapInterval(1); glfwSetTime(0); prevt = glfwGetTime(); @@ -121,7 +133,8 @@ int main() nvgBeginFrame(vg, winWidth, winHeight, pxRatio); - renderDemo(vg, mx, my, winWidth, winHeight, t, blowup, &data); + /* renderDemo(vg, mx, my, winWidth, winHeight, t, 1, &data); */ + render_ln(&ln); renderGraph(vg, 5, 5, &fps); nvgEndFrame(vg); diff --git a/render.c b/render.c @@ -0,0 +1,37 @@ + +#include "render.h" +#include <stdio.h> +#include "nanovg/nanovg.h" + +void draw_node(NVGcontext *vg, struct node *node) +{ + const float r = 50.0; + const float pos = 100.0; + + const float h = r; + const float w = r; + const float x = pos; + const float y = pos; + const float kr = (int)(h * 25.0f); + const float cy = y + (int)(h * 5.0f); + + NVGpaint bg; + + nvgSave(vg); + + bg = nvgRadialGradient(vg, x + (int)(pos * w), cy + 1, kr - 3, kr + 3, + nvgRGBA(0, 0, 0, 64), nvgRGBA(0, 0, 0, 0)); + nvgBeginPath(vg); + nvgCircle(vg, pos, pos, r); + nvgPathWinding(vg, NVG_HOLE); + nvgFillPaint(vg, bg); + nvgFill(vg); + nvgText(vg, x + r / 2.0, y + r + 5.0, node->alias, NULL); + nvgRestore(vg); +} + +void render_ln(struct ln *ln) +{ + for (int i = 0; i < ln->node_count; i++) + draw_node(ln->vg, &ln->nodes[i]); +} diff --git a/render.h b/render.h @@ -0,0 +1,9 @@ + +#ifndef LNVIS_RENDER_H +#define LNVIS_RENDER_H + +#include "ln.h" + +void render_ln(struct ln *ln); + +#endif /* LNVIS_RENDER_H */