aboutsummaryrefslogtreecommitdiffstats
path: root/Lua/src/lua
diff options
context:
space:
mode:
authorwhiteshark0 <whiteshark0@b956fd51-792f-4845-bead-9b4dfca2ff2c>2009-05-04 07:29:52 +0000
committerwhiteshark0 <whiteshark0@b956fd51-792f-4845-bead-9b4dfca2ff2c>2009-05-04 07:29:52 +0000
commit14e96597c65a2402ee4ddc068ff08c7659123a1d (patch)
treeebefcbd5ec72570f68bb7b043365b3d6fb93dd6b /Lua/src/lua
parent2449e4a979c23addcc9110a4dc876d9acd761005 (diff)
downloadusdx-14e96597c65a2402ee4ddc068ff08c7659123a1d.tar.gz
usdx-14e96597c65a2402ee4ddc068ff08c7659123a1d.tar.xz
usdx-14e96597c65a2402ee4ddc068ff08c7659123a1d.zip
old lua test stuff by hawkear removed or commented
new function: Party.SetWinner sets winner of current round new unit ULuaUtils w/ some functions that are or may become useful moved Lua_ClearStack and Lua_ToBinInt to ULuaUtils added first hooks (have a look at hooks.txt in game/plugins until documentation is finished) lua module for TextGl written and finished lua testfile (game/scripts/main.lua) ported to new interface, see game/plugins/LuaTest.usdx git-svn-id: svn://svn.code.sf.net/p/ultrastardx/svn/branches/experimental@1709 b956fd51-792f-4845-bead-9b4dfca2ff2c
Diffstat (limited to 'Lua/src/lua')
-rw-r--r--Lua/src/lua/UHookableEvent.pas9
-rw-r--r--Lua/src/lua/ULuaCore.pas30
-rw-r--r--Lua/src/lua/ULuaParty.pas52
-rw-r--r--Lua/src/lua/ULuaTextGL.pas125
-rw-r--r--Lua/src/lua/ULuaUtils.pas93
5 files changed, 237 insertions, 72 deletions
diff --git a/Lua/src/lua/UHookableEvent.pas b/Lua/src/lua/UHookableEvent.pas
index d6ec2807..a8422026 100644
--- a/Lua/src/lua/UHookableEvent.pas
+++ b/Lua/src/lua/UHookableEvent.pas
@@ -281,13 +281,4 @@ begin
Result := 0;
end;
-{ this is a helper in case an evenet owner don't has no use for the results
- returns number of popped elements }
-function Lua_ClearStack(L: Plua_State): Integer;
- var I: Integer;
-begin
- Result := lua_gettop(L);
- lua_pop(L, Result);
-end;
-
end. \ No newline at end of file
diff --git a/Lua/src/lua/ULuaCore.pas b/Lua/src/lua/ULuaCore.pas
index 403709dd..5c6f2463 100644
--- a/Lua/src/lua/ULuaCore.pas
+++ b/Lua/src/lua/ULuaCore.pas
@@ -117,6 +117,8 @@ type
EventHandles: Array of String; //< Index is Events handle, value is events name. if length(value) is 0 handle is considered unregistred
Plugins: Array of TLuaPlugin;
+
+ eLoadingFinished: THookableEvent;
protected
Modules: Array of TLuaModule; //< modules that has been registred, has to be proctected because fucntions of this unit need to get access
@@ -125,6 +127,8 @@ type
constructor Create;
destructor Destroy;
+ procedure LoadPlugins; //< calls LoadPlugin w/ Plugindir and LoadingFinished Eventchain
+
procedure BrowseDir(Dir: WideString); //< searches for files w/ extension .usdx in the specified dir and tries to load them w/ lua
procedure LoadPlugin(Filename: WideString); //< tries to load filename w/ lua and creates the default usdx lua environment for the plugins state
@@ -177,7 +181,7 @@ var
LuaCore: TLuaCore;
implementation
-uses ULog, UPlatform, ULuaUsdx;
+uses ULog, UPlatform, ULuaUsdx, UMain;
constructor TLuaCore.Create;
begin
@@ -185,6 +189,8 @@ begin
//init EventList w/ nil
EventList := nil;
+
+ eLoadingFinished := nil;
end;
destructor TLuaCore.Destroy;
@@ -208,6 +214,18 @@ begin
inherited;
end;
+{ calls BrowseDir w/ plugin dir and LoadingFinished eventchain }
+procedure TLuaCore.LoadPlugins;
+begin
+ // we have to create event here, because in create it can
+ // not be registred, because LuaCore is no assigned
+ if (not Assigned(eLoadingFinished)) then
+ eLoadingFinished := THookableEvent.Create('Usdx.LoadingFinished');
+
+ BrowseDir(PluginPath);
+ eLoadingFinished.CallHookChain(false);
+end;
+
{ searches for files w/ extension .usdx in the specified
dir and tries to load them w/ lua }
procedure TLuaCore.BrowseDir(Dir: WideString);
@@ -564,7 +582,15 @@ begin
Id := lua_ToInteger(L, lua_upvalueindex(1));
luaL_register(L, PChar('Usdx.' + LuaCore.Modules[Id].Name), @LuaCore.Modules[Id].Functions[0]);
-
+
+ // set the modules table as global "modulename"
+ // so it can be accessed either by Usdx.modulename.x() or
+ // by modulename.x()
+ lua_setglobal(L, PChar(LuaCore.Modules[Id].Name));
+
+ // no we net to push the table again to return it
+ lua_getglobal(L, PChar(LuaCore.Modules[Id].Name));
+
Result := 1; //return table
end
else
diff --git a/Lua/src/lua/ULuaParty.pas b/Lua/src/lua/ULuaParty.pas
index c102b2ac..c563f902 100644
--- a/Lua/src/lua/ULuaParty.pas
+++ b/Lua/src/lua/ULuaParty.pas
@@ -57,6 +57,11 @@ function ULuaParty_Register(L: Plua_State): Integer; cdecl;
of current game were played }
function ULuaParty_GameFinished(L: Plua_State): Integer; cdecl;
+{ Party.SetWinner - sets winner of current party round,
+ if no party game is started or party game is finished
+ it will raise an error }
+function ULuaParty_SetWinner(L: Plua_State): Integer; cdecl;
+
{ Party.GetTeams - returns a table with all information and structure as
in the TPartyGame.Teams array }
function ULuaParty_GetTeams(L: Plua_State): Integer; cdecl;
@@ -66,47 +71,17 @@ function ULuaParty_GetTeams(L: Plua_State): Integer; cdecl;
function ULuaParty_SetTeams(L: Plua_State): Integer; cdecl;
const
- ULuaParty_Lib_f: array [0..3] of lual_reg = (
+ ULuaParty_Lib_f: array [0..4] of lual_reg = (
(name:'Register'; func:ULuaParty_Register),
(name:'GameFinished'; func:ULuaParty_GameFinished),
+ (name:'SetWinner'; func:ULuaParty_SetWinner),
(name:'GetTeams'; func:ULuaParty_GetTeams),
(name:'SetTeams'; func:ULuaParty_SetTeams)
);
implementation
-uses ULuaCore, UParty, SysUtils;
-
-{ converts a lua table with a structure like:
- * = 1 , * = 4 , * = 5
- to an integer with the value:
- 11001
- does not pop anything }
-function Lua_ToBinInt(L: PLua_State; idx: Integer): Integer;
- var
- I: Integer;
-begin
- // default: no bits set
- Result := 0;
-
- lua_checkstack(L, 3);
+uses ULuaCore, ULuaUtils, UParty, SysUtils;
- if (idx < 0) then
- dec(idx); // we will push one value before using this
-
- lua_PushNil(L);
- while (lua_next(L, idx) <> 0) do
- begin
- if (lua_isNumber(L, -1)) then
- begin //check if we got an integer value from 1 to 32
- I := lua_toInteger(L, -1);
- if (I >= 1) and (I <= 32) then
- Result := Result or 1 shl (I - 1);
- end;
-
- // pop value, so key is on top
- lua_pop(L, 1);
- end;
-end;
{ Party.Register - register party mode at party manager
arguments: info: table
@@ -196,6 +171,17 @@ begin
Result := 1;
end;
+{ Party.SetWinner - sets winner of current party round,
+ if no party game is started or party game is finished
+ it will raise an error }
+function ULuaParty_SetWinner(L: Plua_State): Integer; cdecl;
+begin
+ luaL_checktype(L, 1, LUA_TTABLE);
+
+ if (not Party.SetWinner(Lua_ToBinInt(L, 1))) then
+ luaL_error(L, PChar('cann''t set party winner. Is party started and not finished yet?'));
+end;
+
{ Party.GetTeams - returns a table with all information and structure as
in the TPartyGame.Teams array }
function ULuaParty_GetTeams(L: Plua_State): Integer; cdecl;
diff --git a/Lua/src/lua/ULuaTextGL.pas b/Lua/src/lua/ULuaTextGL.pas
index 287695f9..1db41b21 100644
--- a/Lua/src/lua/ULuaTextGL.pas
+++ b/Lua/src/lua/ULuaTextGL.pas
@@ -34,45 +34,114 @@ interface
{$I switches.inc}
uses
- SysUtils,
TextGL,
ULua;
-function luaopen_TextGL (L: Plua_State): Integer; cdecl;
+{ TextGl.Pos(X, Y: Float) : sets font position }
+function ULuaTextGL_Pos(L: Plua_State): Integer; cdecl;
+
+{ TextGl.Size(Size: Float) : sets font size }
+function ULuaTextGL_Size(L: Plua_State): Integer; cdecl;
+
+{ TextGl.Style(Style: int) : sets font style (from 0 to 3) }
+function ULuaTextGL_Style(L: Plua_State): Integer; cdecl;
+
+{ TextGl.Italic(isItalic: boolean) : sets if font is italic }
+function ULuaTextGL_Italic(L: Plua_State): Integer; cdecl;
+
+{ TextGl.Width(Text: String) : returns width of Text if printed
+ w/ current settings in pixels }
+function ULuaTextGL_Width(L: Plua_State): Integer; cdecl;
+
+{ TextGl.Print(Text: String) : prints text to screen w/ current
+ settings}
+function ULuaTextGL_Print(L: Plua_State): Integer; cdecl;
+
+const
+ ULuaTextGl_Lib_f: array [0..5] of lual_reg = (
+ (name:'Pos'; func:ULuaTextGl_Pos),
+ (name:'Size'; func:ULuaTextGl_Size),
+ (name:'Style'; func:ULuaTextGl_Style),
+ (name:'Italic'; func:ULuaTextGl_Italic),
+ (name:'Width'; func:ULuaTextGl_Width),
+ (name:'Print'; func:ULuaTextGl_Print)
+ );
-function ULuaTextGL_Dummy(L: Plua_State): Integer; cdecl;
implementation
-function ULuaTextGL_Dummy(L: Plua_State): Integer; cdecl;
+{ TextGl.Pos(X, Y: Float) : sets font position }
+function ULuaTextGL_Pos(L: Plua_State): Integer; cdecl;
+ var X, Y: Double;
begin
- result:=0; // number of results
+ X := luaL_checknumber(L, 1);
+ Y := luaL_checknumber(L, 2);
+
+ SetFontPos(X, Y);
+
+ Result := 0;
end;
-const
- ULuaTextGL_Lib_f: array [0..1] of lual_reg = (
- (name:'Print';func:ULuaTextGL_Dummy),
- (name:nil;func:nil)
- );
+{ TextGl.Size(Size: Float) : sets font size }
+function ULuaTextGL_Size(L: Plua_State): Integer; cdecl;
+ var Size: Double;
+begin
+ Size := luaL_checknumber(L, 1);
+
+ SetFontSize(Size);
+
+ Result := 0;
+end;
-function luaopen_TextGL (L: Plua_State): Integer; cdecl;
+{ TextGl.Style(Style: int) : sets font style (from 0 to 3) }
+function ULuaTextGL_Style(L: Plua_State): Integer; cdecl;
+ var Style: Integer;
begin
- luaL_register(L,'TextGL',@ULuaTextGL_Lib_f[0]);
- result:=1;
+ Style := luaL_checkinteger(L, 1);
+
+ if (Style >= 0) and (Style <= 3) then
+ SetFontStyle(Style)
+ else
+ luaL_ArgError(L, 1, PChar('number from 0 to 3 expected'));
+
+ Result := 0;
end;
-end.
-{
-procedure BuildFont; // build our bitmap font
-procedure KillFont; // delete the font
-function glTextWidth(const text: string): real; // returns text width
-procedure glPrint(const text: string); // custom GL "Print" routine
-procedure ResetFont(); // reset font settings of active font
-procedure SetFontPos(X, Y: real); // sets X and Y
-procedure SetFontZ(Z: real); // sets Z
-procedure SetFontSize(Size: real);
-procedure SetFontStyle(Style: integer); // sets active font style (normal, bold, etc)
-procedure SetFontItalic(Enable: boolean); // sets italic type letter (works for all fonts)
-procedure SetFontAspectW(Aspect: real);
-procedure SetFontReflection(Enable:boolean;Spacing: real); // enables/disables text reflection
-}
+{ TextGl.Italic(isItalic: boolean) : sets if font is italic }
+function ULuaTextGL_Italic(L: Plua_State): Integer; cdecl;
+ var isItalic: Boolean;
+begin
+ luaL_checkany(L, 1);
+ isItalic := lua_toBoolean(L, 1);
+
+ SetFontItalic(isItalic);
+
+ Result := 0;
+end;
+
+{ TextGl.Width(Text: String) : returns width of Text if printed
+ w/ current settings in pixels }
+function ULuaTextGL_Width(L: Plua_State): Integer; cdecl;
+ var Text: String;
+begin
+ Text := luaL_checkstring(L, 1);
+ lua_pop(L, lua_gettop(L));
+
+ lua_PushNumber(L, glTextWidth(Text));
+
+ Result := 1;
+end;
+
+{ TextGl.Print(Text: String) : prints text to screen w/ current
+ settings}
+function ULuaTextGL_Print(L: Plua_State): Integer; cdecl;
+ var Text: String;
+begin
+ Text := luaL_checkstring(L, 1);
+
+ glPrint(Text);
+
+ Result := 0;
+end;
+
+end.
diff --git a/Lua/src/lua/ULuaUtils.pas b/Lua/src/lua/ULuaUtils.pas
new file mode 100644
index 00000000..c8fa934e
--- /dev/null
+++ b/Lua/src/lua/ULuaUtils.pas
@@ -0,0 +1,93 @@
+{* UltraStar Deluxe - Karaoke Game
+ *
+ * UltraStar Deluxe is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING. If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ * $URL: https://ultrastardx.svn.sourceforge.net/svnroot/ultrastardx/branches/experimental/Lua/src/lua/ULuaGl.pas $
+ * $Id: ULuaGl.pas 1555 2009-01-11 18:19:42Z Hawkear $
+ *}
+
+unit ULuaUtils;
+
+interface
+
+{$IFDEF FPC}
+ {$MODE Delphi}
+{$ENDIF}
+
+{$I switches.inc}
+
+uses ULua;
+
+{ converts a lua table with a structure like:
+ * = 1 , * = 4 , * = 5
+ to an integer with the value:
+ 0b11001
+ does not pop anything }
+function Lua_ToBinInt(L: PLua_State; idx: Integer): Integer;
+
+{ this is a helper in case an evenet owner don't has no use for the results
+ returns number of popped elements }
+function Lua_ClearStack(L: Plua_State): Integer;
+
+
+implementation
+
+{ converts a lua table with a structure like:
+ * = 1 , * = 4 , * = 5
+ to an integer with the value:
+ 0b11001
+ does not pop anything }
+function Lua_ToBinInt(L: PLua_State; idx: Integer): Integer;
+ var
+ I: Integer;
+begin
+ // default: no bits set
+ Result := 0;
+
+ lua_checkstack(L, 3);
+
+ if (idx < 0) then
+ dec(idx); // we will push one value before using this
+
+ lua_PushNil(L);
+ while (lua_next(L, idx) <> 0) do
+ begin
+ if (lua_isNumber(L, -1)) then
+ begin //check if we got an integer value from 1 to 32
+ I := lua_toInteger(L, -1);
+ if (I >= 1) and (I <= 32) then
+ Result := Result or 1 shl (I - 1);
+ end;
+
+ // pop value, so key is on top
+ lua_pop(L, 1);
+ end;
+end;
+
+{ this is a helper in case an evenet owner don't has no use for the results
+ returns number of popped elements }
+function Lua_ClearStack(L: Plua_State): Integer;
+ var I: Integer;
+begin
+ Result := lua_gettop(L);
+ lua_pop(L, Result);
+end;
+
+end. \ No newline at end of file