Subscribe to GameFromScratch on YouTube Support GameFromScratch on Patreon

15. December 2016

 

Today Unity released EditorVR, an experimental build of Unity that enables you to work (partially) in VR.  This means you will be able to layout scenes, modify existing scenes and adjust components in Unity while using a VR headset.  Currently EditorVR supports the HTC Vive and Oculus Rift 15-EditorVR-headsets. 

 

Details from the announcement:

Easily make VR tools and run them in Unity. EditorVR is now available for free, today!

You may have been following our progress with constructing environments for VR in VR. Now it’s your turn. Today, you can download the EditorVR Experimental Build and the EditorVR Unity Asset Package. There is so much more we’re looking to add, but we think that enough of the foundation is there that we no longer want to hold this out from the community.

For now, if you’re building VR games and experiences, EditorVR will likely help you with:

  • Initially laying out a scene in VR
  • Making tweaks to existing VR scenes
  • Making adjustments to components using the Inspector workspace
  • Building your own custom VR tools

At Unity Labs, we are on a mission to explore the long-term view of where creation technology is headed. At our Unite demo last month we gave a final peek at the experience of building out an environment in real-time using VR-specific UI (many thanks to our friends at Campo Santo for providing the beautiful Firewatch scene). You can build directly in the headset, on both Vive and Rift. Learn more about how to use it here.

 

EditorVR was first previewed at Unite 2016 last month, shown in the video below.

GameDev News

15. December 2016

 

Welcome to the next part in the ongoing Closer Look At game engine series.  The Closer Look series combines a review, overview and getting started tutorial in one and aims to give a developer a good idea ofs2closerlook a given game engine is a good fit for them or not.  Today we are looking at the S2Engine, which is a commercial game engine available for purchase on Steam.  Right off the hop the S2Engine is going to have two major strikes against it for many game developers.  First, it’s Windows only.  Figured I’d get the out of the way right up front as if you are looking to create cross platform games, this is not the engine for you.  Second, it’s commercial and closed source, although the price tag is quite low at around $20USD.

 

As always there is an HD video version of this review available here.

 

Without further ado, let’s jump in to the S2Engine.

 

The Tools

The entire development experience in the S2Engine takes place in the S2EngineHD Editor.  Here is the editor in action running the included sample scene:

s1

 

The S2Engine Editor is absolutely loaded with tools, lets take a guided tour.  First off we have the scene window where you can see a live rendering of your game in action:

sceneoptimized

 

Please keep in mind the fuzziness in the picture above comes from the animated gif process, the real-time viewport looks vastly superior to the image above.  The Scene view is where you place entities in your scene and preview the results.

 

You can switch between the various available tools using the following toolbar:

image

Or via menu:

image

 

The Project Window

image

 

This is where the various assets of your game are organized.  It’s an analog to the underlying file system of your project.  The buttons across the left enable you to filter the various different objects ( Animations, Models, FSMs, etc ).

 

The Class Window

image

 

This is where you can instance various game objects.  Select an object, click Create and then click within the scene to place the entity.  The newly created entity (or a selected entity) can then be configured using the lower Params window, which is context sensitive to the selected object.

 

Terrain Tools

image

 

S2Engine has a full terrain editing system in.  The Terrain tool enables you to interactively edit the scenes landscape (height map).  There are tools for raising/lowering, flattening, stepping and painting terrain as well as drawing roads and vegetation.  The actual editing is done in the scene window:

terrain

 

Model Window

image

 

S2Engine supports 3D animated models of FBX format, simply click the Import button in the Project view:

image

 

There are multiple different panels for configuring models, setting and handling animations, managing joints/bones and even vertex level manipulations.

image

 

There is also an animation panel.

image

This enables you to blend animations together, set and remove animation keys and of course, preview animations.

 

Material Window

imageimage

 

A detailed material system allowing multiple layers.  You can control diffuse and normal maps, UV tilting, lighting properties and more.  You also have control over detailed material attributes like alpha blending, animations, scattering, light emission and more.

 

Special FX

image

 

Fine tuned control over multiple special FX of types weather, post processing and environmental.  Fine tune control over all aspects, including water fx, sky, weather, lens effects, etc.  You also have fine tune control over day/night cycles using a keyframe system:

image

 

Cutscene Tool

image

 

S2Engine has a complete system in place for authoring cut scenes.  Includes a curve editor:

image

 

As well as a detailed timeline:

image

 

Hierarchy

image

This is essentially your scene graph.

 

Font Tool

image

Enables you to create png textures for fonts of varying dimensions with font preview.  Fonts are imported in fnt format created using the BMFont tool.

 

GUI Editor

image

Currently in Beta, there is a UI editor available.  You can create a hierarchy of UI widgets and configure them using the Params class panel.

 

imageimage

Currently supported widgets include button, slider, frame, input box, combobox, label, checkbutton, colorbox, image, listbox, groupbox, tabbox and rangeinputbox.

 

Publishing

When you are complete publishing is a pretty straight forward process.  This is one of the advantages of only supporting a single platform publishing is as simple as choosing a file name, starting scene, main script (entry point) and a few other settings and clicking the Publish button.

image

 

Coding

There are two ways to code in S2Engine.  You can use their own scripting language or the visual FSM (Finite State Machine) visual programming language.  The scripting language has the sc2 extension and has a C like syntax.  You can read the language reference here while the API documentation is available here.  Scripts are simply connected to (and thus control) game objects.  Here is an example script that controls a jeep found in the demo game.

#message TakeControl
#message LeftControl
#message LockWaypoints
#message UnlockWaypoints

#use "engine.wav"

var float acc;
var float steer;
var bool brake;
var bool control;
var float steerAng;
var string steerNode;
var string handsNode;
var float oldsteerAng;
var bool _on;

function void Init()
{
	_on=false;
	control=false;
	steerAng=0.0;
	steerNode="steer";
	handsNode="hands";
	AICreateObject(false,false,205.0,200.0);
	resetNodeFlags("camera","visible");
	resetNodeFlags("hands","visible");
	SetSource(10.0,15000.0);
}

function void PostInit()
{
	SetPerObjectMaterialData(3,0.0);
	resetNodeFlags("light01","visible");
	resetNodeFlags("light02","visible");
	_on=false;
}

function void update()
{
	if(control==true)
	{
		acc=0.0;
		steer=0.0;
		brake=false;
		RotateNode(steerNode,"rgt",-steerAng);
		RotateNode(handsNode,"rgt",-steerAng);
		
		SetSourcePitch((PhysicsGetVehicleRPM()*0.0025)+1.0);
		/*LOG( string(PhysicsGetVehicleRPM()) );*/
		
		if(IsKeyPressed("w"))
		{
			acc=1.0;
		}
		if(IsKeyPressed("s"))
		{
			acc=-0.5;
		}
		oldsteerAng=steerAng;
		if(IsKeyPressed("d"))
		{
			steerAng=steerAng+(frametime);
			steer=-1.0;
		}
		else
		{
			if(IsKeyPressed("a"))
			{
				steerAng=steerAng-(frametime);
				steer=1.0;
			}
			else
			{
				steerAng=0.0;
			}
		}
		if(IsKeyPressed(" "))
		{
			brake=true;
		}
		if(steerAng>=35.0)
		{
			steerAng=35.0;
		}
		if( steerAng<=(-35.0) )
		{
			steerAng=-35.0;
		}
		steerAng=ScalarInterpolate(oldsteerAng,steerAng,(frametime/200.0));	
		RotateNode(steerNode,"rgt",steerAng);
		RotateNode(handsNode,"rgt",steerAng);
		PhysicsVehicleControl(steer,acc,brake);
		
		if(acc!=0.0)
		{
			var vec3 fwdaxis;
			fwdaxis=GetWorldAxis("fwd");
			SendMessageMulti("pushOut",string(fwdaxis),400.0,"null");
		}
	}
	else
	{
		PhysicsVehicleControl(0.0,0.0,true);
		if(ObjectIsInRange("player01",300.0))
		{
			SendMessageSingle("player01","DriveVehicle","");
		}
	}
}

function void message()
{
	if( ReceivedMessage("TakeControl") )
	{
		/* lights control */
		var float tod;
		tod=float(GetLevelParam("TimeOfDay"));
		LOG("tod"+string(tod));
		if( (tod>=18.0) && (tod<=24.0) )
		{
			SetPerObjectMaterialData(3,1.0);
			if(!_on)
			{
				SetNodeFlags("light01","visible");
				SetNodeFlags("light02","visible");
				_on=true;
			}
		}
		if( (tod>0.0) && (tod<6.0) )
		{
			SetPerObjectMaterialData(3,1.0);
			if(!_on)
			{
				SetNodeFlags("light01","visible");
				SetNodeFlags("light02","visible");
				_on=true;
			}
		}
		if( (tod>6.0) && (tod<18.0) )
		{
			SetPerObjectMaterialData(3,0.0);
			ResetNodeFlags("light01","visible");
			ResetNodeFlags("light02","visible");
			_on=false;
		}
	
		/*=======================================0*/
		control=true;
		SetNodeFlags("hands","visible");
		PlaySound("engine.wav",true);
	}
	
	if( ReceivedMessage("LeftControl") )
	{
		control=false;
		ResetNodeFlags("hands","visible");
		StopSound();
		SetPerObjectMaterialData(3,0.0);
		ResetNodeFlags("light01","visible");
		ResetNodeFlags("light02","visible");
		_on=false;
	}
}

 

It’s a straight forward enough language, but I generally prefer that engines use an off the shelf scripting engine instead of rolling their own.  This gives the community access to a much larger source of expertise, sample code and generally is much more time tested and stable.  As you can see from the script above, much of the logic and communication is implemented via message passing.

The majority of in game programming however is done using FSM (Finite State Machines ) via the FSM graph.

s2

If you’ve ever worked in Blueprints in Unreal Engine or Flowgraph in CryEngine you should have a pretty good idea how FSM programming works.  You’re code responds to various events and creates program flows using a series of connecting cables to other states.  Each node can have multiple actions, configured like so:

s3

There are dozens of states available, and new ones can easily be created.

image

Variables are easily created as well.

image

In addition to local variables, parameters and globals can also be defined.

image

 

 

The Documentation, Community and Content

The S2Engine has a decent amount of documentation, reference materials, getting started videos and beginner projects.  There are however a few issues, the first of which is English.  The developers primary language is not English and it shows on occasion in the documentation.  The actual UI is very well translated but some of the documentation  is certainly a tad “Engrish”.  Worse, some of the linked starting videos aren’t in English at all.  I have no issue with non-English videos, but I would recommend not linking them directly from an English localized application.

In terms of actual available documentation, there is a Wiki available here, a very solid reference manual available here, and a series of video tutorials available here.  S2Engine also comes with the beginner scene you’ve seen throughout this review.

The community for S2Engine isn’t huge but there is an active forum.  There is also a Trello bug tracking board available on Trello as well as a few other community options.  One impressive thing about the engine is the engine developer is very responsive to user requests and feedback.

One interesting aspect of the S2Engine is the existence of Content DLC.  These are themed content packs you can download and use in your game.  Currently the only content pack is the Medieval content pack shown in the video below. There is another content DLC pack in the works.

 

 

Conclusion

I pointed out the two biggest negatives to this game engine in the very first paragraph.  It’s Windows only, both for tooling and target platforms.  It’s closed source and commercial.  For many those are going to be big enough deal breakers that nothing else really matters.  For the rest though, is this a worthwhile engine?  For a small team effort, there is a massive amount of functionality included in this engine, it’s capable of some staggeringly pretty results and the workflow, once understood, is pretty accessible to people with limited programming experience.

My biggest recommendation to the developer behind this engine is to make a proper demo available.  What will get people to use this engine over the other options is that they prefer the workflow, the tools, the built in assets, etc.  The lack of a current demo is going to vastly limit the potential audience.  Even with a low price tag, few people will spend money to evaluate an engine and having a previous weaker version of your engine available as the trial is certainly a mistake.  When you go to the download section of the website, you are greeted by this text:

NOTE: The version to be downloaded here (1.4.5) is a previous, very old, FREE BETA version. It is useful just for letting you to view How S2ENGINE HD is organized and How it works. It doesn’t represent the final quality of the 1.4.6 Steam version.

This is quite simply a mistake.  A demo is about selling people on your engine, so having them experience a “very old” version is a bad idea.  Always put your best foot forward when showing an engine.  I would recommend creating a version of the current engine that is full featured but either time locked, save limited or publish limited.  You will have a great many more developers willing to give your engine a try. 

I have found the performance to be a bit inconsistent.  I was running consistently at 70+ FPS, then struggled to hit 15FPS for a while with a ton of UI glitches.  Upgrading to the newest nVidia drivers didn’t help.  Then oddly, switching Optimus to use the integrated GPU, then back to dedicated seemed to fix the problems.  Hopefully these problems are localized to me and not widespread.  I wish the developers used a standard UI toolkit like Qt, as their custom implementation can be a bit buggy or not perform as you’d expect.  I also unfortunately experienced a half a dozen crashes while evaluating the engine, including one while making the video version of this review.

 

The Video

Programming , ,

14. December 2016

 

Allegro is an open source, C++ based game framework handling low level tasks such as window creation, input, data loading, drawing images, playing sound and other common 2D game tasks (much like SDL or SFML).  It runs on Mac, Windows, Linux, iPhone and Android devices.  Allegro is a libraryallegro that has been around for years, in fact it started life on the Atari ST computers.  Allegro actually stands for Atari Low Level Game Routines.  It’s an accessible and easy to use framework and one that has a special place in my heart.  The developers at Allegro just released a new version, 5.2.2.

Details of the new release:

Core:

  • Don't accumulate time in the timer while it is stopped.

  • Use dynamic OpenGL ES checks, so binaries produced on newer platforms don't crash on older ones.

  • Destabilize the OpenGL extensions API (BREAKING CHANGE!).

Raspberry Pi port:

  • Add various optimizations.

  • Fix al_set_mouse_xy under X.

Android port:

  • Fix buffer overrun and memory leak in the clipboard support.

  • Add WANT_GLES3 to disable some of the newer features on platforms where they aren't supported.

  • Fix build in Android versions below 3.1.

  • Fix a crash when activity is destroyed/paused.

  • Allow building for android mips, arm64 and mips64.

  • Add al_android_get_jni_env and al_android_get_activity utility functions.

  • Update manifest files for newer Android versions.

Windows port:

  • Handle keyboard input properly when Ctrl is pressed (Tobias Scheuer).

Hurd port:

  • Define a fallback PATH_MAX (Andreas Rönnquist).

OSX port:

  • Clear window to black when going in/out of fullscreen.

  • Fix window centering when going out of FULLSCREEN_WINDOW mode.

  • Fix OSX 10.12 build.

  • Allow 32 bit builds on OSX (MarcusCalhoun-Lopez).

Build system:

  • Fix issues with building on GCC6 on Windows.

  • Fix source directory littering while configuring the Android build.

Python binding:

  • Add support Python 3 (Gabriel Queiroz).

Documentation:

  • Document the behavior of al_set_target_bitmap with respect to transformations (Edgar Reynaldo).

  • Fix typo in al_use_transform docs (Ryan Roden-Corrent).

Examples:

  • Add kerning to the al_get_glyph example in ex_ttf.

  • Various fixes in ex_camera (Erich Erstu).

GameDev News

14. December 2016

 

Krita is a popular open source digital painting application, which just saw a milestone 3.1 release.  Why a milestone release you ask?  This release marks the first time that Krita runs natively on Mac OS thank to a summer of code sponsored project by Julian Thijssen.  A Mac OS release isn’t the only feature of this release.  Full features include:

  • Mac OSX Support
  • Render animations (to Gif, MP4, MKV and OGG formats)
  • Animation Curves and Opacity (another Summer of Code project)
  • New Colour picker
  • New Quick Brush Engine
  • Stop Based Gradient Editor
    • krita_texture_bg
  • Create a stroke around your selection (https://docs.krita.org/Stroke_Selection)
  • Halftone filter added. Find it in the main menu: Filters > Artistic.
  • add a new Eraser Switch Opacity feature, similar to the Eraser Switch Size one.
  • new layer from visible option in layer menu
  • OSX: add quicklook plugin
  • Added support for loading Scribus XML palettes.

 

These features on top of a few dozen other fixes and changes.  Krita certainly is developing nicely and Google’s Summer Of Code program has been a big help.

Krita is available for download here.

GameDev News

14. December 2016

 

Tizen is an open source embedded OS based on the Linux kernel designed to be used in a variety of devices, the most relevant to game developers are Smart TVs and mobile phones.  It came to life out of a home grown OS project at Samsung called Bada.  To date the majority of devices that run Tizen are from Samsung including the Z1 and Z3 smart phones, Gear 2 smart watch and the JU6500 4K smart TV, although other manufacturers are part of the committee.  Basically you can think of Tizen as Samsung’s hedge against Android should something go wrong with that platform.

So… why does this matter to you as game developers?  Well Tizen just announced a contest with some pretty simple rules and $9million in prizes.  Basically over a 9 month period, the top 100 apps in the Tizen app store will get $10,000.  So if you manage to have a top 100 app for the entire duration of the contest, you will make a cool $90,000 on top of any other revenue you make from app sales.  While the Tizen app store may not be the biggest in the world, a $10,000 monthly incentive is sure to draw developer interest.

 

Details of the contest:

HOW TO
PARTICIPATE

  • 1.Develop a Tizen application or game using TIZEN SDK & Tools (http://developer.tizen.org) The target devices should be Samsung Z1, Samsung Z2, Samsung Z3 and further smart phones launch in the 2017.
  • 2.You need to join the Tizen Store seller office
    (http://seller.tizenstore.com) first and follow the instructions on the website to register your applications.
  • 3.Visit the incentive program website, which will be opened on the early of January 2017, and register your app with its basic information.

TIMELINE

Participation Registration Period
Early of Jan, 2017 – Oct 31, 2017 (GMT)
Program Duration
Feb 1, 2017 at 00:00 - Oct 31, 2017 at 23:59(GMT)

INCENTIVE
REWARDS

Prize
$10,000 for top 100 apps every month(The reward can be provided at once per a app)
Judging Criteria
Every month, the 100 eligible applications that have
been downloaded the most times on Tizen Store.(Detailed rules will be released in January 2017)

 

Tizen support is available in several game engines including Unity, Cocos2d-x, GameSalad, OpenFL and GameMaker.

GameDev News

Month List

Popular Comments

Moai Tutorial Part 6: Who put the bomp in the Moai, Moai, Moai. Playing audio in… you guessed it… Moai
Subscribe to GameFromScratch on YouTube Support GameFromScratch on Patreon


17. September 2012

 

In this tutorial, we are going to look at how to play audio using Moai.  This is going to make heavy use of moaigui, so if you haven’t already, you may want to check the prior tutorial in this series.

 

As always, let’s jump right in to the code.  You may notice that the code order is starting to seem a little… odd. This is because I am grouping code mostly in order of newness, as opposed to how it would logically be laid out.  If you are thinking “I wouldn’t order things this way!”, well.. neither would I, normally.

 

screenWidth = MOAIEnvironment.screenWidth
screenHeight = MOAIEnvironment.screenHeight
if screenWidth == nil then screenWidth =1280 end
if screenHeight == nil then screenHeight = 800 end


MOAISim.openWindow("Window",screenWidth,screenHeight)

viewport = MOAIViewport.new()
viewport:setSize(screenWidth,screenHeight)
viewport:setScale(screenWidth,screenHeight)

package.path = './moaigui/?.lua;' .. package.path
require "gui/support/class"
local moaigui = require "gui/gui"
local resources = require "gui/support/resources"
local filesystem = require "gui/support/filesystem"
local inputconstants = require "gui/support/inputconstants"
local layermgr = require "layermgr"


local gui = moaigui.GUI(screenWidth,screenHeight)


gui:addToResourcePath(filesystem.pathJoin("moaigui/resources", "fonts"))
gui:addToResourcePath(filesystem.pathJoin("moaigui/resources", "gui"))
gui:addToResourcePath(filesystem.pathJoin("moaigui/resources", "media"))
gui:addToResourcePath(filesystem.pathJoin("moaigui/resources", "themes"))

layermgr.addLayer("gui",99999, gui:layer())
gui:setTheme("basetheme.lua")
gui:setCurrTextStyle("default")

function onPointerEvent(x, y)
    gui:injectMouseMove(x, y)
end

function onMouseLeftEvent(down)
    if(down) then
        gui:injectMouseButtonDown(inputconstants.LEFT_MOUSE_BUTTON)
    else
        gui:injectMouseButtonUp(inputconstants.LEFT_MOUSE_BUTTON)
    end
end

function onTouchEvent(eventType,idx,x,y,tapCount)
    --gui:injectTouch(eventType,idx,x,y,tapCount)
    onPointerEvent(x, y)
    if (MOAITouchSensor.TOUCH_DOWN == eventType) then
        onMouseLeftEvent(true)
    elseif (MOAITouchSensor.TOUCH_UP == eventType) then
        onMouseLeftEvent(false)
    end
end


if MOAIInputMgr.device.pointer then
    MOAIInputMgr.device.pointer:setCallback(onPointerEvent)
    MOAIInputMgr.device.mouseLeft:setCallback(onMouseLeftEvent)
else
    MOAIInputMgr.device.touch:setCallback(onTouchEvent)
end


function onButtonClick(event,data)
    if(string.find(buttonPlay:getText(),"Play") ==1) then
        song1:play()
        buttonPlay:setText("Stop, that's creeping me out!")

        function checkSongDone()
            local song = song1
            while song1:isPlaying() do coroutine:yield() end

            song1:stop()
            buttonPlay:setText("Play Audio File using Untz")
        end

        if not checkLoop:getChecked() then
            songPlayingThread = MOAICoroutine.new()
            songPlayingThread:run(checkSongDone)
        end
    else
        song1:stop()
        buttonPlay:setText("Play Audio File using Untz")
    end
end

function onButtonRewind(event,data)
    if song1:isPlaying() then
        local pos = song1:getPosition()
        local length = song1:getLength()
        local tenPercent = (length * 1.10) - length
        pos = pos - tenPercent
        if(pos > 0) then
            song1:setPosition(pos)
        end
    end
end

function onButtonFastForward(event,data)
    if song1:isPlaying() then
        local pos = song1:getPosition()
        local length = song1:getLength()
        local tenPercent = (length * 1.10) - length
        pos = pos + tenPercent
        if(pos < length) then
            song1:setPosition(pos)
        end
    end
end

function onCheckboxChanged()
    song1:setLooping(checkLoop:getChecked())
end

function onVolumeChanged()
    MOAIUntzSystem.setVolume(sliderVolume:getCurrValue() / 100)
end



buttonPlay = gui:createButton()
buttonPlay:setPos(0,0)
buttonPlay:setDim(100,25)
buttonPlay:setText("Play Audio File using Untz")
buttonPlay:registerEventHandler(buttonPlay.EVENT_BUTTON_CLICK,nil,onButtonClick)

checkLoop = gui:createCheckBox()
checkLoop:setPos(0,26)
checkLoop:setDim(100,10)
checkLoop:setText("Loop?")
checkLoop:registerEventHandler(checkLoop.EVENT_CHECK_BOX_STATE_CHANGE,nil,onCheckboxChanged)

labelVolume = gui:createLabel()
labelVolume:setText("Volume")
labelVolume:setPos(0,36)
labelVolume:setDim(30,10)

sliderVolume = gui:createHorzSlider()
sliderVolume:setPos(30,36)
sliderVolume:setDim(70,10)
sliderVolume:setRange(0,100)
sliderVolume:setCurrValue(50)
sliderVolume:setValueDisplayLoc(sliderVolume.VALUE_DISPLAY_RIGHT)
sliderVolume:registerEventHandler(sliderVolume.EVENT_SLIDER_VALUE_CHANGED,nil,onVolumeChanged)

buttonRewind = gui:createButton()
buttonRewind:setPos(0,50)
buttonRewind:setDim(50,10)
buttonRewind:setText("Rewind")
buttonRewind:registerEventHandler(buttonRewind.EVENT_BUTTON_CLICK,nil,onButtonRewind)

buttonFastForward = gui:createButton()
buttonFastForward:setPos(51,50)
buttonFastForward:setDim(50,10)
buttonFastForward:setText("Fast Forward")
buttonFastForward:registerEventHandler(buttonFastForward.EVENT_BUTTON_CLICK,nil,onButtonFastForward)

MOAIUntzSystem.initialize ()
MOAIUntzSystem.setVolume(0.5)
song1 = MOAIUntzSound.new()
song1:load("demongirls.ogg")

 

If we run the application( in the Moai-untz host ), we see:

 

image

 

Click the “Play Audio File using Untz” button and the song will play, the slider increases and decreases the volume, the loop check box I bet you can guess what it does.  Rewind and Fast Forward change the playback position by 10% at a time.

 

This looks like a wall of code, but don’t worry, it’s not all that bad.  In reality, most of it is maoigui code that we covered earlier.  We use a couple of new controls ( the check box and slider ), but nothing in the UI code should be all together new.  Most of our new logic is in the click handler methods, so let’s start there.

 

function onButtonClick(event,data)
    if(string.find(buttonPlay:getText(),"Play") ==1) then
        song1:play()
        buttonPlay:setText("Stop, that's creeping me out!")

        function checkSongDone()
            local song = song1
            while song1:isPlaying() do coroutine:yield() end

            song1:stop()
            buttonPlay:setText("Play Audio File using Untz")
        end

        if not checkLoop:getChecked() then
            songPlayingThread = MOAICoroutine.new()
            songPlayingThread:run(checkSongDone)
        end
    else
        song1:stop()
        buttonPlay:setText("Play Audio File using Untz")
    end
end

This is the code that is executed if the brilliantly named buttonPlay button is clicked.  The first thing we do is check to see if the button’s text starts with “Play”, which if it does, that means we are currently not playing a song.  Therefore we start playing our song ( aptly named song1 ) with a call to the play() method.  We then update our button’s text to indicate it’s new status.  The next little bit of code might seem a bit confusing at first, but it is pretty powerful.

 

First we are declaring a nested function checkSongDone().  The next line is where the magic happens.  It’s a while loop that loops until the condition isPlaying() becomes false.  Each pass through the loop we call coroutine:yield().  So, what the heck was that all about? Well… this is going to be a bit tricky to explain, but I will do my best.

 

If you are an old fart like me, you may remember multitasking in the pre-Win95 pre-emptive multitasking days.  Essentially you ran multiple programs at the same time by giving each one a chunk of processing time and a well behaved program was supposed to yield control when it wasn’t busy.  Essentially the operating system switched between running programs and said “ok, your turn” and a well behaved program would quickly exit if it was done doing whatever it needed to do.  Well, in a nutshell that is what is happening here.

 

A coroutine is sort of the Moai version of a thread.  Using coroutines, you are able to run code in parallel, so while our game loop is still running, the function checkSongDone is blissfully churning away in the background checking to see if the song is done.  Thing is, until the song is actually done, the coroutine doesn’t really have anything else to do, so it calls yield().  It is not until that condition is true ( isSongPlaying returns false ) will the function continue execution.  Once this happens we (somewhat redundantly) call song1:stop() then update the button’s text to “Play Audio…”.  The end result of this whole chunk of code is, we essentially created an on song end handler, so when the song is done playing, the text of the button will update.

 

The next chunk of code checks the checkbox checkLoop, which if checked indicates that we want the song to loop continuously.  If it is not checked, that means we want to run our coroutine, which we create with a call to MOAICoroutine.new(), then run with the run() method, passing in the function to run.

 

If the button’s text however indicates that the song is playing, in which case the button acts like a STOP button, simply stopping playback ( even if loop is checked ) and resetting back to the “Play…” text.  I encourage you to look deeper in to Coroutines, they are a very powerful feature of Lua. MOAICoroutine itself is only a thin wrapper over Lua’s coroutine abilities.

 

Also, you may have noticed MOAIThread in the examples and wondered when to using MOAIThread instead of MOAICoroutine.  The short answer is, you dont!  The more accurate and useful short answer is, MOAIThread *IS* MOAICoroutine.  It was renamed and the documentation just hasn’t caught up yet.  Well at least, it hadn’t at the point I wrote this.

 

TL;DR, coroutines are cool, learn about them, use them, love them.

 

function onButtonRewind(event,data)
    if song1:isPlaying() then
        local pos = song1:getPosition()
        local length = song1:getLength()
        local tenPercent = (length * 1.10) - length
        pos = pos - tenPercent
        if(pos > 0) then
            song1:setPosition(pos)
        end
    end
end

function onButtonFastForward(event,data)
    if song1:isPlaying() then
        local pos = song1:getPosition()
        local length = song1:getLength()
        local tenPercent = (length * 1.10) - length
        pos = pos + tenPercent
        if(pos < length) then
            song1:setPosition(pos)
        end
    end
end

This is the code that executes if you hit the Fast Forward or Rewind buttons.  Basically it gets the current position of the song ( playback-wise ) using getPosition() and the length of the song using getLength().  Next we calculate what 10% of the length is, and either add or subtract that amount from the current position depending if we are fast forwarding or rewinding.  Next we check sure to make sure the position is valid after being modified and if it is, we change the position of the song using setPosition().  End result, pressing Fast forward advances the song by 10% of it’s length, while hitting rewind moves it back 10%.

 

function onCheckboxChanged()
    song1:setLooping(checkLoop:getChecked())
end

This is the code that executes if the checkbox checkLoop is checked or unchecked.  It simply toggles the value of setLooping based off the if the checkbox is checked, using getChecked().

 

function onVolumeChanged()
    MOAIUntzSystem.setVolume(sliderVolume:getCurrValue() / 100)
end

This is the code that is executed if the slider sliderVolume is changed.  This uses the MOAIUntzSystem singleton to change the global volume level using setVolume().  setVolume takes a value from 0.0 to 1.0, while the sliders value returned by getCurrValue() is from 0 to 100, so we divide it by 100.

 

So then, what the heck is an Untz?  It’s the included cross platform audio system.  You also have the option to use FMOD, a commercial audio solution.  Untz does the trick though, works on Android and iOS.  There is a very important caveat… if you are going to be running using the UNTZ audio library you need to use the Moai-untz host!!! when running on the PC.  You will get exceptions if you try to call MOAIUntzSystem from the normal Moai host.  The Moai-untz host is in the same directory as the Moai host.

 

Most of the UI code is pretty much identical to what you’ve seen already, so I wont really go into it, that leaves:

 

MOAIUntzSystem.initialize ()
MOAIUntzSystem.setVolume(0.5)
song1 = MOAIUntzSound.new()
song1:load("demongirls.ogg")

This code first initializes the MOAIUntzSystem singleton.  Then it sets the global volume to 50%.  Next we create our song song1 with a call to MOAIUntzSound.new().  Finally load the song ( in the same directory as main.lua ) with a call to load().

 

You might be asking yourself “what sound formats does Untz support?”.  That’s a good question with a tricky answer.  Basically the safe answer is ogg and wav.  The not so safe answer is, it depends on the platform.  For example, iOS, Mac and Windows will work with mp3, but Android wont.  Beyond WAV and OGG, the file is just passed to the native system for handling, so if the platform supports it, Moai supports it.

 

But if you really want to be cross platform, just encode everything in to ogg.  In this day of increasingly wealthy lawyers, the last thing we want to do is deal with a patent encumbered format like mp3!

 

Oh yeah, and the music… it’s seriously creepy stuff and I downloaded it from here.  Obviously you can substitute any song you want from your Justin Beiber collection, just make sure it’s not an MP3 if you are running on Android.  Of course, you can convert from just about any audio format to just about any other audio format using the free and amazing Audacity.  I do get 10% of all proceeds for saying that.  I may need a new business model.

 

Programming , ,

blog comments powered by Disqus

Month List

Popular Comments