floooh / sokol

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

Idea: Add flags to only initialize certain parts in sokol_app #1036

Closed pseregiet closed 1 month ago

pseregiet commented 2 months ago

Hi. This is an idea from SDL2. In SDL2 init function you can specify which subsystems you want to enable. For example you can enable only input if you don't care about video or audio. This would be useful for sokol_app. In my case I'd like to use sokol_app to setup the 3d context (I thought SDL2 does that already, but it only does for OpenGL and Vulkan, not Dx11/Metal). But have the window and events still be handled by SDL2.

flags may be any of the following OR'd together:

SDL_INIT_TIMER: timer subsystem
SDL_INIT_AUDIO: audio subsystem
SDL_INIT_VIDEO: video subsystem; automatically initializes the events subsystem
SDL_INIT_JOYSTICK: joystick subsystem; automatically initializes the events subsystem
SDL_INIT_HAPTIC: haptic (force feedback) subsystem
SDL_INIT_GAMECONTROLLER: controller subsystem; automatically initializes the joystick subsystem
SDL_INIT_EVENTS: events subsystem
SDL_INIT_EVERYTHING: all of the above subsystems
SDL_INIT_NOPARACHUTE: compatibility; this flag is ignored
floooh commented 2 months ago

Basically the opposite of the NOAPI stuff 😄

I don't know yet what to think of that idea TBH. Too many configuration options may create too many paths to test, it would also require to add new platform-specific functions to the public API, and complicate the internal code structure.

Things like SDL_INIT_AUDIO, SDL_INIT_TIMER is already taken care of by having separate sokol headers which you can simply decide to use or not.

But window system, input, 3D API setup and swapchain creation are quite closely related (that's why they all ended up in sokol_app.h in the first place instead of being implemented in independent libraries), and splitting those things up (even internally) may be too messy (since it would require to pass platform-specific data, like window handles/pointers into sokol_app.h).

FWIW, there are examples of how to setup D3D11 and Metal without sokol_app.h, it's not all that much code, the tricky part is usually the window system stuff (which in your case would be handled by SDL).

...and here's a simple example which uses GLFW in "NOAPI" mode together with a Metal (no depth buffer or MSAA support though): https://github.com/floooh/sokol-samples/blob/master/glfw/metal-glfw.m

PS: I do still roll the around the idea in my head to have a "SDL backend" in sokol_app.h, this would be a more robust solution on Linux, where SDL is essentially a system library.

pseregiet commented 2 months ago

SDL2 backed would be awesome, for sure. Thanks for the example code I think I'll figure it out from that.