<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description>The personal blog of Jamie Dale, a computer games programmer from England. Contains news and information on games, programming and technology as well as general nuggets of information from my somewhat random mind.

Warning May contain random bouts of Englishness and traces of Nuts.</description><title>tweex[dot]net</title><generator>Tumblr (3.0; @jdale88)</generator><link>http://blog.tweex.net/</link><item><title>My experience using CMake (and a tutorial)</title><description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For those of you that don’t know &lt;a target="_blank" href="http://www.cmake.org/"&gt;CMake&lt;/a&gt; is a cross platform build system that allows you to define what you want to build and how, before it goes off and creates the correct build scripts for your particular platform and compiler. It is supposed to help you to simplify your build process when compiling for multiple platforms by just having to maintain a single build script that can generate all other build scripts.&lt;/p&gt;
&lt;p&gt;Generally I never had a use for a such a thing since I did all my work on Windows with Visual Studio however when I started doing work I eventually wanted to use on Linux too, I knew I had to look into something. OGRE had started using CMake to generate its build scripts, so I thought I’d look into that. This turned out to be much harder than I expected considering the documentation for CMake seems to range from poor to non-existent and that the examples they link to are either really quick overviews, or really contrived and complicated.&lt;/p&gt;
&lt;p&gt;Still I persisted, and with the use of the OGRE CMake files as an example, and with the help of &lt;a target="_blank" href="http://www.ogre3d.org/forums/memberlist.php?mode=viewprofile&amp;u=1170"&gt;jacmoe&lt;/a&gt; and &lt;a target="_blank" href="http://www.ogre3d.org/forums/memberlist.php?mode=viewprofile&amp;u=11836"&gt;CABAListic&lt;/a&gt; I managed to get a CMake file that worked. It was ugly, it was all in one CMakeLists.txt file (which you shouldn’t really do) but at least it managed build two of my libraries on Linux. Of course, I was never able to test if these libraries ran on Linux since I never worked out how to link those libraries against an executable (or even how to recurse into my examples directory so I could attempt to build the examples). After 6 months I figured it was time for me to work out how to use CMake “properly”, I made myself a simple test case that represented what I ultimately needed to do, and then tried to build the CMake script to generate the correct build scripts for it.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;
&lt;p&gt;&lt;strong&gt;&lt;!-- more --&gt;&lt;/strong&gt;&lt;/p&gt;
Test Case&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For my test case, I have two projects; one that should build as a static library, and one that should build as an executable linked against the static library. These projects are called “Core” and “Other” respectively and are arranged in a directory structure as shown below.&lt;/p&gt;
&lt;pre&gt;/ - The root SDK level
/Bin - Folder to place the built executables in
/Lib - Folder to place the built libraries in
/Source - Contains the source code for the projects
/Source/Core - Contains the source code for the Core project
/Source/Other - Contains the source code for the Other project&lt;/pre&gt;
&lt;p&gt;Now to use CMake properly, we are going to need four CMakeLists.txt files, three of which will contain actual project information (the SDK level, Core level and Other level ones), and one of which will just include some subdirectories (the Source level one). Now let’s take a look at the contents of each of those CMakeLists.txt files in turn.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;SDK Level CMakeLists.txt&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The root SDK level CMakeLists.txt contains the most code in it since it sets up the environment for the rest of the project, as well as having some functions that are used in the other CMakeLists.txt files to make adding more projects easier and reducing code duplication.&lt;/p&gt;
&lt;pre&gt;project(SDK)
cmake_minimum_required(VERSION 2.6)
set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS TRUE)
message(STATUS "Processing ${PROJECT_NAME}")&lt;/pre&gt;
&lt;p&gt;This first section sets up some basic information. It tells CMake that we are building a project called “SDK”, let’s it know the minimum CMake version this file will work with, and also just displays an informative message when CMake generates this project so we know what’s going on. PROJECT_NAME is a CMake variable that will be set to the name of the current project, there are more of these useful variables and they are listed on &lt;a target="_blank" href="http://www.cmake.org/Wiki/CMake_Useful_Variables"&gt;this wiki page&lt;/a&gt;. The ${ and } around PROJECT_NAME are used to tell CMake that we want to use the actual variable, and not just the string “PROJECT_NAME”.&lt;/p&gt;
&lt;p&gt;The next section defines some helper functions that are used through the CMakeLists.txt files of the other projects to help remove code duplication.&lt;/p&gt;
&lt;pre&gt;# Function to list all header files in the current directory, recursing into sub-directories
# HEADER_FILES - To be filled with the found header files
function(sdk_list_header_files HEADER_FILES)
	file(GLOB_RECURSE HEADER_FILES_TMP "*.h" "*.hpp" "*.inl" "*.pch")
	set(HEADER_FILES ${HEADER_FILES_TMP} PARENT_SCOPE)
endfunction()

# Function to list all source files in the current directory, recursing into sub-directories
# SOURCE_FILES - To be filled with the found source files
function(sdk_list_source_files HEADER_FILES)
	file(GLOB_RECURSE SOURCE_FILES_TMP "*.c" "*.cpp")
	set(SOURCE_FILES ${SOURCE_FILES_TMP} PARENT_SCOPE)
endfunction()

# Function to setup some standard project items
# PROJECTNAME - The name of the project being setup
# TARGETDIR - The target directory for output files (relative to CMAKE_SOURCE_DIR)
function(sdk_setup_project_common PROJECTNAME TARGETDIR)
	# Set the Debug and Release names
	set_target_properties(
		${PROJECTNAME} 
		PROPERTIES 
		DEBUG_OUTPUT_NAME ${PROJECTNAME}_d 
		RELEASE_OUTPUT_NAME ${PROJECTNAME}
		)
	
	# Add a post-build step for MSVC to copy the output to the target directory
	if(MSVC)
		add_custom_command(
			TARGET ${PROJECTNAME} 
			POST_BUILD COMMAND 
			copy \"$(TargetPath)\" \"${CMAKE_SOURCE_DIR}/${TARGETDIR}\"
			)
	endif()

	# Setup install to copy the built output to the target directory 
	# (for compilers that don't have post build steps)
	install(
		TARGETS ${PROJECTNAME}
		LIBRARY DESTINATION "${CMAKE_SOURCE_DIR}/${TARGETDIR}"
		ARCHIVE DESTINATION "${CMAKE_SOURCE_DIR}/${TARGETDIR}"
		RUNTIME DESTINATION "${CMAKE_SOURCE_DIR}/${TARGETDIR}"
		)
endfunction()

# Function to setup some project items for an executable or DLL
# PROJECTNAME - The name of the project being setup
function(sdk_setup_project_bin PROJECTNAME)
	sdk_setup_project_common(${PROJECTNAME} Bin)
endfunction()

# Function to setup some project items for static library
# PROJECTNAME - The name of the project being setup
function(sdk_setup_project_lib PROJECTNAME)
	sdk_setup_project_common(${PROJECTNAME} Lib)
endfunction()&lt;/pre&gt;
&lt;p&gt;That’s quite a lot of code, so I’ll quickly run through what it all does.&lt;/p&gt;
&lt;p&gt;The sdk_list_header_files and sdk_list_source_files use something that CMake calls “file globbing” to check each file in the current directory against a glob expression and if they match, adds them to a list; in this case, I am using it to find all files with a certain file extension.&lt;/p&gt;
&lt;p&gt;The sdk_setup_project_common, sdk_setup_project_bin and sdk_setup_project_lib functions all work together to setup some common target information for a project. You should recall from my test case explanation that I wanted built executables to go to the Bin directory, and built libraries to go to the Lib directory; well these are the functions that deal with that. sdk_setup_project_common does all the hard work, specify a post-build and install step to copy the built objects to the target directory, with sdk_setup_project_bin and sdk_setup_project_lib just providing the correct target directory. Since both the debug and release builds are going to the same directory, we need a way to distinguish between the two so they don’t overwrite each other; this is handled by set_target_properties which adds an “_d” to the object name when building in debug.&lt;/p&gt;
&lt;p&gt;The next section sets up the basic build environment. I copied this section straight from the OGRE CMake scripts and it sets a load of flags on your compiler based on your platform, compiler, and architecture and can likely be left alone for most cases. That said, I won’t be going over it here.&lt;/p&gt;
&lt;p&gt;The last section of this file is really simple.&lt;/p&gt;
&lt;pre&gt;include_directories("${PROJECT_SOURCE_DIR}/Source")
add_subdirectory(Source)&lt;/pre&gt;
&lt;p&gt;include_directories is used to add an include path to your compiler so it knows where to find header files; in my case, I just need the top level Source directory so that’s all I add. You will also need to use this to add any third-party include paths so your compiler can find it. add_subdirectory causes CMake to recurse into the subdirectory specified and nicely ends the main SDK CMakeLists.txt file.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Source Level CMakeLists.txt&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This is a super simple file, all it does is tell CMake to recurse into the two project directories.&lt;/p&gt;
&lt;pre&gt;add_subdirectory(Core)
add_subdirectory(Other)&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Core Level CMakeLists.txt&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I will skip over the top two lines since we have seen those before in the SDK file. Basically we are just saying that this file will create a project called “Core”.&lt;/p&gt;
&lt;p&gt;All our work in creating those functions earlier makes adding all the header and source files really simple.&lt;/p&gt;
&lt;pre&gt;sdk_list_header_files(HEADER_FILES)
sdk_list_source_files(SOURCE_FILES)&lt;/pre&gt;
&lt;p&gt;If you don’t want to add all the files from a certain directory, you can either just setup those variables using a manual list, or if it is easier, let the file globbing do its work and then remove the files you don’t want from the list.&lt;/p&gt;
&lt;pre&gt;add_library(${PROJECT_NAME} STATIC ${HEADER_FILES} ${SOURCE_FILES})
sdk_setup_project_lib(${PROJECT_NAME})&lt;/pre&gt;
&lt;p&gt;The last part of the file sets up this project to build as a static library, and uses our sdk_setup_project_lib to configure everything else so it gets copied to the correct location after being built. It should be noted this it is important that you call add_library before calling sdk_setup_project_lib otherwise sdk_setup_project_lib won’t know which target to modify since it won’t be able to find it.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Other Level CMakeLists.txt&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Once again, I will skip over most of this file as it is all stuff you have seen before in the Core CMakeLists.txt file. The only part that is different is the end, so that is what I will focus on.&lt;/p&gt;
&lt;pre&gt;add_executable(${PROJECT_NAME} ${HEADER_FILES} ${SOURCE_FILES})
sdk_setup_project_bin(${PROJECT_NAME})
add_dependencies(${PROJECT_NAME} Core)
target_link_libraries(${PROJECT_NAME} Core)&lt;/pre&gt;
&lt;p&gt;Unlike the Core, Other has to build as an executable so we use add_executable and sdk_setup_project_bin instead of add_library and sdk_setup_project_lib to handle this.&lt;/p&gt;
&lt;p&gt;Also unlike Core, Other has a dependency. add_dependencies is used to inform your compiler than Other depends on Core, and that Core must have been built before Other. target_link_libraries is used to tell the compiler that Other must link against the Core library.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Download&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;You can download my full test case and CMakeLists.txt files from &lt;a target="_blank" href="http://tweex.net/misc/code/CMakeTest.zip"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Overall I feel CMake is a useful tool for when you need to create build scripts for multiple platforms, however I feel that the project currently suffers from poor documentation and examples. Finding out how to do anything often leaves me feeling like Batman trying to solve Edward Nigma’s riddles. Still, I managed to get there in the end.&lt;/p&gt;</description><link>http://blog.tweex.net/post/797875663</link><guid>http://blog.tweex.net/post/797875663</guid><pubDate>Sun, 11 Jul 2010 14:55:44 +0100</pubDate><category>blog</category><category>programming</category><category>tutorials</category></item><item><title>I don’t have a smart phone, I have some shitty old...</title><description>&lt;object width="400" height="336"&gt;&lt;param name="movie" value="http://www.youtube.com/v/FL7yD-0pqZg&amp;rel=0&amp;egm=0&amp;showinfo=0&amp;fs=1"&gt;&lt;/param&gt;&lt;param name="wmode" value="transparent"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/FL7yD-0pqZg&amp;rel=0&amp;egm=0&amp;showinfo=0&amp;fs=1" type="application/x-shockwave-flash" width="400" height="336" allowFullScreen="true" wmode="transparent"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;br/&gt;&lt;br/&gt;&lt;p&gt;I don’t have a smart phone, I have some shitty old Motorola phone, but  if I was going to get a smart phone I’d get one of the Android HTC ones.I’m one of those people who don’t really see what the big deal about the iAnything is.&lt;/p&gt;
&lt;p&gt;Also, it really messed up pronouncing the words “customisable” and “aneurysm”. It reminds  me of playing Portal Prelude.&lt;/p&gt;</description><link>http://blog.tweex.net/post/758785643</link><guid>http://blog.tweex.net/post/758785643</guid><pubDate>Thu, 01 Jul 2010 21:26:33 +0100</pubDate><category>blog</category></item><item><title>senoadiw:

In programming, Yoda’s Condition is defined as:

the...</title><description>&lt;img src="http://29.media.tumblr.com/tumblr_l28ny9NyQi1qaugfgo1_500.jpg"/&gt;&lt;br/&gt;&lt;br/&gt;&lt;p&gt;&lt;a href="http://senoadiw.tumblr.com/post/594115984/the-yoda-condition"&gt;senoadiw&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;In programming, &lt;a title="Yoda's Condition is defined as" href="http://stackoverflow.com/questions/2349378/new-programming-jargon-you-coined/2430307#2430307"&gt;Yoda’s Condition is defined as&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;the act of using &lt;em&gt;if(constant == variable)&lt;/em&gt; instead of &lt;em&gt;if(variable == constant)&lt;/em&gt;, like &lt;em&gt;if(4 == foo)&lt;/em&gt;. Because it’s like saying “if blue is the sky” or “if tall is the man”.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;No matter what &lt;a title="people may argue about its benefit in coding practice" href="http://clarkgrubb.wordpress.com/2010/05/10/neologism-of-the-day-yoda-conditions/"&gt;people may argue about its benefit in coding practice&lt;/a&gt;, still… makes sense it doesn’t.&lt;/p&gt;
&lt;/blockquote&gt;</description><link>http://blog.tweex.net/post/670906195</link><guid>http://blog.tweex.net/post/670906195</guid><pubDate>Sun, 06 Jun 2010 22:57:48 +0100</pubDate></item><item><title>Setting up nForce in Windows 7</title><description>&lt;p&gt;I’ve recently installed Windows 7 on my main PC, removing the old Vista install. While doing this I had some issues getting my on-board nVIDIA nForce 6 network drivers working, and from looking around the internet, I am not alone.&lt;/p&gt;
&lt;p&gt;The solution that eventually worked for me is as follows:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Shut down your PC and turn it off at the mains for ~20 seconds.&lt;/li&gt;
&lt;li&gt;Turn your PC back on and boot into Windows, it should now be able to see and use your nForce network adapters to access the internet, however if you restart Windows you will find that the adapters become disabled again.&lt;/li&gt;
&lt;li&gt;To solve this, go to the nVIDIA website and download the correct nForce drivers for your motherboard, and then install them (you will only be able to install the network drivers if Windows is currently able to use the adapters, so if they are disabled, you will need to repeat step 1 before continuing).&lt;/li&gt;
&lt;li&gt;Reboot your PC and you should now find that your network adapters are working, and will also continue to work after you restart your PC.&lt;/li&gt;
&lt;/ol&gt;</description><link>http://blog.tweex.net/post/507106041</link><guid>http://blog.tweex.net/post/507106041</guid><pubDate>Fri, 09 Apr 2010 03:16:43 +0100</pubDate><category>blog</category><category>tutorials</category></item><item><title>Setting up Redmine on WebFaction</title><description>&lt;p&gt;Since this is something I have recently had to do, and found the documentation to be out of date, I though I would share this working solution with you all. This is based on Redmine 0.9.x, and thanks to Sean F from WebFaction support, these instructions are mostly his.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Go to your WebFaction control panel and create a new MySQL database, making a note of the database name and password.&lt;/li&gt;
&lt;li&gt;Create a new &lt;span class="mediumtext"&gt;Rails 2.3.5 application from your control panel, noting the name of the application.&lt;/span&gt;
&lt;/li&gt;
&lt;li&gt;&lt;span class="mediumtext"&gt;Create a new website to serve the rails application you just created. eg, redmine.domain.com&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class="mediumtext"&gt;Login to your server using SSH&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;
&lt;span class="mediumtext"&gt;Go to your Rails application directory, eg ‘&lt;/span&gt;&lt;span class="mediumtext"&gt;cd ~/webapps/&lt;em&gt;my_redmine_app&lt;/em&gt;’ replacing ‘my_redmine_app’ with the name of your actual Rails application.&lt;/span&gt;
&lt;/li&gt;
&lt;li&gt;&lt;span class="mediumtext"&gt;Run the following commands:&lt;/span&gt;&lt;/li&gt;
&lt;pre&gt;export PATH=$PWD/bin:$PATH&lt;br/&gt;&lt;span class="mediumtext"&gt;export GEM_HOME=$PWD/gems&lt;br/&gt;gem install mysql&lt;br/&gt;wget &lt;a href="http://rubyforge.org/frs/download.php/69449/redmine-0.9.3.tar.gz"&gt;http://rubyforge.org/frs/download.php/69449/redmine-0.9.3.tar.gz&lt;/a&gt;&lt;br/&gt;tar zxf redmine-0.9.3.tar.gz&lt;br/&gt;ln -s redmine-0.9.3 redmine&lt;br/&gt;cd redmine&lt;br/&gt;cp config/database.yml.example config/database.yml&lt;/span&gt;&lt;/pre&gt;
&lt;li&gt;&lt;span class="mediumtext"&gt;Edit the ‘production’ section of  ‘config/database.yml’ to configure it to use&lt;br/&gt; the database you created in step 1. ‘database’ and ‘username’ should  be the&lt;br/&gt; name of the database, and ‘password’ should be the database password.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;
&lt;span class="mediumtext"&gt;Execute the following commands, again from your Redmine application directory. eg ‘&lt;/span&gt;&lt;span class="mediumtext"&gt;cd ~/webapps/&lt;em&gt;my_redmine_app&lt;/em&gt;/redmine&lt;/span&gt;&lt;span class="mediumtext"&gt;’&lt;/span&gt;
&lt;/li&gt;
&lt;pre&gt;RAILS_ENV=production rake config/initializers/session_store.rb&lt;br/&gt;RAILS_ENV=production rake db:migrate&lt;br/&gt;RAILS_ENV=production rake redmine:load_default_data&lt;br/&gt;cd ..&lt;/pre&gt;
&lt;li&gt;&lt;span class="mediumtext"&gt;Edit ‘nginx/conf/nginx.conf’ to change the text  ‘hello_world’ to ‘redmine’.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class="mediumtext"&gt;Restart nginx with the following command:&lt;/span&gt;&lt;/li&gt;
&lt;pre&gt;./bin/restart&lt;/pre&gt;
&lt;/ol&gt;
&lt;p&gt;You should now have a working Redmine install at the website you specified in step 3.&lt;/p&gt;
&lt;p&gt;One last thing you will probably want to do is get a working email setup so that Redmine can email people. I used SMTP for this, the process is quite simple:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Create a new mailbox to handle your Redmine email, making a note of the username and password.&lt;/li&gt;
&lt;li&gt;Go to your Redmine application directory, eg &lt;span class="mediumtext"&gt;‘&lt;/span&gt;&lt;span class="mediumtext"&gt;cd ~/webapps/&lt;em&gt;my_redmine_app&lt;/em&gt;/redmine&lt;/span&gt;&lt;span class="mediumtext"&gt;’ and execute the following:&lt;/span&gt;
&lt;/li&gt;
&lt;pre&gt;cp config/email.yml.example config/email.yml&lt;/pre&gt;
&lt;li&gt;&lt;span class="mediumtext"&gt;Edit ‘config/email.yml’ as described &lt;a href="http://www.redmine.org/wiki/redmine/Email_Configuration"&gt;here&lt;/a&gt;, you will want to edit the ‘production’ entry as follows, replacing the place-holders accordingly:&lt;/span&gt;&lt;/li&gt;
&lt;pre&gt;production:&lt;br/&gt;  delivery_method: :async_smtp&lt;br/&gt;  smtp_settings:&lt;br/&gt;    address: smtp.webfaction.com&lt;br/&gt;    port: 25&lt;br/&gt;    domain: your_domain_name&lt;br/&gt;    authentication: :login&lt;br/&gt;    user_name: "your_mailbox_user"&lt;br/&gt;    password: "your_mailbox_password"&lt;/pre&gt;
&lt;li&gt;&lt;span class="mediumtext"&gt;Restart nginx (see step 10 from above).&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;And that’s that. You should now have email working with your Redmine install.&lt;/p&gt;</description><link>http://blog.tweex.net/post/484784180</link><guid>http://blog.tweex.net/post/484784180</guid><pubDate>Tue, 30 Mar 2010 20:27:00 +0100</pubDate><category>blog</category><category>tutorials</category></item><item><title>New Webhost</title><description>&lt;p&gt;I have now finished the move of all my websites from Hostgator to &lt;a href="http://www.webfaction.com/services/?affiliate=jdale88"&gt;WebFaction&lt;/a&gt;. I’ve been with Hostgator a while now, and while they have been okay, it wasn’t until I set-up the &lt;a href="http://insimnax.com"&gt;Insimnax&lt;/a&gt; website that I noticed how slowly they actually served content; I let it be for a couple of months before I decided to move.&lt;/p&gt;
&lt;p&gt;I considered getting myself a cheap VPS, something I had done before but was left a little unsatisfied with. While I was looking around I came across WebFaction, they essentially manage to provide shared hosting which is very VPS like. While you don’t get full access to the server, you do get SSH access and the ability to install pretty much anything considering the sheer amount of applications they have available.&lt;/p&gt;
&lt;p&gt;I had been pretty frustrated when I set-up the Insimnax website that I had to use three different pieces of software (&lt;a href="http://www.getpixie.co.uk/"&gt;Pixie&lt;/a&gt;, &lt;a href="http://www.mediawiki.org/wiki/MediaWiki"&gt;MediaWiki&lt;/a&gt;, and &lt;a href="http://www.mybboard.net/"&gt;MyBB&lt;/a&gt;) to get all the functionality from the site that I wanted, I would have much rather just used one thing and be done with it. To this end, when I set-up the site again on WebFaction, I just used their one-click installer to create a Ruby on Rails application and installed &lt;a href="http://www.redmine.org/"&gt;Redmine&lt;/a&gt;. Now not only is the site significantly faster than it was before, but all my content for the Insimnax website is hosted in a single piece of software which makes it easier for me to maintain and manage.&lt;/p&gt;
&lt;p&gt;In addition to being able to create a Rails application, you can also do some other nifty things with them. Is your website running slow because it is trying to serve a lot of static content through &lt;a href="http://www.apache.org/"&gt;Apache&lt;/a&gt;? No problem, with WebFaction you would just create a &lt;a href="http://nginx.org/"&gt;nginx&lt;/a&gt; application to handle all your websites static content, and then use your Apache application to only handle your dynamic content. What’s more, because you have SSH access to all your applications, you can tweak the configuration files for them to your hearts content just like you could with a full VPS or Dedicated server. This to me actually makes WebFaction better than a VPS, since the actual OS and server installation is handled by them (including updates) which leaves me to just worry about the things that these applications are serving.&lt;/p&gt;
&lt;p&gt;My next plan with this new account is to create a &lt;a href="http://subversion.tigris.org/"&gt;Subversion&lt;/a&gt; application and migrate my &lt;a href="https://www.assembla.com/"&gt;Assembla&lt;/a&gt; SVN repository for Insimnax to WebFaction. This will offer me several advantages; firstly it will allow me to integrate my SVN repository into Redmine; secondly it will allow to me to let others view my current trunk, because; thirdly it will allow me to give specific access to just the parts of the SVN repository that I want to be seen by the public. This is something I will be able to do since I will have access to the SVN users configuration file, something that is hidden from me at Assembla, and since Assembla has yet to implement per-directory access rights on their SVN repositories, something I simply cannot do with Assembla hosting.&lt;/p&gt;
&lt;p&gt;I will leave you with this piece of information. While I was waiting for my DNS information to propagate, I could traceroute both servers and I found that they were both hosted at the same data centre (&lt;a href="http://www.theplanet.com/"&gt;The Planet&lt;/a&gt;), and both gave me the same ping of ~162ms from my location here in England. However the WebFaction server was the one that gave me the fastest response to my requests, and since the network statistics for both were the same, I can only assume that is because the WebFaction servers are; better configured, have less load, or both.&lt;/p&gt;</description><link>http://blog.tweex.net/post/484726611</link><guid>http://blog.tweex.net/post/484726611</guid><pubDate>Tue, 30 Mar 2010 19:54:34 +0100</pubDate><category>blog</category></item><item><title>Dragonball Z Abridged</title><description>&lt;a href="http://www.tfsabridged.com/episodes/"&gt;Dragonball Z Abridged&lt;/a&gt;: &lt;p&gt;Like many people, I grew up watching Dragonball Z and it has been brought to my attention that not everyone has seen the Dragonball Z Abridged series by Team Four Star. Those of you that haven’t should be considered shamed and should rectify this immediately by clicking on the link and watching the videos; they are truly hilarious.&lt;/p&gt;</description><link>http://blog.tweex.net/post/414307397</link><guid>http://blog.tweex.net/post/414307397</guid><pubDate>Sat, 27 Feb 2010 01:22:00 +0000</pubDate><category>blog</category></item><item><title>Bayonetta</title><description>&lt;p&gt;For those of you that are unaware, &lt;a target="_blank" href="http://www.sega.com/platinumgames/bayonetta/"&gt;Bayonetta&lt;/a&gt; is a game which is touted as being spiritually related to the Devil May Cry series, mainly due to the fact that the game was designed by Hideki Kamiya, the designer of the original Devil May Cry game.&lt;/p&gt;
&lt;p&gt;When the demo came out on XBOX Live I downloaded and played it, then felt very dissatisfied, then played it again and still felt the same. The main problem I have with the game is the controls; I’m not sure why but somehow I can’t correlate the buttons to the actions because to me the actions the buttons perform don’t seem to be deterministic; this ultimately results in random button mashing (and quite often death).&lt;/p&gt;
&lt;p&gt;I’ve played through Devil May Cry and I really enjoyed that game, mainly because I got the controls since they always did the same thing. I am told that Bayonetta isn’t so bad if you play the actual game from the beginning, and if that’s the case then I would consider the game demo to be a failure since I’m not going to be buying the game due to not being sure I can play it (given, I could put it on super-easy one button mode but that would just make me feel like an idiot, and no-one wants to feel like an idiot).&lt;/p&gt;</description><link>http://blog.tweex.net/post/414079300</link><guid>http://blog.tweex.net/post/414079300</guid><pubDate>Fri, 26 Feb 2010 23:11:00 +0000</pubDate><category>blog</category><category>game</category></item><item><title>New Blog</title><description>&lt;p&gt;Well I’ve moved over to tumblr for my blogging needs. This is mainly due to the fact that I don’t really want the hassle of keeping my blog software up-to-date any more; especially now that I have three separate pieces of software to keep up-to-date for the &lt;a target="_blank" href="http://insimnax.com/"&gt;Insimnax SDK&lt;/a&gt; website.&lt;/p&gt;
&lt;p&gt;As a result of this move, and the fact that tumblr doesn’t have an import option, almost all of my really old posts have gone. It’s somewhat sad to throw away around 2 years of content, but it’s also quite liberating since I have only moved over the most recent content and any older content that I felt was interesting or informative. It was nice to look back over the years of blog posts and see how far I have come to get to where I am today; who knows, maybe this new start will prompt me to update more often :)&lt;/p&gt;
&lt;p&gt;I’ll be setting &lt;a href="http://blog.tweex.net"&gt;http://blog.tweex.net&lt;/a&gt; to point here, although it might take a while for the DNS information to propagate correctly.&lt;/p&gt;</description><link>http://blog.tweex.net/post/407669929</link><guid>http://blog.tweex.net/post/407669929</guid><pubDate>Tue, 23 Feb 2010 22:16:32 +0000</pubDate><category>blog</category></item><item><title>If they're Ninja's, why can I see them?</title><description>&lt;p&gt;&lt;b&gt;AI&lt;/b&gt;&lt;br/&gt; Also, why are they dressed in &lt;i&gt;green&lt;/i&gt;?&lt;/p&gt;
&lt;p&gt;My AI demo was finished and handed in. My extension task was behaviour trees and there is a video of the demo below, all in HD and powered by the Insimnax Framework.&lt;/p&gt;
&lt;p&gt;
&lt;object height="401" width="490"&gt;
&lt;param name="movie" value="http://www.youtube.com/v/FKNpGFSDZzM&amp;hl=en_GB&amp;fs=1&amp;hd=1"&gt;
&lt;param name="allowFullScreen" value="true"&gt;
&lt;param name="allowscriptaccess" value="always"&gt;
&lt;embed src="http://www.youtube.com/v/FKNpGFSDZzM&amp;hl=en_GB&amp;fs=1&amp;hd=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" height="401" width="490"&gt;&lt;/embed&gt;&lt;/object&gt;
&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Insimnax Framework&lt;/b&gt;&lt;br/&gt;The &lt;a target="_blank" href="http://insimnax.com/?s=framework"&gt;Insimnax Framework&lt;/a&gt; has finally been released. It is cut down from what I had originally planned since I decided to move the integration of things like physics, GUI, audio etc to the games that use it so that developers could be more flexible. You can grab the full source code (licensed under the MIT license) along with the code for the Insimnax Common Framework (ICL) from &lt;a target="_blank" href="http://insimnax.com"&gt;here&lt;/a&gt;.&lt;br/&gt;&lt;br/&gt;I’m currently in the early stages of working on a level editor for it.&lt;/p&gt;</description><link>http://blog.tweex.net/post/407560985</link><guid>http://blog.tweex.net/post/407560985</guid><pubDate>Sun, 24 Jan 2010 00:00:00 +0000</pubDate><category>blog</category><category>c++</category><category>game development</category><category>university</category></item><item><title>Welcome to the Madhouse</title><description>&lt;p&gt;So what I have been doing since my last update. Well, I have played through Batman: Arkham Asylum, AND YOU SHOULD TOO! It’s a great game (given, the boss fights aren’t up to much) but I love the sneaky, pick people off one a time, parts (the Batclaw has to be my favourite weapon for doing this, just catch someone unaware and yank them over the side of a fence for an instant incapacitation).&lt;br/&gt;&lt;br/&gt;I have also been back at Uni, so have been working on a variety of game programming related tasks. My modules for this semester are Artificial Intelligence Techniques, Advanced 3D Graphics, Network Programming and my Applied Research Project (my dissertation - which is actually a year long module).&lt;/p&gt;
&lt;p&gt;&lt;b&gt;AI&lt;/b&gt;&lt;br/&gt;AI has been interesting so far. We started by looking into State Machines, and then Behaviour Layering before moving onto Steering Systems and Path-finding. The culmination of this is that I currently have a demo application that uses a state machine to drive the steering behaviours of a steering system following a path calculated by an A* path-finding algorithm.&lt;/p&gt;
&lt;p&gt;&lt;img src="http://media.tumblr.com/tumblr_kybbqpeOev1qams0c.png"/&gt;&lt;br/&gt;&lt;i&gt;AI path-finding demo, using the Insimnax Framework&lt;/i&gt;&lt;br/&gt;&lt;br/&gt;&lt;b&gt;A3D&lt;/b&gt;&lt;br/&gt;Advanced 3D Graphics has three “mini-assessments”, one of which I have already completed (Shaders), and the second of which I am currently half way through (Animation). The final one will be large-scene rendering.&lt;br/&gt;&lt;br/&gt;The shaders assignment took the form of a report looking into a particular field. This was a partner exercise and we worked on Post-processing effects.&lt;/p&gt;
&lt;p&gt;The animation assignment is looking at skeletal animation with blending, as well as being able to influence particular bones in a mesh to make them perform certain things (like look at a point of interest). Being able to attach objects to a mesh is also another desired feature. All this is being done in DirectX 9 and I currently have instanced skinned meshes (software and HLSL) with animation event callbacks.&lt;br/&gt;&lt;img src="http://media.tumblr.com/tumblr_kybbsvmDYI1qams0c.png"/&gt;&lt;br/&gt;&lt;i&gt;Instanced, animated, Tiny’s&lt;/i&gt;&lt;br/&gt;&lt;br/&gt;&lt;b&gt;Network Programming&lt;/b&gt;&lt;br/&gt;Network programming involves making a game (I am using C# and XNA) which can have 4 or more players playing over a network. I am making a 4-player version of Asteroids and currently have the “player-chat” lobby part of this working, as well as a nice framework for handling application layer packets.&lt;br/&gt;&lt;br/&gt;&lt;img src="http://media.tumblr.com/tumblr_kybbu6kiNm1qams0c.png"/&gt;&lt;br/&gt;&lt;i&gt;Main lobby&lt;/i&gt;&lt;br/&gt;&lt;br/&gt;&lt;b&gt;ARP&lt;/b&gt;&lt;br/&gt;My dissertation is on the topic of “Visual Scripting Systems for Games”. Not much to report on this yet since it has been mostly research based so far.&lt;br/&gt;&lt;br/&gt;&lt;b&gt;Insimnax Framework&lt;/b&gt;&lt;br/&gt;The Insimnax Framework is a game framework I am currently working on which makes heavy use of &lt;a target="_blank" href="http://www.ogre3d.org"&gt;OGRE 3D&lt;/a&gt;. The framework itself is currently a wrapper to make getting started and using OGRE 3D easier, although I have plans to extend it by adding “systems” such as GUI, Physics and Audio. I am currently using this framework for my AI project and hope to use it for my dissertation too. I plan to release it under an MIT license once it has reached a decent (and somewhat stable) stage of its development.&lt;br/&gt;&lt;br/&gt;&lt;b&gt;… and finally&lt;/b&gt;&lt;br/&gt;Oh yeah, did you hear about Epic releasing the &lt;a target="_blank" href="http://www.udk.com/index.html"&gt;UDK&lt;/a&gt; for &lt;i&gt;free&lt;/i&gt;?! Crazy!&lt;/p&gt;</description><link>http://blog.tweex.net/post/407546601</link><guid>http://blog.tweex.net/post/407546601</guid><pubDate>Thu, 12 Nov 2009 00:00:00 +0000</pubDate><category>blog</category><category>c++</category><category>game development</category><category>university</category><category>xna</category></item><item><title>Templates and Streams; the perfect couple</title><description>&lt;p&gt;Recently I’ve been doing some work templatifying my two I/O classes, and from that have come to the conclusion that templates and streams make for the ultimate generic programming tool. Previously I had a load of old duplicate bloat code in these classes, that was all removed and replaced by one templated member function, and two specialised template functions from it, and best of all since this code is now able to take external streams as input/output sources, it means that I no longer have one code path for handing files, and another code path for serialising to memory. Instead I just pass an fstream when working with files, and a stringstream when I want to serialise to/from memory, simple!&lt;br/&gt;&lt;br/&gt;One of the best changes the templates and streams has made is to my tryParse function. Previously there were a load of these, each handling a different variable type using old C functions. Now the function has been replaced by a single templated function, and a stringstream. If you try and tryParse a type a stringstream can’t handle, it just doesn’t compile, best of all is that you can still expand the function using template specialisation if you want to tryParse to your own custom types.&lt;br/&gt;&lt;br/&gt;&lt;/p&gt;
&lt;pre&gt;//! Try and parse a string to another type
//! \return true if the conversion was ok, false otherwise
template &lt;typename T&gt;
bool tryParse(
	const std::string &amp;str,		//!&lt; String to convert
	T &amp;out				//!&lt; Output variable
	)
{
	std::stringstream sstream;
	sstream.exceptions(std::ios::failbit | std::ios::badbit);
	sstream &lt;&lt; str;
	try { sstream &gt;&gt; out; }
	catch(std::exception&amp;) { return false; }
	return true;
}&lt;/pre&gt;
&lt;p&gt;I actually worked out the total number of lines of code before, and after my changes. The results are below:&lt;br/&gt;&lt;br/&gt;&lt;/p&gt;
&lt;pre&gt;Old:
Reader 		- 183   (header) 1,042 (source)
Writer 		- 209   (header)   839 (source)
TryParse 	- 50    (header)   115 (source)
Total 		- 2,438

New:
Reader 		- 271   (header)   351 (source)
Writer 		- 226   (header)   245 (source)
TryParse 	- 30    (header)     0 (source)
Total 		- 1,123&lt;/pre&gt;
&lt;p&gt;Quite impressive :) (Although I did cheat slightly since the new reader and writer have a macro which removes a lot of code repetition from the header, it probably saves ~39 lines of code from each class, so ~78 in all. Still that would only make it 1,201 lines of code which is still a huge saving for something that has more functionality with less code).&lt;/p&gt;</description><link>http://blog.tweex.net/post/407516876</link><guid>http://blog.tweex.net/post/407516876</guid><pubDate>Sun, 27 Sep 2009 00:00:00 +0100</pubDate><category>blog</category><category>C++</category></item><item><title>[C++] Anything to/from a Hex String</title><description>&lt;p&gt;&lt;b&gt;Introduction&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;I recently needed to be able to convert any instance of an object in C++ to a file so it could be serialised, and then restored later; I did most of this by writing out each member variable of the object individually at the most basic level. This works fine and is easy enough to implement; but then came the time to do the “generic” version, the version that could write out any object (such as a struct) as a whole.&lt;/p&gt;
&lt;p&gt;The I/O classes can write out/read in using either binary or text, the binary version of the generic writer is simply a case of writing out the length of the data, and then each byte of the data. The text version however required me to convert the bytes of data making up the object into something that was sensible as text, I decided to do this by converting each byte into a hex value and adding it to a string. As I was working on this I found that the information about how to do this without using some of the old C-style string manipulation functions is quite sparse on the net, so after much searching, trying, and failing, I now have a solution that works both ways using good old C++ string streams.&lt;/p&gt;
&lt;p&gt;I should probably point out that I don’t think this solution is endian neutral, so you would likely want to add a method of endian detection and switching to ensure consistency across different architectures.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;The Code&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;When a byte is encoded to hex string, each byte becomes two characters in the range 0 to F. For example, the value “10” would be written out as “0A”.&lt;/p&gt;
&lt;p&gt;The code works by using a std::stringstream with hex mode set (std::hex), along with using zero’s for padding (std::setfill(‘0’)) and a fixed width of two for each byte added as hex (std::setw(2)).&lt;/p&gt;
&lt;p&gt;The toHex function works by adding each byte of the passed data into the string stream as an integer with a width of two; because zero’s have been set as padding, any numbers which are added to the stream that are only one character long will be automatically prefixed with a zero resulting in the correct two character hex value being added to the string stream.&lt;/p&gt;
&lt;p&gt;The fromHex function works by reading out two characters from the hex string into a string stream, these characters are then converted back into the byte value when read out from the string stream into an integer. Finally, the value of the integer is stored as the value of the byte, and the string is moved on to the next set of two characters.&lt;/p&gt;
&lt;p&gt;&lt;a target="_blank" href="http://tweex.net/misc/code/hexString"&gt;Get the code&lt;/a&gt;.&lt;/p&gt;</description><link>http://blog.tweex.net/post/407655870</link><guid>http://blog.tweex.net/post/407655870</guid><pubDate>Thu, 24 Sep 2009 00:00:00 +0100</pubDate><category>c++</category><category>tutorials</category></item><item><title>Rant Tags Off</title><description>&lt;p&gt;So to be fair to the PS3, I said that my experience has so far been mostly good (although I have a feeling this is because I haven’t tried to do anything complicated with the XMB yet). I guess my good experience comes from the fact that there are a few games I enjoy playing on it, and also the fact that I can use my debit card in the PlayStation Store which is something I can’t do in the XBOX Marketplace (they don’t accept Maestro cards).&lt;br/&gt;&lt;br/&gt;The games in question are WipEout HD, and Ratchet and Clank. As I said before, WipEout HD is the main reason I bought a PS3, so finding out last week that there was going to be a DLC expansion for the game caused me to almost go running around the office like a crazed fan&lt;strike&gt;girl&lt;/strike&gt;boy, the trailer for the game is below. Can’t wait to try Zone Battle and I really hope their definition of summer is more towards the start of summer :D&lt;/p&gt;
&lt;p&gt;
&lt;object height="401" width="490"&gt;
&lt;param name="movie" value="http://www.youtube.com/v/wlAf6m3Ih2Q&amp;hl=en_GB&amp;fs=1&amp;hd=1"&gt;
&lt;param name="allowFullScreen" value="true"&gt;
&lt;param name="allowscriptaccess" value="always"&gt;
&lt;embed src="http://www.youtube.com/v/wlAf6m3Ih2Q&amp;hl=en_GB&amp;fs=1&amp;hd=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" height="401" width="490"&gt;&lt;/embed&gt;&lt;/object&gt;
&lt;/p&gt;
&lt;p&gt;I have never played a Ratchet and Clank game before this one, and I feel now that I have been missing out. I love it! The humour in the game is fantastic and somewhat dark (which is also the main reason I love Portal so much) and I’m having a great time playing it, even despite the fact that I sometimes get stuck (game play wise) in a level, and that the enemy collision detecting often screws up a bit leaving them stuck (physically) in walls.&lt;/p&gt;</description><link>http://blog.tweex.net/post/407498236</link><guid>http://blog.tweex.net/post/407498236</guid><pubDate>Sun, 07 Jun 2009 00:00:00 +0100</pubDate><category>blog</category></item><item><title>Rant Tags On</title><description>&lt;p&gt;&lt;b&gt;Warning:&lt;/b&gt; Rant ahead, if you are a PS3 fan-boy stop reading now. I don’t need your hate.&lt;br/&gt;&lt;br/&gt;For my birthday this year I bought myself a PS3 to go with my nice new 42” 1080p HDTV, this now completes my collection of current generation consoles as I also have a 360 and a Wii so I can tell you that this opinion comes from someone who isn’t biased to a particular console. So why did I buy it? Two main reasons, I had a shiny new TV and wanted to see what all the fuss about Bluray was, and I also wanted to play &lt;a target="_blank" href="http://www.wipeouthd.com/en_GB/index.html"&gt;WipEout HD&lt;/a&gt;; now leaving aside the fact that buying a console that cost almost £300 to play a £14 game is perhaps a sign of insanity on my part, I have to say my experience so far has been mostly good, bar one thing, “installing… please wait”.&lt;br/&gt;&lt;br/&gt;I believe &lt;a target="_blank" href="http://www.escapistmagazine.com/videos/view/zero-punctuation/759-Bionic-Commando"&gt;this video&lt;/a&gt; (32 - 38 seconds) pretty much sums up my opinion on installing games on the PS3. A friend from work lent me some games to play, one of which was GT5, I had 10 minutes spare before lunch so I thought “Hey, I’ll give it a go”… WRONG! It took 10 minutes just to install the damn thing to the hard drive (what the hell is this, a PC?!) and then took another 20 minutes to download and install updates; perhaps I wouldn’t have minded so much if the hassle of getting the game to play was worth it, but it distinctly wasn’t and I only played the game for about 10 minutes before moving onto another game I had been lent. The most amusing thing about the near constant updates, is when I bought WipEout HD from the PlayStation Store, downloaded it, went to play it and was told that there were five updates for the game… I’m sorry, but I just downloaded over a gigabyte of data and now I need to download more?! Why can’t you just update the version you have on the PlayStation Store so that I don’t have to bother with this? Oh right, yeah, that would make sense wouldn’t it; can’t have that.&lt;br/&gt;&lt;br/&gt;Oh, and just don’t get me started on the XMB. No seriously, don’t.&lt;/p&gt;</description><link>http://blog.tweex.net/post/407479797</link><guid>http://blog.tweex.net/post/407479797</guid><pubDate>Sun, 07 Jun 2009 00:00:00 +0100</pubDate><category>blog</category></item><item><title>Fences and what I do</title><description>&lt;p&gt;I’ve taken to using &lt;a target="_blank" href="http://twitter.com/jdale88"&gt;Twitter&lt;/a&gt; to post updates on since I can do it more informally and whenever I feel like it.&lt;br/&gt;&lt;br/&gt;Firstly, I’ve been using a program called &lt;a target="_blank" href="http://www.stardock.com/products/fences/"&gt;Fences&lt;/a&gt; to help sort out my desktop icons and so far I’m finding it really handy to be able to group up my icons and not have Windows randomly decide to re-arrange them for me… bliss. You can see my desktop &lt;a target="_blank" href="http://tweex.net/misc/desktop/DesktopFences.png"&gt;here&lt;/a&gt;.&lt;br/&gt;&lt;br/&gt;Secondly, BlitzTech (where I work) has released a video of some of the features of the BlitzTech SDK, including the Scripting and Behaviour (State Machine) system which I am currently responsible for developing and maintaining. If you want to see what I work on on a day to day basis then you can find the video &lt;a target="_blank" href="http://www.blitzgamesstudios.com/blitztech/"&gt;here&lt;/a&gt; (it’s the “Tools Demo” one).&lt;/p&gt;</description><link>http://blog.tweex.net/post/407472160</link><guid>http://blog.tweex.net/post/407472160</guid><pubDate>Sun, 05 Apr 2009 00:00:00 +0100</pubDate><category>blog</category><category>game development</category></item><item><title>[C++] Pointer Fun – SafeDelete</title><description>&lt;p&gt;&lt;b&gt;Introduction&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;SafeDelete and SafeArrayDelete are functions that will delete the data a pointer points to before setting it to NULL. The idea being that they leave the pointer in a safe NULL state so that you don’t try and use a pointer which points to invalid memory.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;How to - With a Macro&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;You can implement a SafeDelete set-up using a macro, it might well look something like this:&lt;/p&gt;
&lt;pre&gt;#define SAFE_DELETE(ptr) { delete (ptr); (ptr) = NULL; }&lt;/pre&gt;
&lt;p&gt;Now that’s all well and good, and would perfectly do the job at hand, but we are C++ programmers and as such we prefer not to use macro’s unless we have to; after all, they can be tricky to debug since you can’t set breakpoints in them, they have no type safety which means you can come seriously unstuck if you pass something incorrect into one, and they can be tricky to read.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Attempt #1 – Just pass it a pointer… right?&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Now, you might start off on your quest to convert that macro into a function by writing out the following code:&lt;/p&gt;
&lt;pre&gt;void safeDelete(void *ptr)
{
	delete ptr;
	ptr = NULL;
}&lt;/pre&gt;
&lt;p&gt;All looks fine, you are passing it in a pointer, deleting it and then setting the pointer to NULL… simple. So you use that in your application, the pointer is checked, it isn’t NULL so it gets deleted and set to NULL, cool. Then your code jumps out of the function and… what the hell?! The pointer passed into it is no longer NULL, but the data it points to has been deleted, the pointer is left dangling. Not a very safe SafeDelete.&lt;/p&gt;
&lt;p&gt;So you’re now just left scratching your head, why did that happen? Simple, you passed a copy of the pointer into the function as a parameter, and you only changed that local copy.&lt;/p&gt;
&lt;p&gt;People often have a misconception with pointers that they just somehow exist and magically point to a memory address, in actual fact, a pointer like the one shown above is an object on the &lt;i&gt;stack&lt;/i&gt; that points to a memory address (also known as a pointee). If you know anything about that stack, that should tell you two things; firstly, the pointer object is passed into the function as a &lt;i&gt;copy&lt;/i&gt;; secondly, that copy is destroyed once it falls out of scope.&lt;/p&gt;
&lt;p&gt;The confusion over whether you are passing a pointer as a copy is easy to make since a pointer is essentially two things in one, this can be better explained by showing how const can work with a pointer.&lt;/p&gt;
&lt;pre&gt;void safeDelete(void *const ptr)
{
	delete ptr;
	ptr = NULL;
}&lt;/pre&gt;
&lt;p&gt;The above code should not compile, you should get an error about trying to assign to a value that is const. That is because you have made the pointer const, so you cannot change its value; this serves to clearly show that assigning NULL to the pointer is changing the pointer itself, and not the data it is pointing to.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Attempt #2 – A pointer to… a pointer?&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;If you want to be able to modify the location that a pointer points to after leaving a function (that is, essentially modify the pointer object itself) you need to pass it into the function like you would any other object that you want to modify; by using a pointer or a reference. First let’s look at the pointer to a pointer approach.&lt;/p&gt;
&lt;pre&gt;void safeDelete(void **ptr)
{
	delete *ptr;
	*ptr = NULL;
}&lt;/pre&gt;
&lt;p&gt;Well, that does the job all right. Note that you have to de-reference the pointer to a pointer that was passed into it before performing the required operations on it; this is so you are dealing with the pointer object you passed in from your function, and not the local copy of the pointer to a pointer.&lt;/p&gt;
&lt;p&gt;The problem with this approach is that since pointers to pointers can’t be implicitly casted to a void** you have to do the cast yourself when passing to the function, which just looks ugly (and would get annoying fast).&lt;/p&gt;
&lt;pre&gt;int *ptr = new int(10);
safeDelete((void**)&amp;ptr);&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Attempt #3 – A reference to a pointer (and some trickery)&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Just like you can created pointers to pointers, you can also create a reference to a pointer. These have all the usual features of references in C++ such as never being NULL, the syntax for creating such a thing is shown below.&lt;/p&gt;
&lt;pre&gt;void safeDelete(void *&amp;ptr)
{
	delete ptr;
	ptr = NULL;
}&lt;/pre&gt;
&lt;p&gt;However, this code has a major flaw since it won’t compile. Why? Because you cannot create a reference to void so we need some template trickery to get this working correctly. As well as being able to create template classes, you can also create template functions and we will use this to our advantage to be able to create a reference to a pointer to a generic type.&lt;/p&gt;
&lt;pre&gt;template &lt;typename T&gt;
void safeDelete(T *&amp;ptr)
{
	delete ptr;
	ptr = NULL;
}&lt;/pre&gt;
&lt;p&gt;This uses the template argument as the type for the reference to a pointer, this template trickery also allows us to do away with the nasty void** casting when using the function.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;The final code&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;And here are the finished functions for safeDelete and safeArrayDelete.&lt;b&gt;&lt;br/&gt;&lt;/b&gt;&lt;/p&gt;
&lt;pre&gt;template &lt;typename T&gt;
void safeDelete(T *&amp;ptr)
{
	delete ptr;
	ptr = NULL;
}

template &lt;typename T&gt;
void safeArrayDelete(T *&amp;ptr)
{
	delete[] ptr;
	ptr = NULL;
}&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Note&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;You might be wondering why I don’t do an if to check if the pointer is non-NULL before trying to delete it. Well the default delete operators in C++ automatically ignore the deletion of a NULL (zero) pointer, so you can in-fact delete 0 as many times as you want; adding the if check does nothing but add extra CPU cycles.&lt;/p&gt;</description><link>http://blog.tweex.net/post/407617680</link><guid>http://blog.tweex.net/post/407617680</guid><pubDate>Wed, 08 Oct 2008 00:00:00 +0100</pubDate><category>c++</category><category>tutorials</category></item></channel></rss>
