<?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>Paul Solt &#187; polymorphism</title>
	<atom:link href="http://paulsolt.com/tag/polymorphism/feed/" rel="self" type="application/rss+xml" />
	<link>http://paulsolt.com</link>
	<description>Putting the Inc back in Solt since 2005.</description>
	<lastBuildDate>Fri, 03 Feb 2012 05:04:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>OpenGL/GLUT, Classes (OOP), and Problems</title>
		<link>http://paulsolt.com/2009/07/openglglut-classes-oop-and-problems/</link>
		<comments>http://paulsolt.com/2009/07/openglglut-classes-oop-and-problems/#comments</comments>
		<pubDate>Fri, 31 Jul 2009 06:59:30 +0000</pubDate>
		<dc:creator>Paul Solt</dc:creator>
				<category><![CDATA[Animation Project]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[function pointers]]></category>
		<category><![CDATA[GLUT]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[inheritance]]></category>
		<category><![CDATA[member functions]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[polymorphism]]></category>
		<category><![CDATA[static methods]]></category>

		<guid isPermaLink="false">http://paulsolt.com/?p=156</guid>
		<description><![CDATA[<p>Update: 8/22/10 Checkout the updated framework and post: <a href="http://paulsolt.com/2010/08/glut-object-oriented-framework-on-github/">http://paulsolt.com/2010/08/glut-object-oriented-framework-on-github/</a></p> <p>I created a C style driver program that used OpenGL/GLUT for my <a href="http://paulsolt.com/projects/computer-animation/">computer animation course projects</a>. It worked fine for the first project. However, there were multiple projects and they all started to use the same boiler plate code. In order to reuse code, [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update: 8/22/10</strong> Checkout the updated framework and post: <a href="http://paulsolt.com/2010/08/glut-object-oriented-framework-on-github/">http://paulsolt.com/2010/08/glut-object-oriented-framework-on-github/</a></p>
<p>I created a C style driver program that used OpenGL/GLUT for my <a href="http://paulsolt.com/projects/computer-animation/">computer animation course projects</a>. It worked fine for the first project. However, there were multiple projects and they all started to use the same boiler plate code. In order to reuse code, I decided to refactor and make an extensible class to setup GLUT. My goal was to make it easy to extend the core behavior of the GLUT/OpenGL application.</p>
<p>As I refactored the code I decided to make a base class to perform all the GLUT setup. There&#8217;s a first time for everything and I didn&#8217;t realize the scope of this change until I was committed to it. I will summarize the problems and how to solve them. The code and explanations should provide the basic understanding of what is happening, but it will not compile as it is provided.</p>
<p><strong>Problem 1</strong>: My initial attempt was to pass member functions to the GLUT callback functions. However, you can&#8217;t pass member functions from a class directly to C callback functions. The GLUT callback functions expect a function signature exactly as <strong>function(BLAH)</strong> and I was giving it something that was <strong>function(this, BLAH)</strong>. Where the &#8220;this&#8221; portion was the object passed under the hood.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> AnimationFramework <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
	<span style="color: #0000ff;">void</span> display<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">void</span> run<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">void</span> keyboard<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">char</span> key, <span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">int</span> y<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">void</span> keyboardUp<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">char</span> key, <span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">int</span> y<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">void</span> specialKeyboard<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> key, <span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">int</span> y<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">void</span> specialKeyboard<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> key, <span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">int</span> y<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">void</span> startFramework<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>argv<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>&#8230; in the setup function for GLUT</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> AnimationFramework<span style="color: #008080;">::</span><span style="color: #007788;">startFramework</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>argv<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #666666;">// Initialize GLUT</span>
	glutInit<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>argc, argv<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	glutInitDisplayMode<span style="color: #008000;">&#40;</span>GLUT_RGB <span style="color: #000040;">|</span> GLUT_DOUBLE <span style="color: #000040;">|</span> GLUT_DEPTH<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	glutInitWindowPosition<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">300</span>, <span style="color: #0000dd;">100</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	glutInitWindowSize<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">600</span>, <span style="color: #0000dd;">480</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	glutCreateWindow<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Animation Framework&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> 
&nbsp;
	<span style="color: #666666;">// Function callbacks</span>
	glutDisplayFunc<span style="color: #008000;">&#40;</span>display<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> 		<span style="color: #666666;">// ERROR</span>
	glutKeyboardFunc<span style="color: #008000;">&#40;</span>keyboard<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> 		<span style="color: #666666;">// ERROR</span>
	glutKeyboardUpFunc<span style="color: #008000;">&#40;</span>keyboardUp<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> 	<span style="color: #666666;">// ERROR</span>
	glutSpecialFunc<span style="color: #008000;">&#40;</span>specialKeyboard<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> 	<span style="color: #666666;">// ERROR</span>
	glutSpecialUpFunc<span style="color: #008000;">&#40;</span>specialKeyboardUp<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// ERROR</span>
&nbsp;
	init<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>			<span style="color: #666666;">// Initialize</span>
	glutIdleFunc<span style="color: #008000;">&#40;</span>run<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> 	<span style="color: #666666;">// The program run loop  // ERROR</span>
	glutMainLoop<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>		<span style="color: #666666;">// Start the main GLUT thread</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><strong>Solution 1</strong>: Create static methods, and pass these static methods to the call back functions. All the logic for the AnimationFramework will go into these static methods. I&#8217;ve fixed the compiler errors, but it feels like a step backwards from what I set out to do.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> AnimationFramework <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
	<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> displayWrapper<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> runWrapper<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> keyboardWrapper<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">char</span> key, <span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">int</span> y<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> keyboardUpWrapper<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">char</span> key, <span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">int</span> y<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> specialKeyboardWrapper<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> key, <span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">int</span> y<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> specialKeyboardUpWrapper<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> key, <span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">int</span> y<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">void</span> startFramework<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>argv<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>&#8230; in a setup function for GLUT</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> AnimationFramework<span style="color: #008080;">::</span><span style="color: #007788;">startFramework</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>argv<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #666666;">// Initialize GLUT</span>
	glutInit<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>argc, argv<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	glutInitDisplayMode<span style="color: #008000;">&#40;</span>GLUT_RGB <span style="color: #000040;">|</span> GLUT_DOUBLE <span style="color: #000040;">|</span> GLUT_DEPTH<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	glutInitWindowPosition<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">300</span>, <span style="color: #0000dd;">100</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	glutInitWindowSize<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">600</span>, <span style="color: #0000dd;">480</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	glutCreateWindow<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Animation Framework&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> 
&nbsp;
	<span style="color: #666666;">// Function callbacks</span>
	glutDisplayFunc<span style="color: #008000;">&#40;</span>displayWrapper<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>		<span style="color: #666666;">// NO ERRORS!</span>
	glutKeyboardFunc<span style="color: #008000;">&#40;</span>keyboardWrapper<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	glutKeyboardUpFunc<span style="color: #008000;">&#40;</span>keyboardUpWrapper<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	glutSpecialFunc<span style="color: #008000;">&#40;</span>specialKeyboardWrapper<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	glutSpecialUpFunc<span style="color: #008000;">&#40;</span>specialKeyboardUpWrapper<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	init<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>					<span style="color: #666666;">// Initialize</span>
	glutIdleFunc<span style="color: #008000;">&#40;</span>runWrapper<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> 	<span style="color: #666666;">// The program run loop</span>
	glutMainLoop<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>				<span style="color: #666666;">// Start the main GLUT thread</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>I&#8217;ve made a bit of progress, but when you think about it, static methods aren&#8217;t much different from the plain C functions.</p>
<p><strong>Problem 2</strong>: I want to have the functionality in the instance methods, yet I&#8217;m stuck with static methods. I&#8217;m not encapsulating the behavior inside instance methods, and I have no option for inheritance. I&#8217;ve still failed to hit the initial goal of creating an easy to use object oriented GLUT wrapper that&#8217;s extensible.</p>
<p><strong>Solution 2</strong>: I need to make virtual instance methods in the class and call them from the static callback functions. But I can&#8217;t make the function calls directly, I need a static instance of the class to make the instance function calls. (Re-read, as it&#8217;s a little complex) All I need to do is pass an instance of the class or subclass and I&#8217;ll be able to extend the functionality. It&#8217;s a little tricky/ugly, but it&#8217;s the best method I&#8217;ve found for encapsulating C style GLUT into a C++ application.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> AnimationFramework <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">protected</span><span style="color: #008080;">:</span>
	<span style="color: #0000ff;">static</span> AnimationFramework <span style="color: #000040;">*</span>instance<span style="color: #008080;">;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
	<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> displayWrapper<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> runWrapper<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> keyboardWrapper<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">char</span> key, <span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">int</span> y<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> keyboardUpWrapper<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">char</span> key, <span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">int</span> y<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> specialKeyboardWrapper<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> key, <span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">int</span> y<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> specialKeyboardUpWrapper<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> key, <span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">int</span> y<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">void</span> startFramework<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>argv<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">void</span> run<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> display<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">float</span> dTime<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> keyboard<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">char</span> key, <span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">int</span> y<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> keyboardUp<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">char</span> key, <span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">int</span> y<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> specialKeyboard<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> key, <span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">int</span> y<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> specialKeyboardUp<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> key, <span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">int</span> y<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>The static methods are implemented here:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// Static functions which are passed to Glut function callbacks</span>
&nbsp;
<span style="color: #0000ff;">void</span> AnimationFramework<span style="color: #008080;">::</span><span style="color: #007788;">displayWrapper</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	instance<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>displayFramework<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// calls display(float) with time delta</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">void</span> AnimationFramework<span style="color: #008080;">::</span><span style="color: #007788;">runWrapper</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	instance<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>run<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">void</span> AnimationFramework<span style="color: #008080;">::</span><span style="color: #007788;">keyboardWrapper</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">char</span> key, <span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">int</span> y<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	instance<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>keyboard<span style="color: #008000;">&#40;</span>key,x,y<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">void</span> AnimationFramework<span style="color: #008080;">::</span><span style="color: #007788;">keyboardUpWrapper</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">char</span> key, <span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">int</span> y<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	instance<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>keyboardUp<span style="color: #008000;">&#40;</span>key,x,y<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">void</span> AnimationFramework<span style="color: #008080;">::</span><span style="color: #007788;">specialKeyboardWrapper</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> key, <span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">int</span> y<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	instance<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>specialKeyboard<span style="color: #008000;">&#40;</span>key,x,y<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">void</span> AnimationFramework<span style="color: #008080;">::</span><span style="color: #007788;">specialKeyboardUpWrapper</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> key, <span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">int</span> y<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	instance<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>specialKeyboardUp<span style="color: #008000;">&#40;</span>key,x,y<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The startFramework() method is the same as provided in the Solution 1. </p>
<p><strong>EDIT:</strong><br />
I left out details on how the instance was set. In my program, I subclassed AnimationFramework and created classes for each program I needed overriding the appropriate methods. As an example, KeyFrameFramework was a subclass in my project.</p>
<p>I have a function in my AnimationFramework.h</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> setInstance<span style="color: #008000;">&#40;</span>AnimationFramework <span style="color: #000040;">*</span> framework<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>AnimationFramework.cpp</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">AnimationFramework <span style="color: #000040;">*</span>AnimationFramework<span style="color: #008080;">::</span><span style="color: #007788;">instance</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">void</span> AnimationFramework<span style="color: #008080;">::</span><span style="color: #007788;">setInstance</span><span style="color: #008000;">&#40;</span>AnimationFramework <span style="color: #000040;">*</span>framework<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	instance <span style="color: #000080;">=</span> framework<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The main.cpp looks something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>argv<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
	AnimationFramework <span style="color: #000040;">*</span>f <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> KeyFrameFramework<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// Subclass of AnimationFramework</span>
	f<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>setInstance<span style="color: #008000;">&#40;</span>f<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	f<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>setTitle<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Key Framing:&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	f<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>setLookAt<span style="color: #008000;">&#40;</span><span style="color:#800080;">0.0</span>, <span style="color:#800080;">2.0</span>, <span style="color:#800080;">10.0</span>, <span style="color:#800080;">0.0</span>, <span style="color:#800080;">2.0</span>, <span style="color:#800080;">0.0</span>, <span style="color:#800080;">0.0</span>, <span style="color:#800080;">1.0</span>, <span style="color:#800080;">0.0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	f<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>startFramework<span style="color: #008000;">&#40;</span>argc, argv<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>I set the instance variable of the AnimationFramework class to an AnimationFramework subclass called KeyFrameFramework. Doing so allows me to use polymorphism and call the appropriate functionality that is specific to each animation project. Note: Don&#8217;t set the instance within a constructor, since the object is not fully initialized until the constructor is finished. You need to set the instance after your subclass object has been created.</p>
<p>Let me know if you have any questions. Below are the references I used.</p>
<p><strong>References:</strong></p>
<ul>
<li><a href="http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.5">http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.5</a></li>
<li><a href="http://www.parashift.com/c++-faq-lite/ctors.html">http://www.parashift.com/c++-faq-lite/ctors.html</a></li>
<li><a href="http://www.gamedev.net/community/forums/topic.asp?topic_id=314567">http://www.gamedev.net/community/forums/topic.asp?topic_id=314567</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://paulsolt.com/2009/07/openglglut-classes-oop-and-problems/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
	</channel>
</rss>

