Fortran 2023 interface bindings to SDL3

Greetings,

SDL3 (Simple DirectMedia Layer 3) is a cross-platform C library for building games and multimedia applications. The latest version 3 was released in 2025 and can be seen as stable now. In comparison with its predecessor SDL2, it is a major clean-up and redesign intended to make the API more consistent, with better support for modern platforms.

In 2018, I started working on my (hand-written) interface bindings to SDL2 to create 2D/3D graphics in Fortran. While not complete, the bindings cover the important parts of SDL2, SDL2_image, SDL2_mixer, and SDL2_ttf.

For SDL3, I’m aiming for a complete and auto-generated coverage of the API in Fortran 2023, called fortran-sdl3 (requires SDL ≥ 3.4). After > 37,000 SLOC it’s still far from finished, but already sufficiently completed for creating small games or graphics demos (some are provided in the repo). I’ve tested the bindings on FreeBSD 15 and Debian 13. Windows and macOS should be supported, too (albeit with different linker libraries).

Maybe, some of you find it useful.

Screen Shots

11 Likes

Useful is an understatement. Thanks for keeping up the great work you do, creating bindings for multimedia libraries (in addition to your other great projects). I plan to re-implement and migrate some older projects to SDL3, so your work here will come in more than handy!

1 Like

SDL is a great library, it shows how to create an API. Simple, easy to use from any language, complete and stable. SDL3 is their third iteration in almost 30 years (extremely impressive!). When I am designing a new API, I often consult how SDL does it and learn from them.

I have used SDL3 GPU API, and it’s amazing. GPU APIs are notoriously complex (Vulkan, DirectX, etc.), and the SDL3 GPU API is simple, yet completely usable.

I don’t know the authors at all, but they have done an A+ job.

4 Likes

Under Win 10 with gfortran, I built libfortran-sdl3.a and examples\affine.exe. When run, it asks for libfortran-sdl3.dll

When try to build libfortran-sdl3.dll, I linked to SDL3 and SDL3_image, but got undefined reference to some SDL_Log_*

Where is these log functions? Could you suggest what is missing?

I have installed these:
SDL3-devel-3.4.14-mingw
SDL3_image-devel-3.4.4-mingw

Thanks for help.

[ 52%] Building Fortran object CMakeFiles/fortran-sdl3.dir/src/sdl3_timer.F90.obj
[ 53%] Building Fortran object CMakeFiles/fortran-sdl3.dir/src/sdl3_version.F90.obj
[ 55%] Building Fortran object CMakeFiles/fortran-sdl3.dir/src/sdl3.F90.obj
[ 57%] Building Fortran object CMakeFiles/fortran-sdl3.dir/src/sdl3_image.F90.obj
[ 58%] Linking Fortran shared library libfortran-sdl3.dll
d:/gcc/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/fortran-sdl3.dir/objects.a(sdl3_log.F90.obj):sdl3_log.F90:(.text+0xb3): undefined reference to `SDL_Log_'
d:/gcc/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/fortran-sdl3.dir/objects.a(sdl3_log.F90.obj):sdl3_log.F90:(.text+0x18e): undefined reference to `SDL_LogWarn_'
d:/gcc/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/fortran-sdl3.dir/objects.a(sdl3_log.F90.obj):sdl3_log.F90:(.text+0x269): undefined reference to `SDL_LogVerbose_'
d:/gcc/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/fortran-sdl3.dir/objects.a(sdl3_log.F90.obj):sdl3_log.F90:(.text+0x344): undefined reference to `SDL_LogTrace_'
d:/gcc/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/fortran-sdl3.dir/objects.a(sdl3_log.F90.obj):sdl3_log.F90:(.text+0x429): undefined reference to `SDL_LogMessage_'
d:/gcc/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/fortran-sdl3.dir/objects.a(sdl3_log.F90.obj):sdl3_log.F90:(.text+0x504): undefined reference to `SDL_LogInfo_'
d:/gcc/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/fortran-sdl3.dir/objects.a(sdl3_log.F90.obj):sdl3_log.F90:(.text+0x5df): undefined reference to `SDL_LogError_'
d:/gcc/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/fortran-sdl3.dir/objects.a(sdl3_log.F90.obj):sdl3_log.F90:(.text+0x6ba): undefined reference to `SDL_LogDebug_'
d:/gcc/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/fortran-sdl3.dir/objects.a(sdl3_log.F90.obj):sdl3_log.F90:(.text+0x795): undefined reference to `SDL_LogCritical_'
d:/gcc/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/fortran-sdl3.dir/objects.a(sdl3_notification.F90.obj):sdl3_notification.F90:(.text+0x416): undefined reference to `SDL_ShowNotification'
d:/gcc/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/fortran-sdl3.dir/objects.a(sdl3_notification.F90.obj):sdl3_notification.F90:(.text+0x601): undefined reference to `SDL_ShowNotification'
d:/gcc/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/fortran-sdl3.dir/objects.a(sdl3_opengl.F90.obj):sdl3_opengl.F9:(.text+0x30): undefined reference to `glGetString'
collect2.exe: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/fortran-sdl3.dir/build.make:663: libfortran-sdl3.dll] Error 1
make[1]: *** [CMakeFiles/Makefile2:126: CMakeFiles/fortran-sdl3.dir/all] Error 2
make: *** [makefile:91: all] Error 2

D:\00master\sdl3\fortran-sdl3-master\build_win10>
1 Like

The SDL_Log_* procedures are wrappers in src/sdl3_macro.c.

1 Like

Oh ic. I only treated the f90 file.

After adding it, only SDL_ShowNotification not found.

I should get glGetString from open gl. Haven’t linked to it.

[ 54%] Building Fortran object CMakeFiles/fortran-sdl3.dir/src/sdl3.F90.obj
[ 56%] Building Fortran object CMakeFiles/fortran-sdl3.dir/src/sdl3_image.F90.obj
[ 57%] Building C object CMakeFiles/fortran-sdl3.dir/src/sdl3_macro.c.obj
[ 59%] Linking Fortran shared library libfortran-sdl3.dll
d:/gcc/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/fortran-sdl3.dir/objects.a(sdl3_notification.F90.obj):sdl3_notification.F90:(.text+0x416): undefined reference to `SDL_ShowNotification'
d:/gcc/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/fortran-sdl3.dir/objects.a(sdl3_notification.F90.obj):sdl3_notification.F90:(.text+0x601): undefined reference to `SDL_ShowNotification'
d:/gcc/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/fortran-sdl3.dir/objects.a(sdl3_opengl.F90.obj):sdl3_opengl.F9:(.text+0x30): undefined reference to `glGetString'
collect2.exe: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/fortran-sdl3.dir/build.make:680: libfortran-sdl3.dll] Error 1
make[1]: *** [CMakeFiles/Makefile2:126: CMakeFiles/fortran-sdl3.dir/all] Error 2
make: *** [makefile:91: all] Error 2

Your SDL3 version is probably too old (requires ≥ SDL 3.6.0).

The lastest is 3.4.14

In sdl3_notification.F90, there are:

    function sdl_show_notification_(title, message, image, actions, num_actions) bind(c, name='SDL_ShowNotification')
function sdl_show_notification(title, message, image, actions, num_actions) result(id)

My error is: undefined reference to `SDL_ShowNotification’

SDL3 3.4.14 is too old. The function is not in any release version yet. I’ve had taken the header files from the SDL3 main branch. But you can just comment out the interface for now.

2 Likes

After comment out function sdl_show_notification_() & function sdl_show_notification(), I get libfortran-sdl3.dll and a few exes for the examples, like bship.exe. It works.

For forest.f90, there are multiple definition errors of `_gfortran_set_options’, `_gfortran_os_error_at’, …

It seems libgfortran.a has it. libfortran-sdl3.dll.a also has it. Maybe libfortran-sdl3 doesn’t need to define it? Just use gfortran’s?

[ 32%] Building Fortran object CMakeFiles/fortran-sdl3.dir/src/sdl3.F90.obj
[ 34%] Building Fortran object CMakeFiles/fortran-sdl3.dir/src/sdl3_image.F90.obj
[ 35%] Building C object CMakeFiles/fortran-sdl3.dir/src/sdl3_macro.c.obj
[ 37%] Linking Fortran shared library libfortran-sdl3.dll
[ 59%] Built target fortran-sdl3
[ 60%] Building Fortran object CMakeFiles/affine.dir/examples/affine.f90.obj
[ 62%] Linking Fortran executable D:/00master/sdl3/fortran-sdl3-master/bin/affine.exe
[ 62%] Built target affine
[ 64%] Building Fortran object CMakeFiles/bship.dir/examples/bship.f90.obj
[ 65%] Linking Fortran executable D:/00master/sdl3/fortran-sdl3-master/bin/bship.exe
[ 65%] Built target bship
[ 67%] Building Fortran object CMakeFiles/clear.dir/examples/clear.f90.obj
[ 68%] Linking Fortran executable D:/00master/sdl3/fortran-sdl3-master/bin/clear.exe
[ 68%] Built target clear
[ 70%] Building Fortran object CMakeFiles/dvd.dir/examples/dvd.f90.obj
[ 71%] Linking Fortran executable D:/00master/sdl3/fortran-sdl3-master/bin/dvd.exe
[ 71%] Built target dvd
[ 73%] Building Fortran object CMakeFiles/forest.dir/examples/forest.f90.obj
[ 75%] Linking Fortran executable D:/00master/sdl3/fortran-sdl3-master/bin/forest.exe
d:/gcc/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: d:/gcc/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../lib/libgfortran.a(compile_options.o): in function `_gfortran_set_options':
/home/gfortran/gcc-home/workshop/gcc/objdir64/x86_64-w64-mingw32/libgfortran/../../../gcc-12.1.0/libgfortran/runtime/compile_options.c:147: multiple definition of `_gfortran_set_options'; D:/00master/sdl3/fortran-sdl3-master/lib/libfortran-sdl3.dll.a(d000407.o):(.text+0x0): first defined here
d:/gcc/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: d:/gcc/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../lib/libgfortran.a(error.o): in function `_gfortran_os_error_at':
/home/gfortran/gcc-home/workshop/gcc/objdir64/x86_64-w64-mingw32/libgfortran/../../../gcc-12.1.0/libgfortran/runtime/error.c:384: multiple definition of `_gfortran_os_error_at'; D:/00master/sdl3/fortran-sdl3-master/lib/libfortran-sdl3.dll.a(d000399.o):(.text+0x0): first defined here

forest.f90, palette.f90, version.f90 and window.f90 have errors when compiling.

[ 64%] Building Fortran object CMakeFiles/fortran-sdl3.dir/src/sdl3_image.F90.obj
[ 66%] Building C object CMakeFiles/fortran-sdl3.dir/src/sdl3_macro.c.obj
[ 67%] Linking Fortran shared library libfortran-sdl3.dll
[ 67%] Built target fortran-sdl3
[ 69%] Building Fortran object CMakeFiles/affine.dir/examples/affine.f90.obj
[ 71%] Linking Fortran executable D:/00master/sdl3/fortran-sdl3-master/bin/affine.exe
[ 71%] Built target affine
[ 73%] Building Fortran object CMakeFiles/bship.dir/examples/bship.f90.obj
[ 75%] Linking Fortran executable D:/00master/sdl3/fortran-sdl3-master/bin/bship.exe
[ 75%] Built target bship
[ 76%] Building Fortran object CMakeFiles/clear.dir/examples/clear.f90.obj
[ 78%] Linking Fortran executable D:/00master/sdl3/fortran-sdl3-master/bin/clear.exe
[ 78%] Built target clear
[ 80%] Building Fortran object CMakeFiles/dvd.dir/examples/dvd.f90.obj
[ 82%] Linking Fortran executable D:/00master/sdl3/fortran-sdl3-master/bin/dvd.exe
[ 82%] Built target dvd
[ 83%] Building Fortran object CMakeFiles/gl3core.dir/examples/gl3core.f90.obj
[ 85%] Linking Fortran executable D:/00master/sdl3/fortran-sdl3-master/bin/gl3core.exe
[ 85%] Built target gl3core
[ 87%] Building Fortran object CMakeFiles/glempire.dir/examples/glempire.f90.obj
[ 89%] Linking Fortran executable D:/00master/sdl3/fortran-sdl3-master/bin/glempire.exe
[ 89%] Built target glempire
[ 91%] Building Fortran object CMakeFiles/glvertex.dir/examples/glvertex.f90.obj
[ 92%] Linking Fortran executable D:/00master/sdl3/fortran-sdl3-master/bin/glvertex.exe
[ 92%] Built target glvertex
[ 94%] Building Fortran object CMakeFiles/root3.dir/examples/root3.f90.obj
[ 96%] Linking Fortran executable D:/00master/sdl3/fortran-sdl3-master/bin/root3.exe
[ 96%] Built target root3
[ 98%] Building Fortran object CMakeFiles/smoke.dir/examples/smoke.f90.obj
[100%] Linking Fortran executable D:/00master/sdl3/fortran-sdl3-master/bin/smoke.exe
[100%] Built target smoke

Does it compile using the provided Makefile or fpm.toml?

I wrote a cmakelists.txt to use cmake.