{"id":22582363,"url":"https://github.com/seletz/raylib-hot-code-reload-c-example","last_synced_at":"2025-04-10T19:11:46.904Z","repository":{"id":216017028,"uuid":"740230968","full_name":"seletz/raylib-hot-code-reload-c-example","owner":"seletz","description":"Example of hot code reloading in C","archived":false,"fork":false,"pushed_at":"2024-06-11T06:58:58.000Z","size":237,"stargazers_count":5,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"develop","last_synced_at":"2025-03-24T16:52:59.182Z","etag":null,"topics":["example","hot-reload","raylib","raylib-c"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/seletz.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,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-07T22:21:23.000Z","updated_at":"2025-01-08T21:17:26.000Z","dependencies_parsed_at":"2024-06-11T08:02:07.943Z","dependency_job_id":"1f2f121b-3059-47bd-b0d2-482e50fcc138","html_url":"https://github.com/seletz/raylib-hot-code-reload-c-example","commit_stats":null,"previous_names":["seletz/raylib-hot-code-reload-c-example"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seletz%2Fraylib-hot-code-reload-c-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seletz%2Fraylib-hot-code-reload-c-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seletz%2Fraylib-hot-code-reload-c-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seletz%2Fraylib-hot-code-reload-c-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/seletz","download_url":"https://codeload.github.com/seletz/raylib-hot-code-reload-c-example/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248279767,"owners_count":21077408,"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":["example","hot-reload","raylib","raylib-c"],"created_at":"2024-12-08T06:09:13.148Z","updated_at":"2025-04-10T19:11:46.886Z","avatar_url":"https://github.com/seletz.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Hot Code Reloading in C\n\nMore details about this code can be found in my [blog post](https://seletz.github.io/posts/hotreload-gamecode-in-c).\n\nThis is some example code which demos hot-code reloading\nusing `dlopen(3)` and `dlsym(3)` of game code while the game\nis running.\n\nThe code should work in theory on any UNIX platform.  It's\ntested on Mac OS.\n\nThe \"game\" part is not really there -- this is just a demo\non hot-reloading at the moment.\n\nThe graphics part is done using [Raylib](https://www.raylib.com).\n\n![](assets/screenshot.png)\n\n[![build](https://github.com/seletz/raylib-hot-code-reload-c-example/actions/workflows/cmake-single-platform.yml/badge.svg)](https://github.com/seletz/raylib-hot-code-reload-c-example/actions/workflows/cmake-single-platform.yml)\n\n## Why?\n\nWhen doing graphics programming for games or \"recreational\"\nprogramming, one wants to have a fast feedback loop.\n\nAdditionally, when testing / debugging a game, we often need\nto reload the **logic** of the game and test it with a given\ngame state.  Often enough we can't fully serialise and reload\ngame state from/to disk.\n\n\n## How It Works\n\nThe \"game\" is split into two parts.  A main executable and a\nshared library which holds the game code.\n\nAt startup, the main executable looks for a `game.so` and tries\nto load it.\n\nIn the game loop, when `r` is pressed, the `game.so` shared library\nis reload.\n\nLoading and reloading is done using `dlopen(3)`.  We look for\nsymbols in the shared object using `dlsym(3)` and reset the function\npointers to the new symbols.\n\nFunction pointers are defined for all the functions we need/want to\nreload.\n\n## Game logic\n\nGame state is allocated on the heap using `init_gamestate()`.\n\nIn the game loop, we call `update_gamestate()` for each frame.  This\nfunction is supposed to update the global game state.  Rendering is\ndone by calling `update_frame()`.\n\nAt the beginning of each frame the code checks if the `r` key is\npressed.  If so, the `do_reload()` function is called, which in turn\ncalls `reload()` with the correct parameters to reload `game.so`.\n\n## links\n\nI got inspired to try this by this [blog post](https://medium.com/@TheElkantor/how-to-add-hot-reload-to-your-raylib-proj-in-c-698caa33eb74), which does something similar but for Windows.\n\nI also found [this video](https://youtu.be/Y57ruDOwH1g) by [tsoding](https://www.youtube.com/tsoding) helpful.\n\n## building and running\n\nThis project uses `cmake`.  `cmake` will fetch version `5.0` of\n[raylib](https://www.raylib.com) and build it:\n\n``` bash\n$ brew install cmake\n$ mkdir build\n$ cd build\n$ cmake ..\n$ cmake --build .\n```\n\nThis should generate the `mygame` main executable and the `libgamecode.so` shared library in the `build` directory.\n\nTo run the demo:\n\n``` bash\n$ ./mygame\n```\n\n## Hot Reloading\n\nTo test hot reloading, edit `src/game.c` while the game\nis running -- e.g change the cube color.\n\nThen rebuild:\n\n``` bash\n$ cd build\n$ make\n```\n\nNow hit `r` with the game window active.  The color of the cube\nshould change.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseletz%2Fraylib-hot-code-reload-c-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fseletz%2Fraylib-hot-code-reload-c-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseletz%2Fraylib-hot-code-reload-c-example/lists"}