floooh / sokol

minimal cross-platform standalone C headers
https://floooh.github.io/sokol-html5
zlib License
6.85k stars 485 forks source link

Use sokol to draw lvgl? #491

Closed IngwiePhoenix closed 3 years ago

IngwiePhoenix commented 3 years ago

Hello!

I am currently looking into making an LVGL simulator using Sokol. In terms of porting LVGL, it only needs to know a few things - one, is to have callbacks implemented to handle drawing and filling (https://docs.lvgl.io/latest/en/html/get-started/quick-overview.html#add-lvgl-into-your-project).

Now, LVGL is pretty much a 2D GUI - with only very tiny 3D elements since it's more intended for embedded devices. How do I create a 2D drawing context with Sokol?

Kind regards, Ingwie

floooh commented 3 years ago

Hard to say without knowing much about LVGL. If it's a vector-based API, then sokol-gl might help as an intermediate layer:

https://github.com/floooh/sokol/blob/master/util/sokol_gl.h

The MicroUI sample here does all the rendering via sokol-gl:

https://floooh.github.io/sokol-html5/sgl-microui-sapp.html

...via this code:

https://github.com/floooh/sokol-samples/blob/2eb27fe999f8fc247fa42527ab1133c7b2daae16/sapp/sgl-microui-sapp.c#L395-L516

However, the example code in the documentation you posted looks more like it's a framebuffer based API. In that case you'd want to copy the LVGL framebuffer into a sokol-gfx texture once per frame, and then render that texture somehow. The closest simple example code is the game-of-life sample here:

https://floooh.github.io/sokol-html5/dyntex-sapp.html

The pixels are generated by the CPU and copied into a dynamic texture, which is then used for rendering:

https://github.com/floooh/sokol-samples/blob/2eb27fe999f8fc247fa42527ab1133c7b2daae16/sapp/dyntex-sapp.c#L147-L163

IngwiePhoenix commented 3 years ago

However, the example code in the documentation you posted looks more like it's a framebuffer based API

Spot on! Yes, it's a framebuffer approach. The user provided callback is to return if buffering is still in process or if drawing is done.

Thank you for the pointer! I think I can work from there.

Fun fact: While taking to members of the Playstation Vita community, they had recommended me drawing the buffer into a texture for a LVGL port to the Vita as well. I still got a long way to go in terms of graphics programming...

Have a nice day :)

benstigsen commented 2 years ago

An LVGL + Sokol example drawing something like a button and label would be awesome.