|
| 1 | +{.push raises: [].} |
| 2 | + |
| 3 | +import sprite {.all.} |
| 4 | +import types |
| 5 | + |
| 6 | +type |
| 7 | + LuaStatePtr* = pointer |
| 8 | + LuaNimFunction* = proc (L: LuaStatePtr): cint {.cdecl, raises: [].} |
| 9 | + LuaUDObject* {.importc: "LuaUDObject", header: "pd_api.h", bycopy.} = object |
| 10 | + |
| 11 | + LValType* {.importc: "l_valtype", header: "pd_api.h".} = enum |
| 12 | + kInt, kFloat, kStr |
| 13 | + |
| 14 | + # LuaReg* {.importc: "lua_reg", header: "pd_api.h", bycopy.} = object |
| 15 | + # name* {.importc: "name".}: cstring |
| 16 | + # `func`* {.importc: "func".}: LuaNimFunction |
| 17 | + |
| 18 | + # LuaType* {.size: sizeof(cint).} = enum |
| 19 | + # kTypeNil, kTypeBool, kTypeInt, kTypeFloat, kTypeString, kTypeTable, |
| 20 | + # kTypeFunction, |
| 21 | + # kTypeThread, kTypeObject |
| 22 | + |
| 23 | + LuaType* {.importc: "enum LuaType", header: "pd_api.h", bycopy.} = enum |
| 24 | + kTypeNil, kTypeBool, kTypeInt, kTypeFloat, kTypeString, kTypeTable, |
| 25 | + kTypeFunction, |
| 26 | + kTypeThread, kTypeObject |
| 27 | + |
| 28 | + |
| 29 | +type |
| 30 | + # INNER_C_UNION_pd_api_lua_1* {.importc: "lua_val::no_name", |
| 31 | + # header: "pd_api.h", bycopy, union.} = object |
| 32 | + # intval* {.importc: "intval".}: cuint |
| 33 | + # floatval* {.importc: "floatval".}: cfloat |
| 34 | + # strval* {.importc: "strval".}: cstring |
| 35 | + |
| 36 | + # LuaVal* {.importc: "lua_val", header: "pd_api.h", bycopy.} = object |
| 37 | + # name* {.importc: "name".}: cstring |
| 38 | + # `type`* {.importc: "type".}: LValType |
| 39 | + # v* {.importc: "v".}: INNER_C_UNION_pd_api_lua_1 |
| 40 | + |
| 41 | + PlaydateLua* {.importc: "const struct playdate_lua", header: "pd_api.h", |
| 42 | + bycopy.} = object |
| 43 | + ## these two return 1 on success, else 0 with an error message in outErr |
| 44 | + addFunction {.importc: "addFunction".}: proc (f: LuaNimFunction; |
| 45 | + name: cstring; outErr: ptr cstring): cint {.cdecl, raises: [].} |
| 46 | + # registerClass {.importc: "registerClass".}: proc (name: cstring; |
| 47 | + # reg: ptr LuaReg; vals: ptr LuaVal; |
| 48 | + # isstatic: cint; outErr: ptr cstring): cint {.cdecl.} |
| 49 | + pushFunction* {.importc: "pushFunction".}: proc (f: LuaNimFunction) {.cdecl.} |
| 50 | + indexMetatable {.importc: "indexMetatable".}: proc (): cint {.cdecl.} |
| 51 | + stop* {.importc: "stop".}: proc () {.cdecl, raises: [].} |
| 52 | + start* {.importc: "start".}: proc () {.cdecl, raises: [].} |
| 53 | + ## stack operations |
| 54 | + getArgCount {.importc: "getArgCount".}: proc (): cint {.cdecl, raises: [].} |
| 55 | + getArgType {.importc: "getArgType".}: proc (pos: cint; outClass: ptr cstring): LuaType {.cdecl, raises: [].} |
| 56 | + argIsNil {.importc: "argIsNil".}: proc (pos: cint): cint {.cdecl, raises: [].} |
| 57 | + getArgBool {.importc: "getArgBool".}: proc (pos: cint): cint {.cdecl, raises: [].} |
| 58 | + getArgInt {.importc: "getArgInt".}: proc (pos: cint): cint {.cdecl, raises: [].} |
| 59 | + getArgFloat {.importc: "getArgFloat".}: proc (pos: cint): cfloat {.cdecl, raises: [].} |
| 60 | + getArgString {.importc: "getArgString".}: proc (pos: cint): cstring {.cdecl, raises: [].} |
| 61 | + getArgBytes {.importc: "getArgBytes".}: proc (pos: cint; outlen: ptr csize_t): cstring {.cdecl, raises: [].} |
| 62 | + getArgObject {.importc: "getArgObject".}: proc (pos: cint; `type`: cstring; outud: ptr ptr LuaUDObject): pointer {.cdecl.} |
| 63 | + getBitmap {.importc: "getBitmap".}: proc (pos: cint): LCDBitmapPtr {.cdecl.} |
| 64 | + getSprite {.importc: "getSprite".}: proc (pos: cint): LCDSpritePtr {.cdecl.} |
| 65 | + ## for returning values back to Lua |
| 66 | + pushNil* {.importc: "pushNil".}: proc () {.cdecl, raises: [].} |
| 67 | + pushBool {.importc: "pushBool".}: proc (val: cint) {.cdecl, raises: [].} |
| 68 | + pushInt {.importc: "pushInt".}: proc (val: cint) {.cdecl, raises: [].} |
| 69 | + pushFloat {.importc: "pushFloat".}: proc (val: cfloat) {.cdecl, raises: [].} |
| 70 | + pushString {.importc: "pushString".}: proc (str: cstring) {.cdecl, raises: [].} |
| 71 | + pushBytes {.importc: "pushBytes".}: proc (str: cstring; len: csize_t) {.cdecl, raises: [].} |
| 72 | + pushBitmap {.importc: "pushBitmap".}: proc (bitmap: LCDBitmapPtr) {.cdecl.} |
| 73 | + pushSprite {.importc: "pushSprite".}: proc (sprite: LCDSpritePtr) {.cdecl.} |
| 74 | + pushObject {.importc: "pushObject".}: proc (obj: pointer; `type`: cstring; nValues: cint): ptr LuaUDObject {.cdecl.} |
| 75 | + retainObject {.importc: "retainObject".}: proc (obj: ptr LuaUDObject): ptr LuaUDObject {.cdecl.} |
| 76 | + releaseObject {.importc: "releaseObject".}: proc (obj: ptr LuaUDObject) {.cdecl.} |
| 77 | + setUserValue {.importc: "setUserValue".}: proc (obj: ptr LuaUDObject; slot: cuint) {.cdecl.} |
| 78 | + ## sets item on top of stack and pops it |
| 79 | + getUserValue {.importc: "getUserValue".}: proc (obj: ptr LuaUDObject; slot: cuint): cint {.cdecl.} |
| 80 | + ## pushes item at slot to top of stack, returns stack position |
| 81 | + ## calling lua from C has some overhead. use sparingly! |
| 82 | + callFunction {.importc: "callFunction".}: proc (name: cstring; nargs: cint; |
| 83 | + outerr: ptr cstring): cint {.cdecl, raises: [].} |
| 84 | + |
0 commit comments