<?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>iphone.mitchallen.com &#187; C++</title>
	<atom:link href="http://mitchallen.com/iphone/archives/tag/c/feed" rel="self" type="application/rss+xml" />
	<link>http://mitchallen.com/iphone</link>
	<description>random notes on iPhone development</description>
	<lastBuildDate>Tue, 10 Aug 2010 20:36:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Experimental Database Class</title>
		<link>http://mitchallen.com/iphone/archives/134</link>
		<comments>http://mitchallen.com/iphone/archives/134#comments</comments>
		<pubDate>Fri, 15 May 2009 21:12:54 +0000</pubDate>
		<dc:creator>Mitch  Allen</dc:creator>
				<category><![CDATA[persistence]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[sqlite3]]></category>

		<guid isPermaLink="false">http://mitchallen.com/iphone/?p=134</guid>
		<description><![CDATA[This is an incomplete experiment creating a class to manage an sqlite3 database on the iPhone. The place that needs the most work is the mcaDB::queryDB() method. The plan is to eventually have this method populate an array or list that I can access after the query is finished. There are methods in PHP that [...]]]></description>
			<content:encoded><![CDATA[<p>This is an incomplete experiment creating a class to manage an sqlite3 database on the iPhone.</p>
<p>The place that needs the most work is the mcaDB::<strong>queryDB</strong>() method. The plan is to eventually have this method populate an array or list that I can access after the query is finished. There are methods in PHP that do similar things.</p>
<p>What got me started was Chapter 11: Basic Data Persistence in <a href="http://mitchallen.com/iphone/archives/119">Beginning iPhone Development</a>.</p>
<p>You can find documentation and the API reference for SQLite here: <a href="http://www.sqlite.org/">http://www.sqlite.org</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">/*
 *  mcaDB.h
 *  mcaDB1
 *
 *  Created by Mitchell Allen on 5/15/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/*
 *  Instructions for linking to sqlite3 library
 *
 *  In Xcode:
 *
 *      Click on folder: Groups &amp;amp; File / Frameworks
 *      Select menu: Project / Add to Project ...
 *      Navigate to:  /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator[version].sdk/usr/lib/
 *      Select file: libsqlite3.dylib
 *      Uncheck: Copy items into destination groups folder if needed
 *      Change Reference Type to: Relative to Current SDK
 */</span>
&nbsp;
<span style="color: #6e371a;">#include &quot;/usr/include/sqlite3.h&quot;</span>
&nbsp;
<span style="color: #6e371a;">#include &quot;mcaUtils.h&quot;</span>
&nbsp;
class mcaDB <span style="color: #002200;">&#123;</span>
&nbsp;
protected<span style="color: #002200;">:</span>
&nbsp;
	sqlite3	<span style="color: #002200;">*</span>m_database;
&nbsp;
	bool m_open;
&nbsp;
public<span style="color: #002200;">:</span>
&nbsp;
	<span style="color: #a61390;">void</span> errorDB<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	bool openDB<span style="color: #002200;">&#40;</span> <span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span>szDB <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">void</span> closeDB<span style="color: #002200;">&#40;</span> bool bForce <span style="color: #002200;">&#41;</span>;
&nbsp;
	bool execDB<span style="color: #002200;">&#40;</span> <span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span>szSQL <span style="color: #002200;">&#41;</span>;
&nbsp;
	bool queryDB<span style="color: #002200;">&#40;</span> <span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span>szSQL <span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span>;</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">/*
 *  mcaDB.mm
 *  mcaDB1
 *
 *  Created by Mitchell Allen on 5/15/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */</span>
&nbsp;
<span style="color: #6e371a;">#include &quot;mcaDB.h&quot;</span>
&nbsp;
<span style="color: #a61390;">void</span> mcaDB<span style="color: #002200;">::</span>errorDB<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	mcaUtils_logString<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ERROR&quot;</span>, sqlite3_errmsg<span style="color: #002200;">&#40;</span> m_database <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
bool mcaDB<span style="color: #002200;">::</span>openDB<span style="color: #002200;">&#40;</span> <span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span>szDB <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;mcaDB::openDB( ... )&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
	mcaUtils_logString<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;DATABASE&quot;</span>, szDB <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> sqlite3_open<span style="color: #002200;">&#40;</span> szDB, <span style="color: #002200;">&amp;</span>amp;m_database <span style="color: #002200;">&#41;</span> <span style="color: #002200;">!=</span> SQLITE_OK <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
		errorDB<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
		closeDB<span style="color: #002200;">&#40;</span> <span style="color: #a61390;">true</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #a61390;">return</span> <span style="color: #a61390;">false</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	m_open <span style="color: #002200;">=</span> <span style="color: #a61390;">true</span>;
&nbsp;
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">true</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">void</span> mcaDB<span style="color: #002200;">::</span>closeDB<span style="color: #002200;">&#40;</span> bool bForce <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;mcaDB::closeDB( ... )&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> m_open || bForce <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
		sqlite3_close<span style="color: #002200;">&#40;</span> m_database <span style="color: #002200;">&#41;</span>;
&nbsp;
		m_open <span style="color: #002200;">=</span> <span style="color: #a61390;">false</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
bool mcaDB<span style="color: #002200;">::</span>execDB<span style="color: #002200;">&#40;</span> <span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span>szQuery <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;mcaDB::execDB( ... )&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
	mcaUtils_logString<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;QUERY&quot;</span>, szQuery <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">!</span> m_open <span style="color: #002200;">&#41;</span> <span style="color: #a61390;">return</span> <span style="color: #a61390;">false</span>;	
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> sqlite3_exec<span style="color: #002200;">&#40;</span> m_database, szQuery, <span style="color: #a61390;">NULL</span>, <span style="color: #a61390;">NULL</span>, <span style="color: #a61390;">NULL</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">!=</span> SQLITE_OK <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
		errorDB<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #a61390;">return</span> <span style="color: #a61390;">false</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">true</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
bool mcaDB<span style="color: #002200;">::</span>queryDB<span style="color: #002200;">&#40;</span> <span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span>szSQL <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;mcaDB::queryDB( ... )&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
	mcaUtils_logString<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;QUERY&quot;</span>, szSQL <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">!</span> m_open <span style="color: #002200;">&#41;</span> <span style="color: #a61390;">return</span> <span style="color: #a61390;">false</span>;
&nbsp;
	sqlite3_stmt	<span style="color: #002200;">*</span>stmt;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> sqlite3_prepare_v2<span style="color: #002200;">&#40;</span> m_database, szSQL, <span style="color: #002200;">-</span><span style="color: #2400d9;">1</span>, <span style="color: #002200;">&amp;</span>amp;stmt, <span style="color: #a61390;">nil</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">==</span> SQLITE_OK<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
		<span style="color: #a61390;">while</span><span style="color: #002200;">&#40;</span> sqlite3_step<span style="color: #002200;">&#40;</span> stmt <span style="color: #002200;">&#41;</span> <span style="color: #002200;">==</span> SQLITE_ROW <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
			NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot; ... looping ... &quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
			<span style="color: #a61390;">int</span> iCols <span style="color: #002200;">=</span> sqlite3_column_count<span style="color: #002200;">&#40;</span> stmt <span style="color: #002200;">&#41;</span>;
&nbsp;
			<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> <span style="color: #a61390;">int</span> i <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; i <span style="color: #002200;">&amp;</span>lt; iCols; i<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
				<span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span>colName <span style="color: #002200;">=</span> sqlite3_column_name<span style="color: #002200;">&#40;</span> stmt, i <span style="color: #002200;">&#41;</span>;
&nbsp;
				mcaUtils_logString<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;COLUMN&quot;</span>, colName <span style="color: #002200;">&#41;</span>;
&nbsp;
				<span style="color: #a61390;">long</span> colType <span style="color: #002200;">=</span> sqlite3_column_type<span style="color: #002200;">&#40;</span> stmt, i <span style="color: #002200;">&#41;</span>;
&nbsp;
				<span style="color: #a61390;">switch</span><span style="color: #002200;">&#40;</span> colType <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
					<span style="color: #a61390;">case</span> SQLITE_INTEGER<span style="color: #002200;">:</span>
&nbsp;
						<span style="color: #a61390;">int</span> fldInt <span style="color: #002200;">=</span> sqlite3_column_int<span style="color: #002200;">&#40;</span> stmt, i <span style="color: #002200;">&#41;</span>;
						mcaUtils_logInteger<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;INTEGER&quot;</span>, fldInt <span style="color: #002200;">&#41;</span>;
&nbsp;
						<span style="color: #a61390;">break</span>;
					<span style="color: #a61390;">case</span> SQLITE_FLOAT<span style="color: #002200;">:</span>
&nbsp;
						NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;FLOAT&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
						<span style="color: #a61390;">float</span> fldFloat <span style="color: #002200;">=</span> sqlite3_column_double<span style="color: #002200;">&#40;</span> stmt, i <span style="color: #002200;">&#41;</span>;
						<span style="color: #a61390;">break</span>;
					<span style="color: #a61390;">case</span> SQLITE_TEXT<span style="color: #002200;">:</span>
&nbsp;
						<span style="color: #a61390;">const</span> <span style="color: #a61390;">unsigned</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span> fldStr <span style="color: #002200;">=</span> sqlite3_column_text<span style="color: #002200;">&#40;</span> stmt, i <span style="color: #002200;">&#41;</span>;
						mcaUtils_logString<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;TEXT&quot;</span>, <span style="color: #002200;">&#40;</span><span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> fldStr <span style="color: #002200;">&#41;</span>;
&nbsp;
						<span style="color: #a61390;">break</span>;
					<span style="color: #a61390;">case</span> SQLITE_BLOB<span style="color: #002200;">:</span>
&nbsp;
						<span style="color: #a61390;">int</span> fldSize <span style="color: #002200;">=</span> sqlite3_column_bytes<span style="color: #002200;">&#40;</span> stmt, i <span style="color: #002200;">&#41;</span>;
						mcaUtils_logInteger<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;BLOB (size)&quot;</span>, fldSize <span style="color: #002200;">&#41;</span>;
						<span style="color: #a61390;">break</span>;
					<span style="color: #a61390;">case</span> SQLITE_NULL<span style="color: #002200;">:</span>
						<span style="color: #a61390;">break</span>;
					<span style="color: #a61390;">default</span><span style="color: #002200;">:</span>
						<span style="color: #a61390;">break</span>;
				<span style="color: #002200;">&#125;</span>
			<span style="color: #002200;">&#125;</span>
&nbsp;
		<span style="color: #002200;">&#125;</span>
&nbsp;
		sqlite3_finalize<span style="color: #002200;">&#40;</span>stmt<span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">true</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p><strong>Utility Class</strong></p>
<p>This is just something I whipped up to make it easy to log info in the class above while I debug. This will probably change.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">/*
 *  mcaUtils.h
 *  mcaDB1
 *
 *  Created by Mitchell Allen on 5/15/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */</span>
&nbsp;
<span style="color: #a61390;">void</span> mcaUtils_logString<span style="color: #002200;">&#40;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>szLabel, <span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span> str <span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">void</span> mcaUtils_logInteger<span style="color: #002200;">&#40;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span> szLabel, <span style="color: #a61390;">int</span> i <span style="color: #002200;">&#41;</span>;</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">/*
 *  mcaUtils.mm
 *  mcaDB1
 *
 *  Created by Mitchell Allen on 5/15/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */</span>
&nbsp;
<span style="color: #6e371a;">#include &quot;mcaUtils.h&quot;</span>
&nbsp;
<span style="color: #a61390;">void</span> mcaUtils_logString<span style="color: #002200;">&#40;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span> szLabel, <span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span> str <span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>szTemp <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> alloc<span style="color: #002200;">&#93;</span> initWithUTF8String<span style="color: #002200;">:</span>str<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>szMessage <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> alloc<span style="color: #002200;">&#93;</span> initWithFormat<span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@ = %@&quot;</span>, szLabel, szTemp <span style="color: #002200;">&#93;</span>;
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span> szMessage <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>szTemp release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>szMessage release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">void</span> mcaUtils_logInteger<span style="color: #002200;">&#40;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span> szLabel, <span style="color: #a61390;">int</span> i <span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>szMessage <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> alloc<span style="color: #002200;">&#93;</span> initWithFormat<span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@ = %d&quot;</span>, szLabel, i <span style="color: #002200;">&#93;</span>;
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span> szMessage <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>szMessage release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p><strong>Sample Usage</strong></p>
<p>This could go into say a viewController class file.</p>
<p>Define the name of your database.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#define TEST_DB_FILE	@&quot;/test.db1&quot;</span></pre></div></div>

<p>Build a complete path to your database file.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> dataFilePath;
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>paths <span style="color: #002200;">=</span> NSSearchPathForDirectoriesInDomains<span style="color: #002200;">&#40;</span>NSDocumentDirectory, NSUserDomainMask, <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>documentsDirectory <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>paths objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>documentsDirectory stringByAppendingString<span style="color: #002200;">:</span>TEST_DB_FILE<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Pass the full path of your database to an object &#8211; in say your view controllers <strong>viewDidLoad</strong> method.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">    m_table.initDB<span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self dataFilePath<span style="color: #002200;">&#93;</span> UTF8String<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>This is how you might define the method above. Yes, in a production environment the final method should always close the database before returning.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">void</span> testTable<span style="color: #002200;">::</span>initDB<span style="color: #002200;">&#40;</span> <span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span>szDB <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;START: testTable::initDB( ... )&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">!</span> m_db.openDB<span style="color: #002200;">&#40;</span> szDB <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
		NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot; ... ERROR opening database&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #a61390;">return</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">!</span> m_db.execDB<span style="color: #002200;">&#40;</span>  <span style="color: #bf1d1a;">&quot;DROP TABLE TEST_TABLE;&quot;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Since this is just a test database - we drop the table every time to start fresh.</span>
&nbsp;
		NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot; ... Couldn't drop table ... ignoring &quot;</span> <span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">!</span> m_db.execDB<span style="color: #002200;">&#40;</span>  <span style="color: #bf1d1a;">&quot;CREATE TABLE IF NOT EXISTS TEST_TABLE (ROW INTEGER PRIMARY KEY, PLANET TEXT);&quot;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
		NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot; ... ERROR creating table&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #a61390;">return</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">!</span> m_db.execDB<span style="color: #002200;">&#40;</span>  <span style="color: #bf1d1a;">&quot;INSERT OR REPLACE INTO TEST_TABLE (PLANET) VALUES ('Mercury');&quot;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
		NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot; ... ERROR inserting row&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #a61390;">return</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">!</span> m_db.execDB<span style="color: #002200;">&#40;</span>  <span style="color: #bf1d1a;">&quot;INSERT OR REPLACE INTO TEST_TABLE (PLANET) VALUES ('Venus');&quot;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
		NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot; ... ERROR inserting row&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #a61390;">return</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">!</span> m_db.queryDB<span style="color: #002200;">&#40;</span>  <span style="color: #bf1d1a;">&quot;SELECT COUNT(*) AS myCount FROM TEST_TABLE;&quot;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
		NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot; ... ERROR querying DB&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #a61390;">return</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">!</span> m_db.queryDB<span style="color: #002200;">&#40;</span>  <span style="color: #bf1d1a;">&quot;SELECT * FROM TEST_TABLE;&quot;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
		NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot; ... ERROR querying DB&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #a61390;">return</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
        m_db.closeDB<span style="color: #002200;">&#40;</span> <span style="color: #a61390;">false</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;END: testTable::initDB( ... )&quot;</span> <span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p><strong>Log Output</strong></p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">mcaDB1[3138:20b] START: testTable::initDB( ... )
mcaDB1[3138:20b] mcaDB::openDB( ... )
mcaDB1[3138:20b] DATABASE = /Users/apple/Library/Application Support/iPhone Simulator/User/Applications/.../Documents/test.db1
mcaDB1[3138:20b] mcaDB::execDB( ... )
mcaDB1[3138:20b] QUERY = DROP TABLE TEST_TABLE;
mcaDB1[3138:20b] mcaDB::execDB( ... )
mcaDB1[3138:20b] QUERY = CREATE TABLE IF NOT EXISTS TEST_TABLE (ROW INTEGER PRIMARY KEY, PLANET TEXT);
mcaDB1[3138:20b] mcaDB::execDB( ... )
mcaDB1[3138:20b] QUERY = INSERT OR REPLACE INTO TEST_TABLE (PLANET) VALUES ('Mercury');
mcaDB1[3138:20b] mcaDB::execDB( ... )
mcaDB1[3138:20b] QUERY = INSERT OR REPLACE INTO TEST_TABLE (PLANET) VALUES ('Venus');
mcaDB1[3138:20b] mcaDB::queryDB( ... )
mcaDB1[3138:20b] QUERY = SELECT COUNT(*) AS myCount FROM TEST_TABLE;
mcaDB1[3138:20b]  ... looping ...
mcaDB1[3138:20b] COLUMN = myCount
mcaDB1[3138:20b] INTEGER = 2
mcaDB1[3138:20b] mcaDB::queryDB( ... )
mcaDB1[3138:20b] QUERY = SELECT * FROM TEST_TABLE;
mcaDB1[3138:20b]  ... looping ...
mcaDB1[3138:20b] COLUMN = ROW
mcaDB1[3138:20b] INTEGER = 1
mcaDB1[3138:20b] COLUMN = PLANET
mcaDB1[3138:20b] TEXT = Mercury
mcaDB1[3138:20b]  ... looping ...
mcaDB1[3138:20b] COLUMN = ROW
mcaDB1[3138:20b] INTEGER = 2
mcaDB1[3138:20b] COLUMN = PLANET
mcaDB1[3138:20b] TEXT = Venus
mcaDB1[3138:20b] mcaDB::closeDB( ... )
mcaDB1[3138:20b] END: testTable::initDB( ... )</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://mitchallen.com/iphone/archives/134/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Creating an OpenGL ES project for the iPhone</title>
		<link>http://mitchallen.com/iphone/archives/100</link>
		<comments>http://mitchallen.com/iphone/archives/100#comments</comments>
		<pubDate>Wed, 06 May 2009 14:14:11 +0000</pubDate>
		<dc:creator>Mitch  Allen</dc:creator>
				<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://mitchallen.com/iphone/?p=100</guid>
		<description><![CDATA[Before I post anymore code, I figured now would be a good time to backup and show you how to create an OpenGL ES project for the iPhone. Creating an OpenGL ES project is very simple. Xcode has a wizard for that which will be the starting point. What I&#8217;m going to do is show [...]]]></description>
			<content:encoded><![CDATA[<p>Before I post anymore code, I figured now would be a good time to backup and show you how to create an OpenGL ES project for the iPhone.</p>
<p>Creating an OpenGL ES project is very simple.  Xcode has a wizard for that which will be the starting point.  What I&#8217;m going to do is show you how to tweak it a bit so that you can do most of your programming in C++.</p>
<p><strong>Step 1: Create an OpenGL ES iPhone Project</strong></p>
<ol>
<li><span style="font-weight: normal;">Launch Xcode</span></li>
<li><span style="font-weight: normal;">Under iPhone OS click: Application</span></li>
<li><span style="font-weight: normal;">Click: OpenGL ES Application</span></li>
<li><span style="font-weight: normal;">Click: Choose &#8230;</span></li>
<li><span style="font-weight: normal;">For Save As change to:  MyWorld1</span></li>
<li><span style="font-weight: normal;">Click: Save</span></li>
</ol>
<p><strong>Step 2: Prepare for C++</strong></p>
<ol>
<li>Under Groups &amp; Files expand the Classes folder</li>
<li>Rename EAGLView.m to EAGLView.mm &#8211; see my post on <a href="http://mitchallen.com/iphone/archives/64">Mixing C++ and Objective-C</a> if you don&#8217;t understand why.</li>
<li>Rename MyWorld1AppDelegate.m to MyWorld1AppDelegate.mm</li>
</ol>
<p><strong>Step 3: Add a C++ class for your world</strong></p>
<ol>
<li>Right click on the Classes folder and select Add / New File &#8230;</li>
<li>Under Mac OS X click C and C++</li>
<li>Click on the C++ File icon</li>
<li>Click: Next</li>
<li>Highlight the whole filename and change it to MyWorld1.mm</li>
<li>Make sure Also create &#8220;MyWorld1.h&#8221; is checked</li>
<li>Click: Finish</li>
</ol>
<p><strong>Step 4: Modify MyWorld1.h</strong></p>
<p>Change the contents of MyWorld1.h to the following:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">/*
 *  MyWorld1.h
 *  MyWorld1
 *
 *  Created by Mitchell Allen on 5/6/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */</span>
&nbsp;
<span style="color: #6e371a;">#ifndef  __MYWORLD1__</span>
<span style="color: #6e371a;">#define  __MYWORLD1__</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//  OpenGL ES includes for the iPhone only</span>
<span style="color: #6e371a;">#ifdef TARGET_OS_IPHONE</span>
<span style="color: #6e371a;">#import &lt;OpenGLES/ES1/gl.h&gt;</span>
<span style="color: #6e371a;">#import &lt;OpenGLES/ES1/glext.h&gt;</span>
&nbsp;
<span style="color: #6e371a;">#define SCREEN_X	480</span>
<span style="color: #6e371a;">#define SCREEN_Y	320 </span>
&nbsp;
<span style="color: #6e371a;">#endif</span>
&nbsp;
class MyWorld1 <span style="color: #002200;">&#123;</span>
&nbsp;
public<span style="color: #002200;">:</span>
&nbsp;
	<span style="color: #a61390;">void</span> setupGL<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">void</span> tearDownGL<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">void</span> resizeGL<span style="color: #002200;">&#40;</span> <span style="color: #a61390;">int</span> iWidth, <span style="color: #a61390;">int</span> iHeight <span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">void</span> paintGL<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">void</span> animate<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">void</span> clickStart<span style="color: #002200;">&#40;</span> <span style="color: #a61390;">int</span> x, <span style="color: #a61390;">int</span> y <span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">void</span> clickStop<span style="color: #002200;">&#40;</span> <span style="color: #a61390;">int</span> x, <span style="color: #a61390;">int</span> y <span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">void</span> clickDrag<span style="color: #002200;">&#40;</span> <span style="color: #a61390;">int</span> x, <span style="color: #a61390;">int</span> y <span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">void</span> clickCancel<span style="color: #002200;">&#40;</span> <span style="color: #a61390;">int</span> x, <span style="color: #a61390;">int</span> y <span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span>;
&nbsp;
<span style="color: #6e371a;">#endif</span></pre></div></div>

<p><strong>Step 5: Modify MyWorld1.mm</strong></p>
<p>Change the contents of MyWorld1.mm to the following:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">/*
 *  MyWorld1.mm
 *  MyWorld1
 *
 *  Created by Mitchell Allen on 5/6/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */</span>
&nbsp;
<span style="color: #6e371a;">#include &quot;MyWorld1.h&quot;</span>
&nbsp;
<span style="color: #a61390;">void</span> MyWorld1<span style="color: #002200;">::</span>setupGL<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;MyWorld1View::setupGL()&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Put your OpenGL ES setup code here.</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">void</span> MyWorld1<span style="color: #002200;">::</span>tearDownGL<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;MyWorld1View::tearDownGL()&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Put your OpenGL ES teardown code here.</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">void</span> MyWorld1<span style="color: #002200;">::</span>resizeGL<span style="color: #002200;">&#40;</span> <span style="color: #a61390;">int</span> w, <span style="color: #a61390;">int</span> h <span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// Put your OpenGL ES resize code here.</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">void</span> MyWorld1<span style="color: #002200;">::</span>paintGL<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// Put your OpenGL ES paint code here.</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">void</span> MyWorld1<span style="color: #002200;">::</span>animate<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// Put your OpenGL ES animation code here.</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">void</span> MyWorld1<span style="color: #002200;">::</span>clickStart<span style="color: #002200;">&#40;</span> <span style="color: #a61390;">int</span> x, <span style="color: #a61390;">int</span> y <span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;MyWorld1View::clickStart( %i, %i )&quot;</span>, x, y <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Handle start of user touching the screen</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">void</span> MyWorld1<span style="color: #002200;">::</span>clickStop<span style="color: #002200;">&#40;</span> <span style="color: #a61390;">int</span> x, <span style="color: #a61390;">int</span> y <span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// On iPhone - will not get called if clickCancel is fired.</span>
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;MyWorld1View::clickStop( %i, %i )&quot;</span>, x, y <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Handle end of user touching the screen</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">void</span> MyWorld1<span style="color: #002200;">::</span>clickDrag<span style="color: #002200;">&#40;</span> <span style="color: #a61390;">int</span> x, <span style="color: #a61390;">int</span> y <span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;MyWorld1View::clickDrag( %i, %i )&quot;</span>, x, y <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Handle user dragging finger across the screen</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">void</span> MyWorld1<span style="color: #002200;">::</span>clickCancel<span style="color: #002200;">&#40;</span> <span style="color: #a61390;">int</span> x, <span style="color: #a61390;">int</span> y <span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// On iPhone, will be called instead of clickStop - like when phone rings.</span>
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;MyWorld1View::clickCancel( %i, %i )&quot;</span>, x, y <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Handle app being stopped by something like the phone ringing on an iPhone.</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p><strong>Step 6: Modify EAGLView.h</strong></p>
<p>Change the contents of EAGLView.h to the following. I&#8217;ve put my initials: MCA &#8211; near code I added or changed:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  EAGLView.h</span>
<span style="color: #11740a; font-style: italic;">//  MyWorld1</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  Created by Mitchell Allen on 5/6/09.</span>
<span style="color: #11740a; font-style: italic;">//  Copyright __MyCompanyName__ 2009. All rights reserved.</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
&nbsp;
<span style="color: #6e371a;">#import &lt;UIKit/UIKit.h&gt;</span>
<span style="color: #6e371a;">#import &lt;OpenGLES/EAGL.h&gt;</span>
<span style="color: #6e371a;">#import &lt;OpenGLES/ES1/gl.h&gt;</span>
<span style="color: #6e371a;">#import &lt;OpenGLES/ES1/glext.h&gt;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// MCA - Update</span>
<span style="color: #6e371a;">#include &quot;MyWorld1.h&quot;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/*
This class wraps the CAEAGLLayer from CoreAnimation into a convenient UIView subclass.
The view content is basically an EAGL surface you render your OpenGL scene into.
Note that setting the view non-opaque will only work if the EAGL surface has an alpha channel.
*/</span>
<span style="color: #a61390;">@interface</span> EAGLView <span style="color: #002200;">:</span> UIView <span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #a61390;">@private</span>
    <span style="color: #11740a; font-style: italic;">/* The pixel dimensions of the backbuffer */</span>
    GLint backingWidth;
    GLint backingHeight;
&nbsp;
    EAGLContext <span style="color: #002200;">*</span>context;
&nbsp;
    <span style="color: #11740a; font-style: italic;">/* OpenGL names for the renderbuffer and framebuffers used to render to this view */</span>
    GLuint viewRenderbuffer, viewFramebuffer;
&nbsp;
    <span style="color: #11740a; font-style: italic;">/* OpenGL name for the depth buffer that is attached to viewFramebuffer, if it exists (0 if it does not exist) */</span>
    GLuint depthRenderbuffer;
&nbsp;
    <span style="color: #400080;">NSTimer</span> <span style="color: #002200;">*</span>animationTimer;
    NSTimeInterval animationInterval;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// MCA</span>
	MyWorld1	m_world;	<span style="color: #11740a; font-style: italic;">// C++ object</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> NSTimeInterval animationInterval;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>startAnimation;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>stopAnimation;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>drawView;
&nbsp;
<span style="color: #11740a; font-style: italic;">// MCA</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>touchesBegan<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>touches withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> event;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>touchesCancelled<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>touches withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>touchesEnded<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>touches withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>touchesMoved<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>touches withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p><strong>Step 7: Modify EAGLView.mm</strong></p>
<p>Change the contents of EAGLView.mm to the following. I&#8217;ve put my initials: MCA &#8211; near code I added or changed:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  EAGLView.m</span>
<span style="color: #11740a; font-style: italic;">//  MyWorld1</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  Created by Mitchell Allen on 5/6/09.</span>
<span style="color: #11740a; font-style: italic;">//  Copyright __MyCompanyName__ 2009. All rights reserved.</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #6e371a;">#import &lt;QuartzCore/QuartzCore.h&gt;</span>
<span style="color: #6e371a;">#import &lt;OpenGLES/EAGLDrawable.h&gt;</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;EAGLView.h&quot;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">////////////////////////////////////////////////////</span>
<span style="color: #11740a; font-style: italic;">// MCA - IMPORTANT!  Change from 0 to 1</span>
<span style="color: #6e371a;">#define USE_DEPTH_BUFFER 1</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// A class extension to declare private methods</span>
<span style="color: #a61390;">@interface</span> EAGLView <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> EAGLContext <span style="color: #002200;">*</span>context;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, assign<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSTimer</span> <span style="color: #002200;">*</span>animationTimer;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> createFramebuffer;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> destroyFramebuffer;
&nbsp;
<span style="color: #a61390;">@end</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">@implementation</span> EAGLView
&nbsp;
<span style="color: #a61390;">@synthesize</span> context;
<span style="color: #a61390;">@synthesize</span> animationTimer;
<span style="color: #a61390;">@synthesize</span> animationInterval;
&nbsp;
&nbsp;
<span style="color: #11740a; font-style: italic;">// You must implement this method</span>
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">Class</span><span style="color: #002200;">&#41;</span>layerClass <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>CAEAGLLayer class<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #11740a; font-style: italic;">//The GL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithCoder<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSCoder</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>coder <span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super initWithCoder<span style="color: #002200;">:</span>coder<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// Get the layer</span>
        CAEAGLLayer <span style="color: #002200;">*</span>eaglLayer <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>CAEAGLLayer <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>self.layer;
&nbsp;
        eaglLayer.opaque <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
        eaglLayer.drawableProperties <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span>
                                        <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithBool<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>, kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
        context <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>EAGLContext alloc<span style="color: #002200;">&#93;</span> initWithAPI<span style="color: #002200;">:</span>kEAGLRenderingAPIOpenGLES1<span style="color: #002200;">&#93;</span>;
&nbsp;
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>context || <span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span>EAGLContext setCurrentContext<span style="color: #002200;">:</span>context<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            <span style="color: #002200;">&#91;</span>self release<span style="color: #002200;">&#93;</span>;
            <span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;
        <span style="color: #002200;">&#125;</span>
&nbsp;
		<span style="color: #11740a; font-style: italic;">//////////////////////</span>
		<span style="color: #11740a; font-style: italic;">// MCA</span>
		m_world.setupGL<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
		m_world.resizeGL<span style="color: #002200;">&#40;</span> backingWidth, backingHeight <span style="color: #002200;">&#41;</span>;
		<span style="color: #11740a; font-style: italic;">////////////////////////</span>
&nbsp;
        animationInterval <span style="color: #002200;">=</span> <span style="color: #2400d9;">1.0</span> <span style="color: #002200;">/</span> <span style="color: #2400d9;">60.0</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>drawView <span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #002200;">&#91;</span>EAGLContext setCurrentContext<span style="color: #002200;">:</span>context<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">/////////////////////////</span>
	<span style="color: #11740a; font-style: italic;">// MCA</span>
	m_world.animate<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	m_world.resizeGL<span style="color: #002200;">&#40;</span> backingWidth, backingHeight <span style="color: #002200;">&#41;</span>;
&nbsp;
	m_world.paintGL<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #11740a; font-style: italic;">///////////////////////////</span>
&nbsp;
&nbsp;
    glBindRenderbufferOES<span style="color: #002200;">&#40;</span>GL_RENDERBUFFER_OES, viewRenderbuffer<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#91;</span>context presentRenderbuffer<span style="color: #002200;">:</span>GL_RENDERBUFFER_OES<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>layoutSubviews <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>EAGLContext setCurrentContext<span style="color: #002200;">:</span>context<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>self destroyFramebuffer<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>self createFramebuffer<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>self drawView<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>createFramebuffer <span style="color: #002200;">&#123;</span>
&nbsp;
    glGenFramebuffersOES<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span>, <span style="color: #002200;">&amp;</span>viewFramebuffer<span style="color: #002200;">&#41;</span>;
    glGenRenderbuffersOES<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span>, <span style="color: #002200;">&amp;</span>viewRenderbuffer<span style="color: #002200;">&#41;</span>;
&nbsp;
    glBindFramebufferOES<span style="color: #002200;">&#40;</span>GL_FRAMEBUFFER_OES, viewFramebuffer<span style="color: #002200;">&#41;</span>;
    glBindRenderbufferOES<span style="color: #002200;">&#40;</span>GL_RENDERBUFFER_OES, viewRenderbuffer<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#91;</span>context renderbufferStorage<span style="color: #002200;">:</span>GL_RENDERBUFFER_OES fromDrawable<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CAEAGLLayer<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>self.layer<span style="color: #002200;">&#93;</span>;
    glFramebufferRenderbufferOES<span style="color: #002200;">&#40;</span>GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer<span style="color: #002200;">&#41;</span>;
&nbsp;
    glGetRenderbufferParameterivOES<span style="color: #002200;">&#40;</span>GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, <span style="color: #002200;">&amp;</span>backingWidth<span style="color: #002200;">&#41;</span>;
    glGetRenderbufferParameterivOES<span style="color: #002200;">&#40;</span>GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, <span style="color: #002200;">&amp;</span>backingHeight<span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>USE_DEPTH_BUFFER<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        glGenRenderbuffersOES<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span>, <span style="color: #002200;">&amp;</span>depthRenderbuffer<span style="color: #002200;">&#41;</span>;
        glBindRenderbufferOES<span style="color: #002200;">&#40;</span>GL_RENDERBUFFER_OES, depthRenderbuffer<span style="color: #002200;">&#41;</span>;
        glRenderbufferStorageOES<span style="color: #002200;">&#40;</span>GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight<span style="color: #002200;">&#41;</span>;
        glFramebufferRenderbufferOES<span style="color: #002200;">&#40;</span>GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>glCheckFramebufferStatusOES<span style="color: #002200;">&#40;</span>GL_FRAMEBUFFER_OES<span style="color: #002200;">&#41;</span> <span style="color: #002200;">!=</span> GL_FRAMEBUFFER_COMPLETE_OES<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;failed to make complete framebuffer object %x&quot;</span>, glCheckFramebufferStatusOES<span style="color: #002200;">&#40;</span>GL_FRAMEBUFFER_OES<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
        <span style="color: #a61390;">return</span> <span style="color: #a61390;">NO</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>destroyFramebuffer <span style="color: #002200;">&#123;</span>
&nbsp;
    glDeleteFramebuffersOES<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span>, <span style="color: #002200;">&amp;</span>viewFramebuffer<span style="color: #002200;">&#41;</span>;
    viewFramebuffer <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
    glDeleteRenderbuffersOES<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span>, <span style="color: #002200;">&amp;</span>viewRenderbuffer<span style="color: #002200;">&#41;</span>;
    viewRenderbuffer <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
&nbsp;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>depthRenderbuffer<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        glDeleteRenderbuffersOES<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span>, <span style="color: #002200;">&amp;</span>depthRenderbuffer<span style="color: #002200;">&#41;</span>;
        depthRenderbuffer <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>startAnimation <span style="color: #002200;">&#123;</span>
    self.animationTimer <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSTimer</span> scheduledTimerWithTimeInterval<span style="color: #002200;">:</span>animationInterval target<span style="color: #002200;">:</span>self selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>drawView<span style="color: #002200;">&#41;</span> userInfo<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> repeats<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>stopAnimation <span style="color: #002200;">&#123;</span>
    self.animationTimer <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setAnimationTimer<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSTimer</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newTimer <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>animationTimer invalidate<span style="color: #002200;">&#93;</span>;
    animationTimer <span style="color: #002200;">=</span> newTimer;
<span style="color: #002200;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setAnimationInterval<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSTimeInterval<span style="color: #002200;">&#41;</span>interval <span style="color: #002200;">&#123;</span>
&nbsp;
    animationInterval <span style="color: #002200;">=</span> interval;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>animationTimer<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>self stopAnimation<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>self startAnimation<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc <span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #002200;">&#91;</span>self stopAnimation<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>EAGLContext currentContext<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> context<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>EAGLContext setCurrentContext<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">/////////////////////////</span>
	<span style="color: #11740a; font-style: italic;">// MCA</span>
	m_world.tearDownGL<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>context release<span style="color: #002200;">&#93;</span>;  
    <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// MCA</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>touchesBegan<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>touches withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> event <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// NSLog(@&quot;DEBUG: Touches began&quot; );</span>
&nbsp;
	UITouch <span style="color: #002200;">*</span>touch <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>touches anyObject<span style="color: #002200;">&#93;</span>;
&nbsp;
	CGPoint	pos <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>touch locationInView<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
&nbsp;
	m_world.clickStart<span style="color: #002200;">&#40;</span> pos.x, pos.y <span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>touchesCancelled<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>touches withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// NSLog(@&quot;DEBUG: Touches cancelled&quot;);</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Will be called if something happens - like the phone rings</span>
&nbsp;
	UITouch <span style="color: #002200;">*</span>touch <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>touches anyObject<span style="color: #002200;">&#93;</span>;
&nbsp;
	CGPoint	pos <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>touch locationInView<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
&nbsp;
	m_world.clickCancel<span style="color: #002200;">&#40;</span> pos.x, pos.y <span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>touchesEnded<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>touches withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// NSLog(@&quot;DEBUG: Touches ended&quot;);</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Note that if phone rings, touchesCancelled will fire instead of touchesEnded</span>
&nbsp;
	UITouch <span style="color: #002200;">*</span>touch <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>touches anyObject<span style="color: #002200;">&#93;</span>;
&nbsp;
	CGPoint	pos <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>touch locationInView<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
&nbsp;
	m_world.clickStop<span style="color: #002200;">&#40;</span> pos.x, pos.y <span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>touchesMoved<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>touches withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// NSLog(@&quot;DEBUG: Touches moved&quot; );</span>
&nbsp;
	UITouch <span style="color: #002200;">*</span>touch <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>touches anyObject<span style="color: #002200;">&#93;</span>;
&nbsp;
	CGPoint	pos <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>touch locationInView<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
&nbsp;
	m_world.clickDrag<span style="color: #002200;">&#40;</span> pos.x, pos.y <span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p><strong>Step 8: Click Build and Go</strong></p>
<p>When you run the project you won&#8217;t see much &#8211; because this is an empty shell waiting for you to add your OpenGL code.  If you click on the screen in the simulator, the clicks will appear in the Debugger Console.</p>
]]></content:encoded>
			<wfw:commentRss>http://mitchallen.com/iphone/archives/100/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Refactored Texture Class</title>
		<link>http://mitchallen.com/iphone/archives/91</link>
		<comments>http://mitchallen.com/iphone/archives/91#comments</comments>
		<pubDate>Tue, 05 May 2009 12:43:52 +0000</pubDate>
		<dc:creator>Mitch  Allen</dc:creator>
				<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[texture]]></category>

		<guid isPermaLink="false">http://mitchallen.com/iphone/?p=91</guid>
		<description><![CDATA[From my previous example, I&#8217;ve refactored the texture class. I&#8217;ve moved some of the common code into a new method. /* * mcaTexture.h * * Created by Mitchell Allen on 4/17/09. * Copyright 2009 __MyCompanyName__. All rights reserved. * */ &#160; #ifndef __MCA_TEXTURE__ #define __MCA_TEXTURE__ &#160; #ifdef TARGET_OS_IPHONE #import &#60;OpenGLES/ES1/gl.h&#62; #import &#60;OpenGLES/ES1/glext.h&#62; #endif &#160; class [...]]]></description>
			<content:encoded><![CDATA[<p>From my <a href="http://mitchallen.com/iphone/archives/75">previous example</a>, I&#8217;ve refactored the texture class.  I&#8217;ve moved some of the common code into a new method.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">/*
 *  mcaTexture.h
 *
 *  Created by Mitchell Allen on 4/17/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */</span>
&nbsp;
<span style="color: #6e371a;">#ifndef  __MCA_TEXTURE__</span>
<span style="color: #6e371a;">#define  __MCA_TEXTURE__</span>
&nbsp;
<span style="color: #6e371a;">#ifdef TARGET_OS_IPHONE</span>
<span style="color: #6e371a;">#import &lt;OpenGLES/ES1/gl.h&gt;</span>
<span style="color: #6e371a;">#import &lt;OpenGLES/ES1/glext.h&gt;</span>
<span style="color: #6e371a;">#endif</span>
&nbsp;
class mcaTexture <span style="color: #002200;">&#123;</span>
&nbsp;
protected<span style="color: #002200;">:</span>
&nbsp;
	GLuint m_texture;	<span style="color: #11740a; font-style: italic;">// OpenGL name for the  texture</span>
&nbsp;
	<span style="color: #a61390;">void</span> initTexture<span style="color: #002200;">&#40;</span> <span style="color: #a61390;">size_t</span> w, <span style="color: #a61390;">size_t</span> h, <span style="color: #a61390;">int</span> iType, GLvoid <span style="color: #002200;">*</span>pImage <span style="color: #002200;">&#41;</span>;
&nbsp;
public<span style="color: #002200;">:</span>
&nbsp;
	GLuint getTexture<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">void</span> initWithChecks<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">void</span> initFromImage<span style="color: #002200;">&#40;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>location<span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span>;
&nbsp;
<span style="color: #6e371a;">#endif</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">/*
 *  mcaTexture.mm
 *  mcaGL1
 *
 *  Created by Mitchell Allen on 4/24/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */</span>
&nbsp;
<span style="color: #6e371a;">#include &quot;mcaTexture.h&quot;</span>
&nbsp;
<span style="color: #6e371a;">#define checkImageWidth	64</span>
<span style="color: #6e371a;">#define checkImageHeight 64</span>
<span style="color: #a61390;">static</span> GLubyte checkImage<span style="color: #002200;">&#91;</span> checkImageHeight<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span>checkImageWidth <span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span> <span style="color: #2400d9;">4</span> <span style="color: #002200;">&#93;</span>;
&nbsp;
GLuint mcaTexture<span style="color: #002200;">::</span>getTexture<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> m_texture;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">void</span> mcaTexture<span style="color: #002200;">::</span>initTexture<span style="color: #002200;">&#40;</span> <span style="color: #a61390;">size_t</span> w, <span style="color: #a61390;">size_t</span> h, <span style="color: #a61390;">int</span> iType, GLvoid <span style="color: #002200;">*</span>pImage <span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;START: mcaTexture::initTexture( '...' )&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
	GLint					saveName;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Now use OpenGL ES to generate a name for the texture.</span>
	<span style="color: #11740a; font-style: italic;">// Pass by reference so that our texture variable gets set.</span>
&nbsp;
	glGenTextures<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span>, <span style="color: #002200;">&amp;</span>m_texture<span style="color: #002200;">&#41;</span>;
&nbsp;
	glGetIntegerv<span style="color: #002200;">&#40;</span>GL_TEXTURE_BINDING_2D, <span style="color: #002200;">&amp;</span>saveName<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Bind the texture name. </span>
	glBindTexture<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, m_texture<span style="color: #002200;">&#41;</span>;
&nbsp;
	glTexParameteri<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR<span style="color: #002200;">&#41;</span>;
	glTexParameteri<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR<span style="color: #002200;">&#41;</span>;
	glTexParameteri<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE<span style="color: #002200;">&#41;</span>;
	glTexParameteri<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE<span style="color: #002200;">&#41;</span>;
&nbsp;
	glTexEnvi<span style="color: #002200;">&#40;</span> GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Specify a 2D texture image, providing a pointer to the image data in memory</span>
&nbsp;
	<span style="color: #a61390;">switch</span><span style="color: #002200;">&#40;</span> iType <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">case</span> <span style="color: #2400d9;">1</span><span style="color: #002200;">:</span>
			glTexImage2D<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, <span style="color: #2400d9;">0</span>, GL_RGBA, w, h, <span style="color: #2400d9;">0</span>, GL_RGBA, GL_UNSIGNED_BYTE, pImage<span style="color: #002200;">&#41;</span>;
			<span style="color: #a61390;">break</span>;
		<span style="color: #a61390;">case</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">:</span>
			glTexImage2D<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, <span style="color: #2400d9;">0</span>, GL_RGB, w, h, <span style="color: #2400d9;">0</span>, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, pImage<span style="color: #002200;">&#41;</span>;
			<span style="color: #a61390;">break</span>;
		<span style="color: #a61390;">case</span> <span style="color: #2400d9;">3</span><span style="color: #002200;">:</span>
			glTexImage2D<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, <span style="color: #2400d9;">0</span>, GL_ALPHA, w, h, <span style="color: #2400d9;">0</span>, GL_ALPHA, GL_UNSIGNED_BYTE, pImage<span style="color: #002200;">&#41;</span>;
			<span style="color: #a61390;">break</span>;
		<span style="color: #a61390;">default</span><span style="color: #002200;">:</span>
			<span style="color: #a61390;">break</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	glBindTexture<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, saveName<span style="color: #002200;">&#41;</span>;
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;END: mcaTexture::initTexture()&quot;</span> <span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">void</span> mcaTexture<span style="color: #002200;">::</span>initWithChecks<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> 
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;START: mcaTexture::initWithChecks( '...' )&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">int</span> i, j, c;
&nbsp;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> i <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; i &lt; checkImageHeight; i<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> j <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; j &lt; checkImageWidth; j<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
			c <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>i<span style="color: #002200;">&amp;</span>0x8<span style="color: #002200;">&#41;</span><span style="color: #002200;">==</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">^</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>j<span style="color: #002200;">&amp;</span>0x8<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">==</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">*</span><span style="color: #2400d9;">255</span>;
			checkImage<span style="color: #002200;">&#91;</span>i<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span>j<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>GLubyte<span style="color: #002200;">&#41;</span> c;
			checkImage<span style="color: #002200;">&#91;</span>i<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span>j<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>GLubyte<span style="color: #002200;">&#41;</span> c;
			checkImage<span style="color: #002200;">&#91;</span>i<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span>j<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>GLubyte<span style="color: #002200;">&#41;</span> c;
			checkImage<span style="color: #002200;">&#91;</span>i<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span>j<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span><span style="color: #2400d9;">3</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>GLubyte<span style="color: #002200;">&#41;</span> <span style="color: #2400d9;">255</span>;  <span style="color: #11740a; font-style: italic;">// For ghost effect ( 255 / 2 )</span>
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
&nbsp;
	initTexture<span style="color: #002200;">&#40;</span> checkImageWidth, checkImageHeight, <span style="color: #2400d9;">1</span>, checkImage <span style="color: #002200;">&#41;</span>;
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;END: mcaTexture::initWithChecks( '...' )&quot;</span> <span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">void</span> mcaTexture<span style="color: #002200;">::</span>initFromImage<span style="color: #002200;">&#40;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>location<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;START: mcaTexture::initFromImage( '...' )&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Creates a Core Graphics image from an image file using our location.</span>
	CGImageRef spriteImage <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span>location<span style="color: #002200;">&#93;</span>.CGImage;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Get the width and height of the image.</span>
	<span style="color: #a61390;">size_t</span> w <span style="color: #002200;">=</span> CGImageGetWidth<span style="color: #002200;">&#40;</span>spriteImage<span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">size_t</span> h <span style="color: #002200;">=</span> CGImageGetHeight<span style="color: #002200;">&#40;</span>spriteImage<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">//TODO - resize the width and the height to the nearest power of 2.</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">//Only create a sprite if we were able to properly load the CG image.</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>spriteImage<span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;... image loaded successfully ...&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Allocated memory needed for the bitmap context</span>
		GLubyte <span style="color: #002200;">*</span>spriteData <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>GLubyte <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> <span style="color: #a61390;">malloc</span><span style="color: #002200;">&#40;</span>w <span style="color: #002200;">*</span> h <span style="color: #002200;">*</span> <span style="color: #2400d9;">4</span><span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Use the bitmap creation function provided by the Core Graphics framework. </span>
		CGContextRef spriteContext <span style="color: #002200;">=</span> CGBitmapContextCreate<span style="color: #002200;">&#40;</span>spriteData, w, h, <span style="color: #2400d9;">8</span>, w <span style="color: #002200;">*</span> <span style="color: #2400d9;">4</span>, CGImageGetColorSpace<span style="color: #002200;">&#40;</span>spriteImage<span style="color: #002200;">&#41;</span>, kCGImageAlphaPremultipliedLast<span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// After we create the context, we can draw the sprite image to the context.</span>
		CGContextDrawImage<span style="color: #002200;">&#40;</span>spriteContext, CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0.0</span>, <span style="color: #2400d9;">0.0</span>, <span style="color: #002200;">&#40;</span>CGFloat<span style="color: #002200;">&#41;</span>w, <span style="color: #002200;">&#40;</span>CGFloat<span style="color: #002200;">&#41;</span>h<span style="color: #002200;">&#41;</span>, spriteImage<span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// We don't need the context at this point, so we need to release it to avoid memory leaks.</span>
		CGContextRelease<span style="color: #002200;">&#40;</span>spriteContext<span style="color: #002200;">&#41;</span>;
&nbsp;
		initTexture<span style="color: #002200;">&#40;</span> w, h, <span style="color: #2400d9;">1</span>, spriteData <span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Release the image data, which is now unused.</span>
		<span style="color: #a61390;">free</span><span style="color: #002200;">&#40;</span>spriteData<span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	GLenum	errGL <span style="color: #002200;">=</span> glGetError<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;... glGetError = %1 ...&quot;</span>, errGL <span style="color: #002200;">&#41;</span>;
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;END: mcaTexture::initFromImage( '...' )&quot;</span> <span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://mitchallen.com/iphone/archives/91/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Experimental Texture Class</title>
		<link>http://mitchallen.com/iphone/archives/75</link>
		<comments>http://mitchallen.com/iphone/archives/75#comments</comments>
		<pubDate>Mon, 04 May 2009 12:58:57 +0000</pubDate>
		<dc:creator>Mitch  Allen</dc:creator>
				<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[texture]]></category>

		<guid isPermaLink="false">http://mitchallen.com/iphone/?p=75</guid>
		<description><![CDATA[The code below is my experiment with creating a C++ class for generating and/or loading OpenGL textures for the iPhone (it&#8217;s also an experiment with posting mixed C++ and Objective-C code). Not all graphics will load. It will load the ship.png file from the Apple CrashLanding example. No one codes in a vacuum. A lot [...]]]></description>
			<content:encoded><![CDATA[<p>The code below is my experiment with creating a C++ class for generating and/or loading OpenGL textures for the iPhone (it&#8217;s also an experiment with posting mixed C++ and Objective-C code). Not all graphics will load. It will load the ship.png file from the Apple CrashLanding example.</p>
<p>No one codes in a vacuum. A lot of the code here is from bits I pulled together from Apple sample code as well as the following:</p>
<p><a href="http://www.amazon.com/gp/product/0321498828?ie=UTF8&amp;tag=webmaster.mitchallen-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0321498828">OpenGL(R) SuperBible: Comprehensive Tutorial and Reference (4th Edition)</a><img style="border:none !important; margin:0px !important;" src="http://www.assoc-amazon.com/e/ir?t=webmaster.mitchallen-20&amp;l=as2&amp;o=1&amp;a=0321498828" border="0" alt="" width="1" height="1" /></p>
<p><a href="http://www.amazon.com/gp/product/0321481003?ie=UTF8&amp;tag=webmaster.mitchallen-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0321481003">OpenGL(R) Programming Guide: The Official Guide to Learning OpenGL(R), Version 2.1 (6th Edition)</a><img style="border:none !important; margin:0px !important;" src="http://www.assoc-amazon.com/e/ir?t=webmaster.mitchallen-20&amp;l=as2&amp;o=1&amp;a=0321481003" border="0" alt="" width="1" height="1" /> &#8211; the source of the checkerboard texture code.</p>
<p><a href="http://discussions.apple.com/thread.jspa?messageID=8858657&amp;tstart=0">http://discussions.apple.com/thread.jspa?messageID=8858657&amp;tstart=0</a></p>
<p><a href="http://www.idevgames.com/forum/showthread.php?t=16578">http://www.idevgames.com/forum/showthread.php?t=16578</a></p>
<p><a href="http://andreicostin.com">http://andreicostin.com</a> &#8211; check out his <a href="http://www.youtube.com/watch?v=3KrkU70J4rE">video demo</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">/*
 *  mcaTexture.h
 *
 *  Created by Mitchell Allen on 4/17/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */</span>
&nbsp;
<span style="color: #6e371a;">#ifndef  __MCA_TEXTURE__</span>
<span style="color: #6e371a;">#define  __MCA_TEXTURE__</span>
&nbsp;
<span style="color: #6e371a;">#ifdef TARGET_OS_IPHONE</span>
<span style="color: #6e371a;">#import </span>
<span style="color: #6e371a;">#import </span>
<span style="color: #6e371a;">#endif</span>
&nbsp;
class mcaTexture <span style="color: #002200;">&#123;</span>
&nbsp;
protected<span style="color: #002200;">:</span>
&nbsp;
	GLuint m_texture;	<span style="color: #11740a; font-style: italic;">// OpenGL name for the  texture</span>
&nbsp;
public<span style="color: #002200;">:</span>
&nbsp;
	GLuint getTexture<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">void</span> initWithChecks<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">void</span> initFromImage<span style="color: #002200;">&#40;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>location<span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span>;
&nbsp;
<span style="color: #6e371a;">#endif</span></pre></div></div>

<p>UPDATE: Since the two main methods below have a block of common code &#8211; I refactored it into another method. You can find the updated listing in my next post: <a href="http://mitchallen.com/iphone/archives/91">Refactored Texture Class</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">/*
 *  mcaTexture.mm
 *  mcaGL1
 *
 *  Created by Mitchell Allen on 4/24/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */</span>
&nbsp;
<span style="color: #6e371a;">#include &quot;mcaTexture.h&quot;</span>
&nbsp;
<span style="color: #6e371a;">#define checkImageWidth	64</span>
<span style="color: #6e371a;">#define checkImageHeight 64</span>
<span style="color: #a61390;">static</span> GLubyte checkImage<span style="color: #002200;">&#91;</span> checkImageHeight<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span>checkImageWidth <span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span> <span style="color: #2400d9;">4</span> <span style="color: #002200;">&#93;</span>;
&nbsp;
GLuint mcaTexture<span style="color: #002200;">::</span>getTexture<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> m_texture;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">void</span> mcaTexture<span style="color: #002200;">::</span>initWithChecks<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;START: mcaTexture::initWithChecks( '...' )&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">int</span> i, j, c;
&nbsp;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> i <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; i <span style="color: #002200;">&amp;</span>lt; checkImageHeight; i<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> j <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; j <span style="color: #002200;">&amp;</span>lt; checkImageWidth; j<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
			c <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>i<span style="color: #002200;">&amp;</span>amp;0x8<span style="color: #002200;">&#41;</span><span style="color: #002200;">==</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">^</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>j<span style="color: #002200;">&amp;</span>amp;0x8<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">==</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">*</span><span style="color: #2400d9;">255</span>;
			checkImage<span style="color: #002200;">&#91;</span>i<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span>j<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>GLubyte<span style="color: #002200;">&#41;</span> c;
			checkImage<span style="color: #002200;">&#91;</span>i<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span>j<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>GLubyte<span style="color: #002200;">&#41;</span> c;
			checkImage<span style="color: #002200;">&#91;</span>i<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span>j<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>GLubyte<span style="color: #002200;">&#41;</span> c;
			checkImage<span style="color: #002200;">&#91;</span>i<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span>j<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#91;</span><span style="color: #2400d9;">3</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>GLubyte<span style="color: #002200;">&#41;</span> <span style="color: #2400d9;">255</span>;  <span style="color: #11740a; font-style: italic;">// For ghost effect ( 255 / 2 )</span>
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
&nbsp;
	GLint					saveName;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Now use OpenGL ES to generate a name for the texture.</span>
	<span style="color: #11740a; font-style: italic;">// Pass by reference so that our texture variable gets set.</span>
	glGenTextures<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span>, <span style="color: #002200;">&amp;</span>amp;m_texture<span style="color: #002200;">&#41;</span>;
&nbsp;
	glGetIntegerv<span style="color: #002200;">&#40;</span>GL_TEXTURE_BINDING_2D, <span style="color: #002200;">&amp;</span>amp;saveName<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Bind the texture name.</span>
	glBindTexture<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, m_texture<span style="color: #002200;">&#41;</span>;
&nbsp;
	glTexParameteri<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR<span style="color: #002200;">&#41;</span>;
	glTexParameteri<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR<span style="color: #002200;">&#41;</span>;
	glTexParameteri<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE<span style="color: #002200;">&#41;</span>;
	glTexParameteri<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE<span style="color: #002200;">&#41;</span>;
&nbsp;
	glTexEnvi<span style="color: #002200;">&#40;</span> GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Specify a 2D texture image, providing a pointer to the image data in memory</span>
	glTexImage2D<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, <span style="color: #2400d9;">0</span>, GL_RGBA, checkImageWidth, checkImageHeight, <span style="color: #2400d9;">0</span>, GL_RGBA, GL_UNSIGNED_BYTE, checkImage<span style="color: #002200;">&#41;</span>;
	<span style="color: #11740a; font-style: italic;">// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, spriteData);</span>
	<span style="color: #11740a; font-style: italic;">// glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA, GL_UNSIGNED_BYTE, spriteData);</span>
&nbsp;
	glBindTexture<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, saveName<span style="color: #002200;">&#41;</span>;
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;END: mcaTexture::initWithChecks( '...' )&quot;</span> <span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">void</span> mcaTexture<span style="color: #002200;">::</span>initFromImage<span style="color: #002200;">&#40;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>location<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;START: mcaTexture::initFromImage( '...' )&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Creates a Core Graphics image from an image file using our location.</span>
	CGImageRef spriteImage <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span>location<span style="color: #002200;">&#93;</span>.CGImage;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Get the width and height of the image.</span>
	<span style="color: #a61390;">size_t</span> w <span style="color: #002200;">=</span> CGImageGetWidth<span style="color: #002200;">&#40;</span>spriteImage<span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">size_t</span> h <span style="color: #002200;">=</span> CGImageGetHeight<span style="color: #002200;">&#40;</span>spriteImage<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">//TODO - resize the width and the height to the nearest power of 2.</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">//Only create a sprite if we were able to properly load the CG image.</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>spriteImage<span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;... image loaded successfully ...&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Allocated memory needed for the bitmap context</span>
		GLubyte <span style="color: #002200;">*</span>spriteData <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>GLubyte <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> <span style="color: #a61390;">malloc</span><span style="color: #002200;">&#40;</span>w <span style="color: #002200;">*</span> h <span style="color: #002200;">*</span> <span style="color: #2400d9;">4</span><span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Use the bitmap creation function provided by the Core Graphics framework.</span>
		CGContextRef spriteContext <span style="color: #002200;">=</span> CGBitmapContextCreate<span style="color: #002200;">&#40;</span>spriteData, w, h, <span style="color: #2400d9;">8</span>, w <span style="color: #002200;">*</span> <span style="color: #2400d9;">4</span>, CGImageGetColorSpace<span style="color: #002200;">&#40;</span>spriteImage<span style="color: #002200;">&#41;</span>, kCGImageAlphaPremultipliedLast<span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// After we create the context, we can draw the sprite image to the context.</span>
		CGContextDrawImage<span style="color: #002200;">&#40;</span>spriteContext, CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0.0</span>, <span style="color: #2400d9;">0.0</span>, <span style="color: #002200;">&#40;</span>CGFloat<span style="color: #002200;">&#41;</span>w, <span style="color: #002200;">&#40;</span>CGFloat<span style="color: #002200;">&#41;</span>h<span style="color: #002200;">&#41;</span>, spriteImage<span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// We don't need the context at this point, so we need to release it to avoid memory leaks.</span>
		CGContextRelease<span style="color: #002200;">&#40;</span>spriteContext<span style="color: #002200;">&#41;</span>;
&nbsp;
		GLint					saveName;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Now use OpenGL ES to generate a name for the texture.</span>
		<span style="color: #11740a; font-style: italic;">// Pass by reference so that our texture variable gets set.</span>
		glGenTextures<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span>, <span style="color: #002200;">&amp;</span>amp;m_texture<span style="color: #002200;">&#41;</span>;
&nbsp;
		glGetIntegerv<span style="color: #002200;">&#40;</span>GL_TEXTURE_BINDING_2D, <span style="color: #002200;">&amp;</span>amp;saveName<span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Bind the texture name.</span>
		glBindTexture<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, m_texture<span style="color: #002200;">&#41;</span>;
&nbsp;
		glTexParameteri<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR<span style="color: #002200;">&#41;</span>;
		glTexParameteri<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR<span style="color: #002200;">&#41;</span>;
		glTexParameteri<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE<span style="color: #002200;">&#41;</span>;
		glTexParameteri<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE<span style="color: #002200;">&#41;</span>;
&nbsp;
		glTexEnvi<span style="color: #002200;">&#40;</span> GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE <span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Specify a 2D texture image, providing a pointer to the image data in memory</span>
		glTexImage2D<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, <span style="color: #2400d9;">0</span>, GL_RGBA, w, h, <span style="color: #2400d9;">0</span>, GL_RGBA, GL_UNSIGNED_BYTE, spriteData<span style="color: #002200;">&#41;</span>;
		<span style="color: #11740a; font-style: italic;">// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, spriteData);</span>
		<span style="color: #11740a; font-style: italic;">// glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA, GL_UNSIGNED_BYTE, spriteData);</span>
&nbsp;
		glBindTexture<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, saveName<span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// glEnable(GL_LIGHTING);</span>
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Release the image data, which is now unused.</span>
		<span style="color: #a61390;">free</span><span style="color: #002200;">&#40;</span>spriteData<span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	GLenum	errGL <span style="color: #002200;">=</span> glGetError<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;... glGetError = %1 ...&quot;</span>, errGL <span style="color: #002200;">&#41;</span>;
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;END: mcaTexture::initFromImage( '...' )&quot;</span> <span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p><strong>Sample Usage</strong></p>
<p>The sample code below is incomplete for brevity. It&#8217;s just meant to give you a general idea of how to use the texture class.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">/*
 *  mcaPlane.mm
 *
 *  Created by Mitchell Allen on 4/17/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */</span>
&nbsp;
<span style="color: #6e371a;">#include &quot;mcaPlane.h&quot;</span>
&nbsp;
<span style="color: #a61390;">void</span> mcaPlane<span style="color: #002200;">::</span>init<span style="color: #002200;">&#40;</span> GLfloat xpos, GLfloat ypos, GLfloat fSize <span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// ...</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">void</span> mcaPlane<span style="color: #002200;">::</span>initWithTexture<span style="color: #002200;">&#40;</span> GLfloat xpos, GLfloat ypos, GLfloat fSize, GLuint gTexture <span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	init<span style="color: #002200;">&#40;</span> xpos, ypos, fSize <span style="color: #002200;">&#41;</span>;
&nbsp;
	m_glTexture <span style="color: #002200;">=</span> gTexture;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">void</span> mcaPlane<span style="color: #002200;">::</span>paint<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">!</span> isVisible<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span> <span style="color: #a61390;">return</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">//Push the matrix so we can keep it as it was previously.</span>
	glPushMatrix<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	glMatrixMode<span style="color: #002200;">&#40;</span>GL_MODELVIEW<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">//Enable 2D textures.</span>
	glEnable<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">//Bind this texture.</span>
	glBindTexture<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, m_glTexture <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Set the texture parameters to use a minifying filter and a linear filer.</span>
	glTexParameteri<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">//Store the coordinates/dimensions from the rectangle.</span>
	CGFloat w <span style="color: #002200;">=</span> m_planeSize;
	CGFloat h <span style="color: #002200;">=</span> m_planeSize;
&nbsp;
	<span style="color: #11740a; font-style: italic;">//Translate the OpenGL context to the center for rotation.</span>
	glTranslatef<span style="color: #002200;">&#40;</span> m_xpos <span style="color: #002200;">+</span> w<span style="color: #002200;">/</span><span style="color: #2400d9;">2</span>, m_ypos <span style="color: #002200;">+</span>h<span style="color: #002200;">/</span><span style="color: #2400d9;">2</span>, 0.0f<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">//Apply the rotation over the axis.</span>
	glRotatef<span style="color: #002200;">&#40;</span>m_spriteRotation, 1.0f, 0.0f, 0.0f<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">//Translate back to the top left corner  for drawing.</span>
	glTranslatef<span style="color: #002200;">&#40;</span><span style="color: #002200;">-</span>w<span style="color: #002200;">/</span><span style="color: #2400d9;">2</span>, <span style="color: #002200;">-</span>h<span style="color: #002200;">/</span><span style="color: #2400d9;">2</span>, 0.0f<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">//Draw to match </span>
&nbsp;
	<span style="color: #a61390;">const</span> GLfloat planeVertices<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">&#123;</span>
&nbsp;
            <span style="color: #11740a; font-style: italic;">// Define the plane face</span>
&nbsp;
            <span style="color: #002200;">-</span><span style="color: #2400d9;">1.0</span>,   <span style="color: #2400d9;">1.0</span>, <span style="color: #2400d9;">0.0</span>,            <span style="color: #11740a; font-style: italic;">// top left</span>
            <span style="color: #002200;">-</span><span style="color: #2400d9;">1.0</span>, <span style="color: #002200;">-</span><span style="color: #2400d9;">1.0</span>, <span style="color: #2400d9;">0.0</span>,            <span style="color: #11740a; font-style: italic;">// bottom left</span>
              <span style="color: #2400d9;">1.0</span>, <span style="color: #002200;">-</span><span style="color: #2400d9;">1.0</span>, <span style="color: #2400d9;">0.0</span>,            <span style="color: #11740a; font-style: italic;">// bottom right</span>
             <span style="color: #2400d9;">1.0</span>,    <span style="color: #2400d9;">1.0</span>, <span style="color: #2400d9;">0.0</span>,            <span style="color: #11740a; font-style: italic;">// top right</span>
       <span style="color: #002200;">&#125;</span>;    
&nbsp;
	<span style="color: #a61390;">const</span> GLshort squareTextureCoords<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">&#123;</span>
&nbsp;
           <span style="color: #11740a; font-style: italic;">// Plane</span>
&nbsp;
           <span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">1</span>,       <span style="color: #11740a; font-style: italic;">// top left</span>
           <span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span>,       <span style="color: #11740a; font-style: italic;">// bottom left</span>
           <span style="color: #2400d9;">1</span>, <span style="color: #2400d9;">0</span>,       <span style="color: #11740a; font-style: italic;">// bottom right</span>
           <span style="color: #2400d9;">1</span>, <span style="color: #2400d9;">1</span>,       <span style="color: #11740a; font-style: italic;">// top right</span>
        <span style="color: #002200;">&#125;</span>;
&nbsp;
	glScalef<span style="color: #002200;">&#40;</span> m_planeSize, m_planeSize, m_planeSize <span style="color: #002200;">&#41;</span>;
&nbsp;
	glRotatef<span style="color: #002200;">&#40;</span> m_xRot, 1.0f, 0.0f, 0.0f <span style="color: #002200;">&#41;</span>;
	glRotatef<span style="color: #002200;">&#40;</span> m_yRot, 0.0f, 1.0f, 0.0f <span style="color: #002200;">&#41;</span>;
	glRotatef<span style="color: #002200;">&#40;</span> m_zRot, 0.0f, 0.0f, 1.0f <span style="color: #002200;">&#41;</span>;
&nbsp;
	glEnableClientState<span style="color: #002200;">&#40;</span>GL_VERTEX_ARRAY<span style="color: #002200;">&#41;</span>;
	glEnableClientState<span style="color: #002200;">&#40;</span>GL_TEXTURE_COORD_ARRAY<span style="color: #002200;">&#41;</span>;
&nbsp;
	glBindTexture<span style="color: #002200;">&#40;</span>GL_TEXTURE_2D, m_glTexture <span style="color: #002200;">&#41;</span>;
&nbsp;
	glVertexPointer<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">3</span>, GL_FLOAT, <span style="color: #2400d9;">0</span>, planeVertices <span style="color: #002200;">&#41;</span>;
	glTexCoordPointer<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">2</span>, GL_SHORT, <span style="color: #2400d9;">0</span>, squareTextureCoords <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Draw the plane in white</span>
	glColor4f<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1.0</span>, <span style="color: #2400d9;">1.0</span>, <span style="color: #2400d9;">1.0</span>, <span style="color: #2400d9;">1.0</span><span style="color: #002200;">&#41;</span>;
        glDrawArrays<span style="color: #002200;">&#40;</span>GL_TRIANGLE_FAN, <span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">4</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	glDisableClientState<span style="color: #002200;">&#40;</span>GL_VERTEX_ARRAY<span style="color: #002200;">&#41;</span>;
	glDisableClientState<span style="color: #002200;">&#40;</span>GL_TEXTURE_COORD_ARRAY<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">//Allow transparency and blending.</span>
	glEnable<span style="color: #002200;">&#40;</span>GL_BLEND<span style="color: #002200;">&#41;</span>;
	glBlendFunc<span style="color: #002200;">&#40;</span>GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">//Restore the model view matrix to prevent contamination.</span>
	glPopMatrix<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">	mcaTexture txChecks, txShip;
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;... initializing textures ...&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
	txChecks.initWithChecks<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
	txShip.initFromImage<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Ship.png&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;... initializing objects ...&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
	testPlane<span style="color: #002200;">&#91;</span> <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#93;</span>.initWithTexture<span style="color: #002200;">&#40;</span>  0.0f, 0.0f, 100.f, txChecks.getTexture<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>;
	testPlane<span style="color: #002200;">&#91;</span> <span style="color: #2400d9;">1</span> <span style="color: #002200;">&#93;</span>.initWithTexture<span style="color: #002200;">&#40;</span> 50.0f, 0.0f, 100.f, txChecks.getTexture<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>;
	testPlane<span style="color: #002200;">&#91;</span> <span style="color: #2400d9;">2</span> <span style="color: #002200;">&#93;</span>.initWithTexture<span style="color: #002200;">&#40;</span>  0.0f, 0.0f, 125.f, txShip.getTexture<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://mitchallen.com/iphone/archives/75/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Mixing C++ and Objective-C</title>
		<link>http://mitchallen.com/iphone/archives/64</link>
		<comments>http://mitchallen.com/iphone/archives/64#comments</comments>
		<pubDate>Fri, 10 Apr 2009 21:07:52 +0000</pubDate>
		<dc:creator>Mitch  Allen</dc:creator>
				<category><![CDATA[newbie]]></category>
		<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://mitchallen.com/iphone/?p=64</guid>
		<description><![CDATA[I&#8217;ve been experimenting with mixing C++ and Objective-C. When you port C++ to the iPhone you will probably want to just add your code to an Objective-C project. Here are some of the issues that may lead to compiler warnings and errors: You need to change the extension of your C++ source files from *.cpp [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been experimenting with mixing C++ and Objective-C. </p>
<p>When you port C++ to the iPhone you will probably want to just add your code to an Objective-C project. </p>
<p>Here are some of the issues that may lead to compiler warnings and errors:</p>
<ul>
<li>You need to change the extension of your C++ source files from *.cpp to *.mm</li>
<li>If you reference your C++ *.h file from a *.m file you will get weird syntax errors around your class definition.  The reason is because ANY file in your project that references a C++ *.h file must end in *.mm.  So if your Objective-C code references a C++ file, change the extension to *.mm.</li>
<li>You will get warnings that Objective-C ignores constructors and destructors.  So you will have to break out that code and call it after defining your object.</li>
<li>You will get a weird syntax error if you forget to put a semi-colon after your class definition ( class MyClass { &#8230; } <strong>;</strong> ).  This fixes the error &#8220;<em>error: new types may not be defined in a return type</em>&#8220;.  Xcode generates the brackets but doesn&#8217;t put the semi-colon in automatically.   So be on the lookout for that. You won&#8217;t get this error until you add a method that returns something.  If you started out with a bunch of void methods &#8211; then wonder why it suddenly broke &#8211; that&#8217;s why.</li>
<li>The virtual keyword isn&#8217;t allowed.  In Objective-C all methods are virtual.  If your derived class has a matching method, it will be called instead of the one in the base class &#8211; as if you did declare it virtual.</li>
</ul>
<p>See also:  <strong><a href="http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocCPlusPlus.html">Using C++ With Objective-C (developer.apple.com)</a></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://mitchallen.com/iphone/archives/64/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
