From 14e96597c65a2402ee4ddc068ff08c7659123a1d Mon Sep 17 00:00:00 2001 From: whiteshark0 Date: Mon, 4 May 2009 07:29:52 +0000 Subject: 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 --- Lua/src/base/UMain.pas | 20 ++++--- Lua/src/base/UParty.pas | 23 +++++++- Lua/src/lua/UHookableEvent.pas | 9 --- Lua/src/lua/ULuaCore.pas | 30 +++++++++- Lua/src/lua/ULuaParty.pas | 52 +++++++---------- Lua/src/lua/ULuaTextGL.pas | 125 ++++++++++++++++++++++++++++++++--------- Lua/src/lua/ULuaUtils.pas | 93 ++++++++++++++++++++++++++++++ Lua/src/menu/UDisplay.pas | 22 +++++++- Lua/src/ultrastardx.dpr | 1 + 9 files changed, 290 insertions(+), 85 deletions(-) create mode 100644 Lua/src/lua/ULuaUtils.pas (limited to 'Lua/src') diff --git a/Lua/src/base/UMain.pas b/Lua/src/base/UMain.pas index 57ad01a1..a1c77900 100644 --- a/Lua/src/base/UMain.pas +++ b/Lua/src/base/UMain.pas @@ -118,7 +118,7 @@ var PlayersPlay: integer; CurrentSong : TSong; - Lua : Plua_State; + //Lua : Plua_State; const MAX_SONG_SCORE = 10000; // max. achievable points per song @@ -228,6 +228,9 @@ begin SDL_Init(SDL_INIT_VIDEO or SDL_INIT_TIMER); SDL_EnableUnicode(1); + // create luacore first so other classes can register their events + LuaCore := TLuaCore.Create; + USTime := TTime.Create; VideoBGTimer := TRelativeTimer.Create; @@ -390,7 +393,7 @@ begin end; // Lua - Log.BenchmarkStart(1); + {Log.BenchmarkStart(1); Lua := luaL_newstate; if Lua = nil then Log.LogError('Lua init failed','Lua'); @@ -403,7 +406,7 @@ begin luaopen_TextGL(Lua); // TextGL (Lua) lua_pop(Lua, 1); // remove table from stack luaopen_Texture(Lua); // Texture (Lua) - lua_pop(Lua, 1); // remove table from stack + lua_pop(Lua, 1); // remove table from stack } Log.BenchmarkEnd(1); Log.LogBenchmark('Initializing Lua', 1); @@ -422,13 +425,12 @@ begin Party := TPartyGame.Create; - LuaCore := TLuaCore.Create; - LuaCore.RegisterModule('Log', ULuaLog_Lib_f); LuaCore.RegisterModule('Gl', ULuaGl_Lib_f); + LuaCore.RegisterModule('TextGl', ULuaTextGl_Lib_f); LuaCore.RegisterModule('Party', ULuaParty_Lib_f); - LuaCore.BrowseDir(PluginPath); + LuaCore.LoadPlugins; LuaCore.DumpPlugins; @@ -454,7 +456,7 @@ begin //TTF_Quit(); SDL_Quit(); - lua_close(Lua); + //lua_close(Lua); if assigned(Log) then begin @@ -487,14 +489,14 @@ begin // display done := not Display.Draw; - // FIXME remove this when the Partymode works + {// FIXME remove this when the Partymode works if FileExists(ScriptPath + 'main.lua') then begin if 0 <> luaL_dofile(Lua, PAnsiChar(ScriptPath + 'main.lua')) then begin Log.LogError(lua_tostring(Lua,-1)); end; - end; + end; } SwapBuffers; diff --git a/Lua/src/base/UParty.pas b/Lua/src/base/UParty.pas index 0be7cf61..1566e4e8 100644 --- a/Lua/src/base/UParty.pas +++ b/Lua/src/base/UParty.pas @@ -153,6 +153,10 @@ type function StartGame(Rounds: ARounds): Boolean; + { sets the winner(s) of current round + returns true on success } + function SetWinner(WinBin: Integer): Boolean; + { increases round counter by 1 and clears all round specific information; returns the number of the current round or -1 if last round has already been played } @@ -531,6 +535,18 @@ begin end; end; +{ sets the winner(s) of current round + returns true on success } +function TPartyGame.SetWinner(WinBin: Integer): Boolean; +begin + if (bPartyStarted) and (CurRound >= 0) and (CurRound <= High(Rounds)) then + begin + Rounds[CurRound].Winner := WinBin; + end + else + Result := false; +end; + { increases round counter by 1 and clears all round specific information; returns the number of the current round or -1 if last round has already been played } @@ -674,8 +690,11 @@ begin if (CallLua(Parent, Functions.AfterSing)) then begin // execute default function: - // display party score screen - Display.FadeTo(@ScreenPartyScore); + if (bPartyGame) then + // display party score screen + Display.FadeTo(@ScreenPartyScore) + else //display standard score screen + Display.FadeTo(@ScreenScore); end; end; end; 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 diff --git a/Lua/src/menu/UDisplay.pas b/Lua/src/menu/UDisplay.pas index 1730406f..525b73a9 100644 --- a/Lua/src/menu/UDisplay.pas +++ b/Lua/src/menu/UDisplay.pas @@ -40,7 +40,8 @@ uses gl, glu, SysUtils, - UMusic; + UMusic, + UHookableEvent; type TDisplay = class @@ -61,6 +62,9 @@ type OSD_LastError : String; + ePreDraw: THookableEvent; + eDraw: THookableEvent; + procedure DrawDebugInformation; public NextScreen : PMenu; @@ -131,6 +135,10 @@ begin //Set LastError for OSD to No Error OSD_LastError := 'No Errors'; + + // create events for plugins + ePreDraw := THookableEvent.Create('Display.PreDraw'); + eDraw := THookableEvent.Create('Display.Draw'); end; destructor TDisplay.Destroy; @@ -183,6 +191,7 @@ begin if (not assigned(NextScreen)) and (not BlackScreen) then begin + ePreDraw.CallHookChain(false); CurrentScreen.Draw; //popup mod @@ -197,6 +206,8 @@ begin FadeEnabled := True else if (Ini.ScreenFade = 0) then FadeEnabled := False; + + eDraw.CallHookChain(false); end else begin @@ -215,8 +226,11 @@ begin glPushAttrib(GL_VIEWPORT_BIT); glViewPort(0, 0, 512, 512); + // draw screen that will be faded + ePreDraw.CallHookChain(false); CurrentScreen.Draw; + eDraw.CallHookChain(false); // clear OpenGL errors, otherwise fading might be disabled due to some // older errors in previous OpenGL calls. @@ -255,7 +269,11 @@ begin // blackscreen-hack if not BlackScreen then - NextScreen.Draw // draw next screen + begin + ePreDraw.CallHookChain(false); + NextScreen.Draw; // draw next screen + eDraw.CallHookChain(false); + end else if ScreenAct = 1 then begin glClearColor(0, 0, 0 , 0); diff --git a/Lua/src/ultrastardx.dpr b/Lua/src/ultrastardx.dpr index cc2f7f0d..dcd0d0c7 100644 --- a/Lua/src/ultrastardx.dpr +++ b/Lua/src/ultrastardx.dpr @@ -145,6 +145,7 @@ uses //------------------------------ ULua in 'lib\Lua\ULua.pas', + ULuaUtils in 'lua\ULuaUtils.pas', ULuaGl in 'lua\ULuaGl.pas', ULuaLog in 'lua\ULuaLog.pas', ULuaTextGL in 'lua\ULuaTextGL.pas', -- cgit v1.2.3