From 7a3d6c4f154e56c9d0ed59e37560eaea35f4da48 Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Sat, 11 Apr 2020 07:07:28 -0600 Subject: [PATCH] Add ttf and net modules. Fix nimterop generation. Add separate modules for importing --- nim.cfg | 2 +- src/nim_sdl2/wrapper.nim | 37 ++--------- src/nim_sdl2_image/wrapper.nim | 28 +++----- src/nim_sdl2_mixer/wrapper.nim | 41 +++--------- src/nim_sdl2_net/wrapper.nim | 114 +++++++++++++++++++++++++++++++++ src/nim_sdl2_ttf/wrapper.nim | 113 ++++++++++++++++++++++++++++++++ src/sdl2.nim | 9 +-- src/sdl2_image.nim | 2 + src/sdl2_mixer.nim | 2 + src/sdl2_net.nim | 2 + src/sdl2_ttf.nim | 2 + 11 files changed, 262 insertions(+), 90 deletions(-) create mode 100644 src/nim_sdl2_net/wrapper.nim create mode 100644 src/nim_sdl2_ttf/wrapper.nim create mode 100644 src/sdl2_image.nim create mode 100644 src/sdl2_mixer.nim create mode 100644 src/sdl2_net.nim create mode 100644 src/sdl2_ttf.nim diff --git a/nim.cfg b/nim.cfg index b60a55c..32d62bd 100644 --- a/nim.cfg +++ b/nim.cfg @@ -1 +1 @@ --d:SDL_DL -d:SDL_Static -d:SDL_image_DL -d:SDL_mixer_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_image_DL -d:SDL_mixer_DL -d:SDL_net_DL -d:SDL_ttf_DL \ No newline at end of file diff --git a/src/nim_sdl2/wrapper.nim b/src/nim_sdl2/wrapper.nim index e0db939..7a4db38 100644 --- a/src/nim_sdl2/wrapper.nim +++ b/src/nim_sdl2/wrapper.nim @@ -12,14 +12,9 @@ getHeader( ) 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) + cSkipSymbol @["bool"] + # cDebug() + # cDisableCaching() cOverride: const KEYCODE_UNDERSCORE* = ('_').KeyCodeEnum @@ -149,28 +144,8 @@ cPlugin: 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") +when defined(SDL_Static): + cImport(SDL_Path, recurse = true) else: - cImport(SDL_Path, recurse = true) \ No newline at end of file + cImport(SDL_Path, recurse = true, dynlib = "SDL_LPath") \ No newline at end of file diff --git a/src/nim_sdl2_image/wrapper.nim b/src/nim_sdl2_image/wrapper.nim index eabac76..76ccfd5 100644 --- a/src/nim_sdl2_image/wrapper.nim +++ b/src/nim_sdl2_image/wrapper.nim @@ -1,10 +1,12 @@ -import os, strutils +import os, strutils, strformat import ../nim_sdl2/wrapper import nimterop/[cimport, build] const baseDir = currentSourcePath.parentDir().parentDir().parentDir() - srcDir = baseDir / "build" / "sdl2_image" + buildDir = baseDir / "build" + sdlIncludeDir = buildDir / "sdl2" / "include" + srcDir = buildDir / "sdl2_image" getHeader( "SDL_image.h", @@ -13,23 +15,9 @@ getHeader( altNames = "SDL2_image" ) -static: +# 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: @@ -118,7 +106,7 @@ cPlugin: sym.name = "KeyCodeEnum" -when not defined(SDL_Static): - cImport(SDL_image_Path, recurse = false, dynlib = "SDL_image_LPath") +when defined(SDL_Static): + cImport(SDL_image_Path, recurse = false, flags = &"-I={sdlIncludeDir}") else: - cImport(SDL_image_Path, recurse = false) + cImport(SDL_image_Path, recurse = false, dynlib = "SDL_image_LPath", flags = &"-I={sdlIncludeDir}") diff --git a/src/nim_sdl2_mixer/wrapper.nim b/src/nim_sdl2_mixer/wrapper.nim index e1140b7..6a4d3f5 100644 --- a/src/nim_sdl2_mixer/wrapper.nim +++ b/src/nim_sdl2_mixer/wrapper.nim @@ -1,10 +1,12 @@ -import os, strutils +import os, strutils, strformat import ../nim_sdl2/wrapper as sdl2_wrapper import nimterop/[cimport, build] const baseDir = currentSourcePath.parentDir().parentDir().parentDir() - srcDir = baseDir / "build" / "sdl2_mixer" + buildDir = baseDir / "build" + sdlIncludeDir = buildDir / "sdl2" / "include" + srcDir = buildDir / "sdl2_mixer" getHeader( "SDL_mixer.h", @@ -13,31 +15,9 @@ getHeader( 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 - +# static: + # cDebug() + # cDisableCaching() cPlugin: import strutils, nre @@ -124,8 +104,7 @@ cPlugin: if sym.name == "KeyCode": sym.name = "KeyCodeEnum" - -when not defined(SDL_Static): - cImport(SDL_mixer_Path, recurse = false, dynlib = "SDL_mixer_LPath") +when defined(SDL_Static): + cImport(SDL_mixer_Path, recurse = false, flags = &"-I={sdlIncludeDir}") else: - cImport(SDL_mixer_Path, recurse = false) + cImport(SDL_mixer_Path, recurse = false, dynlib = "SDL_mixer_LPath", flags = &"-I={sdlIncludeDir}") diff --git a/src/nim_sdl2_net/wrapper.nim b/src/nim_sdl2_net/wrapper.nim new file mode 100644 index 0000000..065c6e4 --- /dev/null +++ b/src/nim_sdl2_net/wrapper.nim @@ -0,0 +1,114 @@ +import os, strutils, strformat +import ../nim_sdl2/wrapper as sdl2_wrapper +import nimterop/[cimport, build] + +const + baseDir = currentSourcePath.parentDir().parentDir().parentDir() + buildDir = baseDir / "build" + sdlIncludeDir = buildDir / "sdl2" / "include" + srcDir = buildDir / "sdl2_net" + +getHeader( + "SDL_net.h", + dlurl = "https://www.libsdl.org/projects/SDL_net/release/SDL2_net-2.0.1.tar.gz", + outdir = srcDir, + altNames = "SDL2_net" +) + +# static: +# cDebug() +# cDisableCaching() + + +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 defined(SDL_Static): + cImport(SDL_net_Path, recurse = false, flags = &"-I={sdlIncludeDir}") + cImport(srcDir / "SDLnetsys.h", recurse = false, flags = &"-I={sdlIncludeDir}") +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 diff --git a/src/nim_sdl2_ttf/wrapper.nim b/src/nim_sdl2_ttf/wrapper.nim new file mode 100644 index 0000000..64d31fd --- /dev/null +++ b/src/nim_sdl2_ttf/wrapper.nim @@ -0,0 +1,113 @@ +import os, strutils, strformat +import ../nim_sdl2/wrapper as sdl2_wrapper +import nimterop/[cimport, build] + +const + baseDir = currentSourcePath.parentDir().parentDir().parentDir() + buildDir = baseDir / "build" + sdlDir = buildDir / "sdl2" + sdlIncludeDir = sdlDir / "include" + cmakeModPath = baseDir / "cmake" / "sdl2" + srcDir = buildDir / "sdl2_ttf" + +getHeader( + "SDL_ttf.h", + 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}" +) + +# static: +# cDebug() +# cDisableCaching() + +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 defined(SDL_Static): + cImport(SDL_ttf_Path, recurse = false, flags = &"-I={sdlIncludeDir}") +else: + cImport(SDL_ttf_Path, recurse = false, dynlib = "SDL_ttf_LPath", flags = &"-I={sdlIncludeDir}") diff --git a/src/sdl2.nim b/src/sdl2.nim index 2d575fb..8335308 100644 --- a/src/sdl2.nim +++ b/src/sdl2.nim @@ -1,7 +1,2 @@ -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 +import nim_sdl2 / wrapper +export wrapper \ No newline at end of file diff --git a/src/sdl2_image.nim b/src/sdl2_image.nim new file mode 100644 index 0000000..6acb7c3 --- /dev/null +++ b/src/sdl2_image.nim @@ -0,0 +1,2 @@ +import nim_sdl2_image / wrapper +export wrapper \ No newline at end of file diff --git a/src/sdl2_mixer.nim b/src/sdl2_mixer.nim new file mode 100644 index 0000000..94aed74 --- /dev/null +++ b/src/sdl2_mixer.nim @@ -0,0 +1,2 @@ +import nim_sdl2_mixer / wrapper +export wrapper \ No newline at end of file diff --git a/src/sdl2_net.nim b/src/sdl2_net.nim new file mode 100644 index 0000000..07f14ab --- /dev/null +++ b/src/sdl2_net.nim @@ -0,0 +1,2 @@ +import nim_sdl2_net / wrapper +export wrapper \ No newline at end of file diff --git a/src/sdl2_ttf.nim b/src/sdl2_ttf.nim new file mode 100644 index 0000000..088bd03 --- /dev/null +++ b/src/sdl2_ttf.nim @@ -0,0 +1,2 @@ +import nim_sdl2_ttf / wrapper +export wrapper \ No newline at end of file