ref: 0be6a77dcacc2ab434f6e413fba9c4a57ada0982
parent: f5deb63c8db7b61b29d19ad2fc4128bb8d62f603
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Thu Jan 2 07:25:06 EST 2020
port to Plan 9
--- a/demo/common.c
+++ b/demo/common.c
@@ -1,5 +1,12 @@
+#ifdef __plan9__
+#include <u.h>
+#include <libc.h>
+#define sprintf sprint
+#define NULL nil
+#else
#include <stdio.h>
#include <string.h>
+#endif
#include "common.h"
--- /dev/null
+++ b/demo/mkfile
@@ -1,0 +1,17 @@
+</$objtype/mkfile
+
+TARG=demo
+
+CFLAGS=$CFLAGS -D__plan9__ -p -I../src
+OFILES=\
+ common.$O\
+ microui.$O\
+ plan9.$O\
+ renderer_plan9.$O\
+
+default:V: all
+
+microui.$O: ../src/microui.c
+ $CC $CFLAGS $prereq
+
+</sys/src/cmd/mkone
--- /dev/null
+++ b/demo/plan9.c
@@ -1,0 +1,121 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include <mouse.h>
+#include <keyboard.h>
+#include <thread.h>
+#include "common.h"
+
+static Mousectl *mctl;
+static Keyboardctl *kctl;
+
+void
+threadmain(int argc, char **argv)
+{
+ Rune key;
+ Mouse m;
+ Alt a[] = {
+ { nil, &m, CHANRCV },
+ { nil, nil, CHANRCV },
+ { nil, &key, CHANRCV },
+ { nil, nil, CHANEND },
+ };
+ int oldbuttons, b, nkey, entering;
+ char text[5];
+ mu_Command *cmd;
+ mu_Context *ctx;
+
+ USED(argc); USED(argv);
+
+ if (initdraw(nil, nil, "microui demo") < 0)
+ sysfatal("initdraw: %r");
+ if ((mctl = initmouse(nil, screen)) == nil)
+ sysfatal("initmouse: %r");
+ if ((kctl = initkeyboard(nil)) == nil)
+ sysfatal("initkeyboard: %r");
+
+ a[0].c = mctl->c;
+ a[1].c = mctl->resizec;
+ a[2].c = kctl->c;
+
+ srand(time(0));
+ threadsetname("microui demo");
+
+ ctx = malloc(sizeof(mu_Context));
+ r_init();
+
+ /* init microui */
+ mu_init(ctx);
+ ctx->text_width = r_get_text_width;
+ ctx->text_height = r_get_text_height;
+ ctx->style->font = display->defaultfont;
+
+ oldbuttons = 0;
+ entering = 1;
+ for (;;) {
+ /* process frame */
+ process_frame(ctx);
+
+ /* render */
+ r_clear(mu_color(bg[0], bg[1], bg[2], 255));
+ cmd = nil;
+ while (mu_next_command(ctx, &cmd)) {
+ switch (cmd->type) {
+ case MU_COMMAND_TEXT: r_draw_text(ctx->style->font, cmd->text.str, cmd->text.pos, cmd->text.color); break;
+ case MU_COMMAND_RECT: r_draw_rect(cmd->rect.rect, cmd->rect.color); break;
+ case MU_COMMAND_ICON: r_draw_icon(cmd->icon.id, cmd->icon.rect, cmd->icon.color); break;
+ case MU_COMMAND_CLIP: r_set_clip_rect(cmd->clip.rect); break;
+ }
+ }
+ r_present();
+
+ if (entering) {
+ entering = 0;
+ continue;
+ }
+
+ switch (alt(a)) {
+ case 0: /* mouse */
+ m.xy.x -= screen->r.min.x;
+ m.xy.y -= screen->r.min.y;
+ mu_input_mousemove(ctx, m.xy.x, m.xy.y);
+ if ((b = (m.buttons & 1)) != (oldbuttons & 1))
+ (b ? mu_input_mousedown : mu_input_mouseup)(ctx, m.xy.x, m.xy.y, MU_MOUSE_LEFT);
+ else if ((b = (m.buttons & 2)) != (oldbuttons & 2))
+ (b ? mu_input_mousedown : mu_input_mouseup)(ctx, m.xy.x, m.xy.y, MU_MOUSE_MIDDLE);
+ else if ((b = (m.buttons & 4)) != (oldbuttons & 4))
+ (b ? mu_input_mousedown : mu_input_mouseup)(ctx, m.xy.x, m.xy.y, MU_MOUSE_RIGHT);
+ oldbuttons = m.buttons;
+ break;
+
+ case 1: /* resize */
+ getwindow(display, Refnone);
+ break;
+
+ case 2: /* keyboard */
+ nkey = -1;
+ switch (key) {
+ case Kdel: goto end;
+ case Kshift: nkey = MU_KEY_SHIFT; break;
+ case Kbs: nkey = MU_KEY_BACKSPACE; break;
+ case '\n': nkey = MU_KEY_RETURN; break;
+ default:
+ if (key < 0xf000 || key > 0xffff) {
+ memset(text, 0, sizeof(text));
+ if (runetochar(text, &key) > 0)
+ mu_input_text(ctx, text);
+ }
+ break;
+ }
+ if (nkey >= 0) {
+ mu_input_keydown(ctx, nkey);
+ mu_input_keyup(ctx, nkey);
+ }
+ }
+ }
+
+end:
+ closemouse(mctl);
+ closekeyboard(kctl);
+ threadexitsall(nil);
+}
--- /dev/null
+++ b/demo/renderer_plan9.c
@@ -1,0 +1,148 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include "common.h"
+#include "atlas.inl"
+
+enum
+{
+ Maxcolors = 64,
+};
+
+static u32int rgba[Maxcolors];
+static u16int lastused[Maxcolors];
+static Image *colors[Maxcolors];
+static int numcolors = 0;
+static u16int frame = 0;
+static Image *atlasimage;
+
+static Image *
+getcolor(mu_Color color)
+{
+ u32int c;
+ int i;
+ u16int diff, leasti;
+
+ diff = 0;
+ leasti = 0;
+ c = color.r<<24 | color.g<<16 | color.b<<8 | color.a;
+ if (c == DBlack)
+ return display->black;
+ if (c == DWhite)
+ return display->white;
+ for (i = 0; i < numcolors; i++) {
+ if (rgba[i] == c) {
+ lastused[i] = frame;
+ return colors[i];
+ }
+ if (diff < frame - lastused[i]) {
+ diff = frame - lastused[i];
+ leasti = i;
+ }
+ }
+ if (i >= nelem(colors)) {
+ freeimage(colors[leasti]);
+ rgba[leasti] = c;
+ colors[leasti] = allocimage(display, (Rectangle){ZP, (Point){1, 1}}, RGBA32, 1, c);
+ lastused[leasti] = frame;
+ return colors[leasti];
+ }
+
+ rgba[numcolors] = c;
+ colors[numcolors] = allocimage(display, (Rectangle){ZP, (Point){1, 1}}, RGBA32, 1, c);
+ return colors[numcolors++];
+}
+
+static Rectangle
+screenrect(mu_Rect rect)
+{
+ Rectangle r;
+ r.min = screen->r.min;
+ r.min.x += rect.x;
+ r.min.y += rect.y;
+ r.max = r.min;
+ r.max.x += rect.w;
+ r.max.y += rect.h;
+ return r;
+}
+
+void
+r_init(void)
+{
+ u8int *b;
+ Rectangle r = {ZP, (Point){ATLAS_WIDTH, ATLAS_HEIGHT}};
+ int i;
+
+ b = malloc(sizeof(atlas_texture)*4);
+ for (i = 0; i < sizeof(atlas_texture); i++) {
+ b[i*4+0] = atlas_texture[i];
+ b[i*4+1] = atlas_texture[i];
+ b[i*4+2] = atlas_texture[i];
+ b[i*4+3] = atlas_texture[i];
+ }
+ atlasimage = allocimage(display, r, RGBA32, 1, DWhite);
+ if (loadimage(atlasimage, r, b, sizeof(atlas_texture)*4) < 0)
+ sysfatal("failed to load atlas");
+}
+
+void
+r_draw_rect(mu_Rect rect, mu_Color color)
+{
+ draw(screen, screenrect(rect), getcolor(color), nil, ZP);
+}
+
+void
+r_draw_text(mu_Font font, const char *text, mu_Vec2 pos, mu_Color color) {
+ Point p;
+
+ p = screen->r.min;
+ p.x += pos.x;
+ p.y += pos.y;
+ string(screen, p, getcolor(color), ZP, font, text);
+}
+
+void
+r_draw_icon(int id, mu_Rect rect, mu_Color color)
+{
+ Rectangle r;
+ USED(color); /* FIXME no colors */
+ rect.x += (rect.w - atlas[id].w) / 2;
+ rect.y += (rect.h - atlas[id].h) / 2;
+ rect.w = atlas[id].w;
+ rect.h = atlas[id].h;
+ r = screenrect(rect);
+ draw(screen, r, atlasimage, nil, (Point){atlas[id].x, atlas[id].y});
+}
+
+int
+r_get_text_width(mu_Font font, const char *text, int len)
+{
+ if (len < 0)
+ len = strlen(text);
+ return stringnwidth(font, text, len);
+}
+
+int
+r_get_text_height(mu_Font font)
+{
+ return ((Font*)font)->height;
+}
+
+void
+r_set_clip_rect(mu_Rect rect)
+{
+ replclipr(screen, 0, screenrect(rect));
+}
+
+void
+r_clear(mu_Color color)
+{
+ draw(screen, screen->r, getcolor(color), nil, ZP);
+}
+
+void
+r_present(void)
+{
+ flushimage(display, 1);
+ frame++;
+}
--- a/demo/sdl2.c
+++ b/demo/sdl2.c
@@ -22,7 +22,7 @@
int main(int argc, char **argv) {
- mu_Font dummy = {0};
+ mu_Font dummy = NULL;
unused(argc); unused(argv);