Code Snippets

Back to the Index

I have collected here some small code snippets, essentially the software equivalent of doodling. Feel free to copy this stuff as you see fit.



OpenGL shader based text terminal

2D text on a 2D surface in 3D space

An 8 bit per pixel texture holding ASCII-ish characters is mapped onto a rectangle in 3D space. The fragment shader uses the character value to index into a larger texture holding a font bitmap. A bit of linear equation fiddling is used to generate a ramp function over the character area to render the full character and effectively use mip-mapping for the font texture.

Tarball containing the source code

Mandelbrot Renderer

Mandelbrot set in shades blue

A self-contained program in less than 70 lines of verbously commented C that generates a rendering of the famous mandelbrot set, exported to a ppm file.

Mandelbrot renderer C source

X11 Snippets

X11 Message Box

A self-contained function that tries to mimic the Windows® MessageBox function, without using any toolkits.

X11 message box C source

Sub-window compositing

The composite extension allows redirecting sub-windows to off-screen rendering surfaces.

This only affects rendering, not hit-detection. The mouse can still interact with where the window is supposed to be, and the server sends events like "mouse entered", "mouse clicked", etc. to the sub window, but the server won't render it. The application is expected to composite the window contents onto the parent.

The sample I pieced together creates a window with 3 levels of children. The top level is handled by the application (using XRender for the compositing), the lower level is handled by the XServer, since they aren't redirected.

X11 composite example C source

Window that's always on top

Using X11 client hints to make a window top-most. Depends on the window manager being cooperative to work.

X11 sticky topmost window example C source

Urgent window hint

Setting a client hint to make the window manager bring attention to a window, kind of like my IRC client does when somebody mentions my handle. Depends on the window manager being cooperative to work, and depends on the window manager what actually happens.

X11 urgent window example C source
Back to top  |  Back to the Index