Some simple GUI library

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
WilsonAndMorgan
Posts: 4
Joined: Fri Feb 07, 2025 1:04 pm
Libera.chat IRC: WilsonAndMorgan

Some simple GUI library

Post by WilsonAndMorgan »

I lost my os files a few weeks ago and I have to rewrite my OS and when I enable graphics mode, it's not work as I expect, I don't know why but it's not as same as the OS before. I want some simple GUI library (maybe just some drawline) =P~
User avatar
AndrewAPrice
Member
Member
Posts: 2310
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Re: Some simple GUI library

Post by AndrewAPrice »

By Simple GUI library do you mean a simple drawing library (lines and boxes) rather than an actual GUI framework (buttons, textboxes, etc)? What language? (C? C++? Rust?)

For super simple, you could go with something like this that draws to a bitmap (a grid of pixels) in memory:

https://github.com/ArashPartow/bitmap
https://github.com/a-e-k/canvas_ity
https://github.com/sammycage/plutovg

Lots of people love Cairo. Packed with a ton of features. Likely harder to port.

https://www.cairographics.org/

I ended up going with Skia but it had a huge number of dependencies and was a bit of a pain* to port, although full of features too.
https://skia.org/

* By pain - I mean it's a huge library with a ton of dependencies (PNG, freetype, Vulkan, zlib, etc) and it was a matter of disabling as much as possible except for the few things (such as loading jpegs) that I wanted.
My OS is Perception.
WilsonAndMorgan
Posts: 4
Joined: Fri Feb 07, 2025 1:04 pm
Libera.chat IRC: WilsonAndMorgan

Re: Some simple GUI library

Post by WilsonAndMorgan »

AndrewAPrice wrote: Fri Feb 07, 2025 1:58 pm By Simple GUI library do you mean a simple drawing library (lines and boxes) rather than an actual GUI framework (buttons, textboxes, etc)? What language? (C? C++? Rust?)
sorry I forgot to mention, I use C language. And I just want to draw some lines and boxes to test if my graphcis mode work right, because I mention that it's not like my os a few week ago :)
User avatar
eekee
Member
Member
Posts: 907
Joined: Mon May 22, 2017 5:56 am
Location: Kerbin
Discord: eekee
Contact:

Re: Some simple GUI library

Post by eekee »

Your OS needs to know where the framebuffer starts in memory, how big each pixel is (3 and 4 bytes are common, I think), how long the lines are (in bytes or pixels), and the length of any unused gap between lines. (I think the gap is uncommon.) Given those facts, it can calculate what address to write to for any pixel.

But I start testing before I have all those facts. :) I store some high value into the framebuffer, either at the start or some way in, and look for it on the screen. I usually need to take my glasses off, which for me is like using a powerful magnifying glass. Sometimes I store a run of pixels or a pattern. Some graphics hardware doesn't like anything being written into the gap, but I think that's even rarer than having a gap in the first place. And you can always try shorter runs at different start addresses. It's all easier if you really know the layout of your framebuffer in advance.

One of my tests was to write a pattern without regard for line length or pixel format. The pattern used 8 pixels to represent a byte, storing 255 (or was it 0xffff_ffff?) for a 1 bit and 0 for a 0 bit, as the byte counted up from 0 to 255. From looking at that, I was able to see a few things such as that the visual area didn't start at the start of the framebuffer region in my laptop, the length of lines, and that there was no gap. It was hard to see the pixels though.

I suggest testing in some highcolor or truecolor mode because you don't have to consider a palette; the values you write correspond directly to colors.

Testing the framebuffer merged right into drawing as I wrote a pixel-plotting routine to calculate the address of a pixel and set it. It was rather slow, so after some more testing with it, I wrote solid-box code starting with a `hline` function. hline took the x coord of the left end of the line, the y coord, and a length. it converted the length into a right-end x coord and clipped to the line length, then filled the length with a fast memory fill, if I remember right. The box routine clipped the y coords to the framebuffer and called hline in a loop.

And that was as far as I got. :) If I'd gone further, it would have been to change the clipping code to clip to some values from a supplied struct, useful for clipping to a window and for different framebuffer sizes. A lot of things would have been changed from hard-coded to variable at this stage.

The next steps would have been copying part of an image (with clipping) using similar functions to hline and its box wrapper. That could be the foundation for text, though it would be better to go one step further, expanding an image from 1-bit or 4-bit depth to the framebuffer's depth and applying a color as it's copied. That would be the foundation for full bitmap-font support, with 4-bit for antialiased bitmap fonts. And that's as far as my plans got. :)

I hope that's helpful!
Kaph — a modular OS intended to be easy and fun to administer and code for.
"May wisdom, fun, and the greater good shine forth in all your work." — Leo Brodie
Post Reply