Jump to content
  • entries
    943
  • comments
    5,899
  • views
    923,763

Lua Apps


Josh

2,393 views

 Share

Here's the layout for pure Lua applications. This will run on Android, iOS, Windows, and Mac, with no third-party compilers to mess around with. And because it's JIT compiled by the engine itself, it's fast:

--This function will be called once when the program starts

function App:Start()

 

--Set the application title

self.title="Darkness Awaits"

 

--Create settings table and add defaults

self.settings={}

self.settings.vsync=true

 

--Load the user's settings

self:LoadSettings()

 

--Create a window

self.window=Window:Create(self.title,0,0,1024,768,Window.Titlebar)

 

--Create the graphics context

self.context=Context:Create(self.window,0)

if self.context==nil then return false end

 

--Create a world

self.world=World:Create()

 

--Create a camera

self.camera = Camera:Create()

self.camera:SetPosition(0,5,-10)

 

--Load a test scene

Scene:Load("Scenes\\trash.scene")

 

return true

end

 

--This is our main program loop and will be called continuously until the program ends

function App:Continue()

 

--If window has been closed, end the program

if self.window:Closed() then return false end

 

--Update the world

self.world:Update()

 

--Render the world

self.world:Render()

 

--Refresh the screen

self.context:Swap(self.settings.vsync)

 

--Returning true tells the main program to keep looping

return true

end

 

function App:Pause()

--Pause time updating

Time:Pause()

end

 

function App:Resume()

--Resume time updating

Time:Resume()

end

 

function App:Finish()

--Save user settings to a file

self:SaveSettings()

end

 

--Load user settings from a file

function App:LoadSettings()

return true

end

 

--Save user settings to a file

function App:SaveSettings()

return true

end

 

And just for fun, here's the C++ code that makes all the calls to Lua scripts. It's more low-level than you will need to deal with, but it shows how powerful the script command set is:

bool App::Start()
{
    Int stacksize = Interpreter::GetStackSize();

    //Create New table And assign it To the Global variable "App"
    Interpreter::NewTable();
    Interpreter::SetGlobal("App");

    //Invoke the start script
    If (!Interpreter::ExecuteFile("Scripts\\App.lua"))
    {
		    Print("Error: Failed to execute\"Scripts\\App.lua\".");
		    Return False;
    }

    //Restore the stack size
    Interpreter::SetStackSize(stacksize);

    //Call the App:Start() Function
    Interpreter::GetGlobal("App");
    If (Interpreter::IsTable())
    {
		    Interpreter::PushString("Start");
		    Interpreter::GetTable();
		    If (Interpreter::IsFunction())
		    {
				    Interpreter::PushValue(-2);//Push the app table onto the stack as "self"
				    If (!Interpreter::Invoke(1,1)) Return False;
				    If (Interpreter::IsBool())
				    {
						    If (!Interpreter::ToBool()) Return False;
				    }
				    Else
				    {
						    Return False;
				    }
		    }
    }

    //Restore the stack size
    Interpreter::SetStackSize(stacksize);

    Return True;
}

bool App::Continue()
{
    //Get the stack size
    Int stacksize = Interpreter::GetStackSize();

    //Call the App:Start() Function
    Interpreter::GetGlobal("App");
    If (Interpreter::IsTable())
    {
		    Interpreter::PushString("Continue");
		    Interpreter::GetTable();
		    If (Interpreter::IsFunction())
		    {
				    Interpreter::PushValue(-2);//Push the app table onto the stack as "self"
				    If (!Interpreter::Invoke(1,1))
				    {
						    Print("Error: Script function App:Continue() was not successfully invoked.");
						    Return False;
				    }
				    If (Interpreter::IsBool())
				    {
						    If (!Interpreter::ToBool())
						    {
								    Return False;
						    }
				    }
				    Else
				    {
						    Return False;
				    }
		    }
		    Else
		    {
				    Print("Error: App:Continue() function not found.");
				    Return False;
		    }
    }
    Else
    {
		    Print("Error: App table not found.");
		    Return False;
    }

    //Restore the stack size
    Interpreter::SetStackSize(stacksize);

    Return True;
}

void App::Pause()
{
    //Get the stack size
    Int stacksize = Interpreter::GetStackSize();

    //Call the App:Start() Function
    Interpreter::GetGlobal("App");
    If (Interpreter::IsTable())
    {
		    Interpreter::PushString("Pause");
		    Interpreter::GetTable();
		    If (Interpreter::IsFunction())
		    {
				    Interpreter::PushValue(-2);//Push the app table onto the stack as "self"
				    If (!Interpreter::Invoke(1))
				    {
						    Print("Error: Failed to invoke script function App:Pause()");
						    Return;
				    }
		    }
    }

    //Restore the stack size
    Interpreter::SetStackSize(stacksize);
}

void App::Finish()
{
    //Get the stack size
    Int stacksize = Interpreter::GetStackSize();

    //Call the App:Finish() Function
    Interpreter::GetGlobal("App");
    If (Interpreter::IsTable())
    {
		    Interpreter::PushString("Finish");
		    Interpreter::GetTable();
		    If (Interpreter::IsFunction())
		    {
				    Interpreter::PushValue(-2);//Push the app table onto the stack as "self"
				    If (!Interpreter::Invoke(1))
				    {
						    Print("Error: Failed to invoke script function App:Finish()");
						    Return;
				    }
		    }
    }

    //Restore the stack size
    Interpreter::SetStackSize(stacksize);
}

  • Upvote 2
 Share

3 Comments


Recommended Comments

Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...