<?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>Meh Blog! &#187; Programming</title>
	<atom:link href="http://www.codexsoftware.co.uk/blog/category/computers/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codexsoftware.co.uk/blog</link>
	<description>Computer topics and random rants!</description>
	<lastBuildDate>Wed, 30 Jun 2010 21:31:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>How to determine if an NSTextView is scrolled to the end</title>
		<link>http://www.codexsoftware.co.uk/blog/computers/programming/how-to-determine-if-an-nstextview-is-scrolled-to-the-end/</link>
		<comments>http://www.codexsoftware.co.uk/blog/computers/programming/how-to-determine-if-an-nstextview-is-scrolled-to-the-end/#comments</comments>
		<pubDate>Mon, 29 Mar 2010 21:52:33 +0000</pubDate>
		<dc:creator>arcana</dc:creator>
				<category><![CDATA[Mac]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[NSScrollView]]></category>
		<category><![CDATA[NSTextView]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[scroll to end]]></category>
		<category><![CDATA[scrollbar]]></category>
		<category><![CDATA[scrollbar position]]></category>
		<category><![CDATA[scrolling]]></category>

		<guid isPermaLink="false">http://www.codexsoftware.co.uk/blog/?p=269</guid>
		<description><![CDATA[I found a several articles explaining how to scroll an NSTextView to the end, but I didn&#8217;t find any that explained how to do it only if it was already at the end before new text was appended. Consider this scenario.  You have an NSTextView scrolled to the end and as new text appears in [...]]]></description>
			<content:encoded><![CDATA[<p>I found a several articles explaining how to scroll an NSTextView to the end, but I didn&#8217;t find any that explained how to do it only if it was already at the end before new text was appended.</p>
<p>Consider this scenario.  You have an NSTextView scrolled to the end and as new text appears in the window, you want to see the new text immediately, so it scrolls to the very bottom for you.  Now you decide you want to read some text further up, so you scroll the text upwards.  As you&#8217;re reading, new text gets appended and suddenly it all scrolls down to the bottom again, causing you to lose your place.</p>
<p><span id="more-269"></span>We&#8217;re going to assume the NSTextView is named textView.  Imaginative, I know.</p>
<p>Usually we could append some coloured text and scroll to the end with something like this&#8230;</p>
<pre class="brush: objc;">
- (void)writeText:(NSString *)text
{
	// append some blue text
	NSDictionary *attribs = [NSDictionary dictionaryWithObject:[NSColor blueColor] forKey:NSForegroundColorAttributeName];
	NSAttributedString *stringToAppend = [[NSAttributedString alloc] initWithString:text attributes:attribs];
	[[textView textStorage] appendAttributedString:stringToAppend];
	[stringToAppend release];

	// scroll to the end
	NSRange range = NSMakeRange ([[textView string] length], 0);
	[textView scrollRangeToVisible: range];
}
</pre>
<p>Assuming you&#8217;ve just dropped the NSTextView on to a window or view from Interface Builder, the NSTextView will have been placed inside of an NSScrollView.  That NSScrollView can be queried to determine if the scrollbar was already at the bottom.  You should do this before you append any text.</p>
<pre class="brush: objc;">
	bool scrollToEnd = YES;

	id scrollView = (NSScrollView *)textView.superview.superview;
	if ([scrollView isKindOfClass:[NSScrollView class]]) {
		if ([scrollView hasVerticalScroller]) {
			if (textView.frame.size.height &gt; [scrollView frame].size.height) {
				if (1.0f != [scrollView verticalScroller].floatValue)
					scrollToEnd = NO;
			}
		}
	}
</pre>
<p>Firstly we default scrollToEnd to YES.  Next we check the NSTextView&#8217;s superview&#8217;s superview to make sure it is actually an NSScrollView.  If it is, and if it has a vertical scrollbar and our NSTextView is large enough to require scrolling, then we look at how far the scroll bar has moved.  This can be done by looking at its floatValue.</p>
<p>The floatValue can range from 0.0 to 1.0.  If a scrollbar is at the very bottom (or all the way over to the right if it&#8217;s horizontal) then it&#8217;s floatValue will be 1.0.</p>
<p>Here&#8217;s the complete code.</p>
<pre class="brush: objc;">
- (void)writeText:(NSString *)text
{
	// determine if we should scroll to the end
	bool scrollToEnd = YES;

	id scrollView = (NSScrollView *)textView.superview.superview;
	if ([scrollView isKindOfClass:[NSScrollView class]]) {
		if ([scrollView hasVerticalScroller]) {
			if (textView.frame.size.height &gt; [scrollView frame].size.height) {
				if (1.0f != [scrollView verticalScroller].floatValue)
					scrollToEnd = NO;
			}
		}
	}

	// append some coloured text
	NSDictionary *attribs = [NSDictionary dictionaryWithObject:[NSColor blueColor] forKey:NSForegroundColorAttributeName];
	NSAttributedString *stringToAppend = [[NSAttributedString alloc] initWithString:text attributes:attribs];
	[[textView textStorage] appendAttributedString:stringToAppend];
	[stringToAppend release];

	if (scrollToEnd) {
		// scroll to the end
		NSRange range = NSMakeRange ([[textView string] length], 0);
		[textView scrollRangeToVisible: range];
	}
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.codexsoftware.co.uk/blog/computers/programming/how-to-determine-if-an-nstextview-is-scrolled-to-the-end/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fixing the iTGB 1.2 Memory Leak</title>
		<link>http://www.codexsoftware.co.uk/blog/computers/programming/fixing-the-itgb-1-2-memory-leak/</link>
		<comments>http://www.codexsoftware.co.uk/blog/computers/programming/fixing-the-itgb-1-2-memory-leak/#comments</comments>
		<pubDate>Tue, 01 Sep 2009 15:12:38 +0000</pubDate>
		<dc:creator>arcana</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Garage Games]]></category>
		<category><![CDATA[iTGB]]></category>
		<category><![CDATA[Memory Leak]]></category>
		<category><![CDATA[Torque]]></category>
		<category><![CDATA[Torque 2D for iPhone]]></category>

		<guid isPermaLink="false">http://www.codexsoftware.co.uk/blog/?p=79</guid>
		<description><![CDATA[I recently purchased Torque 2D for the iPhone and came across a large memory leak in their iTGB 1.2 release whenever I ran the game on my iPhone 3G.  I had a very basic platform game running with no engine modifications to the source code at all.  The game would run for about 4 minutes [...]]]></description>
			<content:encoded><![CDATA[<p>I recently purchased Torque 2D for the iPhone and came across a large memory leak in their iTGB 1.2 release whenever I ran the game on my iPhone 3G.  I had a very basic platform game running with no engine modifications to the source code at all.  The game would run for about 4 minutes before running out of memory and exiting.  <span id="more-79"></span>If I opened Organizer in Xcode and looked at the Console tab I&#8217;d see this:</p>
<blockquote><p>Tue Aug 25 21:52:15 unknown SpringBoard[23] : Memory level is urgent (9%) and there are no background apps to ask to exit.<br />
Tue Aug 25 21:53:18 unknown SpringBoard[23] : Application &#8216;MyGame&#8217; exited abnormally with signal 11: Segmentation fault<br />
Tue Aug 25 21:53:19 unknown ReportCrash[4565] : Saved crashreport to /Library/Logs/CrashReporter/LowMemory-2009-08-25-215318.plist using uid: 0 gid: 0, synthetic_euid: 0 egid: 0</p></blockquote>
<p>As I was a complete noob to the engine, I assumed I&#8217;d done something wrong.  I checked my scripts and made sure I wasn&#8217;t allocating memory anywhere and that I wasn&#8217;t doing anything silly like executing scripts within a loop.  Eventually I ran out of ideas and reverted to iTGB 1.1, which didn&#8217;t suffer from the memory leak.</p>
<p>I decided I&#8217;d rather use the latest and greatest offering so I thought I&#8217;d revisit iTGB 1.2. I recompiled TGEGame with <strong>PUAP_OPTIMIZE</strong> and <strong>PUAP_SCRIPT_CHANGE</strong> so the options set are now</p>
<pre class="brush: plain;">
__MACOSX__
TORQUE_DISABLE_MEMORY_MANAGER
TORQUE_PLAYER
TORQUE_RELEASE
PUAP_SCRIPT_CHANGE
PUAP_OPTIMIZE
USE_COMPONENTS
</pre>
<p>I *may* have added the &#8216;R&#8217; on to the end of <strong>TORQUE_DISABLE_MEMORY_MANAGER</strong>. There was a typo in some of the projects. Mostly on release configs but I can&#8217;t remember which ones.</p>
<p>Then I opened the Xcode_iPhone project and switched to the <strong>iTGB_Script_Optimize</strong> target. In release build I made sure these were set:</p>
<pre class="brush: plain;">
__IPHONE__
TORQUE_DISABLE_MEMORY_MANAGER
TORQUE_PLAYER
TORQUE_RELEASE
PUAP_SCRIPT_CHANGE
PUAP_OPTIMIZE
USE_COMPONENTS
</pre>
<p>I&#8217;m pretty sure I added the &#8216;R&#8217; on the end of <strong>TORQUE_DISABLE_MEMORY_MANAGER</strong> this time.</p>
<p>Then I made sure <strong>Compiler</strong><strong> Version</strong> was set to <strong>GCC 4.0</strong> and under <strong>Linking</strong> I changed <strong>C++ Standard Library Type</strong> from <strong>Static</strong> to <strong>Dynamic</strong>.</p>
<p>I also set my <strong>iPhone OS Deployment Target</strong> and <strong>Base SDK</strong> to <strong>iPhone OS 3.0</strong>. You probably don&#8217;t need to do that but I&#8217;m mentioning it anyway. If you&#8217;re having trouble compiling for 3.01 even though you have the latest non-beta iPhone SDK then you may need to create a symbolic link from your Terminal / shell.</p>
<pre class="brush: plain;">
cd /Developer/Platforms/iPhoneOS.platform/DeviceSupport
ln -s 3.0 3.0.1
</pre>
<p>I don&#8217;t have the big memory leak any more <img src='http://www.codexsoftware.co.uk/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.codexsoftware.co.uk/blog/computers/programming/fixing-the-itgb-1-2-memory-leak/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
