Skip to content

Commit

Permalink
Work on scripting engine
Browse files Browse the repository at this point in the history
  • Loading branch information
ZILtoid1991 committed Sep 28, 2023
1 parent 68776e3 commit a906873
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.dub
.history
docs.json
__dummy.html
*.o
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module pixelperfectengine.scripting.globals;
import pixelperfectengine.graphics.raster;
import pixelperfectengine.graphics.bitmap;
import pixelperfectengine.audio.base.handler;
public import pixelperfectengine.system.timer;
public import pixelperfectengine.system.rng;

import collections.hashmap;

Expand All @@ -14,4 +16,5 @@ public ModuleManager modMan;
///Any bitmap to be used by a scripting engine, especially if said scripting engine is an
///external entity (DLL, etc), should be stored here to keep a reference around to avoid
///them being destroyed by the garbage collector.
public HashMap!(string, ABitmap) scrptResMan;
public HashMap!(string, ABitmap) scrptResMan;
public RandomNumberGenerator rng;
20 changes: 16 additions & 4 deletions pixelperfectengine/src/pixelperfectengine/scripting/lua.d
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ package void* luaAllocator(void* ud, void* ptr, size_t osize, size_t nsize) @sys
* Calls a Lua function with the given name and arguments.
* Params:
* state = The Lua state, where the function is located.
* funcName = The name of the function.
* ... = The arguments to be passed to the function.
* Template params:
* T = The return type.
* funcName = The name of the function.
* Returns: An expected return type, which can be set to a tagged algebraic type to avoid potential mismatches.
* Throws: A LuaException if the return type isn't matched, the execution ran into an error, or the function isn't
* found.
*/
public T callLuaFunc(T, string funcName)(lua_State* state, ...) @system {
lua_getglobal(state, funcName);
public T callLuaFunc(T)(lua_State* state, string funcName, ...) @system {
lua_getglobal(state, toStringz(funcName));
if (!lua_isfunction(state, 0 /* LUA_TOP */))
throw new LuaException(8,"Function not found");
foreach (arg; _arguments) {
Expand Down Expand Up @@ -77,6 +77,7 @@ public T callLuaFunc(T, string funcName)(lua_State* state, ...) @system {
} else assert(0, "Argument not supported!");
}
int errorCode = lua_pcall(state, cast(int)_arguments.length, is(T == void) ? 0 : 1, 0);
if (errorCode > 1) throw new LuaException(errorCode, "");
static if (!is(T == void)) {
LuaVar result = LuaVar(state, -1);
lua_pop(state, 1);
Expand Down Expand Up @@ -513,7 +514,7 @@ public class LuaScript {
* Throws: A LuaException if the execution ran into an error, or the function isn't found.
*/
public LuaVar runMain() {
return callLuaFunc!(LuaVar, "main")(state);
return callLuaFunc!(LuaVar)(state, "main");
}
extern(C)
private static const(char*) reader(lua_State* st, void* data, size_t* size) nothrow {
Expand All @@ -532,6 +533,17 @@ public class LuaScript {
* Thrown on errors encountered during Lua script execution.
*/
public class LuaException : PPEException {
public static string createMessageString(int ec) {
switch (ec) {
case 0: return "No errors";
case 1: return "Coroutine yield";
case 2: return "Runtime error";
case 3: return "Syntax error";
case 4: return "Memory allocation error";
case 5: return "Error while running the message handler";
default: return "Error happened on the engine side";
}
}
public int errorCode;
///
@nogc @safe pure nothrow this(int errorCode, string msg, string file = __FILE__, size_t line = __LINE__,
Expand Down
53 changes: 53 additions & 0 deletions pixelperfectengine/src/pixelperfectengine/scripting/lualib.d
Original file line number Diff line number Diff line change
Expand Up @@ -301,4 +301,57 @@ package LuaVar getBitmapResource(string resID) {
ABitmap src = scrptResMan[resID];
if (src !is null) return LuaVar(src);
else return LuaVar.voidType();
}
package int loadBitmapResource(string path, string resID, int paletteOffset) {
try {
import std.stdio : File;
Image img = loadImage(File(path));
switch (img.getBitdepth) {
case 1:
scrptResMan[resID] = loadBitmapFromImage!Bitmap1Bit(img);
break;
case 2:
scrptResMan[resID] = loadBitmapFromImage!Bitmap2Bit(img);
mainRaster.loadPaletteChunk(loadPaletteFromImage(img), cast(ushort)paletteOffset);
break;
case 4:
scrptResMan[resID] = loadBitmapFromImage!Bitmap4Bit(img);
mainRaster.loadPaletteChunk(loadPaletteFromImage(img), cast(ushort)paletteOffset);
break;
case 8:
scrptResMan[resID] = loadBitmapFromImage!Bitmap8Bit(img);
mainRaster.loadPaletteChunk(loadPaletteFromImage(img), cast(ushort)paletteOffset);
break;
case 16:
scrptResMan[resID] = loadBitmapFromImage!Bitmap16Bit(img);
mainRaster.loadPaletteChunk(loadPaletteFromImage(img), cast(ushort)paletteOffset);
break;
default:
scrptResMan[resID] = loadBitmapFromImage!Bitmap32Bit(img);
break;
}
} catch (Exception e) {
return 2;
}
return 0;
}
package int setTileMaterial(int layerID, int tileID, string resID, int paletteSh) {
ITileLayer itl = cast(ITileLayer)mainRaster.layerMap[layerID];
if (itl !is null) {
try {
itl.addTile(scrptResMan[resID], cast(wchar)tileID, cast(ubyte)paletteSh);
} catch (Exception e) {
return 2;
}
return 0;
} else {
return 1;
}
}

package ulong rngSeed() @nogc nothrow {
return rng.seed();
}
package ulong rngDice(uint s) @nogc nothrow {
return rng.dice(s);
}
7 changes: 7 additions & 0 deletions pixelperfectengine/src/pixelperfectengine/system/rng.d
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ public struct RandomNumberGenerator {
reg = (reg>>1) | (bit<<63);
return reg;
}
/**
* Calls seed, then returns the remainder of the seed divided by s.
* Intended to simplify the use of `rng.seed() % s` style of use of this
*/
public ulong dice(const uint s) @nogc @safe pure nothrow {
return seed % s;
}
public ulong opCall() @nogc @safe pure nothrow {
return seed();
}
Expand Down

0 comments on commit a906873

Please sign in to comment.