From aaedb8f6ab2c18839044f0c99df4fb740cf44eba Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Sat, 11 Apr 2020 12:09:18 -0600 Subject: [PATCH] Add sdl gpu and refactor some code --- nim.cfg | 2 +- src/nim_sdl2/wrapper.nim | 20 +++++- src/nim_sdl2_gpu/wrapper.nim | 118 +++++++++++++++++++++++++++++++++ src/nim_sdl2_image/wrapper.nim | 6 +- src/nim_sdl2_mixer/wrapper.nim | 6 +- src/nim_sdl2_net/wrapper.nim | 10 +-- src/nim_sdl2_ttf/wrapper.nim | 9 +-- src/sdl2_all.nim | 13 ++++ src/sdl2_gpu.nim | 2 + 9 files changed, 168 insertions(+), 18 deletions(-) create mode 100644 src/nim_sdl2_gpu/wrapper.nim create mode 100644 src/sdl2_all.nim create mode 100644 src/sdl2_gpu.nim diff --git a/nim.cfg b/nim.cfg index 32d62bd..756b21d 100644 --- a/nim.cfg +++ b/nim.cfg @@ -1 +1 @@ --d:SDL_DL -d:SDL_Static -d:SDL_image_Static -d:SDL_mixer_Static -d:SDL_net_Static -d:SDL_ttf_Static -d:SDL_image_DL -d:SDL_mixer_DL -d:SDL_net_DL -d:SDL_ttf_DL \ No newline at end of file +-d:SDL_DL -d:SDL_Static -d:SDL_image_Static -d:SDL_mixer_Static -d:SDL_net_Static -d:SDL_ttf_Static -d:SDL_gpu_Static -d:SDL_image_DL -d:SDL_mixer_DL -d:SDL_net_DL -d:SDL_ttf_DL -d:SDL_gpu_Git \ No newline at end of file diff --git a/src/nim_sdl2/wrapper.nim b/src/nim_sdl2/wrapper.nim index 7a4db38..df201cc 100644 --- a/src/nim_sdl2/wrapper.nim +++ b/src/nim_sdl2/wrapper.nim @@ -3,7 +3,8 @@ import nimterop/[cimport, build] const baseDir = currentSourcePath.parentDir().parentDir().parentDir() - srcDir = baseDir/"build"/"sdl2" + srcDir = baseDir / "build" / "sdl2" + buildDir = srcDir / "buildcache" getHeader( "SDL.h", @@ -148,4 +149,19 @@ cPlugin: when defined(SDL_Static): cImport(SDL_Path, recurse = true) else: - cImport(SDL_Path, recurse = true, dynlib = "SDL_LPath") \ No newline at end of file + cImport(SDL_Path, recurse = true, dynlib = "SDL_LPath") + +proc getDynlibExt(): string = + when defined(windows): + result = ".dll" + elif defined(linux): + result = ".so[0-9.]*" + elif defined(macosx): + result = ".dylib[0-9.]*" + +proc findDynlib(): string = + const pathRegex = "(lib)?SDL2[_-]?(static)?[0-9.\\-]*\\" & getDynlibExt() + return findFile(pathRegex, buildDir, regex = true) + +const SDLDyLibPath* = findDynlib() +const SDLMainLib* = buildDir / "libSDL2main.a" \ No newline at end of file diff --git a/src/nim_sdl2_gpu/wrapper.nim b/src/nim_sdl2_gpu/wrapper.nim new file mode 100644 index 0000000..974d099 --- /dev/null +++ b/src/nim_sdl2_gpu/wrapper.nim @@ -0,0 +1,118 @@ +import os, strformat +import ../nim_sdl2/wrapper +import nimterop/[build, cimport] + +const + baseDir = currentSourcePath.parentDir().parentDir().parentDir() + buildDir = baseDir / "build" + sdlDir = buildDir / "sdl2" + sdlIncludeDir = sdlDir / "include" + cmakeModPath = baseDir / "cmake" / "sdl2" + srcDir = buildDir / "sdl2_gpu" + +getHeader( + "SDL_gpu.h", + giturl = "https://github.com/grimfang4/sdl-gpu", + outdir = srcDir, + altNames = "SDL2_gpu", + cmakeFlags = &"-DCMAKE_C_FLAGS=-I{sdlIncludeDir} -DCMAKE_MODULE_PATH={cmakeModPath} -DSDL2_LIBRARY={SDLDyLibPath} " & + &"-DSDL2MAIN_LIBRARY={SDLMainLib} -DSDL2_PATH={sdlDir} -DSDL2_INCLUDE_DIR={sdlIncludeDir} -DSDL_gpu_BUILD_DEMOS=OFF" +) + +static: + cDebug() + cDisableCaching() + +cOverride: + type + LogLevelEnum* = enum + LOG_LEVEL_INFO = 0 + LOG_LEVEL_WARNING + LOG_LEVEL_ERROR + +cPlugin: + import strutils, nre + import sets + + const EVENT_TYPES = toHashSet([ + "FIRSTEVENT", + "QUIT", + "APP_TERMINATING", + "APP_LOWMEMORY", + "APP_WILLENTERBACKGROUND", + "APP_DIDENTERBACKGROUND", + "APP_WILLENTERFOREGROUND", + "APP_DIDENTERFOREGROUND", + "DISPLAYEVENT", + "WINDOWEVENT", + "SYSWMEVENT", + "KEYDOWN", + "KEYUP", + "TEXTEDITING", + "TEXTINPUT", + "KEYMAPCHANGED", + "MOUSEMOTION", + "MOUSEBUTTONDOWN", + "MOUSEBUTTONUP", + "MOUSEWHEEL", + "JOYAXISMOTION", + "JOYBALLMOTION", + "JOYHATMOTION", + "JOYBUTTONDOWN", + "JOYBUTTONUP", + "JOYDEVICEADDED", + "JOYDEVICEREMOVED", + "CONTROLLERAXISMOTION", + "CONTROLLERBUTTONDOWN", + "CONTROLLERBUTTONUP", + "CONTROLLERDEVICEADDED", + "CONTROLLERDEVICEREMOVED", + "CONTROLLERDEVICEREMAPPED", + "FINGERDOWN", + "FINGERUP", + "FINGERMOTION", + "DOLLARGESTURE", + "DOLLARRECORD", + "MULTIGESTURE", + "CLIPBOARDUPDATE", + "DROPFILE", + "DROPTEXT", + "DROPBEGIN", + "DROPCOMPLETE", + "AUDIODEVICEADDED", + "AUDIODEVICEREMOVED", + "SENSORUPDATE", + "RENDER_TARGETS_RESET", + "RENDER_DEVICE_RESET", + "USEREVENT", + "LASTEVENT", + ]) + + # Symbol renaming examples + proc onSymbol*(sym: var Symbol) {.exportc, dynlib.} = + # Remove prefixes or suffixes from procs + sym.name = sym.name.replace(re"^GPU_", "") + + if sym.kind == nskProc or sym.kind == nskType or sym.kind == nskConst: + if sym.name != "_": + sym.name = sym.name.strip(chars = {'_'}) + + sym.name = sym.name.replace(re"^SDL_", "") + + if sym.name.startsWith("SDLK_"): + sym.name = sym.name.replace("SDLK_", "KEYCODE_") + + if EVENT_TYPES.contains(sym.name): + sym.name = "EVENT_" & sym.name + + if sym.name == "ThreadID": + sym.name = "CurrentThreadID" + if sym.name == "GLContextResetNotification": + sym.name = "GLContextResetNotificationEnum" + if sym.name == "KeyCode": + sym.name = "KeyCodeEnum" + +when defined(SDL_gpu_Static): + cImport(SDL_gpuPath, recurse = true, flags = &"-I={sdlIncludeDir} -f=ast2") +else: + cImport(SDL_gpuPath, recurse = true, dynlib = "SDL_gpuLPath", flags = &"-I={sdlIncludeDir} -f=ast2") \ No newline at end of file diff --git a/src/nim_sdl2_image/wrapper.nim b/src/nim_sdl2_image/wrapper.nim index 76ccfd5..30a6039 100644 --- a/src/nim_sdl2_image/wrapper.nim +++ b/src/nim_sdl2_image/wrapper.nim @@ -106,7 +106,7 @@ cPlugin: sym.name = "KeyCodeEnum" -when defined(SDL_Static): - cImport(SDL_image_Path, recurse = false, flags = &"-I={sdlIncludeDir}") +when defined(SDL_image_Static): + cImport(SDL_image_Path, recurse = false, flags = &"-I={sdlIncludeDir} -f=ast2") else: - cImport(SDL_image_Path, recurse = false, dynlib = "SDL_image_LPath", flags = &"-I={sdlIncludeDir}") + cImport(SDL_image_Path, recurse = false, dynlib = "SDL_image_LPath", flags = &"-I={sdlIncludeDir} -f=ast2") diff --git a/src/nim_sdl2_mixer/wrapper.nim b/src/nim_sdl2_mixer/wrapper.nim index 6a4d3f5..6af7a35 100644 --- a/src/nim_sdl2_mixer/wrapper.nim +++ b/src/nim_sdl2_mixer/wrapper.nim @@ -104,7 +104,7 @@ cPlugin: if sym.name == "KeyCode": sym.name = "KeyCodeEnum" -when defined(SDL_Static): - cImport(SDL_mixer_Path, recurse = false, flags = &"-I={sdlIncludeDir}") +when defined(SDL_mixer_Static): + cImport(SDL_mixer_Path, recurse = false, flags = &"-I={sdlIncludeDir} -f=ast2") else: - cImport(SDL_mixer_Path, recurse = false, dynlib = "SDL_mixer_LPath", flags = &"-I={sdlIncludeDir}") + cImport(SDL_mixer_Path, recurse = false, dynlib = "SDL_mixer_LPath", flags = &"-I={sdlIncludeDir} -f=ast2") diff --git a/src/nim_sdl2_net/wrapper.nim b/src/nim_sdl2_net/wrapper.nim index 065c6e4..4a4d824 100644 --- a/src/nim_sdl2_net/wrapper.nim +++ b/src/nim_sdl2_net/wrapper.nim @@ -106,9 +106,9 @@ cPlugin: sym.name = "KeyCodeEnum" -when defined(SDL_Static): - cImport(SDL_net_Path, recurse = false, flags = &"-I={sdlIncludeDir}") - cImport(srcDir / "SDLnetsys.h", recurse = false, flags = &"-I={sdlIncludeDir}") +when defined(SDL_net_Static): + cImport(SDL_net_Path, recurse = false, flags = &"-I={sdlIncludeDir} -f=ast2") + cImport(srcDir / "SDLnetsys.h", recurse = false, flags = &"-I={sdlIncludeDir} -f=ast2") else: - cImport(SDL_net_Path, recurse = false, dynlib = "SDL_net_LPath", flags = &"-I={sdlIncludeDir}") - cImport(srcDir / "SDLnetsys.h", recurse = false, flags = &"-I={sdlIncludeDir}") \ No newline at end of file + cImport(SDL_net_Path, recurse = false, dynlib = "SDL_net_LPath", flags = &"-I={sdlIncludeDir} -f=ast2") + cImport(srcDir / "SDLnetsys.h", recurse = false, flags = &"-I={sdlIncludeDir} -f=ast2") \ No newline at end of file diff --git a/src/nim_sdl2_ttf/wrapper.nim b/src/nim_sdl2_ttf/wrapper.nim index 64d31fd..a21cebd 100644 --- a/src/nim_sdl2_ttf/wrapper.nim +++ b/src/nim_sdl2_ttf/wrapper.nim @@ -15,7 +15,8 @@ getHeader( dlurl = "https://www.libsdl.org/projects/SDL_ttf/release/SDL2_ttf-2.0.15.tar.gz", outdir = srcDir, altNames = "SDL2_ttf", - cmakeFlags = &"-DCMAKE_C_FLAGS=-I{sdlIncludeDir} -DCMAKE_MODULE_PATH={cmakeModPath} -DSDL2_LIBRARY=2.0.12 -DSDL2_PATH={sdlDir}" + cmakeFlags = &"-DCMAKE_C_FLAGS=-I{sdlIncludeDir} -DCMAKE_MODULE_PATH={cmakeModPath} " & + &"-DSDL2MAIN_LIBRARY={SDLMainLib} -DSDL2_LIBRARY={SDLDyLibPath} -DSDL2_PATH={sdlDir}" ) # static: @@ -107,7 +108,7 @@ cPlugin: if sym.name == "KeyCode": sym.name = "KeyCodeEnum" -when defined(SDL_Static): - cImport(SDL_ttf_Path, recurse = false, flags = &"-I={sdlIncludeDir}") +when defined(SDL_ttf_Static): + cImport(SDL_ttf_Path, recurse = false, flags = &"-I={sdlIncludeDir} -f=ast2") else: - cImport(SDL_ttf_Path, recurse = false, dynlib = "SDL_ttf_LPath", flags = &"-I={sdlIncludeDir}") + cImport(SDL_ttf_Path, recurse = false, dynlib = "SDL_ttf_LPath", flags = &"-I={sdlIncludeDir} -f=ast2") diff --git a/src/sdl2_all.nim b/src/sdl2_all.nim new file mode 100644 index 0000000..639ed21 --- /dev/null +++ b/src/sdl2_all.nim @@ -0,0 +1,13 @@ +import sdl2 +import sdl2_gpu +import sdl2_image +import sdl2_mixer +import sdl2_net +import sdl2_ttf + +export sdl2 +export sdl2_gpu +export sdl2_image +export sdl2_mixer +export sdl2_net +export sdl2_ttf \ No newline at end of file diff --git a/src/sdl2_gpu.nim b/src/sdl2_gpu.nim new file mode 100644 index 0000000..bcc6598 --- /dev/null +++ b/src/sdl2_gpu.nim @@ -0,0 +1,2 @@ +import nim_sdl2_gpu / wrapper +export wrapper \ No newline at end of file