Browse Source

Sample crashes on exit only if the high scores menu gets opened.

Other than that crash, I do believe that the documents leak.

I fixed quite a bit of the Lua errors that I introduced when switching the code from Python to Lua.
Nate Starkey 13 years ago
parent
commit
970fda9111

+ 4 - 4
Samples/luainvaders/data/high_score.rml

@@ -53,23 +53,23 @@ end
 function HighScore.OnLoad(window)
 	Window.OnWindowLoad(window)
 	local datagrid = window:GetElementById('datagrid')
-	datagrid:AddEventListener('rowupdate', HighScore.OnRowAdd, False)
+	datagrid:AddEventListener('rowupdate', HighScore.OnRowAdd, false)
 end
 	
 function HighScore.OnKeyDown(event)
 	if event.parameters['key_identifier'] == rocket.key_identifier.RETURN then
-		game.SetHighScoreName(event.current_element.value)
+		Game.SetHighScoreName(event.current_element.value)
     end
 end
 		</script>
 	</head>
-	<body template="luawindow" onload="HighScore.OnLoad(element) game.SubmitHighScore()" onunload="game.SetHighScoreName('Anon')">
+	<body template="luawindow" onload="HighScore.OnLoad(element) Game.SubmitHighScore()" onunload="Game.SetHighScoreName('Anon')">
 		<datagrid id="datagrid" source="high_scores.scores">
 			<col fields="name,name_required" formatter="name" width="40%">Pilot:</col>
 			<col fields="colour" formatter="ship" width="20%">Ship:</col>
 			<col fields="wave" width="20%">Wave:</col>
 			<col fields="score" width="20%">Score:</col>
 		</datagrid>
-		<button onclick="Window.LoadMenu('main_menu')">Main Menu</button>
+		<button onclick="Window.LoadMenu('main_menu',document)">Main Menu</button>
 	</body>
 </rml>

+ 3 - 3
Samples/luainvaders/data/main_menu.rml

@@ -29,9 +29,9 @@ end
 	</head>
 	<body template="luawindow" onload="Window.OnWindowLoad(element) document.context:LoadDocument('data/logo.rml'):Show()" onunload="MainMenu.CloseLogo(document)">
 		<button onclick="document.context:LoadDocument('data/start_game.rml'):Show() document:Close()">Start Game</button><br />
-		<button onclick="Window.LoadMenu('high_score')">High Scores</button><br />
-		<button onclick="Window.LoadMenu('options')">Options</button><br />
-		<button onclick="Window.LoadMenu('help')">Help</button><br />
+		<button onclick="Window.LoadMenu('high_score',document)">High Scores</button><br />
+		<button onclick="Window.LoadMenu('options',document)">Options</button><br />
+		<button onclick="Window.LoadMenu('help',document)">Help</button><br />
 		<button onclick="Game.Shutdown()">Exit</button>
 	</body>
 </rml>

+ 1 - 1
Samples/luainvaders/data/options.rml

@@ -35,7 +35,7 @@ function Options.LoadOptions(document)
     
     document:GetElementById(options['graphics']).checked = true
     document:GetElementById('reverb').checked = options['reverb']
-    document:GetElementById("3d").checked = options['3d']
+    document:GetElementById('3d').checked = options['3d']
 end
 
 function Options.SaveOptions(event, document):

+ 1 - 1
Samples/luainvaders/data/pause.rml

@@ -18,7 +18,7 @@
 			}
 		</style>
 	</head>
-	<body template="luawindow" onload="Window.OnWindowLoad(element) game.SetPaused(true)" onunload="game.SetPaused(false)">
+	<body template="luawindow" onload="Window.OnWindowLoad(element) Game.SetPaused(true)" onunload="Game.SetPaused(false)">
 		<br />
 		<p>Are you sure you want to end this game?</p>
 		<button onclick="Window.LoadMenu('high_score') document.context.documents['game_window']:Close()">Yes</button>

+ 7 - 7
Samples/luainvaders/data/start_game.rml

@@ -26,26 +26,26 @@
 		<script>
 StartGame = StartGame or {}
 
-function StartGame.SetupGame(event)
+function StartGame.SetupGame(event,document)
 	if event.parameters['difficulty'] == 'hard' then
-		game.SetDifficulty(game.difficulty.HARD)
+		Game.SetDifficulty(Game.difficulty.HARD)
 	else
-		game.SetDifficulty(game.difficulty.EASY)
+		Game.SetDifficulty(Game.difficulty.EASY)
     end
 	
     --some lua trickery coming up. Compiling a string at runtime that will return the values,
     --because they are comma separated. For example, if the user chose "Denim", then the
     --compiled string would look like "return 21,96,189"
-    local red,green,blue = loadstring('return '..event.parameters['colour'])
-	game.SetDefenderColour(Colourb(red,green,blue,255))
+    local red,green,blue = loadstring('return '..event.parameters['colour']) ()
+	Game.SetDefenderColour(Colourb.new(red,green,blue,255))
 	
-	local elem = Window.LoadMenu('game'):GetElementById('game')
+	local elem = Window.LoadMenu('game',document):GetElementById('game')
 	elem:Focus()
 end
 		</script>
 	</head>
 	<body template="luawindow">
-		<form onsubmit="StartGame.SetupGame(event)">
+		<form onsubmit="StartGame.SetupGame(event,document)">
 			<div>
 				<p>
 					Difficulty:<br />

+ 7 - 7
Source/Core/Lua/Element.cpp

@@ -109,26 +109,26 @@ int ElementGetAttribute(lua_State* L, Element* obj)
     case Variant::BYTE:
     case Variant::CHAR:
     case Variant::INT:
-        lua_pushinteger(L,*(int*)var);
+        lua_pushinteger(L,var->Get<int>());
         break;
     case Variant::FLOAT:
-        lua_pushnumber(L,*(float*)var);
+        lua_pushnumber(L,var->Get<float>());
         break;
     case Variant::COLOURB:
-        LuaType<Colourb>::push(L,(Colourb*)var,false);
+        LuaType<Colourb>::push(L,&var->Get<Colourb>(),false);
         break;
     case Variant::COLOURF:
-        LuaType<Colourf>::push(L,(Colourf*)var,false);
+        LuaType<Colourf>::push(L,&var->Get<Colourf>(),false);
         break;
     case Variant::STRING:
-        lua_pushstring(L,((String*)var)->CString());
+        lua_pushstring(L,var->Get<String>().CString());
         break;
     case Variant::VECTOR2:
         //according to Variant.inl, it is going to be a Vector2f
-        LuaType<Vector2f>::push(L,((Vector2f*)var),false);
+        LuaType<Vector2f>::push(L,&var->Get<Vector2f>(),false);
         break;
     case Variant::VOIDPTR:
-        lua_pushlightuserdata(L,(void*)var);
+        lua_pushlightuserdata(L,var->Get<void*>());
         break;
     default:
         lua_pushnil(L);

+ 8 - 8
Source/Core/Lua/Event.cpp

@@ -51,7 +51,7 @@ int EventGetAttrparameters(lua_State* L)
     Event* evt = LuaType<Event>::check(L,1);
     LUACHECKOBJ(evt);
     const Dictionary* params = evt->GetParameters();
-    int index;
+    int index = 0;
     String key;
     Variant* value;
 
@@ -66,26 +66,26 @@ int EventGetAttrparameters(lua_State* L)
         case Variant::BYTE:
         case Variant::CHAR:
         case Variant::INT:
-            lua_pushinteger(L,*(int*)value);
+            lua_pushinteger(L,value->Get<int>());
             break;
         case Variant::FLOAT:
-            lua_pushnumber(L,*(float*)value);
+            lua_pushnumber(L,value->Get<float>());
             break;
         case Variant::COLOURB:
-            LuaType<Colourb>::push(L,(Colourb*)value,false);
+            LuaType<Colourb>::push(L,&value->Get<Colourb>(),false);
             break;
         case Variant::COLOURF:
-            LuaType<Colourf>::push(L,(Colourf*)value,false);
+            LuaType<Colourf>::push(L,&value->Get<Colourf>(),false);
             break;
         case Variant::STRING:
-            lua_pushstring(L,((String*)value)->CString());
+            lua_pushstring(L,value->Get<String>().CString());
             break;
         case Variant::VECTOR2:
             //according to Variant.inl, it is going to be a Vector2f
-            LuaType<Vector2f>::push(L,((Vector2f*)value),false);
+            LuaType<Vector2f>::push(L,&value->Get<Vector2f>(),false);
             break;
         case Variant::VOIDPTR:
-            lua_pushlightuserdata(L,(void*)value);
+            lua_pushlightuserdata(L,value->Get<void*>());
             break;
         default:
             lua_pushnil(L);

+ 17 - 3
Source/Core/Lua/LuaEventListener.cpp

@@ -10,10 +10,12 @@ typedef Rocket::Core::ElementDocument Document;
 
 LuaEventListener::LuaEventListener(const String& code, Element* element) : EventListener()
 {
+    //compose function
     String function = "return function (event,element,document) ";
     function.Append(code);
     function.Append(" end");
 
+    //make sure there is an area to save the function
     lua_State* L = Interpreter::GetLuaState();
     lua_getglobal(L,"EVENTLISTENERFUNCTIONS");
     if(lua_isnoneornil(L,-1))
@@ -24,14 +26,14 @@ LuaEventListener::LuaEventListener(const String& code, Element* element) : Event
         lua_getglobal(L,"EVENTLISTENERFUNCTIONS");
     }
     int tbl = lua_gettop(L);
-    strFunc = lua_typename(L,lua_type(L,tbl));
 
-    luaL_loadstring(L,function.CString()); //pushes the compiled string to the top of the stack
+    //compile,execute,and save the function
+    luaL_loadstring(L,function.CString());
     if(lua_pcall(L,0,1,0) != 0)
         Interpreter::Report();
-    strFunc = lua_typename(L,lua_type(L,-1));
     luaFuncRef = luaL_ref(L,tbl); //creates a reference to the item at the top of the stack in to the table we just created
     lua_pop(L,1); //pop the EVENTLISTENERFUNCTIONS table
+
     attached = element;
     parent = element->GetOwnerDocument();
     strFunc = function;
@@ -45,9 +47,21 @@ LuaEventListener::LuaEventListener(int ref, Element* element)
     parent = element->GetOwnerDocument();
 }
 
+LuaEventListener::~LuaEventListener()
+{
+    if(attached)
+        attached->RemoveReference();
+    if(parent)
+        parent->RemoveReference();
+}
+
 /// Process the incoming Event
 void LuaEventListener::ProcessEvent(Event& event)
 {
+    //not sure if this is the right place to do this, but if the element we are attached to isn't a document, then
+    //the 'parent' variable will be NULL, because element->ower_document hasn't been set on the construction. We should
+    //correct that
+    if(!parent) parent = attached->GetOwnerDocument();
     lua_State* L = Interpreter::GetLuaState();
     String strtype;
     int top = lua_gettop(L); 

+ 2 - 0
Source/Core/Lua/LuaEventListener.h

@@ -19,6 +19,8 @@ public:
     //should take care to have the proper order. The order is event,element,document.
     LuaEventListener(int ref, Element* element);
 
+    virtual ~LuaEventListener();
+
     /// Process the incoming Event
 	virtual void ProcessEvent(Event& event);
 private: