Jump to content

Robust LuaJIT integration in Topaz Game Toolkit!


Jarrod Davis

259 views

 Share

Topaz has a robust LuaJIT integration feature. With just two interfaces, you can interface the Lua programming language to Delphi with relative ease.

image.png

Bind Delphi to Lua:
 

type
  Color = class
    class procedure AutoSetup(aLua: ILua);
    class procedure Make(aContext: ILuaContext);
    class procedure Makef(aContext: ILuaContext);
  end;
  
class procedure Color.AutoSetup(aLua: ILua);
begin
  RegColor(aLua, 'WHITE', 255, 255, 255, 255);
  RegColor(aLua, 'RED', 255, 0, 0, 255);
  RegColor(aLua, 'GREEN', 0, 255, 0, 255);
  RegColor(aLua, 'BLUE', 0, 0, 255, 255);
end;

// function ColorMake(aRed: Byte; aGreen: Byte; aBlue: Byte; aAlpha: Byte): SDL_Color;
class procedure Color.Make(aContext: ILuaContext);
var
  aRed: Byte;
  aGreen: Byte;
  aBlue: Byte;
  aAlpha: Byte;
begin
  aRed := Round(aContext.GetValue(vtDouble, 1).AsNumber);
  aGreen := Round(aContext.GetValue(vtDouble, 2).AsNumber);
  aBlue := Round(aContext.GetValue(vtDouble, 3).AsNumber);
  aAlpha := Round(aContext.GetValue(vtDouble, 4).AsNumber);
  PushColor(aContext, 1, aRed, aGreen, aBlue, aAlpha);
end;

// function ColorMake(aRed: Single; aGreen: Single; aBlue: Single; aAlpha: Single): SDL_Color;
class procedure Color.Makef(aContext: ILuaContext);
var
  aRed: Single;
  aGreen: Single;
  aBlue: Single;
  aAlpha: Single;
begin
  aRed := aContext.GetValue(vtDouble, 1).AsNumber;
  aGreen := aContext.GetValue(vtDouble, 2).AsNumber;
  aBlue := aContext.GetValue(vtDouble, 3).AsNumber;
  aAlpha := aContext.GetValue(vtDouble, 4).AsNumber;
  PushColor(aContext, 1, Round(aRed*255.0), Round(aGreen*255.0), Round(aBlue*255.0), Round(aAlpha*255.0));
end;  

// register this class with LuaJIT
Lua.RegisterRoutines('', Color, 'Color');

Then from Lua you can:

color1 = Color.Make(210,20,58,255)
color2 = Color.Makef(0.5, 0.5, 0.5, 1.0)
print("Color1.r=" .. tostring(color1.r))

And using LuaJIT over regular Lua, you get the added advantage of increased performance. 😎

 Share

0 Comments


Recommended Comments

There are no comments to display.

×
×
  • Create New...