zserge / fenster

The most minimal cross-platform GUI library
MIT License
811 stars 42 forks source link

Fenster

Fenster /ˈfɛnstɐ/ -- a German word for "window".

This library provides the most minimal and highly opinionated way to display a cross-platform 2D canvas. If you remember Borland BGI or drawing things in QBASIC or INT 10h- you know what I mean. As a nice bonus you also get cross-platform keyboard/mouse input and audio playback in only a few lines of code.

What it does for you

Example

Here's how to draw white noise:

// main.c
#include "fenster.h"
#define W 320
#define H 240
int main() {
  uint32_t buf[W * H];
  struct fenster f = { .title = "hello", .width = W, .height = H, .buf = buf };
  fenster_open(&f);
  while (fenster_loop(&f) == 0) {
    for (int i = 0; i < W; i++) {
      for (int j = 0; j < H; j++) {
        fenster_pixel(&f, i, j) = rand();
      }
    }
  }
  fenster_close(&f);
  return 0;
}

Compile it and run:

# Linux
cc main.c -lX11 -lasound -o main && ./main
# macOS
cc main.c -framework Cocoa -framework AudioToolbox -o main && ./main
# windows
cc main.c -lgdi32 -lwinmm -o main.exe && main.exe

That's it.

API

API is designed to be a polling loop, where on every iteration the framebuffer get updated and the user input (mouse/keyboard) can be polled.

struct fenster {
  const char *title; /* window title */
  const int width; /* window width */
  const int height; /* window height */
  uint32_t *buf; /* window pixels, 24-bit RGB, row by row, pixel by pixel */
  int keys[256]; /* keys are mostly ASCII, but arrows are 17..20 */
  int mod;       /* mod is 4 bits mask, ctrl=1, shift=2, alt=4, meta=8 */
  int x;         /* mouse X coordinate */
  int y;         /* mouse Y coordinate */
  int mouse;     /* 0 = no buttons pressed, 1 = left button pressed */
};

See examples/drawing-c for more old-school drawing primitives, but also feel free to experiment with your own graphical algorithms!

License

Code is distributed under MIT license, feel free to use it in your proprietary projects as well.