Last active
August 1, 2023 06:53
-
-
Save monomere/ea837076dcfa1ae94c39de000d0ebf55 to your computer and use it in GitHub Desktop.
dear imgui setup script :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Original Author: monomere | |
# This is Public Domain, but would be cool if you still credit me :) | |
import os, inquirer, urllib.request, typing, json | |
IMGUI_REPO = 'ocornut/imgui' | |
T = typing.TypeVar('T') | |
TupleTagCommit = tuple[str, str] | |
def get_version_list(repo: str) -> list[TupleTagCommit]: | |
web = urllib.request.urlopen(f'https://api.github.com/repos/{repo}/tags') | |
data = web.read() | |
encoding = web.info().get_content_charset('utf-8') | |
json_data = json.loads(data.decode(encoding)) | |
tags: list[TupleTagCommit] = [] | |
for json_tag in json_data: | |
tags.append((json_tag['name'], json_tag['commit']['sha'])) | |
return tags | |
def dup(x: T) -> tuple[T, T]: return (x, x) | |
def github_url(repo: str, branch: str, fname: str): | |
return f'https://raw.githubusercontent.com/{repo}/{branch}/{fname}' | |
def download(url, fname): | |
print(f'{fname}: GET {url}') | |
os.makedirs(os.path.dirname(fname), exist_ok=True) | |
urllib.request.urlretrieve(url, fname) | |
imgui_sources = [ | |
'imgui.cpp', | |
'imgui_demo.cpp', | |
'imgui_draw.cpp', | |
'imgui_tables.cpp', | |
'imgui_widgets.cpp', | |
'imgui_internal.h', | |
'imgui.h', | |
'imconfig.h', | |
'imstb_rectpack.h', | |
'imstb_textedit.h', | |
'imstb_truetype.h', | |
] | |
platform_backend_sources = { | |
'android': ('imgui_impl_android.cpp', 'imgui_impl_android.h'), | |
'glfw': ('imgui_impl_glfw.cpp', 'imgui_impl_glfw.h'), | |
'osx': ('imgui_impl_osx.mm', 'imgui_impl_osx.h'), | |
'sdl2': ('imgui_impl_sdl2.cpp', 'imgui_impl_sdl2.h'), | |
'sdl3': ('imgui_impl_sdl3.cpp', 'imgui_impl_sdl3.h'), | |
'win32': ('imgui_impl_win32.cpp', 'imgui_impl_win32.h'), | |
} | |
rendering_backend_sources = { | |
'dx9': ('imgui_impl_dx9.cpp', 'imgui_impl_dx9.h'), | |
'dx10': ('imgui_impl_dx10.cpp', 'imgui_impl_dx10.h'), | |
'dx11': ('imgui_impl_dx11.cpp', 'imgui_impl_dx11.h'), | |
'dx12': ('imgui_impl_dx12.cpp', 'imgui_impl_dx12.h'), | |
'metal': ('imgui_impl_metal.mm', 'imgui_impl_metal.h'), | |
'opengl2': ('imgui_impl_opengl2.cpp', 'imgui_impl_opengl2.h'), | |
'opengl3': ('imgui_impl_opengl3.cpp', 'imgui_impl_opengl3_loader.h', 'imgui_impl_opengl3.h'), | |
'sdlrenderer': ('imgui_impl_sdlrenderer.cpp', 'imgui_impl_sdlrenderer.h'), | |
'vulkan': ('imgui_impl_vulkan.cpp', 'imgui_impl_vulkan.h'), | |
'wgpu': ('imgui_impl_wgpu.cpp', 'imgui_impl_wgpu.h'), | |
} | |
def main(): | |
versions = { | |
'docking': 'docking', | |
'master': 'master' | |
} | |
versions.update({ | |
tag: commit for (tag, commit) | |
in get_version_list(IMGUI_REPO) | |
}) | |
print("Setting up a Dear ImGui thing:") | |
r = inquirer.prompt([ | |
inquirer.Text( | |
'outdir', 'enter the output directory', | |
default = 'imgui' | |
), | |
inquirer.List( | |
'platform', 'choose the platform backend', | |
platform_backend_sources.items() | |
), | |
inquirer.Checkbox( | |
'renderer', 'choose the rendering backend(s)', | |
rendering_backend_sources.items() | |
), | |
inquirer.List( | |
'version', 'choose the version', | |
versions.items() | |
) | |
]) | |
if r is None: | |
print('none!') | |
return 1 | |
outdir: str = r['outdir'].rstrip('/') | |
if len(r['renderer']) == 0: | |
print('Notice: no renderer selected.') | |
files: list[tuple[str, str]] = [] | |
files += map(dup, imgui_sources) | |
platform_backend = r['platform'] | |
files += map(lambda x: (x, f'backends/{x}'), platform_backend[:-1]) | |
files.append((platform_backend[1], f'backends/{platform_backend[1]}')) | |
for backend in r['renderer']: | |
files += map(lambda x: (x, f'backends/{x}'), backend[:-1]) | |
files.append((backend[1], f'backends/{backend[1]}')) | |
print(f'Downloading the following files into {outdir}/:') | |
for fname, urlname in files: | |
print(f'- {fname} ({urlname})') | |
for fname, urlname in files: | |
download( | |
github_url(IMGUI_REPO, r['version'], urlname), | |
f'{outdir}/{fname}' | |
) | |
if __name__ == '__main__': | |
r = main() | |
if r is not None: exit(r) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment