Skip to content

Commit

Permalink
Merge branch 'siro20-nuklear_softrast'
Browse files Browse the repository at this point in the history
  • Loading branch information
vurtun committed Oct 7, 2017
2 parents ed57400 + e5850a0 commit 24a7536
Show file tree
Hide file tree
Showing 5 changed files with 1,480 additions and 1 deletion.
13 changes: 13 additions & 0 deletions demo/x11_rawfb/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Install
BIN = zahnrad

# Flags
CFLAGS = -std=c89 -pedantic -O2 -Wunused -DRAWFB_XRGB_8888

SRC = main.c
OBJ = $(SRC:.c=.o)

$(BIN):
@mkdir -p bin
rm -f bin/$(BIN) $(OBJS)
$(CC) $(SRC) $(CFLAGS) -D_GNU_SOURCE -D_POSIX_C_SOURCE=200809L -o bin/$(BIN) -lX11 -lXext -lm
221 changes: 221 additions & 0 deletions demo/x11_rawfb/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
/* 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 RAWFB_RGBX_8888
#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;
}

Loading

0 comments on commit 24a7536

Please sign in to comment.