Nuklear/demo/x11_rawfb/main.c
Patrick Rudolph 2eb72b26e4 Add software rasterizer library and demo.
The software rasterizer is to be used with raw framebuffer
devices, where no GPU or X11 is available.
The demo emulates a raw framebuffer on X11 using XShmImage / XImage.

Features implemented:
* Drawing primitives
* Drawing polygons (using Darel Rex Finley algorithm)
* Drawing arcs and circles (using Bresenham's elipses algorithm)
* Drawing images using nearest filtering
* Bounds check on every operation
* Fastpath for lines
* Font rendering using nearest filtering
* Window resize
* Thread safe implementation by using a context
* Fixed lower and upper scissors on fast-path
* Adapted coding style to nuklear's style
* Implemented text scissors

Color formats:
Define one of them at compile time.
* RAWFB_RGBX_8888 (32bpp)
* RAWFB_XRGB_8888 (32bpp)

Tested:
The library has been tested on Lenovo Thinkpad T500 and is able to render
more than 30fps on a single core with no further optimizations and VSNYC enabled.

TODO:
* Improve font rendering by using filters.
* Account font foreground color.

Usage:
The raw framebuffer library needs a "texture" that holds the prerendered
font data. The texture is used at runtime to blit the letters onto screen.
You have to provide the framebuffer address, dimension and pitch.

Signed-off-by: Patrick Rudolph <siro@das-labor.org>
2017-10-07 12:08:24 +02:00

222 lines
6.2 KiB
C

/* nuklear - v1.17 - public domain */
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <limits.h>
#include <math.h>
#include <sys/time.h>
#include <unistd.h>
#include <time.h>
#define NK_INCLUDE_FIXED_TYPES
#define NK_INCLUDE_STANDARD_IO
#define NK_INCLUDE_STANDARD_VARARGS
#define NK_INCLUDE_DEFAULT_ALLOCATOR
#define NK_IMPLEMENTATION
#define NK_XLIBSHM_IMPLEMENTATION
#define NK_RAWFB_IMPLEMENTATION
#define NK_INCLUDE_FONT_BAKING
#define NK_INCLUDE_DEFAULT_FONT
#define NK_INCLUDE_SOFTWARE_FONT
#include "../../nuklear.h"
#include "nuklear_rawfb.h"
#include "nuklear_xlib.h"
#define DTIME 20
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define UNUSED(a) (void)a
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) < (b) ? (b) : (a))
#define LEN(a) (sizeof(a)/sizeof(a)[0])
typedef struct XWindow XWindow;
struct XWindow {
Display *dpy;
Window root;
Visual *vis;
Colormap cmap;
XWindowAttributes attr;
XSetWindowAttributes swa;
Window win;
int screen;
unsigned int width;
unsigned int height;
};
static void
die(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fputs("\n", stderr);
exit(EXIT_FAILURE);
}
static void*
xcalloc(size_t siz, size_t n)
{
void *ptr = calloc(siz, n);
if (!ptr) die("Out of memory\n");
return ptr;
}
static long
timestamp(void)
{
struct timeval tv;
if (gettimeofday(&tv, NULL) < 0) return 0;
return (long)((long)tv.tv_sec * 1000 + (long)tv.tv_usec/1000);
}
static void
sleep_for(long t)
{
struct timespec req;
const time_t sec = (int)(t/1000);
const long ms = t - (sec * 1000);
req.tv_sec = sec;
req.tv_nsec = ms * 1000000L;
while(-1 == nanosleep(&req, &req));
}
/* ===============================================================
*
* EXAMPLE
*
* ===============================================================*/
/* This are some code examples to provide a small overview of what can be
* done with this library. To try out an example uncomment the include
* and the corresponding function. */
/*#include "../style.c"*/
/*#include "../calculator.c"*/
#include "../overview.c"
#include "../node_editor.c"
/* ===============================================================
*
* DEMO
*
* ===============================================================*/
int
main(void)
{
long dt;
long started;
int running = 1;
int status;
XWindow xw;
struct rawfb_context *rawfb;
void *fb = NULL;
unsigned char tex_scratch[512 * 512];
XSizeHints hints;
/* X11 */
memset(&xw, 0, sizeof xw);
xw.dpy = XOpenDisplay(NULL);
if (!xw.dpy) die("Could not open a display; perhaps $DISPLAY is not set?");
xw.root = DefaultRootWindow(xw.dpy);
xw.screen = XDefaultScreen(xw.dpy);
xw.vis = XDefaultVisual(xw.dpy, xw.screen);
xw.cmap = XCreateColormap(xw.dpy,xw.root,xw.vis,AllocNone);
xw.swa.colormap = xw.cmap;
xw.swa.event_mask =
ExposureMask | KeyPressMask | KeyReleaseMask |
ButtonPress | ButtonReleaseMask| ButtonMotionMask |
Button1MotionMask | Button3MotionMask | Button4MotionMask | Button5MotionMask|
PointerMotionMask | KeymapStateMask | EnterWindowMask | LeaveWindowMask;
xw.win = XCreateWindow(xw.dpy, xw.root, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, 0,
XDefaultDepth(xw.dpy, xw.screen), InputOutput,
xw.vis, CWEventMask | CWColormap, &xw.swa);
XStoreName(xw.dpy, xw.win, "X11");
XMapWindow(xw.dpy, xw.win);
XGetWindowAttributes(xw.dpy, xw.win, &xw.attr);
xw.width = (unsigned int)xw.attr.width;
xw.height = (unsigned int)xw.attr.height;
/* Framebuffer emulator */
status = nk_xlib_init(xw.dpy, xw.vis, xw.screen, xw.win, xw.width, xw.height, &fb);
if (!status || !fb)
return 0;
/* GUI */
rawfb = nk_rawfb_init(fb, tex_scratch, xw.width, xw.height, xw.width * 4);
if (!rawfb)
running = 0;
/* style.c */
/*set_style(ctx, THEME_WHITE);*/
/*set_style(ctx, THEME_RED);*/
/*set_style(ctx, THEME_BLUE);*/
/*set_style(ctx, THEME_DARK);*/
while (running) {
/* Input */
XEvent evt;
started = timestamp();
nk_input_begin(&rawfb->ctx);
while (XCheckWindowEvent(xw.dpy, xw.win, xw.swa.event_mask, &evt)) {
if (XFilterEvent(&evt, xw.win)) continue;
nk_xlib_handle_event(xw.dpy, xw.screen, xw.win, &evt, rawfb);
}
nk_input_end(&rawfb->ctx);
/* GUI */
if (nk_begin(&rawfb->ctx, "Demo", nk_rect(50, 50, 200, 200),
NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|
NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE)) {
enum {EASY, HARD};
static int op = EASY;
static int property = 20;
nk_layout_row_static(&rawfb->ctx, 30, 80, 1);
if (nk_button_label(&rawfb->ctx, "button"))
fprintf(stdout, "button pressed\n");
nk_layout_row_dynamic(&rawfb->ctx, 30, 2);
if (nk_option_label(&rawfb->ctx, "easy", op == EASY)) op = EASY;
if (nk_option_label(&rawfb->ctx, "hard", op == HARD)) op = HARD;
nk_layout_row_dynamic(&rawfb->ctx, 25, 1);
nk_property_int(&rawfb->ctx, "Compression:", 0, &property, 100, 10, 1);
}
nk_end(&rawfb->ctx);
if (nk_window_is_closed(&rawfb->ctx, "Demo")) break;
/* -------------- EXAMPLES ---------------- */
/*calculator(ctx);*/
overview(&rawfb->ctx);
node_editor(&rawfb->ctx);
/* ----------------------------------------- */
/* Draw framebuffer */
nk_rawfb_render(rawfb, nk_rgb(30,30,30), 1);
/* Emulate framebuffer */
XClearWindow(xw.dpy, xw.win);
nk_xlib_render(xw.win);
XFlush(xw.dpy);
/* Timing */
dt = timestamp() - started;
if (dt < DTIME)
sleep_for(DTIME - dt);
}
nk_rawfb_shutdown(rawfb);
nk_xlib_shutdown();
XUnmapWindow(xw.dpy, xw.win);
XFreeColormap(xw.dpy, xw.cmap);
XDestroyWindow(xw.dpy, xw.win);
XCloseDisplay(xw.dpy);
return 0;
}