Capricorn 76
These Lua tutorials demonstrate how to initialise the graphics engine.

Starting up the graphics engine

---------------------------
---------------------------
-- Tutorial: Graphics engine. Test 1
-- Starting up the graphics engine.
--
-- Copyright (c) 2006-2008 Capricorn 76 Pty. Ltd.
---------------------------
---------------------------

---------------------------
---------------------------
-- PROGRAM STARTS HERE
---------------------------
---------------------------

---------------------------
-- Initialise the graphics engine, using a software renderer
---------------------------
local graphicsInitialised = IGraphics:init( 'SOFTWARE', -- Instead of using hard-coded strings, we could IApp:loadScript('constantsVideoDrivers.e76script') and use the pre-defined constants.
                                640, 480,       -- Width/height
                                false,          -- Fullscreen
                                16,         -- Bits/color depth
                                false,          -- Shadows
                                false,          -- Antialiasing
                                false,          -- VSync
                                30,         -- Frame rate
                                false);     -- Silent errors are off, so ScriptENGINE will raise an error is something's not right

if (graphicsInitialised) then
    ---------------------------
    -- Run the app processing loop for 5 seconds
    ---------------------------
    IApp:run(5000);

    ---------------------------
    -- Close the engine, the window will close, and the program will terminate
    ---------------------------
    IGraphics:destroy();
end

GUI button and subscriber

---------------------------
---------------------------
-- Tutorial: Graphics engine. Test 2
-- Adding a GUI button and ScriptENGINE GUI subscriber to handle the GUI events.
--
-- Copyright (c) 2006-2008 Capricorn 76 Pty. Ltd.
---------------------------
---------------------------

---------------------------
-- GUI handler
---------------------------
function OnGui(callerId, eventType, userData)

    ---------------------------
    -- User clicked button
    ---------------------------
    if (IGraphics:buttonClicked('buttonClose', callerId, eventType)) then
        IApp:stop();
        return true;    -- Handled event
    end
end

---------------------------
---------------------------
-- PROGRAM STARTS HERE
---------------------------
---------------------------

---------------------------
-- Initialise the graphics engine, using a software renderer
---------------------------
local graphicsInitialised = IGraphics:init( 'SOFTWARE', -- Instead of using hard-coded strings, we could IApp:loadScript('constantsVideoDrivers.e76script') and use the pre-defined constants.
                                640, 480,       -- Width/height
                                false,          -- Fullscreen
                                16,         -- Bits/color depth
                                false,          -- Shadows
                                false,          -- Antialiasing
                                false,          -- VSync
                                30,         -- Frame rate
                                false);     -- Silent errors are off, so ScriptENGINE will raise an error is something's not right

if (graphicsInitialised) then
    ---------------------------
    -- Create a GUI button
    ---------------------------
    IGraphics:createButton('buttonClose', 'Close', '0.4 0.4 0.6 0.5');

    ---------------------------
    -- Subscribe to GUI events
    ---------------------------
    local subGui = IEvents:subscribeGui('OnGui');

    ---------------------------
    -- Run the app processing loop until IApp:stop() is called
    ---------------------------
    IApp:run();

    ---------------------------
    -- Stop subscribing to GUI events
    ---------------------------
    IEvents:unsubscribe(subGui);

    ---------------------------
    -- Close the engine, the window will close, and the program will terminate
    ---------------------------
    IGraphics:destroy();
end


Using ScriptENGINE options

---------------------------
---------------------------
-- Tutorial: Graphics engine. Test 3
-- Using ScriptENGINE options to hold program options like graphics engine settings.
--
-- Copyright (c) 2006-2008 Capricorn 76 Pty. Ltd.
---------------------------
---------------------------

---------------------------
-- GUI handler
---------------------------
function OnGui(callerId, eventType, userData)

    ---------------------------
    -- User clicked button
    ---------------------------
    if (IGraphics:buttonClicked('buttonClose', callerId, eventType)) then
        IApp:stop();
        return true;    -- Handled event
    end
end

---------------------------
-- Load ScriptENGINE options
-- In a real program, you would create your options-files using the ScriptENGINE SDK, save them to file, and then load them here.
-- But for this tutorial, we'll create some options here and now...
---------------------------
local function loadOptions()
    IApp:setOption('videoSettings', 'driver', 'SOFTWARE');
    IApp:setOptionInt('videoSettings', 'width', 640);
    IApp:setOptionInt('videoSettings', 'height', 480);
    IApp:setOptionBool('videoSettings', 'fullScreen', false);
    IApp:setOptionInt('videoSettings', 'bits', 16);
    IApp:setOptionBool('videoSettings', 'shadows', false);
    IApp:setOptionBool('videoSettings', 'antiAlias', false);
    IApp:setOptionBool('videoSettings', 'vsync', false);
    IApp:setOptionInt('videoSettings', 'frameRate', 30);
end

---------------------------
---------------------------
-- PROGRAM STARTS HERE
---------------------------
---------------------------

---------------------------
-- Load in program options.
-- Usually this would be from file using IApp:loadOptions()
---------------------------
loadOptions();

---------------------------
-- Initialise the graphics engine, using the ScriptENGINE options defined values
---------------------------
local graphicsInitialised = IGraphics:init( IApp:getOption('videoSettings', 'driver'),      -- Driver
                                IApp:getOptionInt('videoSettings', 'width'),        -- Width
                                IApp:getOptionInt('videoSettings', 'height'),       -- Height
                                IApp:getOptionBool('videoSettings', 'fullScreen'),  -- Fullscreen
                                IApp:getOptionInt('videoSettings', 'bits'),     -- Bits/color depth
                                IApp:getOptionBool('videoSettings', 'shadows'), -- Shadows
                                IApp:getOptionBool('videoSettings', 'antiAlias'),   -- Antialiasing
                                IApp:getOptionBool('videoSettings', 'vsync'),       -- VSync
                                IApp:getOptionInt('videoSettings', 'frameRate'),    -- Frame rate
                                false);     -- Silent errors are off, so ScriptENGINE will raise an error is something's not right

if (graphicsInitialised) then
    ---------------------------
    -- Create a GUI button
    ---------------------------
    IGraphics:createButton('buttonClose', 'Close', '0.4 0.4 0.6 0.5');

    ---------------------------
    -- Subscribe to GUI events
    ---------------------------
    local subGui = IEvents:subscribeGui('OnGui');

    ---------------------------
    -- Run the app processing loop until IApp:stop() is called
    ---------------------------
    IApp:run();

    ---------------------------
    -- Stop subscribing to GUI events
    ---------------------------
    IEvents:unsubscribe(subGui);

    ---------------------------
    -- Close the engine, the window will close, and the program will terminate
    ---------------------------
    IGraphics:destroy();
end


Loading ScriptENGINE GUIs

---------------------------
---------------------------
-- Tutorial: Graphics engine. Test 4
-- Initialise the graphics engine, and load a GUI from file.
--
-- Copyright (c) 2006-2008 Capricorn 76 Pty. Ltd.
---------------------------
---------------------------

---------------------------
-- GUI handler
---------------------------
function OnGui(callerId, eventType, userData)

    ---------------------------
    -- User clicked button
    ---------------------------
    if (IGraphics:buttonClicked('buttonClose', callerId, eventType)) then
        IApp:stop();
        return true;    -- Handled event
    end
end

---------------------------
---------------------------
-- PROGRAM STARTS HERE
-- All global code (outside of any functions) is first run.
-- Then the ScriptENGINE automatically run the State Machine by looking for the OnStateRun function
---------------------------
---------------------------
function OnStateRun(scriptName, param)
    ---------------------------
    -- Initialise the graphics engine, using the ScriptENGINE options defined values
    ---------------------------
    local graphicsInitialised = IGraphics:init( 'SOFTWARE', -- Instead of using hard-coded strings, we could IApp:loadScript('constantsVideoDrivers.e76script') and use the pre-defined constants.
                                    640, 480,       -- Width/height
                                    false,          -- Fullscreen
                                    16,         -- Bits/color depth
                                    false,          -- Shadows
                                    false,          -- Antialiasing
                                    false,          -- VSync
                                    30,         -- Frame rate
                                    false);     -- Silent errors are off, so ScriptENGINE will raise an error is something's not right

    if (graphicsInitialised) then
        ---------------------------
        -- Load a gui from file
        ---------------------------
        IGraphics:loadGui('test4.e76gui');

        ---------------------------
        -- Subscribe to GUI events
        ---------------------------
        local subGui = IEvents:subscribeGui('OnGui');

        ---------------------------
        -- Run the app processing loop until IApp:stop() is called
        ---------------------------
        IApp:run();

        ---------------------------
        -- Stop subscribing to GUI events
        ---------------------------
        IEvents:unsubscribe(subGui);

        ---------------------------
        -- Close the engine, the window will close, and the program will terminate
        ---------------------------
        IGraphics:destroy();
    end
end

Low-level Graphics Drawing

---------------------------
---------------------------
-- Tutorial: Graphics engine. Test 5
-- Initialise the graphics engine,
-- Load a GUI from file,
-- Load a skin,
-- Subscribe to a low-level drawing event to do our own drawing each frame.
--
-- Copyright (c) 2006-2008 Capricorn 76 Pty. Ltd.
---------------------------
---------------------------

---------------------------
---------------------------
-- PROGRAM STARTS HERE
-- All global code (outside of any functions) is first run.
-- Then the ScriptENGINE automatically run the State Machine by looking for the OnStateRun function
---------------------------
---------------------------
function OnStateRun(scriptName, param)
    ---------------------------
    -- Initialise the graphics engine, using the ScriptENGINE options defined values
    ---------------------------
    local graphicsInitialised = IGraphics:init( 'SOFTWARE', -- Instead of using hard-coded strings, we could IApp:loadScript('constantsVideoDrivers.e76script') and use the pre-defined constants.
                                    640, 480,       -- Width/height
                                    false,          -- Fullscreen
                                    16,         -- Bits/color depth
                                    false,          -- Shadows
                                    false,          -- Antialiasing
                                    false,          -- VSync
                                    30,         -- Frame rate
                                    false);     -- Silent errors are off, so ScriptENGINE will raise an error is something's not right

    if (graphicsInitialised) then
        ---------------------------
        -- Load a gui from file
        ---------------------------
        IGraphics:loadGui('test4.e76gui');

        ---------------------------
        -- Subscribe to GUI events
        ---------------------------
        local subGui = IEvents:subscribeGui('OnGui');
        local subDraw = IEvents:subscribeDrawPostGui('OnDraw');

        ---------------------------
        -- Load a skin
        ---------------------------
        ISkin:activate('metallic.e76skin');

        ---------------------------
        -- Run the app processing loop until IApp:stop() is called
        ---------------------------
        IApp:run();

        ---------------------------
        -- Stop subscribing to GUI events
        ---------------------------
        IEvents:unsubscribe(subGui);
        IEvents:unsubscribe(subDraw);

        ---------------------------
        -- Close the engine, the window will close, and the program will terminate
        ---------------------------
        IGraphics:destroy();
    end
end

---------------------------
-- The Graphics draw event subscriber
---------------------------
function OnDraw(userData)
    -- Get the current mouse position
    local pos = IMouse:getPosition();

    -- Animate the radius between 0 - 0.05;
    local radius = math.mod(IApp:getTime(), 1000)/1000; -- This will cycle between 0 - 1 every 1000ms
    radius = radius * 0.05; -- Scale the radius range to 0-0.05

    -- Draw an animated circle
    IGraphics:draw2DCircle(pos, radius, 20, '1 0 0 1');

    -- Draw an image
    IGraphics:draw2DImage('subDraw.png', pos, true);

end

---------------------------
-- GUI handler
---------------------------
function OnGui(callerId, eventType, userData)

    ---------------------------
    -- User clicked button
    ---------------------------
    if (IGraphics:buttonClicked('buttonClose', callerId, eventType)) then
        IApp:stop();
        return true;    -- Handled event
    end
end

Copyright © 2006-23 Sep 2009 Capricorn 76 Pty. Ltd. (created on Wed Sep 23 16:49:12 2009)