<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>db-in&#039;s blog &#187; Frameworks</title>
	<atom:link href="http://db-in.com/blog/category/frameworks/feed/" rel="self" type="application/rss+xml" />
	<link>http://db-in.com/blog</link>
	<description></description>
	<lastBuildDate>Mon, 05 Mar 2012 15:15:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Universal Framework iPhone iOS (2.0)</title>
		<link>http://db-in.com/blog/2011/07/universal-framework-iphone-ios-2-0/</link>
		<comments>http://db-in.com/blog/2011/07/universal-framework-iphone-ios-2-0/#comments</comments>
		<pubDate>Tue, 12 Jul 2011 16:55:58 +0000</pubDate>
		<dc:creator>Diney Bomfim</dc:creator>
				<category><![CDATA[App]]></category>
		<category><![CDATA[Feature]]></category>
		<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[iPad]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">http://db-in.com/blog/?p=1442</guid>
		<description><![CDATA[Learn how to construct a Custom Framework to iOS Devices and distribute it.]]></description>
			<content:encoded><![CDATA[<p><a href="http://db-in.com/blog/wp-content/uploads/2011/07/framework_ios_2.png"><img src="http://db-in.com/blog/wp-content/uploads/2011/07/framework_ios_2.png" alt="" title="framework_ios_2" width="300" height="300" class="alignright size-full wp-image-1447" /></a>Hello my friends,</p>
<p>Due to some bugs and questions with the old tutorial, I&#8217;m creating this new one, much more simpler and less bugs than the another one. I&#8217;ll not post the old link here because everything you need to know you can find right here.</p>
<p>Nowadays, exist few alternatives to create a Framework to iOS, changing the default Xcode Script, which could not be a good choice if you want to publish the APPs constructed with your custom Framework. I&#8217;ll treat here about how to construct an Universal Framework to iOS, using the default tools from Xcode.</p>
<p>Let&#8217;s start!<br />
<span id="more-1442"></span></p>
<p><a name="list_contents"></a><br />
Here is a little list of contents to orient your reading:</p>
<table width="675">
<tr>
<th colspan=2>List of Contents to this Tutorial</th>
</tr>
<tr>
<td valign="top">
<ul>
<li><a href="#framework_ios">Framework on iOS? Really?</a></li>
<li><a href="#understanding">Understanding Universal, Dynamic and Static concepts</a></li>
<li><a href="#framework_project">Constructing a Framework Project</a>
<ul>
<li><a href="#step_1">1. Create the Project</a></li>
<li><a href="#step_2">2. Framework Classes</a></li>
</ul>
</li>
<li><a href="#creating_framework">Creating the Framework</a>
<ul>
<li><a href="#step_3">3. Create a Framework Target</a></li>
<li><a href="#step_4">4. Bundle Setup</a></li>
<li><a href="#step_5">5. Adding code and resources to the Bundle (Framework)</a></li>
</ul>
</li>
<li><a href="#building_universal">Building the Universal Framework</a>
<ul>
<li><a href="#step_6">6. Creating Universal Target</a></li>
<li><a href="#step_7">7. Lipo Tool Script</a></li>
</ul>
</li>
<li><a href="#importing">Importing your Universal Framework</a>
<ul>
<li><a href="#step_8">8. Importing</a></li>
</ul>
</li>
<li><a href="#conclusion">Conclusion</a></li>
</ul>
</td>
</tr>
</table>
<p>You can download the project I used in this article:</p>
<p><a href="http://db-in.com/downloads/framework-ios.zip" onmousedown="_gaq.push(['_trackEvent', 'Framework iOS', 'Xcode', 'Download']);"><img class="alignleft" title="download" src="http://db-in.com/imgs/download_button.png" alt="Download Xcode project files to iPhone"/><br />
<strong>Download now</strong><br />
Xcode project files to Framework-iOS<br />
403kb<br />
</a><br/></p>
<p><br/><a name="faq"></a></p>
<h2><strong>FAQ</strong></h2>
<p><a href="#list_contents">top</a><br />
First off, I want to make sure you understand what this Framework to iOS can do, this can safe your time reading this article:</p>
<ol>
<li><strong>Can I use this Framework as a Bundle to store my files, XIBs, images?</strong><br />
A: Yes, you can and now it&#8217;s very very easy to retrieve your files.</li>
<li><strong>Can I use this Framework to import other Frameworks, like import UIKit, CoreGraphics, OpenGL?</strong><br />
A: No. There is no way to do that. Your code in this custom Framework can import classes from other frameworks normally, but it is just a reference, classes from another framework will not be compiled at this time. So you must import the referenced Framework on the new project as well.</li>
<li><strong>Will be my code visible to others?</strong><br />
A: No. This Framework will export a compiled binary, so anyone can see inside it. You can make the same for some other files, like XIBs.</li>
<li><strong>Why I need this?</strong><br />
A: This is for developers/teams that want to share their codes without shows the entire code (.m/.c/.cpp files). Besides this is for who want to organize compiled code + resources (images, videos, sounds, XIBs, plist, etc) into one single place. And this is also for that teams that want to work together above the same base (framework).</li>
</ol>
<p><br/><a name="framework_ios"></a></p>
<h2><strong>Framework on iOS? Really?</strong></h2>
<p><a href="#list_contents">top</a><br />
<a href="http://db-in.com/blog/wp-content/uploads/2011/04/framework_icon.png"><img src="http://db-in.com/blog/wp-content/uploads/2011/04/framework_icon.png" alt="" title="framework_icon" width="128" height="128" class="alignleft size-full wp-image-1397" /></a>Ok buddies, let&#8217;s make something clear, many people had said:&#8221;iOS doesn&#8217;t support custom Frameworks!&#8221;, &#8220;Custom Framework is not allowed at iOS!&#8221;, &#8220;Doesn&#8217;t exist custom Framework on iOS!&#8221; and many other discouraging things like these. Look, I&#8217;ve made many frameworks and worked with many others, I don&#8217;t believe that is really impossible to use a Framework on iOS. According to my experience and knowledge about Frameworks, it&#8217;s absolutely feasible a custom Framework on iOS Devices. If we think more about this issue we can find an elegant solution, right? First, let&#8217;s understand what a Framework really is, here is the definition of framework by Apple&#8217;s eyes:</p>
<blockquote><p>A framework is a hierarchical directory that encapsulates shared resources, such as a dynamic shared library, nib files, image files, localized strings, header files, and reference documentation in a single package.</p></blockquote>
<p>Doesn&#8217;t make many sense something with this description not be allowed in iOS, thinking in architecture and structure. Apple also says:</p>
<blockquote><p>A framework is also a bundle and its contents can be accessed using Core Foundation Bundle Services or the Cocoa NSBundle class. However, unlike most bundles, a framework bundle does not appear in the Finder as an opaque file. A framework bundle is a standard directory that the user can navigate.</p></blockquote>
<p>Good, now thinking about iOS security, performance and size, the only thing in a Framework definition which doesn&#8217;t fit in iOS technology is the &#8220;dynamic shared library&#8221;. The words &#8220;dynamic&#8221; and &#8220;shared&#8221; are not welcome in the iOS architecture. So the Apple allows us to work and distribute something called &#8220;<strong>Static Library</strong>&#8220;. I don&#8217;t like it! It&#8217;s not so easy as a Cocoa Framework, if a developer got your Static Library, he needs to set a header path too, or import the header files&#8230; it&#8217;s not useful, that&#8217;s a shit!</p>
<p><a href="http://db-in.com/blog/wp-content/uploads/2011/05/static_library_no.png"><img src="http://db-in.com/blog/wp-content/uploads/2011/05/static_library_no.png" alt="" title="static_library_no" width="128" height="128" class="alignright size-full wp-image-1403" /></a></p>
<p>Well, so a Framework concept is absolutely compatible with iOS, except by the &#8220;dynamic shared library&#8221;, on the other hand Apple says that a &#8220;static library&#8221; is OK for iOS. So if we replace the &#8220;dynamic shared libraries&#8221; by a &#8220;static library&#8221; we could construct a Custom Framework to iOS, right?</p>
<p>Right!!!!</p>
<p>This is exactly what we&#8217;ll make in this article, let&#8217;s construct a Framework with Static Library, by the way, an Universal Framework.</p>
<p><br/><a name="understanding"></a></p>
<h2><strong>Understanding Universal, Dynamic and Static concepts</strong></h2>
<p><a href="#list_contents">top</a><br />
Simple answer:</p>
<ul>
<li>Universal: Which works perfect on all architectures. iOS devices uses <strong>armv6</strong> and <strong>armv7</strong>, iOS simulator on MacOS X uses <strong>i386</strong>.</li>
<li>Dynamic: The compiler doesn&#8217;t include the target files directly. The classes/libraries are already pre-compiled (binary format) and lies on the system path. Besides, the dynamic libraries can be shared by many applications. This is exactly what Cocoa Framework is.</li>
<li>Static: It represents that classes/libraries which is compiled by the compiler at the build phase. These files can&#8217;t be shared by other applications and relies on application path.</li>
</ul>
<p>Simple as that. If you need more informations about Dynamic VS Static libraries, try this <a href="http://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/OverviewOfDynamicLibraries.html" target="_blank">Apple&#8217;s Documentation</a>.</p>
<p>No more concepts, hands at work!</p>
<p><br/><a name="framework_project"></a></p>
<h2><strong>Constructing a Framework Project</strong></h2>
<p><a href="#list_contents">top</a><br />
<a name="step_1"></a><br />
<h3>1. Create the Project:</h3>
<p><a href="#list_contents">top</a><br />
I want to show you step by step of the entire process, so let&#8217;s start with the most basic, create an iOS project. You can choose one application template in Xcode, this is not really important, but remember to choose one template which could test your Framework code before export it.<br />
<div id="attachment_1399" class="wp-caption aligncenter" style="width: 610px"><a href="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_new_project.jpg"><img src="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_new_project.jpg" alt="Create an application project." title="xcode_new_project" width="600" class="size-full wp-image-1399" /></a><p class="wp-caption-text">Create an application project.</p></div></p>
<p><br/><a name="step_2"></a></p>
<h3>2. Framework Classes:</h3>
<p><a href="#list_contents">top</a><br />
<div id="attachment_1400" class="wp-caption alignleft" style="width: 267px"><a href="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_project_navigator.jpg"><img src="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_project_navigator.jpg" alt="Create your framework classes." title="xcode_project_navigator" width="257" height="286" class="size-full wp-image-1400" /></a><p class="wp-caption-text">Create your framework classes.</p></div></p>
<p>Remember to create an &#8220;import header&#8221; to make everything simpler and organized to the user of your framework. Remember to write this header file with a framework notation, just as shown in the image bellow. Also remember to create your classes taking care to hide the classes which should not be visible to the other developers (users of your framework). We will set the public and private headers soon, but it&#8217;s very important to you protect the &#8220;core&#8221; classes, I mean, that classes which you don&#8217;t want to make visible to other developers.</p>
<p>For those private classes, you could import their header inside the &#8220;<strong>.m</strong>&#8221; (or .mm, .cpp, etc) file of a public class, by doing this you protect the header of private classes. Well, I know you probably already know that, I&#8217;m saying just to reinforce.</p>
<p>Remember that organization is 90% of a good framework, so try follow all the Apple advices to create your classes names, methods, properties, functions, etc.</p>
<p><br/><a name="creating_framework"></a></p>
<h2><strong>Creating the Framework</strong></h2>
<p><a href="#list_contents">top</a><br />
<a name="step_3"></a><br />
<h3>3. Create a Framework Target:</h3>
<p><a href="#list_contents">top</a><br />
OK, let&#8217;s create a target to compile our framework. Click on the icon of your project in the project navigator at the left and hit the button &#8220;Add Target&#8221;. A new window will come up. Now is our first trick. Instead to create a &#8220;Cocoa Touch Static Library&#8221; or a &#8220;Cocoa Framework&#8221; we will create a &#8220;Bundle&#8221; target.</p>
<p>A Bundle? Really? Yes! I can explain. A &#8220;Cocoa Framework&#8221; target can&#8217;t be compiled to armv6/armv7 and Xcode doesn&#8217;t allow us to use &#8220;Static Libraries&#8221; in a &#8220;Cocoa Framework&#8221;, so we can&#8217;t use this target. On the other hand, we can&#8217;t use &#8220;Cocoa Touch Static Library&#8221; either, because it doesn&#8217;t use the framework structure that we want.</p>
<p>Now, the <strong>Bundle</strong> target could be the best choice. It can hold any file we want, we can compile source code inside it and&#8230; we can turn it into a framework. To say the truth, almost all &#8220;Framework &#038; Library&#8221; targets could be turned into a framework too, even the &#8220;Cocoa Touch Static Library&#8221;, throughout this article you probably will figure out how. For now, let&#8217;s create a <strong>Bundle</strong> target.</p>
<div id="attachment_1402" class="wp-caption aligncenter" style="width: 610px"><a href="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_new_target.jpg"><img src="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_new_target.jpg" alt="Create a Bundle target rather than Cocoa Touch Static Library." title="xcode_new_target" width="600" class="size-full wp-image-1402" /></a><p class="wp-caption-text">Create a Bundle target rather than Cocoa Touch Static Library.</p></div>
<p><br/><a name="step_4"></a></p>
<h3>4. Bundle Setup:</h3>
<p><a href="#list_contents">top</a><br />
It&#8217;s time to make all the necessary changes to the Bundle target. Different than the old tutorial. You don&#8217;t need to clean up anything. Just know that everything else will be ignored (linked frameworks .plist files, .pch etc&#8230;).</p>
<p>I&#8217;m sure you already know this, but just to reinforce, here is the Build Setting screen, you can find it by clicking on the project icon in the left project navigator and then clicking in the &#8220;Build Setting&#8221; tab.<br />
<div id="attachment_1405" class="wp-caption aligncenter" style="width: 610px"><a href="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_build_settings-e1304231010699.jpg"><img src="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_build_settings-e1304231010699.jpg" alt="You must make a special Build Setting to turn a Bundle into a framework." title="xcode_build_settings" width="600" class="size-full wp-image-1405" /></a><p class="wp-caption-text">You must make a special Build Setting to turn a Bundle into a framework.</p></div></p>
<p>Here is our second great trick, or should be better to say &#8220;tricks&#8221;. Let&#8217;s change the &#8220;<strong>Build Setting</strong>&#8221; following this list:</p>
<ul>
<li><strong><em>Base SDK</em></strong>: <span style="color: #3366ff;"><strong>Latest iOS (iOS X.X)</strong></span> (in the X.X will appear the number of the lastest iOS SDK installed on your machine).</li>
<li><strong><em>Architectures</em></strong>: <span style="color: #3366ff;"><strong>$(ARCHS_STANDARD_32_BIT) armv6</strong></span> (it’s very important to be exactly this value including the space before &#8220;armv6&#8243;) This setting is valid to Xcode 4.2, if you are using an old version, use the &#8220;Standard (armv6 armv7)&#8221; option. (the values for this property depend on the value of the item bellow, so set that first).</li>
<li><strong><em>Build Active Architecture Only</em></strong>: <span style="color: #3366ff;"><strong>NO</strong></span> (otherwise we can&#8217;t compile to armv6 and armv7 at the same time).</li>
<li><strong><em>Valid Architecture</em></strong>: <span style="color: #3366ff;"><strong>$(ARCHS_STANDARD_32_BIT)</strong></span> (it&#8217;s very important to be exactly this value). If your Xcode is showing two lines with armv6 and armv7, delete then and insert this value in one single line.</li>
<li><strong><em>Dead Code Stripping</em></strong>: <span style="color: #3366ff;"><strong>NO</strong></span>.</li>
<li><strong><em>Link With Standard Libraries</em></strong>: <span style="color: #3366ff;"><strong>NO</strong></span>.</li>
<li><strong><em>Mach-O Type</em></strong>: <span style="color: #3366ff;"><strong>Relocatable Object File</strong></span>. This is the most important change. Here, we instruct the compiler to treat the Bundle as a relocatable file, by doing this, we can turn it into a framework with the wrapper setting.</li>
<li><strong><em>Other Linker Flags</em></strong>: This setting is not mandatory, but if you are planning to use any kind of C++ code (.cpp or .mm) on this framework, Chris Moore (on the comments) advises to use the &#8220;-lstdc++&#8221; option. In this case could be a good idea to use &#8220;-ObjC&#8221; too, to avoid conflicts in old compilers.</li>
<li><strong><em>Wrapper Extension</em></strong>: <span style="color: #3366ff;"><strong>framework</strong></span>. Here we change the Bundle to a Framework. To Xcode, frameworks is just a folder with the extension <em>.framework</em>, which has inside one or more compiled binary sources, resources and some folders, a folder, usually called Headers, contains all the public headers.</li>
<li><strong><em>Generate Debug Symbols</em></strong>: <span style="color: #3366ff;"><strong>NO</strong></span> (this is a very important setting, otherwise the framework will not work on other computers/profiles).</li>
<li><strong><em>Precompile Prefix Header</em></strong>: <span style="color: #3366ff;"><strong>NO</strong></span>.</li>
<li><strong><em>Prefix Header</em></strong>: <span style="color: #3366ff;"><strong>&#8220;&#8221;</strong></span>. (Leave it blank).</li>
</ul>
<p><span style="color: #ff0000;"><strong>IMPORTANT:</strong> Since the Xcode 4.x the architectures armv6 has no longer support. So, to create a real Universal Framework we must make a small &#8220;hack&#8221;:</span></p>
<ol>
<li>After change the settings above close the Xcode, find the .xcodeproj (the project file) in Finder and then &#8220;Show Package Contents&#8221;.</li>
<li>Open the file &#8220;project.pbxproj&#8221; into a text editor.</li>
<li>Delete all the lines with VALID_ARCHS = “$(ARCHS_STANDARD_32_BIT)”.</li>
</ol>
<p><br/><a name="step_5"></a></p>
<h3>5. Adding code and resources to the Bundle (Framework)</h3>
<p><a href="#list_contents">top</a><br />
It&#8217;s time to place the content in our framework and define the public headers. To do that, with the Bundle target selected, click on the &#8220;<strong>Build Phase</strong>&#8221; tab. At bottom, hit the button &#8220;<strong>Add Phase</strong>&#8221; and then &#8220;<strong>Add Copy Headers</strong>&#8220;.</p>
<p>Open the recently created &#8220;<strong>Copy Headers</strong>&#8221; section and separate your public headers from the private or project headers. The difference here is:</p>
<ul>
<li>Public: Headers that other developers must know in order to work with your framework. In the final framework product, these headers will be visible even to Xcode.</li>
<li>Private: Headers that is not necessary to other developers, but is good for consult or for reference. These headers will not be visible to Xcode, but will be in the framework folder.</li>
<li>Project: Headers that the other developers nor Xcode have access. In reality these headers will not be placed in the final product, this is just to instruct the compiler to create your custom framework.</li>
</ul>
<p>Now, open the &#8220;<strong>Compile Source</strong>&#8221; section and put there all your <strong>.m</strong>, <strong>.c</strong>, <strong>.mm</strong>, <strong>.cpp</strong> or any other compilable source file.</p>
<p>If your framework include not compilable files, like images, sounds and other resources, you can place them in the &#8220;<strong>Copy Bundle Resources</strong>&#8221; section. Later, when we generate the final framework, all your resources will be placed in a folder called &#8220;Resources&#8221;, but you can change it. That folder is very important, because it will be part of the path to retrieve your resources from the framework product.</p>
<p><span style="color: #3366ff;"><strong>Tip:</strong> To add many files at once, click on the “+” button and write the files’ extension on the search field. For example “.m”, “.c”, “.cpp”, “.h”, etc. This can save a lot of time.</span></p>
<p>This is how your &#8220;<strong>Build Phase</strong>&#8221; will looks like:<br />
<div id="attachment_1406" class="wp-caption aligncenter" style="width: 610px"><a href="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_copy_headers.jpg"><img src="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_copy_headers.jpg" alt="Define your compilable source and the headers." title="xcode_copy_headers" width="600" class="size-full wp-image-1406" /></a><p class="wp-caption-text">Define your compilable source and the headers.</p></div></p>
<p><br/><a name="building_universal"></a></p>
<h2><strong>Building the Universal Framework</strong></h2>
<p><a href="#list_contents">top</a><br />
<a name="step_6"></a><br />
<h3>6. Creating Universal Target:</h3>
<p><a href="#list_contents">top</a><br />
To join both architectures products into one, we must to use the <strong>Lipo Tool</strong>. It&#8217;s a tool which comes with iOS SDK, just to know, it is in &#8220;<Xcode Folder>/Platforms/iPhoneOS.platform/Developer/usr/bin&#8221;, its file name is &#8220;lipo&#8221;. But we don&#8217;t need to know this path, Xcode can deal with it to us.</p>
<p>Add a new target, hit the &#8220;<strong>Add Target</strong>&#8221; button, just as you did with Bundle Target. At this time a good choice is the &#8220;Aggregate&#8221; target. It doesn&#8217;t create any product directly, its purposes is just to aggregate another targets and/or run some scripts, exactly what we want! To use the Lipo Tool we&#8217;ll need to create a &#8220;<strong>Run Script</strong>&#8221; at the &#8220;<strong>Build Phase</strong>&#8220;.</p>
<div id="attachment_1409" class="wp-caption aligncenter" style="width: 610px"><a href="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_build_all.jpg"><img src="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_build_all.jpg" alt="Use the &quot;Aggregate&quot; target to construct a run script." title="xcode_build_all" width="600" class="size-full wp-image-1409" /></a><p class="wp-caption-text">Use the &quot;Aggregate&quot; target to construct a run script.</p></div>
<p><br/><a name="step_7"></a></p>
<h3>7. Lipo Tool Script:</h3>
<p><a href="#list_contents">top</a><br />
This will be our greatest trick. The following script will make everything we need. It will compile the Framework Target to iOS Device and Simulator at once, will merge them with Lipo tool and will organize a good Framework Bundle structure.</p>
<p>Copy and paste this on your &#8220;Run Script&#8221; phase:</p>
<table width="675">
<tbody>
<tr>
<th>Xcode Script to Lipo Tool</th>
</tr>
</tbody>
</table>
<pre class="brush:cpp">
# Sets the target folders and the final framework product.
FMK_NAME=FI
FMK_VERSION=A

# Install dir will be the final output to the framework.
# The following line create it in the root folder of the current project.
INSTALL_DIR=${SRCROOT}/Products/${FMK_NAME}.framework

# Working dir will be deleted after the framework creation.
WRK_DIR=build
DEVICE_DIR=${WRK_DIR}/Release-iphoneos/${FMK_NAME}.framework
SIMULATOR_DIR=${WRK_DIR}/Release-iphonesimulator/${FMK_NAME}.framework

# Building both architectures.
xcodebuild -configuration "Release" -target "${FMK_NAME}" -sdk iphoneos
xcodebuild -configuration "Release" -target "${FMK_NAME}" -sdk iphonesimulator

# Cleaning the oldest.
if [ -d "${INSTALL_DIR}" ]
then
rm -rf "${INSTALL_DIR}"
fi

# Creates and renews the final product folder.
mkdir -p "${INSTALL_DIR}"
mkdir -p "${INSTALL_DIR}/Versions"
mkdir -p "${INSTALL_DIR}/Versions/${FMK_VERSION}"
mkdir -p "${INSTALL_DIR}/Versions/${FMK_VERSION}/Resources"
mkdir -p "${INSTALL_DIR}/Versions/${FMK_VERSION}/Headers"

# Creates the internal links.
# It MUST uses relative path, otherwise will not work when the folder is copied/moved.
ln -s "${FMK_VERSION}" "${INSTALL_DIR}/Versions/Current"
ln -s "Versions/Current/Headers" "${INSTALL_DIR}/Headers"
ln -s "Versions/Current/Resources" "${INSTALL_DIR}/Resources"
ln -s "Versions/Current/${FMK_NAME}" "${INSTALL_DIR}/${FMK_NAME}"

# Copies the headers and resources files to the final product folder.
cp -R "${DEVICE_DIR}/Headers/" "${INSTALL_DIR}/Versions/${FMK_VERSION}/Headers/"
cp -R "${DEVICE_DIR}/" "${INSTALL_DIR}/Versions/${FMK_VERSION}/Resources/"

# Removes the binary and header from the resources folder.
rm -r "${INSTALL_DIR}/Versions/${FMK_VERSION}/Resources/Headers" "${INSTALL_DIR}/Versions/${FMK_VERSION}/Resources/${FMK_NAME}"

# Uses the Lipo Tool to merge both binary files (i386 + armv6/armv7) into one Universal final product.
lipo -create "${DEVICE_DIR}/${FMK_NAME}" "${SIMULATOR_DIR}/${FMK_NAME}" -output "${INSTALL_DIR}/Versions/${FMK_VERSION}/${FMK_NAME}"

rm -r "${WRK_DIR}"
</pre>
<p>Now build the Aggregate target. Doesn&#8217;t matter you build for iOS Device or Simulator, this script will create a working folder, compile the framework target twice in there (device + simulator) and will output a folder called &#8220;<strong>Products</strong>&#8221; located in the project root folder. There is your <strong>Universal Framework to iOS</strong>!</p>
<p><strong>Congratulations!</strong></p>
<p><br/><a name="importing"></a></p>
<h2><strong>Importing your Universal Framework</strong></h2>
<p><a href="#list_contents">top</a><br />
<a name="step_8"></a><br />
<h3>8. Importing:</h3>
<p><a href="#list_contents">top</a><br />
To test your Universal Framework, create a new Xcode project, select the Application target and go to &#8220;<strong>Build Phase</strong>&#8221; tab. Open the section &#8220;<strong>Link Binary With Libraries</strong>&#8221; and hit the &#8220;<strong>+</strong>&#8221; to add a new Framework. Click the &#8220;<strong>Add Other&#8230;</strong>&#8221; button and select your Universal Framework. Remember, you must to select the &#8220;<strong>.framework</strong>&#8221; folder. Remember to import your Framework Principal Header as a framework notation. Xcode will use your public headers in the Code Completion.</p>
<p>Let&#8217;s understand what happens until here:<br />
When we set the &#8220;Mach-O Type&#8221; to &#8220;Relocatable Object File&#8221; the Xcode understand that everything related to that package will be like a &#8220;Binary Archive&#8221; like a ZIP. But that archive must be compiled again in new projects.<br />
<a href="http://db-in.com/blog/wp-content/uploads/2011/07/xcode_bundle_import.jpg"><img src="http://db-in.com/blog/wp-content/uploads/2011/07/xcode_bundle_import.jpg" alt="" title="xcode_bundle_import" width="298" height="262" class="alignleft size-full wp-image-1445" /></a><br />
Then, when we create a Framework Bundle structure, Xcode understand that everything inside it is organized in folders like &#8220;Headers&#8221; and <CompiledCode>. But, as any other external bundle, to retrieve the resources you must load the external bundle. To iOS this could be an annoying step, however our Framework structure can help. Just Click and Drag on your Framework icon from &#8220;<strong>Project Navigator</strong>&#8221; to the &#8220;<strong>Copy Bundle Resources</strong>&#8220;. By doing this all the resources in your Framework will be copied to your Application Main Bundle.</p>
<p>Now, to retrieve the resources, make use of the main bundle, just as you are used to:</p>
<table width="675">
<tbody>
<tr>
<th>Framework Bundle</th>
</tr>
</tbody>
</table>
<pre class="brush:cpp">
[[NSBundle mainBundle] pathForResource:@"FI.framework/Resources/FileName"
                                ofType:@"fileExtension"];
</pre>
<p><br/><a name="conclusion"></a></p>
<h2><strong>Conclusion</strong></h2>
<p><a href="#list_contents">top</a><br />
Well done, my friends! As we are used, let&#8217;s make a final review and take care with some possible problems.</p>
<ul>
<li>In a common Xcode project, create a Bundle target.</li>
<li>Make the necessary setup, place your sources, headers and resources in it.</li>
<li>Create an Aggreate target and place a <strong>Run Script</strong> in it.</li>
</ul>
<p>One last advice: Take care with your classes structure. If you set, for example, the ClassB.h as a Project or Private header, but in your code you import it into a Public header, this will cause conflicts.</p>
<p>And one last tip: Notice in the sample project I removed the scheme for the &#8220;Bundle Target&#8221;. We don&#8217;t need that scheme any more, because the new script will manage the compilation to us.</p>
<p>That&#8217;s all, buddies.<br />
Enjoy your Framework to iOS!</p>
<p>Thanks for reading,<br />
See you soon!</p>
]]></content:encoded>
			<wfw:commentRss>http://db-in.com/blog/2011/07/universal-framework-iphone-ios-2-0/feed/</wfw:commentRss>
		<slash:comments>124</slash:comments>
		</item>
		<item>
		<title>NinevehGL is HERE!</title>
		<link>http://db-in.com/blog/2011/06/ninevehgl-is-here/</link>
		<comments>http://db-in.com/blog/2011/06/ninevehgl-is-here/#comments</comments>
		<pubDate>Wed, 22 Jun 2011 17:59:36 +0000</pubDate>
		<dc:creator>Diney Bomfim</dc:creator>
				<category><![CDATA[3D]]></category>
		<category><![CDATA[Feature]]></category>
		<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[Engine]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[iPad]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[iPod]]></category>
		<category><![CDATA[NinevehGL]]></category>
		<category><![CDATA[OpenGL ES]]></category>

		<guid isPermaLink="false">http://db-in.com/blog/?p=1436</guid>
		<description><![CDATA[The NinevehGL, the brand new 3D engine to iOS is now on Open Beta phase. Become a Beta Tester and make the download right now!]]></description>
			<content:encoded><![CDATA[<p>Hello my friends,</p>
<p>After long months, just waiting for a single day, was a long wait, I know, but today the things will change a little bit. Today is THAT day.</p>
<p>After working through the nights, polishing every piece of code, thinking and rethinking routines&#8230; finally IT is here!</p>
<p>I&#8217;m very very happy to announce that NinevehGL is HERE!<br />
<span id="more-1436"></span><br />
<br/></p>
<h2><strong>Ladies and Gentlemen</strong></h2>
<p>With a great pleasure, let me introduce you the <a href="http://nineveh.gl">NinevehGL</a>!<br />
<br/><br />
<a href="http://db-in.com/blog/wp-content/uploads/2011/06/ngl_divulgation-e1308762967637.jpg"><img src="http://db-in.com/blog/wp-content/uploads/2011/06/ngl_divulgation-e1308762967637.jpg" alt="" title="ngl_divulgation" width="670" height="501" class="aligncenter size-full wp-image-1438" /></a></p>
<p><br/></p>
<h2><strong>Keep it Simple</strong></h2>
<p>You know me, I&#8217;m so bored with many web sites, softwares, 3D engines and technologies that make our life a hell with their docs (or the absence of docs), poor tutorials, complex setups, complex API or even worst&#8230; they are paid! God Dammit!!!</p>
<ul>
<li>So, let&#8217;s try something different, something simple! Starting with the web site: <a href="http://nineveh.gl">nineveh.gl</a>, just it, simple and easy.</li>
<li>What about tutorials or docs? Very simple: <a href="http://nineveh.gl/docs/tutorials/">http://nineveh.gl/docs/tutorials/</a>, video tutorials!</li>
<li>Long time to learn? Maybe… what do you think about just 30 min? 10 videos, 3 min each one. Sounds good?</li>
</ul>
<p><br/></p>
<h2><strong>Awesome features</strong></h2>
<p>There are many cool features in NinevehGL. But I think 3 are the &#8220;Killer Features&#8221;:</p>
<ul>
<li>OpenGL ES 2.0 (Programmable Pipeline): It uses the newest OpenGL ES version. More power, faster, lighter, better and shaders! With NinevehGL you can use all the power of the shaders and programmable pipeline.</li>
<li>Import directly from 3D softwares! NinevehGL doesn&#8217;t need plugins or special 3D formats to import your files! Make use of Wavefront OBJ files or COLLADA files (all 3D softwares export one of them). NinevehGL is ready to import them.</li>
<li>Made with pure Objective-C (Obj-C)! Yes, as an iOS developer, when I use OpenGL I expect to see Obj-C code, not C++ or C. NinevehGL is purely made with the native iOS language. Classes and routines follow all the Apple/Cocoa Touch guidelines.</li>
</ul>
<p>Well, another great thing about NinevehGL is that it&#8217;s FREE! A 3D engine for iOS totally FREE!</p>
<p>This is a short post just to tell you about this great news. I hope you like it.</p>
<p>See you, guys!</p>
]]></content:encoded>
			<wfw:commentRss>http://db-in.com/blog/2011/06/ninevehgl-is-here/feed/</wfw:commentRss>
		<slash:comments>56</slash:comments>
		</item>
		<item>
		<title>Creating Universal Framework to iPhone iOS</title>
		<link>http://db-in.com/blog/2011/05/creating-universal-framework-to-iphone-ios/</link>
		<comments>http://db-in.com/blog/2011/05/creating-universal-framework-to-iphone-ios/#comments</comments>
		<pubDate>Sun, 01 May 2011 19:50:50 +0000</pubDate>
		<dc:creator>Diney Bomfim</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Feature]]></category>
		<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[iPad]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">http://db-in.com/blog/?p=1394</guid>
		<description><![CDATA[Learn how to construct a Custom Framework to iOS Devices and distribute it.]]></description>
			<content:encoded><![CDATA[<p><a href="http://db-in.com/blog/wp-content/uploads/2011/04/framework_ios.png"><img src="http://db-in.com/blog/wp-content/uploads/2011/04/framework_ios.png" alt="" title="framework_ios" width="200" height="200" class="alignright size-full wp-image-1396" /></a>Hello my friends,</p>
<p>I felt a good impulse to write a little article about this theme, because there are few poor informations about it around the web. In many cases, are wrong or incomplete informations. So if you want to write a framework to iPhone and distribute it, you are in the right place!</p>
<p>I&#8217;ll treat here about how to construct an Universal Framework to iOS, what are the necessary configurations and everything else related to. We&#8217;ll focus on Xcode 4, but this is also valid to Xcode 3.x.</p>
<p>Let&#8217;s start!<br />
<span id="more-1394"></span></p>
<h2><span style="color:#FF0000">&#8211;WARNING&#8211;</span></h2>
<p><strong>This content is outdated! There is a new version of this article (2.0) teaching how to create an Universal Framework to iOS, much simpler and reliable:</strong><br />
<a href="http://db-in.com/blog/2011/07/universal-framework-iphone-ios-2-0/">http://db-in.com/blog/2011/07/universal-framework-iphone-ios-2-0/</a></p>
<p><a name="list_contents"></a><br />
Here is a little list of contents to orient your reading:</p>
<table width="675">
<tr>
<th colspan=2>List of Contents to this Tutorial</th>
</tr>
<tr>
<td valign="top">
<ul>
<li><a href="#framework_ios">Framework on iOS? Really?</a></li>
<li><a href="#understanding">Understanding Universal, Dynamic and Static concepts</a></li>
<li><a href="#framework_project">Constructing a Framework Project</a>
<ul>
<li><a href="#step_1">1. Create the Project</a></li>
<li><a href="#step_2">2. Framework Classes</a></li>
</ul>
</li>
<li><a href="#compiling_framework">Compiling a Framework</a>
<ul>
<li><a href="#step_3">3. Create a Framework Target</a></li>
<li><a href="#step_4">4. Bundle Setup</a></li>
<li><a href="#step_5">5. Adding code and resources to the Bundle (Framework)</a></li>
<li><a href="#step_6">6. Schemes Setup</a></li>
<li><a href="#step_7">7. Building the Framework</a></li>
</ul>
</li>
<li><a href="#building_universal">Building the Universal Framework</a>
<ul>
<li><a href="#step_8">8. Creating Universal Target</a></li>
<li><a href="#step_9">9. Lipo Tool Script</a></li>
</ul>
</li>
<li><a href="#testing">Testing your Universal Framework</a>
<ul>
<li><a href="#step_10">10. Testing</a></li>
</ul>
</li>
<li><a href="#conclusion">Conclusion</a></li>
</ul>
</td>
</tr>
</table>
<p>You can download the project I used in this article:</p>
<p><a href="http://db-in.com/downloads/framework-ios.zip" onmousedown="_gaq.push(['_trackEvent', 'Framework iOS', 'Xcode', 'Download']);"><img class="alignleft" title="download" src="http://db-in.com/imgs/download_button.png" alt="Download Xcode project files to iPhone"/><br />
<strong>Download now</strong><br />
Xcode project files to Framework-iOS<br />
403kb<br />
</a><br/></p>
<p><br/><a name="framework_ios"></a></p>
<h2><strong>Framework on iOS? Really?</strong></h2>
<p><a href="#list_contents">top</a><br />
<a href="http://db-in.com/blog/wp-content/uploads/2011/04/framework_icon.png"><img src="http://db-in.com/blog/wp-content/uploads/2011/04/framework_icon.png" alt="" title="framework_icon" width="128" height="128" class="alignleft size-full wp-image-1397" /></a>Ok buddies, let&#8217;s make something clear, many people had said:&#8221;iOS doesn&#8217;t support custom Frameworks!&#8221;, &#8220;Custom Framework is not allowed at iOS!&#8221;, &#8220;Doesn&#8217;t exist custom Framework on iOS!&#8221; and many other discouraging things like these. Look, I&#8217;ve made many frameworks and worked with many others, I don&#8217;t believe that is really impossible to use a Framework on iOS. According to my experience and knowledge about Frameworks, it&#8217;s absolutely feasible a custom Framework on iOS Devices. If we think more about the issue we can find an elegant solution, right? First, let&#8217;s understand what a Framework really is, here is the definition of framework by Apple&#8217;s eyes:</p>
<blockquote><p>A framework is a hierarchical directory that encapsulates shared resources, such as a dynamic shared library, nib files, image files, localized strings, header files, and reference documentation in a single package.</p></blockquote>
<p>Doesn&#8217;t make many sense something with this description not be allowed in iOS, thinking in architecture and structure. Apple also says:</p>
<blockquote><p>A framework is also a bundle and its contents can be accessed using Core Foundation Bundle Services or the Cocoa NSBundle class. However, unlike most bundles, a framework bundle does not appear in the Finder as an opaque file. A framework bundle is a standard directory that the user can navigate.</p></blockquote>
<p>Good, now thinking about iOS security, performance and size, the only thing in a Framework definition which doesn&#8217;t fit in iOS technology is the &#8220;dynamic shared library&#8221;. The words &#8220;dynamic&#8221; and &#8220;shared&#8221; are not welcome in the iOS architecture. So the Apple allows us to work and distribute something called &#8220;<strong>Static Library</strong>&#8220;. I don&#8217;t like it! It&#8217;s not so easy as a Cocoa Framework, if a developer got your Static Library, he needs to set a header path too, or import the header files&#8230; it&#8217;s not useful, that&#8217;s a shit!</p>
<p><a href="http://db-in.com/blog/wp-content/uploads/2011/05/static_library_no.png"><img src="http://db-in.com/blog/wp-content/uploads/2011/05/static_library_no.png" alt="" title="static_library_no" width="128" height="128" class="alignright size-full wp-image-1403" /></a></p>
<p>Well, so a Framework concept is absolutely compatible with iOS, except by the &#8220;dynamic shared library&#8221;, on the other hand Apple says that a &#8220;static library&#8221; is OK for iOS. So if we replace the &#8220;dynamic shared libraries&#8221; by a &#8220;static library&#8221; we could construct a Custom Framework to iOS, right?</p>
<p>Right!!!!</p>
<p>This is exactly what we&#8217;ll make in this article, let&#8217;s construct a Framework with Static Library, by the way, an Universal Framework.</p>
<p><br/><a name="understanding"></a></p>
<h2><strong>Understanding Universal, Dynamic and Static concepts</strong></h2>
<p><a href="#list_contents">top</a><br />
Simple answer:</p>
<ul>
<li>Universal: Which works perfect on all architectures. iOS devices uses <strong>armv6</strong> and <strong>armv7</strong>, iOS simulator on MacOS X uses <strong>i386</strong>.</li>
<li>Dynamic: The compiler doesn&#8217;t include the target files directly. The classes/libraries are already pre-compiled (binary format) and lies on the system path. Besides, the dynamic libraries can be shared by many applications. This is exactly what Cocoa Framework is.</li>
<li>Static: It represents that classes/libraries which is compiled by the compiler at the build phase. These files can&#8217;t be shared by other applications and relies on application path.</li>
</ul>
<p>Simple as that. If you need more informations about Dynamic VS Static libraries, try this <a href="http://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/OverviewOfDynamicLibraries.html" target="_blank">Apple&#8217;s Documentation</a>.</p>
<p>No more concepts, hands at work!</p>
<p><br/><a name="framework_project"></a></p>
<h2><strong>Constructing a Framework Project</strong></h2>
<p><a href="#list_contents">top</a><br />
<a name="step_1"></a><br />
<h3>1. Create the Project:</h3>
<p><a href="#list_contents">top</a><br />
I want to show you step by step of the entire process, so let&#8217;s start with the most basic, create an iOS project. You can choose one application template in Xcode, this is not really so important, but remember to choose one template which could test your Framework code before export it.<br />
<div id="attachment_1399" class="wp-caption aligncenter" style="width: 610px"><a href="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_new_project.jpg"><img src="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_new_project.jpg" alt="Create an application project." title="xcode_new_project" width="600" class="size-full wp-image-1399" /></a><p class="wp-caption-text">Create an application project.</p></div></p>
<p><br/><a name="step_2"></a></p>
<h3>2. Framework Classes:</h3>
<p><a href="#list_contents">top</a><br />
<div id="attachment_1400" class="wp-caption alignleft" style="width: 267px"><a href="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_project_navigator.jpg"><img src="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_project_navigator.jpg" alt="Create your framework classes." title="xcode_project_navigator" width="257" height="286" class="size-full wp-image-1400" /></a><p class="wp-caption-text">Create your framework classes.</p></div>Now it&#8217;s time to create (or import) your framework classes, just as you are used to in any other application. Remember that organization is 80% of a good framework, so try follow all the Apple advices to create your classes names, methods, properties, functions, etc.</p>
<p>Remember to create an import header to make everything more simple and organized to the user of your framework. Remember to write this header file with a framework notation, just as shown in the image bellow. Also remember to create your classes taking care to hide the classes which should not be visible to the other developers (users of your framework). We will set the public and private headers soon, but it&#8217;s very important to you protect the &#8220;core&#8221; classes, I mean, that classes which you don&#8217;t want to make visible to other developers.</p>
<p>For those private classes, you could import their header inside the &#8220;<strong>.m</strong>&#8221; (or .mm, .cpp, etc) file of a public class, by doing this you protect the header of private classes. Well, I know you probably already know that, I&#8217;m saying just to reinforce.</p>
<p>After you have all the classes (and also other files, like images, sounds, etc.), you are ready to compile a custom framework. In the next step, we&#8217;ll create a target to compile our framework, this will be our first big trick!</p>
<div id="attachment_1401" class="wp-caption aligncenter" style="width: 457px"><a href="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_framework_header.jpg"><img src="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_framework_header.jpg" alt="Create your framework header with framework notation." title="xcode_framework_header" width="447" height="157" class="size-full wp-image-1401" /></a><p class="wp-caption-text">Create your framework header with framework notation.</p></div>
<p><br/><a name="compiling_framework"></a></p>
<h2><strong>Compiling a Framework</strong></h2>
<p><a href="#list_contents">top</a><br />
<a name="step_3"></a><br />
<h3>3. Create a Framework Target:</h3>
<p><a href="#list_contents">top</a><br />
OK, let&#8217;s create a target to compile our framework. Click on the icon of your project in the project navigator at the left and hit the button &#8220;Add Target&#8221;. A new window will come up. Now is our first trick. Instead to create a &#8220;Cocoa Touch Static Library&#8221; or a &#8220;Cocoa Framework&#8221; we will create a &#8220;Bundle&#8221; target.</p>
<p>A Bundle? Really? Yes! I can explain. A &#8220;Cocoa Framework&#8221; target can&#8217;t be compiled to armv6/armv7 and Xcode doesn&#8217;t allow us to use &#8220;Static Libraries&#8221; in a &#8220;Cocoa Framework&#8221;, so we can&#8217;t use this target. On the other hand, we can&#8217;t use &#8220;Cocoa Touch Static Library&#8221; either, because it doesn&#8217;t use the framework structure that we want.</p>
<p>Now, the <strong>Bundle</strong> target could be the best choice. It can hold any file we want, we can compile source code inside it and&#8230; we can turn it into a framework. To say the truth, almost all &#8220;Framework &#038; Library&#8221; targets could be turned into a framework too, even the &#8220;Cocoa Touch Static Library&#8221;, throughout this article you probably will figure out why. For now, let&#8217;s create a <strong>Bundle</strong> target.</p>
<div id="attachment_1402" class="wp-caption aligncenter" style="width: 610px"><a href="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_new_target.jpg"><img src="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_new_target.jpg" alt="Create a Bundle target rather than Cocoa Touch Static Library." title="xcode_new_target" width="600" class="size-full wp-image-1402" /></a><p class="wp-caption-text">Create a Bundle target rather than Cocoa Touch Static Library.</p></div>
<p><br/><a name="step_4"></a></p>
<h3>4. Bundle Setup:</h3>
<p><a href="#list_contents">top</a><br />
<a href="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_remove_framework.jpg"><img src="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_remove_framework.jpg" alt="" title="xcode_remove_framework" width="314" height="113" class="alignleft size-full wp-image-1404" /></a>It&#8217;s time to make all the necessary changes to the Bundle target. First of all, make sure you have cleaned up all the default files from the Bundle target. Remove the linked frameworks (you can find it clicking in the &#8220;<strong>Build Phase</strong>&#8221; tab and then they are in the section &#8220;<strong>Link Binary With Libraries</strong>&#8220;) and delete the Preference List file (.plist), the pre-compiled headers (.pch) and the language files.</p>
<p>I&#8217;m sure you already know this, but just to reinforce, here is the Build Setting screen, you can find it by clicking on the project icon in the left project navigator and then clicking in the &#8220;Build Setting&#8221; tab.<br />
<div id="attachment_1405" class="wp-caption aligncenter" style="width: 610px"><a href="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_build_settings-e1304231010699.jpg"><img src="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_build_settings-e1304231010699.jpg" alt="You must make a special Build Setting to turn a Bundle into a framework." title="xcode_build_settings" width="600" class="size-full wp-image-1405" /></a><p class="wp-caption-text">You must make a special Build Setting to turn a Bundle into a framework.</p></div></p>
<p>Here is our second great trick, or should be better to say &#8220;tricks&#8221;. Let&#8217;s change the &#8220;<strong>Build Setting</strong>&#8221; following this list:</p>
<ul>
<li><strong><em>Architectures</em></strong>: <span style="color: #3366ff;"><strong>Standard (armv6 armv7)</strong></span> (the values for this property depend on the value of the item bellow).</li>
<li><strong><em>Base SDK</em></strong>: <span style="color: #3366ff;"><strong>Latest iOS (iOS X.X)</strong></span> (in the X.X will appear the number of the lastest iOS SDK installed on your machine).</li>
<li><strong><em>Build Active Architecture Only</em></strong>: <span style="color: #3366ff;"><strong>NO</strong></span> (this is a very important set, if it&#8217;s YES then we can&#8217;t compile to armv6 and armv7 at the same time).</li>
<li><strong><em>Supported Platforms</em></strong>: <span style="color: #3366ff;"><strong>iphonesimulator iphoneos</strong></span>.
<li><strong><em>Valid Architecture</em></strong>: <span style="color: #3366ff;"><strong>$(ARCHS_STANDARD_32_BIT)</strong></span> (it&#8217;s very important to be exactly this value, seems there is a bug in Xcode 4, once you set i386 as supported platforms to iOS, it can&#8217;t be removed anymore and it could generate errors in your project. So, to avoid any architectures error, use this value).</li>
<li><strong><em>Installation Directory</em></strong>: [optional change] I like to set this to <span style="color: #3366ff;"><strong>$(BUILT_PRODUCTS_DIR)</strong></span>, but this is not really relevant to our purposes here. You can set it to a path of your choice.</li>
<li><strong><em>Mac OS X Deployment Target</em></strong>: <span style="color: #3366ff;"><strong>Compiler Default</strong></span>.</li>
<li><strong><em>Dead Code Stripping</em></strong>: <span style="color: #3366ff;"><strong>NO</strong></span>.</li>
<li><strong><em>Link With Standard Libraries</em></strong>: <span style="color: #3366ff;"><strong>NO</strong></span>.</li>
<li><strong><em>Mach-O Type</em></strong>: <span style="color: #3366ff;"><strong>Relocatable Object File</strong></span>. This is the most important change. Here, we instruct the compiler to treat the Bundle as a relocatable file, by doing this, we can turn it into a framework with the wrapper setting.</li>
<li><strong><em>Other Linker Flags</em></strong>: [optional change] <span style="color: #3366ff;"><strong>-ObjC</strong></span>, could be good make sure the compiler understands what&#8217;s the language it is compiling for, this can reduce the warnings at the compilation.</li>
<li><strong><em>Info.plist File</em></strong>: <span style="color: #3366ff;"><em>empty</em></span>, remove any value from this field.</li>
<li><strong><em>Wrapper Extension</em></strong>: <span style="color: #3366ff;"><strong>framework</strong></span>. Here we change the Bundle to a Framework. To Xcode, frameworks is just a folder with the extension <em>.framework</em>, which has inside one or more compiled binary sources, resources and some folders, a folder, usually called Headers, contains all the public headers.</li>
<li><strong><em>Precompile Prefix Header</em></strong>: <span style="color: #3366ff;"><strong>NO</strong></span>.</li>
<li><strong><em>Prefix Header</em></strong>: <span style="color: #3366ff;"><em>empty</em></span>, remove any value from this field.</li>
<li><strong><em>Generate Debug Symbols</em></strong>: <span style="color: #3366ff;"><strong>NO</strong></span> (this is a very important setting, otherwise your framework will not work on another computer/profile).</li>
</ul>
<p><br/><a name="step_5"></a></p>
<h3>5. Adding code and resources to the Bundle (Framework)</h3>
<p><a href="#list_contents">top</a><br />
It&#8217;s time to place the content in our framework and define the public headers. To do that, with the Bundle target selected, click on the &#8220;<strong>Build Phase</strong>&#8221; tab. At bottom, hit the button &#8220;<strong>Add Phase</strong>&#8221; and then &#8220;<strong>Add Copy Headers</strong>&#8220;.</p>
<p>Open the recently created &#8220;<strong>Copy Headers</strong>&#8221; section and separate your public headers from the private or project headers. The difference here is:</p>
<ul>
<li>Public: Headers that other developers must know in order to work with your framework. In the final framework product, these headers will be visible even to Xcode.</li>
<li>Private: Headers that is not necessary to other developers, but is good for consult or for reference. These headers will not be visible to Xcode, but will be in the framework folder.</li>
<li>Project: Headers that the other developers nor Xcode have access. In reality these headers will not be placed in the final product, this is just to instruct the compiler to create your custom framework.</li>
</ul>
<p>Now, open the &#8220;<strong>Compile Source</strong>&#8221; section and put there all your <strong>.m</strong>, <strong>.c</strong>, <strong>.mm</strong>, <strong>.cpp</strong> or any other compilable source file. If your framework include not compilable files, like images, sounds and other resources, you can place them in the &#8220;<strong>Copy Bundle Resources</strong>&#8221; section, in the example of this article, I used no resources. This is how your &#8220;<strong>Build Phase</strong>&#8221; will looks like:<br />
<div id="attachment_1406" class="wp-caption aligncenter" style="width: 610px"><a href="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_copy_headers.jpg"><img src="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_copy_headers.jpg" alt="Define your compilable source and the headers." title="xcode_copy_headers" width="600" class="size-full wp-image-1406" /></a><p class="wp-caption-text">Define your compilable source and the headers.</p></div></p>
<p><br/><a name="step_6"></a></p>
<h3>6. Schemes Setup</h3>
<p><a href="#list_contents">top</a><br />
This is an important change to compile our framework. You don&#8217;t need to compile it in the Debug modes, we just need the Release mode, because if you have any Macro in your code which works only in Debug mode, like DEBUG macro, it will assume the configuration of the product that the other developers will make. So to us now, only the Release is important.</p>
<p>You can change it accessing the menu <strong>Product > Manage Schemes&#8230;</strong> or by the schemes short cut: the drop list in front at the Build/Run bottom in the top left corner of the Xcode window.</p>
<p>At this point could be a good idea to delete all the current schemes and then press the button &#8220;<strong>AutoCreate Schemes Now</strong>&#8220;. The important is select your framework target and hit the button &#8220;<strong>Edit</strong>&#8221; at the bottom. In the next window, change the &#8220;<strong>Build Configuration</strong>&#8221; from Debug to Release. Change to Release in all the situations: Run, Test, Profile, Analyze and Archive. By doing this, we ensure that our framework target will always compile the Release version.</p>
<div id="attachment_1407" class="wp-caption aligncenter" style="width: 610px"><a href="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_manage_schemes.jpg"><img src="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_manage_schemes.jpg" alt="Build the custom framework only to Release." title="xcode_manage_schemes" width="600"  class="size-full wp-image-1407" /></a><p class="wp-caption-text">Build the custom framework only to Release.</p></div>
<p><br/><a name="step_7"></a></p>
<h3>7. Building the Framework</h3>
<p><a href="#list_contents">top</a><br />
OK, here is the annoying step of this process. Until now, I don&#8217;t figure out a more easy an elegant way to do this. You must to compile this target twice: one to <strong>iOS Device</strong> (this will compile for the architectures armv6/arvm7) and to <strong>iOS Simulator</strong> (can be iPhone or iPad with any SDK, doesn&#8217;t matter, the simulator always will compile for the architecture i386).</p>
<p>I&#8217;ve tried another way, like create an &#8220;integrator&#8221; and I placed two targets to its &#8220;Target Dependencies&#8221;, but trust me, it&#8217;s worst, because you need to change 2 targets instead 1 in cases when you change the framework content. Besides, the dependencies will assume the architectures of the &#8220;integrator&#8221;, so you need to compile twice too. Anyway&#8230;  the best solution I found until now is use only 1 target and build it twice. If you find a better one, please tell me.</p>
<div id="attachment_1408" class="wp-caption aligncenter" style="width: 380px"><a href="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_compile_ios_simulator.jpg"><img src="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_compile_ios_simulator.jpg" alt="Compile the framework target twice: iOS Device and Simulator." title="xcode_compile_ios_simulator" width="370" height="194" class="size-full wp-image-1408" /></a><p class="wp-caption-text">Compile the framework target twice: iOS Device and Simulator.</p></div>
<p>After the compile complete, you can see in the &#8220;<strong>Products</strong>&#8221; folder in the Project Navigator at the left, that your framework product now is active. Right click on it and hit &#8220;<strong>Show in Finder</strong>&#8220;. Take a look at this product, its is your iOS Framework! Great!</p>
<p>But we&#8217;re not done yet. You can test this product in other applications if you want, but it will only work to one architecture. We must to create an Universal version of this product. If you look around your framework product in the Finder, you&#8217;ll see that it is in a folder called &#8220;Release-iphoneos&#8221; (if you didn&#8217;t change the destination folder in the Build Settings). And if you have compiled to Simulator too, you&#8217;ll see another folder called &#8220;Release-iphonesimulator&#8221; which contains the same framework product, but this version is for architecture i386.</p>
<p>So, let&#8217;s join both products into one.</p>
<p><br/><a name="building_universal"></a></p>
<h2><strong>Building the Universal Framework</strong></h2>
<p><a href="#list_contents">top</a><br />
<a name="step_8"></a><br />
<h3>8. Creating Universal Target:</h3>
<p><a href="#list_contents">top</a><br />
To join both architectures products into one, we must to use the <strong>Lipo Tool</strong>. It&#8217;s a tool which comes with iOS SDK, just to know, it is in &#8220;<Xcode Folder>/Platforms/iPhoneOS.platform/Developer/usr/bin&#8221;, its file name is &#8220;lipo&#8221;. But we don&#8217;t need to know this path, Xcode can deal with it to us.</p>
<p>To use the Lipo Tool we&#8217;ll need to create a &#8220;<strong>Run Script</strong>&#8221; at the &#8220;<strong>Build Phase</strong>&#8220;, you can create it in your previously Bundle Target, but my advice is to create another target. I say to create another target to avoid compiling errors. This script will need to use &#8220;Release-iphoneos&#8221; and &#8220;Release-iphonesimulator&#8221; folders, so if the folders or products inside them not exist yet, the compiler will generate errors.</p>
<p>Let&#8217;s add a new target, hit the &#8220;<strong>Add Target</strong>&#8221; button, just as you did with Bundle Target. At this time a good choice is the &#8220;Aggregate&#8221; target. It doesn&#8217;t create any product directly, its purposes is just to aggregate another targets and run some scripts, exactly what we want!</p>
<div id="attachment_1409" class="wp-caption aligncenter" style="width: 610px"><a href="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_build_all.jpg"><img src="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_build_all.jpg" alt="Use the &quot;Aggregate&quot; target to construct a run script." title="xcode_build_all" width="600" class="size-full wp-image-1409" /></a><p class="wp-caption-text">Use the &quot;Aggregate&quot; target to construct a run script.</p></div>
<p><br/><a name="step_9"></a></p>
<h3>9. Lipo Tool Script:</h3>
<p><a href="#list_contents">top</a><br />
We are almost there, now it&#8217;s time to create the Run Script to use the Lipo Tool. Well, you probably will need to make some changes in the code bellow to fit it to your framework name or folders (if you set different folders to build for). Here is the script to use:</p>
<table width="675">
<tbody>
<tr>
<th>Xcode Script to Lipo Tool</th>
</tr>
</tbody>
</table>
<pre class="brush:cpp">.
if [ "${ACTION}" = "build" ]
then
# Set the target folders and the final framework product.
    INSTALL_DIR=${SYMROOT}/FI.framework
    DEVICE_DIR=${SYMROOT}/Release-iphoneos
    SIMULATOR_DIR=${SYMROOT}/Release-iphonesimulator

# Create and renews the final product folder.
    mkdir -p "${INSTALL_DIR}"
    rm -rf "${INSTALL_DIR}"
    rm -rf "${INSTALL_DIR}/FI"

# Copy the header files to the final product folder.
    ditto "${DEVICE_DIR}/FI.framework/Headers" "${INSTALL_DIR}/Headers"

# Use the Lipo Tool to merge both binary files (i386 + armv6/armv7) into one Universal final product.
    lipo -create "${DEVICE_DIR}/FI.framework/FI" "${SIMULATOR_DIR}/FI.framework/FI" -output "${INSTALL_DIR}/FI"
fi
.</pre>
<div id="attachment_1411" class="wp-caption aligncenter" style="width: 610px"><a href="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_run_script.jpg"><img src="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_run_script.jpg" alt="Lipo script to join products for architectures i386 and armv6/armv7." title="xcode_run_script" width="600" class="size-full wp-image-1411" /></a><p class="wp-caption-text">Lipo script to join products for architectures i386 and armv6/armv7.</p></div>
<p>Now build the Aggregate target. At this time doesn&#8217;t matter if you build for iOS Device or Simulator, because all that it will make is run the script, nothing more. The above script creates the final Universal Framework at the ${SYMROOT} path, this is the same path of your Bundle target. So you can right click on the Framework product in the &#8220;Products&#8221; again and hit &#8220;Show in Finder&#8221;. Move to the level with &#8220;Release-iphoneos&#8221; and &#8220;Release-iphonesimulator&#8221; folders. Now, there is a new folder with the &#8220;.framework&#8221; extension, it&#8217;s your <strong>Universal Framework to iOS</strong>!</p>
<p><strong>Congratulations!</strong></p>
<p><br/><a name="testing"></a></p>
<h2><strong>Testing your Universal Framework</strong></h2>
<p><a href="#list_contents">top</a><br />
<a name="step_10"></a><br />
<h3>10. Testing:</h3>
<p><a href="#list_contents">top</a><br />
To test your Universal Framework could be a good idea take it from that folder and place it in another one more easy to find, like the Desktop or ~/Library/Frameworks. Well, to avoid take it from that folder every time, you can change the Run Script above. Just change the &#8220;<strong>INSTALL_DIR</strong>&#8221; variable and set a path of your choice, make sure you choose a path with the write permissions. A good choice could be use the Xcode variable <strong>${SRCROOT}</strong> which represents your project folder root, so you can set <strong>${SRCROOT}/Products</strong> or something like that.</p>
<p>Anyway, with your Universal Framework in an &#8220;user friendly path&#8221;, create a new Xcode project, select the Application target and go to &#8220;<strong>Build Phase</strong>&#8221; tab. Open the section &#8220;<strong>Link Binary With Libraries</strong>&#8221; and hit the &#8220;<strong>+</strong>&#8221; to add a new Framework. Click the &#8220;<strong>Add Other&#8230;</strong>&#8221; button and select your Universal Framework. Remember, you must to select the &#8220;<strong>.framework</strong>&#8221; folder.</p>
<p>After the importing, Xcode will know everything about your custom framework. So try to write some code using your framework classes. Remember to import your Framework Principal Header as a framework notation. Xcode will use your public headers in the Code Completion.</p>
<div id="attachment_1414" class="wp-caption aligncenter" style="width: 610px"><a href="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_framework_active.jpg"><img src="http://db-in.com/blog/wp-content/uploads/2011/05/xcode_framework_active.jpg" alt="Xcode knows your public headers and can use them in the Code Completion." title="xcode_framework_active" width="600" class="size-full wp-image-1414" /></a><p class="wp-caption-text">Xcode knows your public headers and can use them in the Code Completion.</p></div>
<p>I like to use the Xcode Midnight Color Theme, so the classes of the project are green and the framework classes are blue. I love blue classes! <img src='http://db-in.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><br/><a name="conclusion"></a></p>
<h2><strong>Conclusion</strong></h2>
<p><a href="#list_contents">top</a><br />
Well done, my friends! As we are used, let&#8217;s make a final review and take care with some possible problems.</p>
<ul>
<li>In a common Xcode project, create a Bundle target.</li>
<li>Make the necessary setup, place your sources, headers and resources in it and finally change the its Schemes configuration.</li>
<li>Compile to iOS Device (armv6/armv7) and to Simulator (i386).</li>
<li>Create an Aggreate target and place a <strong>Run Script</strong> in it.</li>
<li>Enjoy your real <strong>Universal Framework to iOS</strong>!</li>
</ul>
<p>To finish I want to talk about some issues that I had:</p>
<ul>
<li>Take special care with the architectures. Don&#8217;t write the i386 directly in the <strong><em>Valid Architectures</em></strong> setting, always use the <strong>$(ARCHS_STANDARD_32_BIT)</strong>. Plus, always set the <strong><em>Build Active Architecture Only</em></strong> to <strong>NO</strong>.</li>
<li>Take care with your classes structure. If you set, for example, the ClassB.h as a Project or Private header, but in your code you import it into a Public header, this will cause conflicts.</li>
<li>Take care with the <strong><em>Install Directory</em></strong> and <strong><em>Skip Install</em></strong> settings. They can generate warning if the folders permissions or paths are not right set.</li>
<li>Finally, take a special care with the Lipo Tool script. It could generate warnings and/or errors if the paths was not found or the folder permissions was not right set.</li>
</ul>
<p>This is all, buddy.<br />
As I told you, the annoying thing is that you need build 3 times to generate a new Universal Framework: 2 x Bundle target (one to iOS Device and other to the Simulator) and 1x to the Aggregate target (which will run a script). But by my experience, this is not to much, you will create the final product only few times. At the great majority, you will test your Framework classes directly with the Application target in your Framework Xcode Project.</p>
<p>Enjoy your real Framework to iOS!</p>
<p>Thanks for reading,<br />
See you soon!</p>
]]></content:encoded>
			<wfw:commentRss>http://db-in.com/blog/2011/05/creating-universal-framework-to-iphone-ios/feed/</wfw:commentRss>
		<slash:comments>46</slash:comments>
		</item>
		<item>
		<title>NinevehGL Features</title>
		<link>http://db-in.com/blog/2011/04/ninevehgl-features/</link>
		<comments>http://db-in.com/blog/2011/04/ninevehgl-features/#comments</comments>
		<pubDate>Tue, 26 Apr 2011 15:12:36 +0000</pubDate>
		<dc:creator>Diney Bomfim</dc:creator>
				<category><![CDATA[3D]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Feature]]></category>
		<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[Engine]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[iPad]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[NinevehGL]]></category>
		<category><![CDATA[OpenGL ES]]></category>

		<guid isPermaLink="false">http://db-in.com/blog/?p=1381</guid>
		<description><![CDATA[The time for the NinevehGL is coming. Check now its features and what you could expect from.]]></description>
			<content:encoded><![CDATA[<p><a href="http://db-in.com/blog/wp-content/uploads/2011/04/ngl_feather.png"><img src="http://db-in.com/blog/wp-content/uploads/2011/04/ngl_feather.png" alt="" title="ngl_feather" width="130" height="200" class="alignleft size-full wp-image-1390" /></a>Hello everyone!</p>
<p>Today I want to talk about the NinevehGL. Talk more about the features and about what it can offer to us. NinevehGL is almost done, I want to be as fast as possible, but I don&#8217;t want to launch it without everything seems great: the documentation, the tutorials, the official website and obviously, the NinevehGL itself.</p>
<p>Soon I&#8217;ll start posting videos and many divulgations about it, but right now in this article let&#8217;s see some images taken directly from the NinevehGL running.</p>
<p>Let&#8217;s start!</p>
<p><span id="more-1381"></span><br />
<br/></p>
<h2><strong>At a glance</strong></h2>
<p>The NinevehGL is a 3D engine fully made with the most pure Objective-C, right on top of Cocoa Touch Framework. So at the first moment, the NinevehGL is a 3D engine only for iOS. I intend to port NinevehGL to MacOSX (desktops with Obj-C) and also for ActionScript 3.0, using the new great API code named Molehill. ActionScript is my old programming language, today I don&#8217;t hold much lover for it, but I know that there are many people that still loving Flash. Another important step is to port NinevehGL to JavaScript to work with WebGL. But at the beginning, NinevehGL will work only with OpenGL ES 2.x for iOS.</p>
<p>Now the most important question: &#8220;Why NinevehGL instead of PowerVR, Oolong, SIO2, Torque, Ogre, UDK, Unity, Wolfenstein, Shiva, Galaxy, or any other? Why should I choose NinevehGL instead any other 3D engine?&#8221;.</p>
<p>I always tried to convince everyone to buy what I was selling, trying to convince everybody from my point of view. But not this time, I don&#8217;t want to sell anything, I don&#8217;t want to convince you. If you are using some other engine and glad with that, just continue with it, you don&#8217;t have to change. Also, if you are a great OpenGL programmer and have your very own model and structure, continue with it, don&#8217;t change anything. Because, again, I don&#8217;t want to convince anyone at this time. Just as OpenGL, I&#8217;ll focus on the most important thing to me: development!</p>
<p>I took everything that I&#8217;ve seen of worst in other engines and I&#8217;ve tried to make it better in NinevehGL. I took all my frustations with the actual engines, all my luggage as developer, all my knowledge about 3D world and put all of them in the NinevehGL. So, if you are like me and you have some insatisfaction with the other engines, for you, NinevehGL could really nice for you! I know I&#8217;m telling this every time, but I love this concept:&#8221;Keep it simple!&#8221; This is the NinevehGL&#8217;s main principle, everything was made to be simple. There are no commands that need more than one single line of code. If you want to make one thing, you need one line, nothing else.</p>
<p>Now I&#8217;ll describe in details the most important features of NinevehGL. Remember, I don&#8217;t want to convince you of anything. I&#8217;m sure you are an advanced programmer and can form your own opinion about what&#8217;s coming next. If you like it, great! If don&#8217;t, it&#8217;s great as well! Just tell me why and I&#8217;ll try to make better.</p>
<p>Here is a features&#8217; list.</p>
<table width="675">
<tr>
<th>NinevehGL Features</th>
</tr>
<tr>
<td>Imports OBJ file.</td>
</tr>
<tr>
<td>Imports DAE (COLLADA) file.</td>
</tr>
<tr>
<td>Works with NGL binary file (exclusive of NinevehGL).</td>
</tr>
<tr>
<td>Cache 3D files to optimize the loading in next times (around 95% less loading time).</td>
</tr>
<tr>
<td>Works with OpenGL ES 2.x (programmable pipeline).</td>
</tr>
<tr>
<td>Full integrations with custom Shaders.</td>
</tr>
<tr>
<td>Supports 3D and 2D application.</td>
</tr>
<tr>
<td>Programming interface is 100% User Friendly and totally independent of OpenGL version.</td>
</tr>
<tr>
<td>Absolutely oriented to performance and minimum size (NinevehGL is 10% &#8211; 60% faster than any other engine).</td>
</tr>
<tr>
<td>Full support for PVRTC (even to without header generated by MacOS).</td>
</tr>
<tr>
<td>Automatically calculates normals and tangent space to work with lights and bump maps.</td>
</tr>
</table>
<p><br/><a name="importing"></a></p>
<h2><strong>Importing 3D files</strong></h2>
<p>This is the most problematic topic to me considering other engines. As OpenGL doesn&#8217;t import 3D files directly, you must use an engine that imports a file good for you or use one of the files accepted by that engine, like POD to PowerVR engine.</p>
<p>This is the first problem that NinevehGL works to solve, it can import the two most popular 3D file formats: COLLADA and WaveFront OBJ. You know what a mess the 3D imports with 3D softwares are. Each software has its own file format and implements its importing routines at its own way. So it&#8217;s not hard to export an OBJ from 3DS Max and when you try to import in Maya, BOOM, error! To tell the truth, the incompatibility with the files when exporting from a 3D software to another is common.</p>
<p>The NinevehGL has a full importing routine. What that means? If your OBJ file has informations about the specular, the bump map, the reflection map or anything supported by OBJ file, NinevehGL will import and parse everything for you with no losses. The same is true for COLLADA files.<br />
<div id="attachment_1383" class="wp-caption aligncenter" style="width: 610px"><a href="http://db-in.com/blog/wp-content/uploads/2011/04/importing_example.jpg"><img src="http://db-in.com/blog/wp-content/uploads/2011/04/importing_example.jpg" alt="NinevehGL supports OBJ and DAE files." title="importing_example" width="600" height="600" class="size-full wp-image-1383" /></a><p class="wp-caption-text">NinevehGL supports OBJ and DAE files.</p></div></p>
<p><br/><a name="caching"></a></p>
<h2><strong>Caching 3D files</strong></h2>
<p>This is the greatest feature in my opinion. NinevehGL works with a third kind of file, the NGL binary file. It&#8217;s a binary file containing a full 3D model with all informations necessary to NinevehGL and also OpenGL. This file is loaded through streaming, so it&#8217;s extremely fast. There are no 3D softwares that can export it, it&#8217;s exclusive from NinevehGL. So how you can use it? There are two ways: First, you can convert your OBJ or COLLADA file on-line! Yes, you read it right, on-line convertion! No plugins, no installations, no complex things, you can generate NGL file on-line! Just access the NinevehGL website, choose the files on your machine and hit the button!</p>
<p>The other way, is the most fascinating one. NGL file is generated automatically by NinevehGL at the first time you load a new file from OBJ or COLLADA. This file will be stored locally in the device under the folder <em><Application_Home>/Library/NinevehGL</em>. What is great here, is that this folder is fully backed up by iTunes. So imagine this:</p>
<ol>
<li>You make an APP, which loads OBJ and COLLADA files.</li>
<li>An user make the download of your APP from the APP Store.</li>
<li>The user runs your APP at the first time, the NinevehGL loads around 20 3D file in 12 secs, let&#8217;s suppose.</li>
<li>The next times that this user runs your APP, the loading time will be 0.12 secs, for example.</li>
</ol>
<p>What happened???<br />
It&#8217;s great! At the second run, the NinevehGL automatically identified  that those 20 3D files were already parsed before, so it will use the cached NGL binary files instead the original ones!</p>
<p>But this feature doesn&#8217;t stop here! Remember, NinevehGL was made to make everything be simple! So, let&#8217;s suppose your application loads the 3D files through the internet connection instead loading it locally. No problems! NinevehGL compares the modification date of the files and choose the most recent. Imagine this:</p>
<ol>
<li>Your APP loads the 3D files from the internet, because you want to make updates without submeting a new APP to APP Store.</li>
<li>An User downloads your APP. First run, 20 3D files in 12 secs.</li>
<li>In the next times, NinevehGL will use NGL binary files, loading them in 0.12 secs.</li>
<li>You make an update and upload some new 3D files.</li>
<li>All the users that have your APP will receive the update, with 10 new 3D files replacing the old ones, so NinevehGL will parse the new downloaded files in 10 secs and it&#8217;ll generate new NGL binary files.</li>
<li>In the next runs of your application, NinevehGL will use the new generated NGL files and the loads will happen in 0.08 secs.</li>
</ol>
<p>I cry of happiness every time I explain this routine. It&#8217;s amazing, wonderful, outstanding, great, it&#8217;s&#8230; it&#8217;s&#8230;  &#8220;magical&#8221;! Well, this is just my opinion about it. But I&#8217;m sure you can imagine how great would be to the final user: &#8220;At the first run, a loading of few seconds, next times, no loading? Where is the loading of this APP?&#8221;. The loading happens in a snap.</p>
<div id="attachment_1384" class="wp-caption aligncenter" style="width: 610px"><a href="http://db-in.com/blog/wp-content/uploads/2011/04/importing_ngl_example.jpg"><img src="http://db-in.com/blog/wp-content/uploads/2011/04/importing_ngl_example.jpg" alt="NinevehGL automatically saves NGL binary files locally." title="importing_ngl_example" width="600" height="600" class="size-full wp-image-1384" /></a><p class="wp-caption-text">NinevehGL automatically saves NGL binary files locally.</p></div>
<p>The last thing I want to say about this feature is the optimization. Again, the NinevehGL was made to be EXTREMELY FAST! So, if you load a NGL binary file directly, it will not create a cache file to it, because it is already optimized, it&#8217;ll just create cache from non optimized files (OBJ and COLLADA). Now you know how to convert your 3D models to NGL binary file, you can convert them on-line, at the official NinevehGL website, or can just run your application in the simulator and take the binary file generated at the first run and find the file at <em><Application_Home>/Library/NinevehGL</em>!</p>
<p>In order to avoid confusion, it&#8217;s important to say that NinevehGL will not be responsible to manage the internet connection for the file downloading. So you can&#8217;t inform an URL to load a 3D file. What the NinevehGL will make is manage the files locally.</p>
<p><br/><a name="shaders"></a></p>
<h2><strong>Custom Shaders</strong></h2>
<p>The NinevehGL was made to work with OpenGL programmable pipeline. So obviously it&#8217;ll create its own shaders, based on the materials (loaded from 3D files or materials created directly in the code). Well, the most fun part of the programmable pipeline is exactly the shaders, so will NinevehGL block us of using our own shaders? Of course NOT! NinevehGL is absolutely user friendly, it&#8217;s flexible!</p>
<p>The other engines working with programmable pipeline are used to give to us only two choices. Working only with your very own shaders or working only with their shaders! Well, this is not a choice, but rather it&#8217;s a dilema! But, the NinevehGL offers another way, a third way: &#8220;all of above&#8221;! Yes, your read it right, NinevehGL has an API Shader capable to integrate two or more shaders into only one program. WOW, what that means?</p>
<p>That means you can create your own shaders, just as you are used to, implementing your own lights effects, materials or anything else you want. Then you pass your shaders to the NinevehGL and it&#8217;ll integrate your custom shaders with the shaders generated by the NinevehGL materials. No conflicts, everything works fine together!</p>
<p>This works for Vertex Shader and/or Fragment Shader. An important thing to say is that there are no special constraints, you can even use a shader that you already have, with many variables inside, many functions and the main function. The NinevehGL Shader API will interpretate the code inside the shaders and will fuse all together.</p>
<div id="attachment_1385" class="wp-caption aligncenter" style="width: 610px"><a href="http://db-in.com/blog/wp-content/uploads/2011/04/importing_shaders_example.jpg"><img src="http://db-in.com/blog/wp-content/uploads/2011/04/importing_shaders_example.jpg" alt="NinevehGL can merge your custom shader with its own shaders." title="importing_shaders_example" width="600" height="326" class="size-full wp-image-1385" /></a><p class="wp-caption-text">NinevehGL can merge your custom shader with its own shaders.</p></div>
<p><br/><a name="tangent_space"></a></p>
<h2><strong>Calculating Normals and Tangent Space</strong></h2>
<p>This is the robust side of NinevehGL. It&#8217;s very fast even in the hard processing scopes, like calculations of normals and tangent space for imported models. Normals and tangent space are very important to generate real time lights, reflections, bump effects and many other things. NinevehGL automatically calculates the normals and generates the tangent space for each mesh that needs it.</p>
<div id="attachment_1386" class="wp-caption aligncenter" style="width: 610px"><a href="http://db-in.com/blog/wp-content/uploads/2011/04/tangent_space_example.jpg"><img src="http://db-in.com/blog/wp-content/uploads/2011/04/tangent_space_example.jpg" alt="NinevehGL automatically generates normals and tangent space, if needed." title="tangent_space_example" width="600" height="600" class="size-full wp-image-1386" /></a><p class="wp-caption-text">NinevehGL automatically generates normals and tangent space, if needed.</p></div>
<p>NinevehGL can automatically generate some effects, like specular lights, ambient light, emissive light, bump map, reflections and other ones specified by NinevehGL materials. NinevehGL doesn&#8217;t impose any limitations to you, it just works around the limits of the OpenGL. So, in the iOS, for example, you can work with very high mesh models. Obviously this is not a common situation, but sometimes it could be better to see high mesh models on the screen. Even to these high meshes, NinevehGL will continue to produce the normals, tangent space, specular lights, bump maps, reflections and everything else you need.</p>
<div id="attachment_1388" class="wp-caption aligncenter" style="width: 610px"><a href="http://db-in.com/blog/wp-content/uploads/2011/04/specular_light_example.jpg"><img src="http://db-in.com/blog/wp-content/uploads/2011/04/specular_light_example.jpg" alt="NinevehGL also works with very high mesh models." title="specular_light_example" width="600" height="600" class="size-full wp-image-1388" /></a><p class="wp-caption-text">NinevehGL also works with very high mesh models.</p></div>
<p>Now I want to show you some other features that are also great, but I&#8217;ll not talk in deep here.</p>
<p><br/><a name="textures"></a></p>
<h2><strong>Optimizing Textures</strong></h2>
<p>Just to keep it fresh on our minds, NinevehGL was made to operate in the maximum performance. So even the loaded textures will be optimized. If you choose to work with an opaque OpenGL Layer (the default), the textures could be optimized to the format RGB_5_6_5, which is the best choice without alpha channel. If you choose to work with transparent layer, the textures could be optimized to the format RGBA_4_4_4_4.</p>
<p>The optimization process will happens automatically and you don&#8217;t need to worry about it. Even to PVRTC compressed texture format, these optimization could happen, if needed to. The textures, as any other external file, can be loaded from any path locally and the NinevehGL is ready to manage the local paths to you.</p>
<p>Plus, just as the 3D files, textures has a kind of &#8220;cache&#8221;, that means if you load an image more than one time, the NinevehGL will manage the load locally and will not spend more memory with the same cached image. This is a very important optimization, mostly in cases when a 3D file use the same image for many things, like to the ambient, diffuse and specular maps.</p>
<p><br/><a name="more"></a></p>
<h2><strong>Many other things</strong></h2>
<p>There are in NinevehGL many other important features, but I&#8217;ll just point it because other engines also have these features, so it&#8217;s not that exclusive. Like:</p>
<ul>
<li>Cameras with projections (perspective and orthographic).</li>
<li>Customizations of OpenGL behaviors, like the render buffers.</li>
<li>Simple API to make 3D transformations (like obj.rotateX += 1.0).</li>
<li>Always use OpenGL optimization features, as Buffer Objects, array of structures, optimized texture formats, etc.</li>
</ul>
<p>One great thing to talk a little bit more is about the auto-corrections. It&#8217;s very common that the 3D modelers create their models with different scales, I mean, a 3D file which contains a spoon could has the range of vertices going through -1000.0 to 1000.0 and other file which contains a house with the range of -0.5 to 0.7, for example. By placing both files in a 3D softwares you probably will see a house at the side of the empire state in format of a spoon.</p>
<p>For a 3D softwares this is not a big deal, because there are an infinity work space and you can simply reorganize your objects visually. But for a programming language, this is a problem and gets even bigger if the object is so big or so far that you can&#8217;t even see it on the screen.</p>
<p>The NinevehGL auto-correction can solve this problem for you, because it can normalize the vertices positions to fit on the screen (or in a specific range you want). By doing so you can control the size of your models without having to export it again from the 3D software. So in the case of the house and the spoon you could set the auto-correction to fit the house at the range -10.0 to 10.0 and the spoon to -0.1 and 0.1, simple as that (one line of code).</p>
<p><br/><a name="outside"></a></p>
<h2><strong>Outside the Scope</strong></h2>
<p>What you should not expect from NinevehGL? Here is a list:</p>
<ul>
<li>The NinevehGL is not a game engine, it&#8217;s a 3D engine.</li>
<li>So you shouldn&#8217;t expect physics controllers or sound controllers. You should construct this kind of stuff by yourself.</li>
<li>No animation, YET! Animations is very important in the 3D world, but the first version of NinevehGL will not come with this feature. (Animations = Character Rigging + Bones)</li>
<li>No collisions, YET! There are two techniques to deal with collision, bounding box and bounding mesh. In this version, NinevehGL will not implement any of those.</li>
</ul>
<p>Obviously any item in this list can change, because the NinevehGL is flexible. Consider the above list as an &#8220;out of scope&#8221; list only for the first version of the NinevehGL.</p>
<p><br/><a name="conclusion"></a></p>
<h2><strong>Conclusion</strong></h2>
<p>I&#8217;m sure you have now a solid opinion about NinevehGL, good or not, so let me know what you think, post a comment bellow, send me an email, tweet me&#8230; anything.</p>
<p>Again, I don&#8217;t want to make promisses, but I really want to see this engine released soon. So I&#8217;m sure it will come in the first half of this year (2011). I&#8217;m just finishing some details now and organizing everything for the first release.</p>
<p>Thanks for reading,</p>
<p>And see you very soon!</p>
<div id="attachment_1382" class="wp-caption aligncenter" style="width: 610px"><a href="http://db-in.com/blog/wp-content/uploads/2011/04/ninevehgl_featured.jpg"><img src="http://db-in.com/blog/wp-content/uploads/2011/04/ninevehgl_featured.jpg" alt="NinevehGL will come in the first half of 2011." title="ninevehgl_featured" width="600" height="475" class="size-full wp-image-1382" /></a><p class="wp-caption-text">NinevehGL will come in the first half of 2011.</p></div>
]]></content:encoded>
			<wfw:commentRss>http://db-in.com/blog/2011/04/ninevehgl-features/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>NResponder tutorial</title>
		<link>http://db-in.com/blog/2010/11/nresponder-tutorial/</link>
		<comments>http://db-in.com/blog/2010/11/nresponder-tutorial/#comments</comments>
		<pubDate>Tue, 16 Nov 2010 00:21:08 +0000</pubDate>
		<dc:creator>Diney Bomfim</dc:creator>
				<category><![CDATA[ActionScript3.0]]></category>
		<category><![CDATA[Feature]]></category>
		<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[Nineveh]]></category>

		<guid isPermaLink="false">http://db-in.com/blog/?p=550</guid>
		<description><![CDATA[Introduction Hello everyone! Welcome to this article! Today I want to show you something called Nineveh Responder engine, or just NResponder. And to do so, I will cover just four points, nothing more: Nineveh What?: What is this? What is it for? NResponder&#8217;s features: What news does it bring? NResponder usage: How to use it?&#8230;]]></description>
			<content:encoded><![CDATA[<h2><strong>Introduction</strong></h2>
<p>Hello everyone!</p>
<p>Welcome to this article!</p>
<p>Today I want to show you something called Nineveh Responder engine, or just NResponder.<br />
And to do so, I will cover just four points, nothing more:</p>
<ul>
<li><strong>Nineveh What?</strong>: What is this? What is it for?</li>
<li><strong>NResponder&#8217;s features</strong>: What news does it bring?</li>
<li><strong>NResponder usage</strong>: How to use it?</li>
<li><strong>REAL WORLD example</strong>: NResponder in action!</li>
</ul>
<p>OK, so let&#8217;s start!</p>
<p>Here are the source files of NResponder and the project files used in this tutorial:</p>
<p><a href="http://nineveh-responder.googlecode.com/files/Nineveh-Responder.zip"><img src="http://db-in.com/imgs/source_button.jpg" border=0/></a><a href="http://db-in.com/frameworks/nineveh/tuto_1.zip"><img src="http://db-in.com/imgs/download_button.jpg" border=0/></a><br />
<span id="more-550"></span></p>
<h2><strong>Nineveh What?</strong></h2>
<p>Do you remember of the city Nineveh? OK, nothing like that.</p>
<p>Nineveh is a new Framework to AS3 under construction. The point here is not to talk about this Framework, but just is important to say that NResponder is one of the engines and APIs of the Nineveh Framework. Something new about this framework is that it has engines and APIs independents, like NResponder, and these engines and APIs can work fine outside the framework.</p>
<p>OK, but what the kind of engine is NResponder?</p>
<p>An Event Engine!</p>
<p>For years I&#8217;ve used the Event Engine of default Flash framework called EventDispatcher. Oh God!</p>
<p>How boring it was! You know:</p>
<ul>
<li> Create some kind of instance or inherit from EventDispatcher class.</li>
<li> Add listeners into it.</li>
<li>Create a VERY LIMITED AND STRICT event-class inherited from Event class (in majority of cases). And you need to use just event-class AS PARAMETER when dispatching.</li>
<li>Create a new instance of that custom event-class.</li>
<li>Dispatch your custom Event using the above instance of EventDispatcher (well, I mean, if you have some instance at the time of dispatch).</li>
<li>Wait for Capture phase, Target Phase, Bubbling Phase.</li>
</ul>
<p>Ooooh&#8230;  just STOP!!!</p>
<p>If you are boring with all of that, like me, NResponder is for you!<br />
If you are completely new to Events in Flash, NResponder is for you as well!<br />
And if you are new just delete the points that I just spoke above.</p>
<p>NResponder is much much faster than native Event Engine of Flash.<br />
You don&#8217;t need to create any instance, if you don&#8217;t want to.<br />
You can manage and control events of your entire application.</p>
<p>Imagine this scene:<br />
You start you application, place 3 buttons on the stage, and load an external SWF to play a video. When you press one button, the video playing in the other SWF needs to be stopped, because another SWF will be loaded above it.</p>
<p>Well&#8230;  with EventDispatcher you should have a big path to follow until finally catch it up.<br />
But with NResponder, all that you need, is just 2 lines of code!<br />
One in the Button class and another one in the video player! Just as simple as that!<br />
Well, this is just the natural behavior of NResponder, I haven&#8217;t even talked about features yet.</p>
<p>You can use NResponder for everything related to events or fire actions, mouse and keyboard and touch interactions, etc.<br />
As stated in the title of this article, you can completely say &#8220;Good Bye&#8221; to EventDispatcher.</p>
<p>OK, now you know what NResponder is.<br />
If you want, you can see the project page on Google: <a href="http://code.google.com/p/nineveh-responder/">http://code.google.com/p/nineveh-responder/</a></p>
<p>Check the blog page here: <a href="http://www.actionscript.org/resources/admin/de/editor.php?name=wysiwyg2&amp;refresh=33963203">http://db-in.com/blog/nineveh-framework/nresponder/</a></p>
<p>You can download the NResponder here: <a href="http://nineveh-responder.googlecode.com/files/Nineveh-Responder.zip">http://nineveh-responder.googlecode.com/files/Nineveh-Responder.zip</a></p>
<p>Obviously you can check the project page on Google to see the Documentation, check the SVN, learn with the Wiki and so forth, but here I will teach you step by step guide in a tutorial to use the great power of NResponder.</p>
<p>Now let&#8217;s dig further.</p>
<p><br/></p>
<h2><strong>NResponder&#8217;s features</strong></h2>
<p>To talk about the features of NResponder I want to give you a little comparison.</p>
<p>In the following table you can see the native Envent Engine (or API) compared to the features of NResponder.</p>
<table class="innertable">
<tbody>
<tr>
<th>Feature</th>
<th align="center">Event API<br />
(Adobe® Flash®)</th>
<th align="center">Signals<br />
(Robert Penner)</th>
<th align="center">NResponder<br />
(Diney Bomfim)</th>
</tr>
<tr>
<td><strong>Used without instance</strong></td>
<td align="center"><img src="http://db-in.com/imgs/no.gif" alt="No" /></td>
<td align="center"><img src="http://db-in.com/imgs/no.gif" alt="No" /></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
</tr>
<tr>
<td><strong>Used with instance</strong></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
</tr>
<tr>
<td><strong>Specify number of dispatch arguments</strong></td>
<td align="center"><img src="http://db-in.com/imgs/no.gif" alt="No" /></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
</tr>
<tr>
<td><strong>Dynamic dispatch arguments</strong></td>
<td align="center"><img src="http://db-in.com/imgs/no.gif" alt="No" /></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
</tr>
<tr>
<td><strong>Change dispatch arguments in Run-Time</strong></td>
<td align="center"><img src="http://db-in.com/imgs/no.gif" alt="No" /></td>
<td align="center"><img src="http://db-in.com/imgs/no.gif" alt="No" /></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
</tr>
<tr>
<td><strong>Remove all</strong></td>
<td align="center"><img src="http://db-in.com/imgs/no.gif" alt="No" /></td>
<td align="center">limited to instance</td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
</tr>
<tr>
<td><strong>Specify a delay time</strong></td>
<td align="center"><img src="http://db-in.com/imgs/no.gif" alt="No" /></td>
<td align="center"><img src="http://db-in.com/imgs/no.gif" alt="No" /></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
</tr>
<tr>
<td><strong>Set the number of replies</strong></td>
<td align="center"><img src="http://db-in.com/imgs/no.gif" alt="No" /></td>
<td align="center">Just 1</td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
</tr>
<tr>
<td><strong>Ordenate</strong></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
</tr>
<tr>
<td><strong>Can associate with a target</strong></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
<td align="center">only NativeSignal</td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
</tr>
<tr>
<td><strong>Can pause without remove</strong></td>
<td align="center"><img src="http://db-in.com/imgs/no.gif" alt="No" /></td>
<td align="center"><img src="http://db-in.com/imgs/no.gif" alt="No" /></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
</tr>
<tr>
<td><strong>Can stop propagation</strong></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
<td align="center"><img src="http://db-in.com/imgs/no.gif" alt="No" /></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
</tr>
<tr>
<td><strong>Change attributes in Run-Time</strong></td>
<td align="center"><img src="http://db-in.com/imgs/no.gif" alt="No" /></td>
<td align="center"><img src="http://db-in.com/imgs/no.gif" alt="No" /></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
</tr>
<tr>
<td><strong>Garbage Collect</strong></td>
<td align="center">only weak references</td>
<td align="center"><img src="http://db-in.com/imgs/no.gif" alt="No" /></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
</tr>
</tbody>
</table>
<p>I will focus on the most important and more usable features:</p>
<p><br/></p>
<h4><strong>Static and instantiated mode</strong></h4>
<p>With NResponder you can choose the best way to work. Use it as a static class, without creating any constant or variable, or use it as an instance, a more object oriented approach.</p>
<p>An important thing to say is that the instantiated mode offers you more control over the event, like pausing it without remove it.</p>
<p><br/></p>
<h4><strong>Custom number of arguments and changes</strong></h4>
<p>This is really one of my favorite features. As you know, in the native Flash&#8217;s Event Engine the dispatcher MUST and NEED to inform ONE, and just one, Event instance. And the call back functions need to expect that kind of event.</p>
<p>So, for example, what you have is:</p>
<pre class="brush:as3">
dispatcherInstance.addEventListener("aCustomEvent", callBackFunction);
dispatcherInstance.dispatchEvent(new MyCustomEvent("aCustomEvent"));
...
private function callBackFunction(event:MyCustomEvent):void
{
...
}
</pre>
<p>This is boring!!!</p>
<p>If you want to pass some kind of data to your callBackFunction() you need to put new variables to your MyCustomEvent class!</p>
<p>Just forget it!!!</p>
<p>With NResponder you can pass how many parameters you want and can have a call back function like this:</p>
<pre class="brush:as3">private function callBackFunction(aNumber:Number,
                                  aString:String,
                                  customObject:ACustomObject):void
{
...
}</pre>
<p>This give you so much more flexibility to work!</p>
<p>Because you can make your call back functions more readable and more reusable.</p>
<p><br/></p>
<h4><strong>Number of replies</strong></h4>
<p>This is one of my favorite too.</p>
<p>With this feature you can simple set the number of times that a specific event (or Action, in NResponder language) will happen! How many you want, just 5, or until 21, anything!<br />
One very common and useful usage of this is for view classes.<br />
As you know, when you start a view class and it is not on the stage yet, its property of &#8220;stage&#8221; is null. So widely accepted technique among developers is the use of the following:</p>
<pre class="brush:as3">
public function Main()
{
    if (this.stage)
    {
        init(null);
    }
    else
    {
        this.addEventListener(Event.ADDED_TO_STAGE, init);
    }
}

public function init()
{
    if (this.hasEventListener(Event.ADDED_TO_STAGE))
    {
        this.removeEventListener(Event.ADDED_TO_STAGE, init);
    }
}
</pre>
<p>You need to add and remove, make checks to hasEventListeners, to use this routine just ONCE!<br />
It&#8217;s sucks!!!<br />
With NResponder all that you need is set the number of replies, in this case 1, and stay focused on your code! Because NResponder will be responsible to clean up the mess later, delete what needs to, frees the memory and go on.</p>
<p><br/></p>
<h4><strong>Delay time</strong></h4>
<p>This can be very helpful in some situations.<br />
The delay time specify the time, in seconds, that will be the minimum to wait between two consecutive dispatches.<br />
For example, you have a button in your game that the user needs to press continuously. But you want a little interval of time between two clicks, like 0.5 secs. With NResponder all that you need to do is to set one property and everything will be controlled by the NResponder engine.</p>
<p><br/></p>
<h4><strong>Combined removes</strong></h4>
<p>With the native Event Engine if you set a lot of listeners and later you need to remove them, you&#8217;ll have to do so one by one.</p>
<p>With NResponder you can make combined removes, like remove all listeners of a target, remove all listeners of a specific call back function, remove all listeners of a specif type (Action), like Mouse.CLICK, or even make combination like remove all listeners of a specific target with a specific call back function.<br />
Don&#8217;t worry if this don&#8217;t make sense to you now, when I show you the examples, everything will become clearer.</p>
<p>OK, these are the most important features of NResponder.</p>
<p>I&#8217;ll not mention the features that have correlated in native Event Engine, like order or stop propagations, because I suppose that you already know it from EventDispatcher.</p>
<p><br/></p>
<h4><strong>One more thing&#8230;</strong></h4>
<p>Something that you may be thinking now is: &#8220;OK, you&#8217;ve talked about mouse events, keyboard, touches&#8230; and said that I can say Goog Bye to native Event Engine&#8230; but are not mouse, keyboard and touches native events?&#8221;.<br />
Yeah!<br />
You are right!<br />
With NResponder you don&#8217;t need any instance of EventDispatcher in your application, you don&#8217;t need to use the processing core of native Event Engine.<br />
But if you want to, NResponder gives you a way to use the native system under the NResponder engine system.<br />
Obviously to work with mouse events, keyboard, touches, etc, NResponder need to use a little portion of the native Event Engine, because it&#8217;s the only way to catch this kind of event in Flash.<br />
But again, NResponder can do this so much better than using the native EventDispatcher process.</p>
<p><br/></p>
<h2><strong>NResponder usage</strong></h2>
<p>Hands to work!</p>
<p>For now we&#8217;ll just create a project that prints (trace) some responses on the screen. So you&#8217;ll need an IDE to compile and debug the code, like Flash Professional, Flash Builder, Flash Develop, FDT, etc.</p>
<p>OK, now let&#8217;s code!</p>
<p><br/></p>
<h4><strong>Static or instantiated mode</strong></h4>
<p>The first important step is to import the NResponder to your classes. Some IDEs do this automatically for you (Flash Builder, FDT, Flash Develop,&#8230;) when you are typing. NResponder package is inside the &#8220;appkit&#8221; (a SubFramework of Nineveh Framework) and inside the package &#8220;responders&#8221;. This path is just a convention of Nineveh Framework. Of course, you will need to import this package in all classes that you want to use NResponder.</p>
<pre class="brush:as3">
package
{
    import flash.display.MovieClip;
   
    import appkit.responders.NResponder;

    public class Main
    {
        public function Main():void
        {
        }
    }
}
</pre>
<p>Now, about the static and instantiated mode. Every time you add a new NResponder (register an event, add a call back function, register a listener, whatever, is the same thing for NResponder), the NResponder will create a little instance of Responder, this instance will go to the core of NResponder and will be the responsible to execute the commands in the engine&#8217;s core. This is a behavior, NResponder will always do this. The cool thing here is that you can get a copy of this instance when you add a new. And you have some flexible ways to do so:<br />
IMPORTANT: The following code is not real, is just to illustrate the both modes. In the next section (Creating a NResponder) you&#8217;ll see a real code.</p>
<pre class="brush:as3; highlight: [12, 15, 16, 18]">
package
{
    import flash.display.MovieClip;
   
    import appkit.responders.NResponder;

    public class Main
    {
        public function Main():void
        {
            // This is the static mode
            NResponder.add();
           
            // This is the instantiated mode.
            var responder:NResponder;
            responder = new NResponder();
            // Or even
            responder = NResponder.add();
        }
    }
}
</pre>
<p>You could asking: &#8220;wait, why the &#8216;new&#8217; create an instance and the static call return an instance too?&#8221;</p>
<p>Well, you&#8217;ll understand soon, the &#8220;new&#8221; constructor is generic, and the static &#8220;adds&#8221; (in plural) have some shortcuts to you. As I&#8217;ve mentioned earlier, the NResponder can work with native events in Flash, so the different &#8220;adds&#8221; help you to create it. Although the &#8220;new&#8221; constructor make it, you&#8217;ll need to write more parameters to it.</p>
<p>Reasons why:<br />
The static and instantiated mode doesn&#8217;t have any impact in performance of memory of your application. The unique reason to use instantiated is if you need to change some behaviors of NResponder after creating it. Like pausing it without deleting, for example. But if you don&#8217;t need further changes, don&#8217;t worry with any instance.</p>
<p><br/></p>
<h4><strong>Creating a NResponder</strong></h4>
<p>Now let&#8217;s see how to set a call back function to an event in Flash, or more directly, how to create a NResponder. Here you&#8217;ll define your listener, will specify the Action (same as Type in EventDispatcher&#8217;s language) and all kind of things that NResponder will make for you.<br />
IMPORTANT: It&#8217;s here, in this section that you can say &#8220;Good bye&#8221; to EventDispatcher!</p>
<p>OK, the first thing that I want to show is how you normally do with EventDispatcher. When you want to use a custom event you do something like this:</p>
<pre class="brush:as3">
var myDispatcher:EventDispatcher;
myDispatcher.addEventListener('myCustomEventType',
                              callBackFunction, false, 0, true);
...
myDispatcher.dispatchEvent(new CustomEvent('myCustomEventType'));
</pre>
<p>This sucks!</p>
<p>Because the only way for you to dispatch your CustomEvent is using the &#8220;myDispatcher&#8221; instance! And look at this line of addEventListener, it is so big, completely unnecessary.<br />
Now to do the same thing with NResponder all that you need is:</p>
<pre class="brush:as3">
NResponder.add('myCustomEventType', callBackFunction);
...
NResponder.dispatch('myCustomEventType');
</pre>
<p>We&#8217;ll talk about &#8220;dispatch&#8221; later, but for now, as you can see, you don&#8217;t need to worry with additional parameters like use capture, use weak references, because NResponder doesn&#8217;t need these kind of things to work with NResponder. Here is a little difference between the EventDispatcher and NResponder. The &#8216;myCustomEventType&#8217; is the type of event to EventDispatcher, but NResponder has called it &#8220;Action&#8221;. So henceforth let&#8217;s call &#8220;Action&#8221; everything that means &#8220;Event Type&#8221; for EventDispatcher.</p>
<p>And as I&#8217;ve mentioned earlier, you can get a reference to the instance returned by the &#8220;add&#8221; method:</p>
<pre class="brush:as3">
var myReponder:NResponder = NResponder.add('myCustomEventType',
                                           callBackFunction);
</pre>
<p>Remember that this instance doesn&#8217;t change anything, it&#8217;s just for you get more control over the NResponder instance.<br />
OK, now let&#8217;s take a look at the NResponder working with native Event API. The method to create it is very similar. If you want to set a click in a button, for example, using the native Event API you probably make something like that:</p>
<pre class="brush:as3">
// probably inherited from MovieClip or Sprite.
var myButton:CustomButton;
myButton.addEventListener(MouseEvent.CLICK,
                          clickFunction, false, 0, true);
</pre>
<p>OK, now let&#8217;s translate this to NResponder&#8217;s language:</p>
<pre class="brush:as3">
// probably inherited from MovieClip or Sprite.
var myButton:CustomButton;
NResponder.addNative(myButton, MouseEvent.CLICK, clickFunction);
</pre>
<p>Just simple as that. Using the method &#8220;addNative&#8221; is very similar to &#8220;add&#8221;, with only one difference: the first parameter is the target of the native Event.</p>
<p>Some people may get confused at this point. But all that you need to remember is to ask &#8220;Who is responsible for dispatching this native event? Who is inherited from EventDispatcher class?&#8221;, so you take this object and pass it on the first parameter to &#8220;addNative&#8221; method.<br />
Like this:<br />
<img src="http://db-in.com/imgs/addnative_example.jpg" border="0"/></p>
<p>You can use NResponder to all kind of native events. The benefits is that it will work faster, more cleaner and you can use all the features of the NResponder. The following topics will explain about it.</p>
<p>Well, as you&#8217;ve noticed, as a Good Practice in programming to avoid misspelling, is advisable to you store your custom Actions in a class with &#8220;public static constants&#8221;, like this:</p>
<pre class="brush:as3">
public static const CUSTOM_ACTION:String = 'myCustomAction';
...
NResponder.add(CUSTOM_ACTION, callBackFunction);
</pre>
<p>This is just a tip, although in this article to simplify the purposes I&#8217;ll use literal strings to illustrate the examples.</p>
<p><br/></p>
<h4><strong>Dispatch and arguments</strong></h4>
<p>After creating a NResponder with the &#8220;adds&#8221; methods (add() and addNative()) you can dispatch these responders from anywhere in your application, even in different SWF files (loaded files in the same application). All that you need is to inform the &#8220;Action&#8221; you want to dispatch and the parameters that you want, if needed.<br />
Like this:</p>
<pre class="brush:as3">
// This will dispatch "myAction" with no arguments.
// So the callBack function doesn't need to expect any parameter.

NResponder.dispatch('myAction');

// This will dispatch "anotherAction"
// with the arguments 35 (Number) and "aString" (String)
// So the callBack function should expect
// these parameter (Number and String in this order).

NResponder.dispatch('anotherAction', [35.5, 'aString']);
</pre>
<p>The dispatch() method is simple, it will dispatch the specified Action to the whole application.</p>
<p>OK, so you could ask &#8220;If NResponder will dispatch an Action for anyone that has been registered in NResponder, what will it make with buttons, for example? Will it dispatch a click to all? How will it set what button to dispatch for?&#8221;.<br />
Good Point!<br />
The answer is: using the Target parameter!</p>
<p>Do you remember in addNative() that you need to specify the Target as the first parameter? It&#8217;s now that it makes the difference. If you specify a target when creating a NResponder, that target will work like a lock and this NResponder will only respond when it&#8217;s target is specified. Well, this might be a little confusing by now, but just think that NResponderCore will care about this for you. If you are using a native event, like MouseEvents, KeyboardEvents or any other, this will happens automatically during the dispatch. Just don&#8217;t worry about it.<br />
If you want, you can specify a target without using the native events. You may use this to take the benefits of this &#8220;lock&#8221; that target provides. But this is a more advanced topic and I don&#8217;t want to confuse you with this here. If you want to know more about this just check this Wiki of the project:</p>
<p><a href="http://code.google.com/p/nineveh-responder/wiki/DispatchAdvanced">http://code.google.com/p/nineveh-responder/wiki/DispatchAdvanced</a></p>
<p>Let&#8217;s move.</p>
<p><br/></p>
<h4><strong>Number of replies</strong></h4>
<p>As I&#8217;ve told you, this is one of the most interesting features to me. This lets you specify how many times you want a NResponder performs and after the very last one, it will automatically be removed and cleaned. All that you need is specify a number after the listener parameter in the &#8220;adds&#8221; methods.<br />
Like this:</p>
<pre class="brush:as3">
NResponder.add('myCustomEventType', callBackFunction, 5);
NResponder.addNative(myButton, MouseEvent.CLICK, clickButton, 2);
NResponder.addNative(this, Event.ADDED_TO_STAGE, init, 1);
</pre>
<p>The code above means that:</p>
<ul>
<li>&#8220;myCustomEventType&#8221; will only response 5 times</li>
<li>The CLICK on &#8220;myButton&#8221; will happens just 2 times</li>
<li>The &#8220;init&#8221; listener will happens just once when the target &#8220;this&#8221; be placed on the stage.</li>
</ul>
<p>You can specify how many times you want. Obviously you can&#8217;t specify negative number of times. And just for convention, the 0 (default) number means that NResponder will be performed infinitely.</p>
<p><br/></p>
<h4><strong>Delay time</strong></h4>
<p>The delay time is the minimum time necessary between two consecutive dispatches.<br />
Imagine that you have a button in your game and you don&#8217;t want the user start to click on it without stopping. You want the at least the user to wait 2.5 sec until he makes a new click. Well, all that you need is specify the next parameter after the number of replies in the &#8220;adds&#8221; methods.</p>
<pre class="brush:as3">
NResponder.addNative(myButton, MouseEvent.CLICK, clickButton, 0, 2.5);
</pre>
<p>This means that the &#8220;clickButton&#8221; listener just will happens again if the next click occurs after 2.5 sec. If the user make a click during the delay of 2.5 sec that click will be ignored. The 0 (default) means &#8220;no delay&#8221;.</p>
<p><br/></p>
<h4><strong>Has and remove</strong></h4>
<p>Just like the EventDispatcher class, NResponder provide a way for you to check what is registered inside the NResponderCore and a way to remove it. The advantage here is that you can combine and make multiple tasks at once. Let&#8217;s start with a simple case, you want to know your application has any registered Action of &#8220;myAction&#8221;, so:</p>
<pre class="brush:as3">
NResponder.has('myAction');
</pre>
<p>So simple!</p>
<p>This will return &#8220;true&#8221; if you have at least one registered NResponder with &#8220;myAction&#8221;. Now, imagine that you want to know if it has an &#8220;myAction&#8221; Action linked with a specific call back function.</p>
<pre class="brush:as3">
NResponder.has('myAction', myFunction);
</pre>
<p>This will return &#8220;true&#8221; if you have at least one registered NResponder with &#8220;myAction&#8221; as Action and &#8220;myFunction&#8221; as Listener. And at last, you can check if this is true for a specific target.</p>
<pre class="brush:as3">
NResponder.has('myAction', myFunction, myTarget);
</pre>
<p>This will return &#8220;true&#8221; if you have at least one registered NResponder with &#8220;myAction&#8221; as Action and &#8220;myFunction&#8221; as Listener and &#8220;myTarget&#8221; as Target. The funny thing here, is that you can combine those 3 parameters, leading to the following:</p>
<pre class="brush:as3">
NResponder.has('myAction', null, myTarget);
NResponder.has(null, myFunction, myTarget);
NResponder.has(null, myFunction, null);
NResponder.has(null, null, myTarget);
</pre>
<p>The four line above means:</p>
<ol>
<li>Has at least one registered NResponder with &#8220;myAction&#8221; as Action and &#8220;myTarget&#8221; as Target?</li>
<li>Has at least one registered NResponder with &#8220;myTarget&#8221; responding to &#8220;myFunction&#8221;?</li>
<li>Has at least one registered NResponder responding to &#8220;myFunction&#8221;, independently of Actions and Target?</li>
<li>Has at least one registered NResponder with &#8220;myTarget&#8221; as target, regardless of Listeners and Actions?</li>
</ol>
<p>The null means that you want to ignore that parameter and focus on the others. These combination can be very helpful in the case that you don&#8217;t have more access to one or two of those parameters. The most helpful of the combination feature is in remove method. You can remove a lot of NResponders with just one line of code. The remove() method works exactly as has() method:</p>
<pre class="brush:as3">
NResponder.remove(null, null, myTarget);
</pre>
<p>The line above means that you&#8217;ll remove all the NResponder of a specific Target (&#8220;myTarget&#8221;). Now think of a button. You can register a NResponder to the &#8220;Mouse Over&#8221; action, another one to the &#8220;Mouse Out&#8221;, another to &#8220;Mouse Down&#8221;, one to &#8220;Mouse Up&#8221; and another one &#8220;Mouse Move&#8221;. Well, 5 NResponders. And after all, when you don&#8217;t want this button anymore, you can remove all these NResponder in just one line, like this:</p>
<pre class="brush:as3">
NResponder.addNative(myButton, MouseEvent.MOUSE_OVER, overList);
NResponder.addNative(myButton, MouseEvent.MOUSE_OUT, outList);
NResponder.addNative(myButton, MouseEvent.MOUSE_DOWN, startDragList);
NResponder.addNative(myButton, MouseEvent.MOUSE_UP, stopDragList);
NResponder.addNative(myButton, MouseEvent.MOUSE_MOVE, draggingList);
...
NResponder.remove(null, null, myButton);
</pre>
<p>Remember that NResponderCore is responsible for all cleaning, and it will know exactly how to clean everything with just one line of remove() method.</p>
<p><br/></p>
<h4><strong>Benefits of instantiated mode</strong></h4>
<p>With instantiated mode you can change the order of dispatches of the same Actions, you can perform the call back function directly, you can check some attributes of that instance of NResponder, change the values of delay, number of replies and so on. I&#8217;ll not talk about all of these benefits, you can find information about they at the Wiki of Project page and at docs:</p>
<p><a href="http://code.google.com/p/nineveh-responder/w/list">http://code.google.com/p/nineveh-responder/w/list</a></p>
<p><a href="http://db-in.com/frameworks/nineveh/doc/">http://db-in.com/frameworks/nineveh/doc/</a></p>
<p>But I do want to talk here about a benefit called &#8220;paused&#8221;. This property is a boolean in which &#8220;true&#8221; indicates paused. As the name suggests, you can pause the NResponder without removing it. This can be very helpful if you want to interrupt a NResponder just for a while.</p>
<p>For example, imagine that in your application you have a 3D interface that reacts to mouse position. But if the user hits the space bar it will appear a pop-up and for the time the user stay on this pop-up you want your 3D interface to stop to reacting to mouse position. Well, all that you need is use NResponder in instantiated mode and pause it when you need.<br />
Like this:</p>
<pre class="brush:as3">
var myResponder:NResponder = NResponder.addNative(my3DInterface, MouseEvent.MOUSE_MOVE, interactFunction);
...
// When start the pop-up, use this to pause this NResponder.
myResponder.paused = true;
...
// When close the pop-up, use this to reactivate this NResponder again.
myResponder.paused = false;
</pre>
<p>Obviously you can remove and add the NResponder again using the static mode, but at this way you save CPU and memory, because pausing is much much faster than adding and removing.</p>
<p>Here I&#8217;ve presented to you some features, the most important and most used, of the NResponder Engine. Remember to check the project page to see everything about NResponder. For now, this is all that we need to say &#8220;Good Bye to EventDispatcher&#8221;. With just these features you can do almost everything with NResponder.</p>
<p>Now let&#8217;s move to some real world example!</p>
<p><br/></p>
<h2><strong>REAL WORLD example</strong></h2>
<p>Finally let&#8217;s see a real example of NResponder working. </p>
<p><br/></p>
<h4><strong>At a glance</strong></h4>
<p>In this example we will create a very simple shoot game&#8217;s routine. To show you how the features and benefits of NResponder can change how you think about your work, let&#8217;s refine a little this shoot game. Imagine that the gun in this game is an old single-action revolver, so at every shoot the hammer of this revolver needs to be pulled again and this action may take about 1.5 sec. And more, the gun just has 6 bullets, after 6 shots the user must reload the gun.<br />
First let&#8217;s imagine what we need to construct this with the native Event API, how many Booleans, timers, a lot of &#8220;if then else&#8221;, and variables we need to use to achieve this behavior. I&#8217;ll show a possible way to do this, but in the middle, before the code start to get more complex, with a lot of annoying things, we&#8217;ll change and use the NResponder.</p>
<p>Now, about the project files, is important to say that I&#8217;ve placed some two-handed comments. You know, that kind of comment block that you can toggle on/off between two codes. Like this:</p>
<pre class="brush:as3">
/*
...
/*/
The code here is activated.
//*/

---------

//*
The code here is activated.
/*/
...
//*/
</pre>
<p>So in the project files I let this kind of comment for you see the difference between the native Event API and NResponder. You can easily toggle between both adding a &#8220;/&#8221; at the first line of the comment block.</p>
<p>Oh right, let&#8217;s start!</p>
<p><br/></p>
<h4><strong>Constructing with native Event API</strong></h4>
<p>At this point I&#8217;m sure you already look at the project files. So I&#8217;ll just describe what you&#8217;ve found in FLA file:<br />
<a href="http://db-in.com/imgs/tuto_1_screen1.jpg" target="_blank"><img src="http://db-in.com/imgs/tuto_1_screen1.jpg" border="0/" height="347" width="700"/></a></p>
<p>Nothing new here, just a stage with 3 MovieClips: the shot area, the reload button and the bullets. Inside the bullets MovieClip we have a MovieClip for each bullet. All of these have instance names that we&#8217;ll use in the main class later. As you can see, in the library we export a view called VShootAnimation. It is just a view class to control the animation of the shoot using a texture atlas (poof.png).</p>
<p>Now I&#8217;ll start with the NResponderExample just using the native Event API.</p>
<pre class="brush:as3">
/**
 * NResponderExample class
 *
 * @version NResponderExample v1.0
 * @author Diney Bomfim
 */
package
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;
   
    /**
     * Simple shoot game to illustrate the usage of NResponder.
     */
    public dynamic class NResponderExample extends MovieClip
    {
       
    //**************************************************
    //  Protected / Private Properties
    //**************************************************
       
        private var shoots:int;
       
    //**************************************************
    //  Initialization
    //**************************************************
       
        /**
         * The initialization of NResponderExample class.
         */
        public function NResponderExample():void
        {
            if (stage)
            {
                init(null);
            }
            else
            {
                addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
            }
        }
       
    //**********************************************************************************************************
    //
    //  Protected / Private Methods
    //
    //**********************************************************************************************************
       
        /**
         * Initiate the View of the application.
         *
         * @param    e
         */
        private function init(e:Event):void
        {
            // Remove the listener
            removeEventListener(Event.ADDED_TO_STAGE, init);
           
            // Definitions
            doReload(null);
           
            // Listeners
            this.btReload.addEventListener(MouseEvent.MOUSE_DOWN, doReload, false, 0, true);
        }
       
        /**
         * Make a Shoot on the valid stage area.
         *
         * @param    e
         */
        private function doShoot(e:MouseEvent):void
        {
            trace('a Shoot');
           
            var shootAnim:VShootAnimation = new VShootAnimation();
            shootAnim.x = stage.mouseX;
            shootAnim.y = stage.mouseY;
           
            addChild(shootAnim);
           
            this.mcBullets['mcBullet' + shoots].visible = false;
           
            ++shoots;
        }
       
        /**
         * Reload the gun.
         *
         * @param    e
         */
        private function doReload(e:MouseEvent):void
        {
            shoots = 1;
           
            for (var i:int = 1; i <= 6; i++)
            {
                this.mcBullets['mcBullet' + i].visible = true;
            }
           
            // Listeners
            this.mcShootArea.addEventListener(MouseEvent.MOUSE_DOWN, doShoot, false, 0, true);
        }
    }
}
</pre>
<p>Well, the above class will be our main class of the FLA file.</p>
<ul>
<li>First in the constructor of the class (NResponderExample()), just to follow a best practice we put Event.ADDED_TO_STAGE to guarantee that no errors will happens if this file is loaded by another SWF file.</li>
<li>In the init() method we remove the previously Event.ADDED_TO_STAGE, make the first reload to the gun and add the listener of the reload button.</li>
<li>In the doReload() method we make all the bullets inside the mcBullets visible again and add the listener that will make the shoots.</li>
<li>In the doShoot() method we place our VShootAnimation view class in the mouse screen position and remove one bullet from the screen.</li>
</ul>
<p>At this point the code remains simple. You could test and see what happens.<br />
But our somewhat refined behavior is not implemented yet. The bullets doesn't has a delay to simulate the old revolver, the shoots doesn't stop after the sixth and we have a bug in the line this.mcBullets['mcBullet' + shoots].visible = false; when the shoots doesn't stop after the sixth.</p>
<p>Well, try to think what should be necessary to implement our behavior.<br />
Take some minutes if necessary looking at this code and try to implement this behavior.<br />
Remember all the problems above.<br />
To do this we would probably place a lot of "if then else" in our code, create timer checks, Booleans, oh god... really boring!</p>
<p>But let's STOP here and migrate all our events to NResponder Engine.</p>
<p><br/></p>
<h4><strong>Solving the problems with NResponder</strong></h2>
<p>Now it's the BIG POINT of this article. It's right now! It's right here!<br />
It's here that the magic happens with NResponder!<br />
Believe it or not, but to solve ALL these problems, to implement our behavior to the above code, ALL that we need is to REPLACE 3 LINES of the code and may even EXCLUDE ONE!!!</p>
<p>Just it, nothing more!</p>
<pre class="brush:as3">
/**
 * NResponderExample class
 *
 * @version NResponderExample v1.0
 * @author Diney Bomfim
 */
package
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;
   
    import appkit.responders.NResponder;
   
    /**
     * Simple shoot game to illustrate the usage of NResponder.
     */
    public dynamic class NResponderExample extends MovieClip
    {
       
    //**************************************************
    //  Protected / Private Properties
    //**************************************************
       
        private var shoots:int;
       
    //**************************************************
    //  Initialization
    //**************************************************
       
        /**
         * The initialization of NResponderExample class.
         */
        public function NResponderExample():void
        {
            if (stage)
            {
                init(null);
            }
            else
            {
                /*
                addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
                /*/
                NResponder.addNative(this, Event.ADDED_TO_STAGE, init, 1);
                //*/
            }
        }
       
    //**********************************************************************************************************
    //
    //  Protected / Private Methods
    //
    //**********************************************************************************************************
       
        /**
         * Initiate the View of the application.
         *
         * @param    e
         */
        private function init(e:Event):void
        {
            // Remove the listener
            //removeEventListener(Event.ADDED_TO_STAGE, init); // Don't need this any more.
           
            // Definitions
            doReload(null);
           
            // Listeners
            /*
            this.btReload.addEventListener(MouseEvent.MOUSE_DOWN, doReload, false, 0, true);
            /*/
            NResponder.addNative(this.btReload, MouseEvent.MOUSE_DOWN, doReload);
            //*/
        }
       
        /**
         * Make a Shoot on the valid stage area.
         *
         * @param    e
         */
        private function doShoot(e:MouseEvent):void
        {
            trace('a Shoot');
           
            var shootAnim:VShootAnimation = new VShootAnimation();
            shootAnim.x = stage.mouseX;
            shootAnim.y = stage.mouseY;
           
            addChild(shootAnim);
           
            this.mcBullets['mcBullet' + shoots].visible = false;
           
            ++shoots;
        }
       
        /**
         * Reload the gun.
         *
         * @param    e
         */
        private function doReload(e:MouseEvent):void
        {
            shoots = 1;
           
            for (var i:int = 1; i <= 6; i++)
            {
                this.mcBullets['mcBullet' + i].visible = true;
            }
           
            // Listeners
            /*
            this.mcShootArea.addEventListener(MouseEvent.MOUSE_DOWN, doShoot, false, 0, true);
            /*/
            NResponder.addNative(this.mcShootArea, MouseEvent.MOUSE_DOWN, doShoot, 6, 1.5);
            //*/
        }
    }
}
</pre>
<p>As you can see, first using the feature of "number of replies" we make our Event.ADDED_TO_STAGE listener run once and automatically be excluded.</p>
<p>With the same feature we make our gun dispatch just 6 times and stop.<br />
And using the delay behavior we make our gun wait 1.5 sec between two consecutive shoots.<br />
Well, literally, you can say "Good Bye" to EventDispatcher in your application!</p>
<p><br/></p>
<h4><strong>Conclusion</strong></h4>
<p>Well, obviously that I've shown a specific example where the NResponder fits perfectly and you can think that this is just an isolated example. Just remember all the features and facilities that I've told you here, remember to check the project page and the docs to know all the power of NResponder.</p>
<p>But if you still have doubts about how helpful and powerful NResponder can be, I just want you to remember two things, just two little things:</p>
<ul>
<li>NResponder is completely independent, can dispatch from everywhere to everywhere and can carry everything with it.</li>
<li>With NResponder we can realize new things, new horizons that we didn't realize before. Perhaps, before it, we've just followed another ways.</li>
</ul>
<p>Finally, hope I've helped you and hope you try NResponder, test it, try to replace it in your own code, make some performance tests, in another words, ENJOY!<br />
Again I'll place here the links about NResponder and if you have any doubt about NResponder, just email me!</p>
<p>Project page: <a href="http://code.google.com/p/nineveh-responder/">http://code.google.com/p/nineveh-responder/</a></p>
<p>Blog page: <a href="http://www.actionscript.org/resources/admin/de/editor.php?name=wysiwyg2&amp;refresh=1522575863">http://db-in.com/blog/nineveh-framework/nresponder/</a></p>
<p>Download the NResponder: <a href="http://nineveh-responder.googlecode.com/files/Nineveh-Responder.zip">http://nineveh-responder.googlecode.com/files/Nineveh-Responder.zip</a></p>
<p>Thanks for everyone that read this article.</p>
<p>See you later!</p>
]]></content:encoded>
			<wfw:commentRss>http://db-in.com/blog/2010/11/nresponder-tutorial/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>NResponder Released &#8211; The New Flash Event Engine</title>
		<link>http://db-in.com/blog/2010/08/nresponder-released-flash-event-engine/</link>
		<comments>http://db-in.com/blog/2010/08/nresponder-released-flash-event-engine/#comments</comments>
		<pubDate>Sun, 15 Aug 2010 09:17:44 +0000</pubDate>
		<dc:creator>Diney Bomfim</dc:creator>
				<category><![CDATA[ActionScript3.0]]></category>
		<category><![CDATA[Feature]]></category>
		<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[Nineveh]]></category>

		<guid isPermaLink="false">http://db-in.com/blog/?p=309</guid>
		<description><![CDATA[I&#8217;m very glad to announce that Nineveh-Responder (NResponder) was released. Official code page: http://code.google.com/p/nineveh-responder/ NResponder is a new way to work with events in ActionScript 3.0 globally. You can cross events between different classes or even in different SWF files. With NResponder you add so much more flexibility to your application because you can changes&#8230;]]></description>
			<content:encoded><![CDATA[<p><a href="http://db-in.com/imgs/nineveh_responder.png" target="_blank"><img class="alignleft size-medium wp-image-240" style="border: 0pt none;" title="Nineveh Responder Engine" src="http://db-in.com/imgs/nineveh_responder.png" alt="Nineveh Responder Engine" width="240" height="240" /></a>I&#8217;m very glad to announce that Nineveh-Responder (NResponder) was released.</p>
<p><br/></p>
<p>Official code page:<br />
<a href="http://code.google.com/p/nineveh-responder/" target="_blank">http://code.google.com/p/nineveh-responder/</a></p>
<p><br/></p>
<p>NResponder is a new way to work with events in ActionScript 3.0 globally. You can cross events between different classes or even in different SWF files. With NResponder you add so much more flexibility to your application because you can changes the parameters &#8220;on the fly&#8221; in Run-Time. NResponder is inspired on Objective-C approach and is very oriented to performance and memory. You don&#8217;t need to create any instance if you don&#8217;t want.</p>
<p>For example, you can do:</p>
<pre class="brush:as3">
// Instead this
myObject.addEventListener('anEventType', initialize, false, 0, true);

// Use this
NResponder.add('anEventType', initialize);
</pre>
<p><br/></p>
<p><strong>FEATURES</strong><br />
Bellow some characteristics of NResponder:</p>
<ul>
<li>Work in static or instantiated mode;</li>
<li>Can specify the number of arguments to dispatch;</li>
<li>Can change these arguments &#8220;on the fly&#8221; (in Run-Time);</li>
<li>Can remove all NResponders;</li>
<li>Can specify the number of replies;</li>
<li>Can ordinate the replies;</li>
<li>Can associate each listener with one target;</li>
<li>Can have a delay to the next dispatch;</li>
<li>Can pause or resume the replies;</li>
<li>Can change all this attributes &#8220;on the fly&#8221; (in Run-Time);</li>
<li>Can join to native Event API of Adobe® Flash® and continue to use all this features;</li>
<li>Can instantiate and control each NResponder without change the others.</li>
</ul>
<p><br/></p>
<p><strong>LEARN TO USE IT</strong><br />
The NResponder engine is full documented and you can check all the docs here:<br />
<a href="http://db-in.com/frameworks/nineveh/doc" target="_blank">http://db-in.com/frameworks/nineveh/doc</a></p>
<p>Below you can watch videos tutorials with the features and real world examples of the usage of NResponder.</p>
<p>Youtube 5 chapters:<br />
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="480" height="385" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/dGQHxya0qeA?fs=1" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="480" height="385" src="http://www.youtube.com/v/dGQHxya0qeA?fs=1" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p><br/></p>
<p><span style="font-size: large; font-weight: bold;">Download</span><br />
You can make the download of sources here:<br />
<a href="http://nineveh-responder.googlecode.com/files/Nineveh-Responder.zip" target="_blank">http://nineveh-responder.googlecode.com/files/Nineveh-Responder.zip</a></p>
<p>Or visit the official code page of the NResponder:<br />
<a href="http://code.google.com/p/nineveh-responder/" target="_blank">http://code.google.com/p/nineveh-responder/</a></p>
<p>Here is the blog page:<br />
<a href="http://db-in.com/blog/nineveh-framework/nresponder/">http://db-in.com/blog/nineveh-framework/nresponder/</a></p>
<p><br/></p>
<p>Remember to give me a feedback if you use it.</p>
<p><br/></p>
<p>See you in the next post.</p>
]]></content:encoded>
			<wfw:commentRss>http://db-in.com/blog/2010/08/nresponder-released-flash-event-engine/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
	</channel>
</rss>

