commit c5af3b2214dfc1e8479a4b94a5faf6345139fa91 Author: Joey Yakimowich-Payne Date: Fri Apr 10 18:43:17 2020 -0600 WIP: not working diff --git a/nim.cfg b/nim.cfg new file mode 100644 index 0000000..b60a55c --- /dev/null +++ b/nim.cfg @@ -0,0 +1 @@ +-d:SDL_DL -d:SDL_Static -d:SDL_image_DL -d:SDL_mixer_DL \ No newline at end of file diff --git a/nim_sdl2.nimble b/nim_sdl2.nimble new file mode 100644 index 0000000..cb9fbfb --- /dev/null +++ b/nim_sdl2.nimble @@ -0,0 +1,16 @@ +# Package + +version = "0.1.0" +author = "Joey Yakimowich-Payne" +description = "SDL2 Autogenerated wrapper" +license = "MIT" +srcDir = "src" + + + +# Dependencies + +requires "nim >= 1.0.6", "nimterop#head" + +task buildSDL2, "Build SDL2 example": + exec "nimble c -f -r src/nim_sdl2/wrapper.nim" \ No newline at end of file diff --git a/src/nim_sdl2/wrapper.nim b/src/nim_sdl2/wrapper.nim new file mode 100644 index 0000000..e0db939 --- /dev/null +++ b/src/nim_sdl2/wrapper.nim @@ -0,0 +1,176 @@ +import os +import nimterop/[cimport, build] + +const + baseDir = currentSourcePath.parentDir().parentDir().parentDir() + srcDir = baseDir/"build"/"sdl2" + +getHeader( + "SDL.h", + dlurl = "https://www.libsdl.org/release/SDL2-2.0.12.tar.gz", + outdir = srcDir +) + +static: + cDebug() + cDisableCaching() +# # let contents = readFile(srcDir/"include/SDL_gpu.h") +# # let newContents = contents.replace(re"""#include "SDL.h"""", "#include ", limit=1). +# # replace(re"""#include "begin_code.h"""", "", limit=1). +# # replace(re"""#include "close_code.h"""", "", limit=1) + +# # writeFile(srcDir/"include/SDL_gpu.h", newContents) + +cOverride: + const KEYCODE_UNDERSCORE* = ('_').KeyCodeEnum + + type + BlitMap* {.incompleteStruct.} = object + RWops* {.incompleteStruct.} = object + + GameControllerBindType* = enum + CONTROLLER_BINDTYPE_NONE = 0, + CONTROLLER_BINDTYPE_BUTTON, + CONTROLLER_BINDTYPE_AXIS, + CONTROLLER_BINDTYPE_HAT + + ButtonBindHat* = object + hat*: int + hatMask*: int + + GameControllerButtonBind* = object + case bindType*: GameControllerBindType + of CONTROLLER_BINDTYPE_BUTTON: + button*: cint + of CONTROLLER_BINDTYPE_AXIS: + axis*: cint + of CONTROLLER_BINDTYPE_HAT: + hat*: ButtonBindHat + of CONTROLLER_BINDTYPE_NONE: + discard + + # This should work, but also overrides some types + # Nimterop bug? @genotrance, comment these out + # and uncomment the checks in onSymbol for it to kind of work + # (it compiles but overrides nim types) + Uint64* = uint64 + Uint32* = uint32 + Uint16* = uint16 + Uint8* = uint8 + + Sint64* = int64 + Sint32* = int32 + Sint16* = int16 + Sint8* = int8 + Bool* = bool + +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.} = + # Get rid of leading and trailing underscores + # sym.name = sym.name.strip(chars = {'_'}) + + # Remove prefixes or suffixes from procs + if sym.name == "__MACOSX__": + sym.name = "MACOSX" + if sym.kind == nskProc or sym.kind == nskType or sym.kind == nskConst: + if sym.name != "_": + sym.name = sym.name.replace(re"^_+", "") + sym.name = sym.name.replace(re"_+$", "") + 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" + + # if sym.name == "Uint64": + # sym.name = "uint64" + # if sym.name == "Uint32": + # sym.name = "uint32" + # if sym.name == "Uint16": + # sym.name = "uint16" + # if sym.name == "Uint8": + # sym.name = "uint8" + + # if sym.name == "Sint64": + # sym.name = "int64" + # if sym.name == "Sint32": + # sym.name = "int32" + # if sym.name == "Sint16": + # sym.name = "int16" + # if sym.name == "Sint8": + # sym.name = "int8" + # if sym.name == "Bool": + # sym.name = "bool" + + +when not defined(SDL_Static): + cImport(SDL_Path, recurse = true, dynlib = "SDL_LPath") +else: + cImport(SDL_Path, recurse = true) \ No newline at end of file diff --git a/src/nim_sdl2_image/wrapper.nim b/src/nim_sdl2_image/wrapper.nim new file mode 100644 index 0000000..eabac76 --- /dev/null +++ b/src/nim_sdl2_image/wrapper.nim @@ -0,0 +1,124 @@ +import os, strutils +import ../nim_sdl2/wrapper +import nimterop/[cimport, build] + +const + baseDir = currentSourcePath.parentDir().parentDir().parentDir() + srcDir = baseDir / "build" / "sdl2_image" + +getHeader( + "SDL_image.h", + dlurl = "https://www.libsdl.org/projects/SDL_image/release/SDL2_image-2.0.5.tar.gz", + outdir = srcDir, + altNames = "SDL2_image" +) + +static: + # cDebug() + # cDisableCaching() + let contents = readFile(srcDir/"SDL_image.h") + let newContents = contents.replace("""#include "SDL.h" +#include "SDL_version.h" +#include "begin_code.h"""", """ +#include +#include +#include +"""). + replace("""#include "close_code.h"""", "#include ") + + writeFile(srcDir/"SDL_image.h", newContents) + +# cOverride: +# type + + +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.} = + # Get rid of leading and trailing underscores + # sym.name = sym.name.strip(chars = {'_'}) + + # Remove prefixes or suffixes from procs + if sym.name == "__MACOSX__": + sym.name = "MACOSX" + if sym.kind == nskProc or sym.kind == nskType or sym.kind == nskConst: + if sym.name != "_": + sym.name = sym.name.replace(re"^_+", "") + sym.name = sym.name.replace(re"_+$", "") + 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 not defined(SDL_Static): + cImport(SDL_image_Path, recurse = false, dynlib = "SDL_image_LPath") +else: + cImport(SDL_image_Path, recurse = false) diff --git a/src/nim_sdl2_mixer/wrapper.nim b/src/nim_sdl2_mixer/wrapper.nim new file mode 100644 index 0000000..e1140b7 --- /dev/null +++ b/src/nim_sdl2_mixer/wrapper.nim @@ -0,0 +1,131 @@ +import os, strutils +import ../nim_sdl2/wrapper as sdl2_wrapper +import nimterop/[cimport, build] + +const + baseDir = currentSourcePath.parentDir().parentDir().parentDir() + srcDir = baseDir / "build" / "sdl2_mixer" + +getHeader( + "SDL_mixer.h", + dlurl = "https://www.libsdl.org/projects/SDL_mixer/release/SDL2_mixer-2.0.4.tar.gz", + outdir = srcDir, + altNames = "SDL2_mixer" +) + +static: + cDebug() + cDisableCaching() + let contents = readFile(srcDir/"SDL_mixer.h") + let newContents = contents.replace("""#include "SDL_stdinc.h" +#include "SDL_rwops.h" +#include "SDL_audio.h" +#include "SDL_endian.h" +#include "SDL_version.h" +#include "begin_code.h" +""", """ +#include +#include +#include +#include +#include +#include +"""). + replace("""#include "close_code.h"""", "#include ") + + writeFile(srcDir/"SDL_mixer.h", newContents) + +# cOverride: +# type + + +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.} = + # Get rid of leading and trailing underscores + # sym.name = sym.name.strip(chars = {'_'}) + + # Remove prefixes or suffixes from procs + if sym.name == "__MACOSX__": + sym.name = "MACOSX" + if sym.kind == nskProc or sym.kind == nskType or sym.kind == nskConst: + if sym.name != "_": + sym.name = sym.name.replace(re"^_+", "") + sym.name = sym.name.replace(re"_+$", "") + 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 not defined(SDL_Static): + cImport(SDL_mixer_Path, recurse = false, dynlib = "SDL_mixer_LPath") +else: + cImport(SDL_mixer_Path, recurse = false) diff --git a/src/sdl2.nim b/src/sdl2.nim new file mode 100644 index 0000000..2d575fb --- /dev/null +++ b/src/sdl2.nim @@ -0,0 +1,7 @@ +import nim_sdl2 / wrapper as sdl2_wrapper +import nim_sdl2_image / wrapper as sdl2_image_wrapper +import nim_sdl2_mixer / wrapper as sdl2_mixer_wrapper + +export sdl2_wrapper +export sdl2_image_wrapper +export sdl2_mixer_wrapper \ No newline at end of file