1 module pixelperfectengine.scripting.lualib; 2 3 import pixelperfectengine.scripting.lua; 4 import pixelperfectengine.scripting.globals; 5 6 import pixelperfectengine.graphics.layers.interfaces; 7 import pixelperfectengine.graphics.layers.base; 8 import pixelperfectengine.graphics.layers.trnstilelayer; 9 import pixelperfectengine.graphics.raster; 10 import pixelperfectengine.graphics.bitmap; 11 12 import pixelperfectengine.system.file; 13 14 import bindbc.lua; 15 16 import std.conv : to; 17 import std.string : toLowerInPlace; 18 /** 19 * Registers the PPE standard library for a Lua script, so engine functions can be called through a Lua script. 20 * Params: 21 * state = the state where the scripts need to be registered. 22 */ 23 public void registerLibForScripting(lua_State* state) { 24 lua_register(state, "setPaletteIndex", ®isterDFunction!setPaletteIndex); 25 lua_register(state, "getPaletteIndex", ®isterDFunction!getPaletteIndex); 26 lua_register(state, "getLayer", ®isterDFunction!getLayer); 27 28 lua_register(state, "getLayerType", ®isterDFunction!(getLayerType)); 29 lua_register(state, "setLayerRenderingMode", ®isterDFunction!setLayerRenderingMode); 30 lua_register(state, "scrollLayer", ®isterDFunction!(scrollLayer)); 31 lua_register(state, "relScrollLayer", ®isterDFunction!(relScrollLayer)); 32 lua_register(state, "getLayerScrollX", ®isterDDelegate!(Layer.getSX)); 33 lua_register(state, "getLayerScrollY", ®isterDDelegate!(Layer.getSY)); 34 35 lua_register(state, "readMapping", ®isterDFunction!readMapping); 36 lua_register(state, "tileByPixel", ®isterDFunction!tileByPixel); 37 lua_register(state, "writeMapping", ®isterDFunction!writeMapping); 38 lua_register(state, "getTileWidth", ®isterDDelegate!(ITileLayer.getTileWidth)); 39 lua_register(state, "getTileHeight", ®isterDDelegate!(ITileLayer.getTileHeight)); 40 lua_register(state, "getMapWidth", ®isterDDelegate!(ITileLayer.getMX)); 41 lua_register(state, "getMapHeight", ®isterDDelegate!(ITileLayer.getMY)); 42 lua_register(state, "getTileWidth", ®isterDDelegate!(ITileLayer.getTX)); 43 lua_register(state, "getTileHeight", ®isterDDelegate!(ITileLayer.getTY)); 44 lua_register(state, "clearTilemap", ®isterDDelegate!(ITileLayer.clearTilemap)); 45 lua_register(state, "addTile", ®isterDDelegate!(ITileLayer.addTile)); 46 lua_register(state, "getTile", ®isterDDelegate!(ITileLayer.getTile)); 47 lua_register(state, "removeTile", ®isterDDelegate!(ITileLayer.removeTile)); 48 49 lua_register(state, "ttl_setA", ®isterDFunction!ttl_setA); 50 lua_register(state, "ttl_setB", ®isterDFunction!ttl_setB); 51 lua_register(state, "ttl_setC", ®isterDFunction!ttl_setC); 52 lua_register(state, "ttl_setD", ®isterDFunction!ttl_setD); 53 lua_register(state, "ttl_setx_0", ®isterDFunction!ttl_setx_0); 54 lua_register(state, "ttl_sety_0", ®isterDFunction!ttl_sety_0); 55 lua_register(state, "ttl_getA", ®isterDFunction!ttl_getA); 56 lua_register(state, "ttl_getB", ®isterDFunction!ttl_getB); 57 lua_register(state, "ttl_getC", ®isterDFunction!ttl_getC); 58 lua_register(state, "ttl_getD", ®isterDFunction!ttl_getD); 59 lua_register(state, "ttl_getx_0", ®isterDFunction!ttl_getx_0); 60 lua_register(state, "ttl_gety_0", ®isterDFunction!ttl_gety_0); 61 62 lua_register(state, "moveSprite", ®isterDFunction!(moveSprite)); 63 lua_register(state, "relMoveSprite", ®isterDFunction!(relMoveSprite)); 64 lua_register(state, "getSpriteCoordinate", ®isterDDelegate!(ISpriteLayer.getSpriteCoordinate)); 65 lua_register(state, "setSpriteSlice", ®isterDFunction!setSpriteSlice); 66 lua_register(state, "getSpriteSlice", ®isterDDelegate!(ISpriteLayer.getSlice)); 67 lua_register(state, "addSprite", ®isterDDelegate!(ISpriteLayer.addSprite)); 68 lua_register(state, "removeSprite", ®isterDDelegate!(ISpriteLayer.removeSprite)); 69 lua_register(state, "getPaletteID", ®isterDDelegate!(ISpriteLayer.getPaletteID)); 70 lua_register(state, "setPaletteID", ®isterDDelegate!(ISpriteLayer.setPaletteID)); 71 lua_register(state, "scaleSpriteHoriz", ®isterDDelegate!(ISpriteLayer.scaleSpriteHoriz)); 72 lua_register(state, "scaleSpriteVert", ®isterDDelegate!(ISpriteLayer.scaleSpriteVert)); 73 lua_register(state, "getScaleSpriteHoriz", ®isterDDelegate!(ISpriteLayer.getScaleSpriteHoriz)); 74 lua_register(state, "getScaleSpriteVert", ®isterDDelegate!(ISpriteLayer.getScaleSpriteVert)); 75 76 lua_register(state, "getBitmapWidth", ®isterDDelegate!(ABitmap.width)); 77 lua_register(state, "getBitmapHeight", ®isterDDelegate!(ABitmap.height)); 78 } 79 package void scrollLayer(Layer l, LuaVar x, LuaVar y) { 80 l.scroll(cast(int)x, cast(int)y); 81 } 82 package void relScrollLayer(Layer l, LuaVar x, LuaVar y) { 83 l.relScroll(cast(int)x, cast(int)y); 84 } 85 package uint getPaletteIndex(ushort n) @safe @nogc nothrow { 86 return mainRaster.getPaletteIndex(n).base; 87 } 88 package uint setPaletteIndex(ushort n, uint c) @nogc nothrow { 89 Color c0; 90 c0.base = c; 91 return mainRaster.setPaletteIndex(n, c0).base; 92 } 93 package LuaVar getLayer(int n) { 94 Layer l = mainRaster.getLayer(n); 95 if (l is null) 96 return LuaVar.voidType(); 97 else 98 return LuaVar(l); 99 } 100 package string getLayerType(Layer l) { 101 return l.getLayerType().to!string(); 102 } 103 package long readMapping(void* target, int x, int y) @nogc nothrow { 104 ITileLayer l = cast(ITileLayer)target; 105 MappingElement me = l.readMapping(x, y); 106 return *cast(uint*)&me; 107 } 108 package long tileByPixel(void* target, int x, int y) @nogc nothrow { 109 ITileLayer l = cast(ITileLayer)target; 110 MappingElement me = l.tileByPixel(x, y); 111 return *cast(uint*)&me; 112 } 113 package bool setLayerRenderingMode(Layer target, string mode) { 114 char[] mode0 = mode.dup; 115 toLowerInPlace(mode0); 116 switch (mode0) { 117 case "copy": 118 target.setRenderingMode(RenderingMode.Copy); 119 break; 120 case "blitter": 121 target.setRenderingMode(RenderingMode.Blitter); 122 break; 123 case "alphablend": 124 target.setRenderingMode(RenderingMode.AlphaBlend); 125 break; 126 case "multiply": 127 target.setRenderingMode(RenderingMode.Multiply); 128 break; 129 case "multiplybl": 130 target.setRenderingMode(RenderingMode.MultiplyBl); 131 break; 132 case "screen": 133 target.setRenderingMode(RenderingMode.Screen); 134 break; 135 case "screenbl": 136 target.setRenderingMode(RenderingMode.ScreenBl); 137 break; 138 case "add": 139 target.setRenderingMode(RenderingMode.Add); 140 break; 141 case "addbl": 142 target.setRenderingMode(RenderingMode.AddBl); 143 break; 144 case "subtract": 145 target.setRenderingMode(RenderingMode.Subtract); 146 break; 147 case "subtractbl": 148 target.setRenderingMode(RenderingMode.SubtractBl); 149 break; 150 case "diff": 151 target.setRenderingMode(RenderingMode.Diff); 152 break; 153 case "diffbl": 154 target.setRenderingMode(RenderingMode.DiffBl); 155 break; 156 case "and": 157 target.setRenderingMode(RenderingMode.AND); 158 break; 159 case "or": 160 target.setRenderingMode(RenderingMode.OR); 161 break; 162 case "xor": 163 target.setRenderingMode(RenderingMode.XOR); 164 break; 165 default: 166 return false; 167 } 168 return true; 169 } 170 package void setSpriteRenderingMode(ISpriteLayer target, int n, string mode) { 171 char[] mode0 = mode.dup; 172 toLowerInPlace(mode0); 173 switch (mode0) { 174 case "copy": 175 target.setSpriteRenderingMode(n, RenderingMode.Copy); 176 break; 177 case "blitter": 178 target.setSpriteRenderingMode(n, RenderingMode.Blitter); 179 break; 180 case "alphablend": 181 target.setSpriteRenderingMode(n, RenderingMode.AlphaBlend); 182 break; 183 case "multiply": 184 target.setSpriteRenderingMode(n, RenderingMode.Multiply); 185 break; 186 case "multiplybl": 187 target.setSpriteRenderingMode(n, RenderingMode.MultiplyBl); 188 break; 189 case "screen": 190 target.setSpriteRenderingMode(n, RenderingMode.Screen); 191 break; 192 case "screenbl": 193 target.setSpriteRenderingMode(n, RenderingMode.ScreenBl); 194 break; 195 case "add": 196 target.setSpriteRenderingMode(n, RenderingMode.Add); 197 break; 198 case "addbl": 199 target.setSpriteRenderingMode(n, RenderingMode.AddBl); 200 break; 201 case "subtract": 202 target.setSpriteRenderingMode(n, RenderingMode.Subtract); 203 break; 204 case "subtractbl": 205 target.setSpriteRenderingMode(n, RenderingMode.SubtractBl); 206 break; 207 case "diff": 208 target.setSpriteRenderingMode(n, RenderingMode.Diff); 209 break; 210 case "diffbl": 211 target.setSpriteRenderingMode(n, RenderingMode.DiffBl); 212 break; 213 case "and": 214 target.setSpriteRenderingMode(n, RenderingMode.AND); 215 break; 216 case "or": 217 target.setSpriteRenderingMode(n, RenderingMode.OR); 218 break; 219 case "xor": 220 target.setSpriteRenderingMode(n, RenderingMode.XOR); 221 break; 222 default: 223 target.setSpriteRenderingMode(n, RenderingMode.init); 224 break; 225 } 226 } 227 package void writeMapping(void* target, int x, int y, uint val) @nogc nothrow { 228 ITileLayer l = cast(ITileLayer)target; 229 MappingElement me = *cast(MappingElement*)&val; 230 l.writeMapping(x, y, me); 231 } 232 package Box setSpriteSlice(void* target, int n, LuaVar x0, LuaVar y0, LuaVar x1, LuaVar y1) { 233 ISpriteLayer l = cast(ISpriteLayer)target; 234 return l.setSlice(n, Box(cast(int)x0, cast(int)y0, cast(int)x1, cast(int)y1)); 235 } 236 package void moveSprite(ISpriteLayer target, int n, LuaVar x, LuaVar y) { 237 target.moveSprite(n, cast(int)x, cast(int)y); 238 } 239 package void relMoveSprite(ISpriteLayer target, int n, LuaVar x, LuaVar y) { 240 target.relMoveSprite(n, cast(int)x, cast(int)y); 241 } 242 /+package void addSprite(ISpriteLayer target, ABitmap s, int n, LuaVar x, LuaVar y, ushort paletteSel, 243 LuaVar scaleHoriz, LuaVar scaleVert) { 244 target.addSprite(s, n, cast(int)x, cast(int)y, paletteSel, cast(int)scaleHoriz, cast(int)scaleVert); 245 }+/ 246 package short ttl_getA(ITTL target) @nogc nothrow { 247 return target.A; 248 } 249 package short ttl_getB(ITTL target) @nogc nothrow { 250 return target.B; 251 } 252 package short ttl_getC(ITTL target) @nogc nothrow { 253 return target.C; 254 } 255 package short ttl_getD(ITTL target) @nogc nothrow { 256 return target.D; 257 } 258 package short ttl_getx_0(ITTL target) @nogc nothrow { 259 return target.x_0; 260 } 261 package short ttl_gety_0(ITTL target) @nogc nothrow { 262 return target.y_0; 263 } 264 package short ttl_setA(ITTL target, LuaVar val) { 265 return target.A(cast(short)val); 266 } 267 package short ttl_setB(ITTL target, LuaVar val) { 268 return target.B(cast(short)val); 269 } 270 package short ttl_setC(ITTL target, LuaVar val) { 271 return target.C(cast(short)val); 272 } 273 package short ttl_setD(ITTL target, LuaVar val) { 274 return target.D(cast(short)val); 275 } 276 package short ttl_setx_0(ITTL target, LuaVar val) { 277 return target.x_0(cast(short)val); 278 } 279 package short ttl_sety_0(ITTL target, LuaVar val) { 280 return target.y_0(cast(short)val); 281 }