{"id":18847826,"url":"https://github.com/interkosmos/fortran-sdl2","last_synced_at":"2025-04-14T08:09:50.215Z","repository":{"id":139286139,"uuid":"123346989","full_name":"interkosmos/fortran-sdl2","owner":"interkosmos","description":"Fortran 2008 interface bindings to SDL 2.0","archived":false,"fork":false,"pushed_at":"2025-01-16T14:38:31.000Z","size":2913,"stargazers_count":39,"open_issues_count":0,"forks_count":5,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-14T08:09:44.052Z","etag":null,"topics":["fortran","fortran-package-manager","fpm","game-development","gamedev","opengl","sdl","sdl2"],"latest_commit_sha":null,"homepage":"","language":"Fortran","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/interkosmos.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2018-02-28T21:53:47.000Z","updated_at":"2025-04-07T20:42:30.000Z","dependencies_parsed_at":"2023-09-24T12:52:41.078Z","dependency_job_id":null,"html_url":"https://github.com/interkosmos/fortran-sdl2","commit_stats":{"total_commits":287,"total_committers":1,"mean_commits":287.0,"dds":0.0,"last_synced_commit":"2e08994a2fe0ac165109701fdbfde902e8c77b69"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/interkosmos%2Ffortran-sdl2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/interkosmos%2Ffortran-sdl2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/interkosmos%2Ffortran-sdl2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/interkosmos%2Ffortran-sdl2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/interkosmos","download_url":"https://codeload.github.com/interkosmos/fortran-sdl2/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248843867,"owners_count":21170492,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fortran","fortran-package-manager","fpm","game-development","gamedev","opengl","sdl","sdl2"],"created_at":"2024-11-08T03:09:49.698Z","updated_at":"2025-04-14T08:09:50.165Z","avatar_url":"https://github.com/interkosmos.png","language":"Fortran","funding_links":[],"categories":[],"sub_categories":[],"readme":"# fortran-sdl2\n\nA collection of ISO C binding interfaces to\n[Simple DirectMedia Layer 2.0](https://www.libsdl.org/) (SDL 2.0), for\n2D and 3D game programming in Fortran. SDL versions tested against:\n\nLibrary    | Version\n-----------|---------\nSDL2       | 2.28.2\nSDL2_image | 2.6.3\nSDL2_mixer | 2.6.3\nSDL2_ttf   | 2.20.2\n\nFor bindings to\n[SDL2_gfx](https://www.ferzkopp.net/Software/SDL2_gfx/Docs/html/index.html),\nsee [fortran-sdl2_gfx](https://github.com/freevryheid/fortran-sdl2_gfx).\n\n## Build Instructions\n\nIf not present already, install SDL 2.0 with development headers (and\noptionally: [SDL_image 2.0](https://www.libsdl.org/projects/SDL_image/),\n[SDL_mixer 2.0](https://www.libsdl.org/projects/SDL_mixer/), and/or\n[SDL_ttf 2.0](https://www.libsdl.org/projects/SDL_ttf/)). On FreeBSD, run:\n\n```\n# pkg install devel/sdl20 graphics/sdl2_image audio/sdl2_mixer graphics/sdl2_ttf\n```\n\nEither use GNU/BSD make, [xmake](https://xmake.io/) to build *fortran-sdl2*, or\nthe [Fortran Package Manager](https://github.com/fortran-lang/fpm) (fpm).\n\n### Make\n\nRun `make sdl2` to compile the static library `libsdl2.a`:\n\n```\n$ git clone --depth 1 https://github.com/interkosmos/fortran-sdl2\n$ cd fortran-sdl2/\n$ make sdl2\n```\n\nOn Microsoft Windows, you have to set `LIBGL` and `LIBGLU`:\n\n```\n$ make all LIBGL=-lopengl32 LIBGLU=-lglu32\n```\n\nOn macOS, replace `-lGL -lGLU` with `-framework OpenGL`. You can override the\ndefault compiler (`gfortran`) by passing the `FC` argument, for example:\n\n```\n$ make all FC=/opt/intel/bin/ifort\n```\n\nLink your Fortran project with `libsdl2.a` and (optionally) `libsdl2_*.a`, or\nsimply with `libfortran-sdl2.a`:\n\n| Library           | Compilation         | Linking                                                         |\n|-------------------|---------------------|-----------------------------------------------------------------|\n| SDL2              | `make sdl2`         | `libsdl2.a -lSDL2`                                              |\n| SDL2_image        | `make sdl2_image`   | `libsdl2.a libsdl2_image.a -lSDL2 -lSDL2_image`                 |\n| SDL2_mixer        | `make sdl2_mixer`   | `libsdl2.a libsdl2_mixer.a -lSDL2 -lSDL2_mixer`                 |\n| SDL2_ttf          | `make sdl2_ttf`     | `libsdl2.a libsdl2_ttf.a -lSDL2 -lSDL2_ttf`                     |\n| *all*             | `make all`          | `libfortran-sdl2.a -lSDL2 -lSDL2_image -lSDL2_mixer -lSDL2_ttf` |\n\n### xmake\n\nBuild all static libraries with:\n\n```\n$ xmake\n```\n\nBuild a particular library with:\n\n```\n$ xmake build \u003cname\u003e\n```\n\nThe default output directory is `build/`.\n\n## Example\n\nThe following example shows how to fill a rectangle, using the hardware renderer.\n\n```fortran\n! example.f90\nprogram main\n    use, intrinsic :: iso_c_binding, only: c_associated, c_null_char, c_ptr\n    use, intrinsic :: iso_fortran_env, only: stdout =\u003e output_unit, stderr =\u003e error_unit\n    use :: sdl2\n    implicit none\n\n    integer, parameter :: SCREEN_WIDTH  = 640\n    integer, parameter :: SCREEN_HEIGHT = 480\n\n    type(c_ptr)     :: window\n    type(c_ptr)     :: renderer\n    type(sdl_event) :: event\n    type(sdl_rect)  :: rect\n    integer         :: rc\n    logical         :: is_running\n\n    ! Initialise SDL.\n    if (sdl_init(SDL_INIT_VIDEO) \u003c 0) then\n        write (stderr, '(\"SDL Error: \", a)') sdl_get_error()\n        stop\n    end if\n\n    ! Create the SDL window.\n    window = sdl_create_window('Fortran SDL 2.0' // c_null_char, \u0026\n                               SDL_WINDOWPOS_UNDEFINED, \u0026\n                               SDL_WINDOWPOS_UNDEFINED, \u0026\n                               SCREEN_WIDTH, \u0026\n                               SCREEN_HEIGHT, \u0026\n                               SDL_WINDOW_SHOWN)\n\n    if (.not. c_associated(window)) then\n        write (stderr, '(\"SDL Error: \", a)') sdl_get_error()\n        stop\n    end if\n\n    ! Create the renderer.\n    renderer = sdl_create_renderer(window, -1, 0)\n\n    ! Set position and size of the rectangle.\n    rect = sdl_rect(50, 50, 250, 250)\n\n    ! Event loop.\n    is_running = .true.\n\n    do while (is_running)\n        ! Catch events.\n        do while (sdl_poll_event(event) \u003e 0)\n            select case (event%type)\n                case (SDL_QUITEVENT)\n                    is_running = .false.\n            end select\n        end do\n\n        ! Fill screen black.\n        rc = sdl_set_render_draw_color(renderer, \u0026\n                                       uint8(0), \u0026\n                                       uint8(0), \u0026\n                                       uint8(0), \u0026\n                                       uint8(SDL_ALPHA_OPAQUE))\n        rc = sdl_render_clear(renderer)\n\n        ! Fill the rectangle.\n        rc = sdl_set_render_draw_color(renderer, \u0026\n                                       uint8(127), \u0026\n                                       uint8(255), \u0026\n                                       uint8(0), \u0026\n                                       uint8(SDL_ALPHA_OPAQUE))\n        rc = sdl_render_fill_rect(renderer, rect)\n\n        ! Render to screen and wait 20 ms.\n        call sdl_render_present(renderer)\n        call sdl_delay(20)\n    end do\n\n    ! Quit gracefully.\n    call sdl_destroy_renderer(renderer)\n    call sdl_destroy_window(window)\n    call sdl_quit()\nend program main\n```\n\nTo compile the source code with GNU Fortran, run:\n\n```\n$ gfortran `sdl2-config --cflags` -o example example.f90 libsdl2.a `sdl2-config --libs`\n```\n\n## Further Examples\n\nSome demo applications can be found in `examples/`:\n\n* [**alpha**](examples/alpha.png) makes one color of an image transparent (software renderer).\n* [**cyclic**](examples/cyclic.png) implements a 2D [cyclic cellular automaton](https://en.wikipedia.org/wiki/Cyclic_cellular_automaton) (hardware renderer).\n* [**draw**](examples/draw.png) draws some shapes (hardware renderer).\n* [**dvd**](examples/dvd.png) loads a PNG file with SDL_image and lets it bounce on the screen (hardware renderer).\n* **events** polls SDL events (software renderer).\n* [**fire**](examples/fire.png) renders the [DOOM fire effect](http://fabiensanglard.net/doom_fire_psx/) (hardware renderer).\n* [**forest**](examples/forest.png) implements a cellular automaton, based on the [forest fire model](https://rosettacode.org/wiki/Forest_fire) (hardware renderer).\n* [**gl**](examples/gl.png) renders a triangle with OpenGL 1.3.\n* [**gl3d**](examples/gl3d.png) rotates textured cubes with OpenGL 1.3.\n* [**glsphere**](examples/glsphere.png) rotates the camera around GLU spheres.\n* [**image**](examples/image.png) loads and displays an image (software renderer).\n* **info** prints debug information to console (software renderer).\n* **log** prints log messages with `SDL_Log()` (software renderer).\n* [**logo**](examples/logo.png) lets you fly through a field of Fortran logos (hardware renderer).\n* **msgbox** shows a simple message box (software renderer).\n* [**opera**](examples/opera.png) plays an OGG file with SDL_mixer (software renderer).\n* [**pixel**](examples/pixel.png) copies an SDL_Surface to an SDL_Texture pixelwise (hardware renderer).\n* [**scaling**](examples/scaling.png) displays a scaled image (software renderer).\n* [**text**](examples/text.png) outputs text with SDL_ttf (hardware renderer).\n* **vertex** shows geometry renderer of SDL 2.0.18.\n* [**voxel**](examples/voxel.png) renders a [voxel space](https://github.com/s-macke/VoxelSpace) with direct pixel manipulation. Use arrow keys and Q, A for camera movement (hardware renderer).\n* **window** opens a window and fills rectangles (software renderer).\n\nCompile all examples with:\n\n```\n$ make examples\n```\n\nIf you prefer xmake, build and run an example with:\n\n```\n$ xmake build \u003cname\u003e\n$ xmake run \u003cname\u003e\n```\n\nTo compile all examples, simply run:\n\n```\n$ xmake build examples\n```\n\n## Compatibility\n\nAll Fortran interface names are written in snake case. For instance,\n`SDL_CreateWindow()` can be accessed by calling Fortran interface\n`sdl_create_window()`. The same is valid for derived types and their\ncomponents. Enums and constants have kept their original names.\n\n### Null-Termination of Strings\n\nA `c_null_char` must be appended to all strings passed to the interfaces,\nexcept `sdl_set_hint()` and `sdl_log*()`, which are wrappers that terminate the\narguments for convenience.\n\n### SDL_Color\n\nSDL 2.0 stores RGB colour values as `Uint8`. As Fortran does not feature unsigned\ntypes, the intrinsic procedure `transfer()` has to be used to transfer bit\npatterns directly. For example:\n\n```fortran\ntype(sdl_color) :: color\n\ncolor = sdl_color(r = transfer([255, 1], 1_c_int8_t)\n                  g = transfer([127, 1], 1_c_int8_t)\n                  b = transfer([  0, 1], 1_c_int8_t)\n                  a = transfer([SDL_ALPHA_OPAQUE, 1], 1_c_int8_t))\n```\n\nThe Fortran bindings provide a utility function `uint8()` that simplifies the\nconversion:\n\n```fortran\ncolor = sdl_color(r = uint8(255), \u0026\n                  g = uint8(127), \u0026\n                  b = uint8(0), \u0026\n                  a = uint8(SDL_ALPHA_OPAQUE))\n```\n\n### SDL_Surface\n\nC pointers in derived types like `sdl_surface` must be converted to Fortran\ntypes manually by calling the intrinsic procedure `c_f_pointer()`. For instance,\nto assign the `sdl_pixel_format` pointer in `sdl_surface`:\n\n```fortran\ntype(sdl_pixel_format), pointer :: pixel_format\ntype(sdl_surface),      pointer :: surface\n\n! Convert C pointer to Fortran pointer.\ncall c_f_pointer(surface%format, pixel_format)\n```\n\nThe C struct `SDL_Surface` stores RGB pixel values as `Uint8`. Use `transfer()`\nand `ichar()` to convert `Uint8` to Fortran signed integer. For example:\n\n```fortran\ninteger, parameter              :: X = 10\ninteger, parameter              :: Y = 20\ninteger(kind=int8)              :: r, g, b\ninteger(kind=c_int32_t)         :: pixel\ninteger(kind=c_int8_t), pointer :: pixels(:)\ntype(sdl_pixel_format), pointer :: pixel_format\ntype(sdl_surface),      pointer :: surface\n\n! Load BMP file into SDL_Surface.\nsurface =\u003e sdl_load_bmp('image.bmp' // c_null_char)\n\n! Get SDL_PixelFormat.\ncall c_f_pointer(surface%format, pixel_format)\n\n! Get Fortran array of pixel pointers.\ncall c_f_pointer(surface%pixels, pixels, shape=[surface%pitch * surface%h])\n\n! Get single pixel of coordinates X and Y. Convert to Fortran integer.\npixel = ichar(transfer(pixels((Y - 1) * surface%pitch + X), 'a'), kind=c_int32_t)\n\n! Get RGB values of pixel.\ncall sdl_get_rgb(pixel, pixel_format, r, g, b)\n```\n\n### SDL_Log\n\nOnly a single string message can be passed to `sdl_log()` and `sdl_log_*()`\nroutines, as Fortran does not support ellipsis arguments. For example:\n\n```fortran\ncall sdl_log_debug(SDL_LOG_CATEGORY_TEST, 'debug message')\n```\n\nYou may have to write your log message to a string first:\n\n```fortran\ncharacter(len=32) :: msg\ninteger           :: rc\n\nwrite (msg, '(a, i0)') 'Error: ', rc\ncall sdl_log(msg)\n```\n\nLog messages will be trimmed and null-terminated by the wrapper routines.\n\n### Events\n\nThe SDL event [SDL_QUIT](https://wiki.libsdl.org/SDL_EventType#SDL_QUIT) has\nbeen renamed to `SDL_QUITEVENT` in Fortran to avoid conflict with interface\n`sdl_quit()`.\n\n## Documentation\n\nGenerate the source code documentation with\n[FORD](https://github.com/cmacmackin/ford). Add FORD with `pip`, for example:\n\n```\n$ python3 -m venv virtual-environment/\n$ source virtual-environment/bin/activate\n$ python3 -m pip install ford\n```\n\nOr, instead, just install the package in your user directory:\n\n```\n$ python3 -m pip install --user ford\n```\n\nThen, run:\n\n```\n$ ford project.md -d ./src\n```\n\nOpen `doc/index.html` in a web browser.\n\n## Coverage\n\nSee [coverage](COVERAGE.md) for the current status.\n\n## Credits\n\nThanks go to [angelog0](https://github.com/angelog0).\n\n## Licence\n\nISC\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finterkosmos%2Ffortran-sdl2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Finterkosmos%2Ffortran-sdl2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finterkosmos%2Ffortran-sdl2/lists"}