Minimal Cross-Platform Graphics
zserge.com
zserge.com
To be able to call that function, I LoadLibrary("dwmapi.dll"), and then GetProcAddress(dwm, "DwmFlush").
vsync feedback support has to be queried (look at vulkan API/wayland API). Usually it is called "presentation" something.
DWM always runs with vsync so presents never happen more frequently than screen refreshes.
https://github.com/erkkah/tigr
Brief discussion:
This example I think needs editing:
memset(f->buf, rgb, f->width*f->height*sizeoof(uint32_t));
it's described as a way to "fill the complete framebuffer with a solid colour", but 'rgb' is shown in the previous snipped to be 'uint32_t'. That is not how 'memset()' [1] works, it will only use the least significant 8 bits of the 'int'-typed value argument. So it would use whatever blue bits where in 'rgb', only.For clearing (all zero) it's usable, and then I would recommend writing it as:
memset(f->buf, 0, f->width * f->height * sizeof *f->buf);
this both fixes the typo ("sizeoof" sounds like a C lecturer being punched in the guts), and avoids duplicating the type and instead inferring it from the actual object, which is more DRY and a style I've been advocating for ... years. .. (sizeof *f->buf) ..
Because otherwise thats one stray space char away from a bad day ..But no, I don't think there are any downsides, but there is this notion that things should look like what they are, which is popular in C.
With parens, I would suggest spaces to make it
sizeof (*f->buf)
that makes it look less like a function call in many styles.it's probably a bit more efficient than fenster on x-windows because it uses shared memory, and i think the programming interface is a little nicer (see the readme above for code examples)
apps i've written in yeso include an adm-3a terminal emulator, a tetris game, a raytracer, an rpn calculator, a fractal explorer, and so on
i haven't ported yeso to win32/64, macos, android, or the browser canvas yet, just x-windows, the linux framebuffer (partly), and a window system implemented in yeso called wercam
it includes bindings for c, python (via cffi), and lua (via luajit's ffi), and presumably you could use it from zig or rust in the same way as fenster, but i haven't tried
For the rendering, ideally it needs GPU support.
Input needs much more work, here's an overview for Windows: http://blog.ngedit.com/2005/06/13/whats-broken-in-the-wm_key...
Windows' Sleep() function has default resolution 15.6ms, that's not enough for realtime rendering, and relatively hard to fix, ideally need a modern OS and a waitable timer created with high resolution flag.
Here's my attempt at making something similar, couple years ago: https://github.com/Const-me/Vrmac
Very easy to fix. Call this at the start of your real-time process:
https://learn.microsoft.com/en-us/windows/win32/api/timeapi/...
Use an argument of 1, and windows will come knocking at ~1Khz. This is extremely effective in my experience. Allows you to do some pretty crazy stuff on 1 thread.
Impossible given the platform limitations at that time, on Linux that code works on top of GLES 3.1. I didn't want too many Windows-only features there.
> No matrix transform routines?
There's no need for that. That library is for C#, which includes these routines in the standard library of the language for many years now, since .NET core 1.0: https://learn.microsoft.com/en-us/dotnet/api/system.numerics...
SDL already walked this path. So did pygame.
It turns out without shaders, and all their complexity, you basically can’t do anything useful at high resolutions.
/shrug
It’s too slow; compositing, transformations, all the things people want to do are very much harder to implement in software on top of a naive graphics stack.
Simple doesn’t mean naive; you can have a simple api that is excellent.
…but this is a naïve implementation, and it won’t work in any useful way at scale.
People complain about ballooning complexity, but they often forget that stuff (eg. specialised graphics hardware) wasnt invented by a bunch of idiots.
It was invented because the naive approach, that they used before was found to be in practice fundamentally limiting, inferior and failed to meet people’s expectations.
Of course, if you vastly lower your expectations, and target say, 320x240 at 8bit colour, you can happily have a naïve implementation that works just fine
(Don't believe me? Quote:
> Having this we can now draw complex polygons and would probably need a “flood fill” algorithm. Typically it is implemented using a queue of pixels to check and paint, but we can use recursion, as long as the filled area remains small enough to not overflow the stack
^ This is the very definition of a naive implementation, and https://github.com/zserge/fenster/blob/e71d493fa6d544243dd60... settings one pixel at a time, is too. Delightfully charming as it might be to write your own functions that set one pixel at a time, it's a joke, really, if you expect to do anything serious)
But for quick hacking / porting old demos / writing emulators and also text based UI it can be fast enough.
With the added benefit of small footprint, high compatibility and fast startup time.
The Lite editor https://github.com/rxi/lite is using pure software rendering (on top of SDL) in a rather naïve fashion but it still renders full 32bit colors at full resolution at more than 60FPS on my computer, not the best solution but still surprisingly fast given the simplicity of the renderer.
This is typically a case where simple/naïve can beat a juggernaut like Electron.
https://github.com/rxi/lite/blob/master/src/rencache.c#L4
I think you'll find that they found the naive approach was sufficiently poor, performance wise, that additional optimizations had to be applied on-top.
> But for quick hacking / porting old demos / writing emulators and also text based UI it can be fast enough.
/shrug
If you want to use it, use it. It's 'good enough'...
> if you vastly lower your expectations
Lol, show me one library that does this without going through an abstraction layer like Vulkan. Details like this are not even documented by GPU vendors and you'd need to reverse engineer every supported GPU architecture yourself.
Guy writes a library with the design goals of making basic, retro-like graphics functionality easy and fun with a minimum of code, and certain Hackernews dogpile him because it can't be integrated with an AAA pipeline or whatever.
It's a 2D framebuffer library for placing single pixels like in mode 13 *eyerolling* (GPUs are hardly useful for this type of stuff).
If you want something more complete, check out the sokol headers (shameless plug): https://floooh.github.io/sokol-html5/
...but that can hardly be called 'minimal' anymore.
For applications like emulators, or making vintage games (like Doom) run on modern platforms, that approach makes a lot of sense.
My intuition says otherwise but I admit I don't have math on hand to back it up.
(the resulting framebuffer can then of course be dumped into a GPU texture for rendering, but that just offers a bit more flexibility, eg embedding the emulated system into a 3D rendered world)
PS: of course one could build a GPU command list to render such a video frame somehow, but I bet just building this command list is more expensive then just doing the video decode with the CPU. It would basically come down to one draw command per (emulated system) pixel in the worst case.
If you had a frame buffer for the actual screen and you tried to do even a 1 to 2x2 expansion on the CPU's time, that'd have to be a serious speed hit. Presumably GPU hardware can do that sort of thing.
Window system composers should also be able to upscale bitmaps on their own though, and nothing prevents them to use the GPU for this.
the benefit of the gpu is no longer (since maybe late last millennium) that you don't have the memory bandwidth to your framebuffer; it's that you can do a lot of computation per pixel
typically the gpu advantage is only about an order of magnitude tho
- Anyone with experience in it / any possible synergies?
Could this be compiled as an αpε? Such a thing would make many heads explode.
https://github.com/abainbridge/deadfrog-lib/blob/master/src/.... It is janky though.
> It is janky though
So what? It can be improved :)
Would you like to try to make an APE out of it? It would then be not just different Linuxes, but also MacOS and Windows, a bit like https://x410.dev/ or the free software vcxserv https://sourceforge.net/projects/vcxsrv/
We have discord. You should join us!
FWIW I'm currently working on a DNS server with nice extra features like Multicast for mDNS/Bonjour: it could be used to publish the X server DISPLAY and make it accessible via service-discovery
Who is "you", here?
One of many HN discussions about the topic here: