<?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>BncApps &#187; C++</title>
	<atom:link href="http://bncapps.com/category/cplusplus/feed/" rel="self" type="application/rss+xml" />
	<link>http://bncapps.com</link>
	<description>Open Source Applications and General Technology Blog</description>
	<lastBuildDate>Sat, 11 Dec 2010 23:48:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Shape objects in C++ (PSP)</title>
		<link>http://bncapps.com/shape-objects-in-c-psp/</link>
		<comments>http://bncapps.com/shape-objects-in-c-psp/#comments</comments>
		<pubDate>Mon, 15 Dec 2008 02:42:18 +0000</pubDate>
		<dc:creator>Brandon</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[psp]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[random number c++]]></category>
		<category><![CDATA[shape movements]]></category>
		<category><![CDATA[shape objects]]></category>

		<guid isPermaLink="false">http://bncapps.com/?p=206</guid>
		<description><![CDATA[We already discussed shape object creation and movements in VB.NET, but now lets take a look at it in C++. The principals are the same, create an Array or ArrayList for the rectangle object, and set its x and y position as well as its width, height and color. This example will show shape object [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://bncapps.com/?p=91">We already discussed shape object creation and movements in VB.NET</a>, but now lets take a look at it in C++.</p>
<p>The principals are the same, create an Array or ArrayList for the rectangle object, and set its x and y position as well as its width, height and color. This example will show shape object creation in C++. This code was originally written to run on the Playstation Portable (PSP) but if you know your way around C++, you should be able to get it running on windows without a problem.<br />
<center><script type="text/javascript"><!--
google_ad_client = "pub-5038888073735758";
google_ad_slot = "0739265609";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</center><br />
<span id="more-206"></span><br />
The compiled binary (EBOOT) for PSP for the full game will be posted in a few weeks. The original game was written in VB.NET (Posted soon!), then ported to Java (Posted in a few weeks) and now being ported to C++ for PSP. In the future it may be ported to Objective C so it can run on the iPhone/iPod touch.</p>
<p>Here is a useful function for generating a random number on the PSP in C++ (Given to me by thecobra over at the <a href="http://www.qj.net">qj.net</a> forums)</p>
<pre class="cpp">&nbsp;
<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">int</span> GetRandomNum<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">int</span> lo, <span style="color: #0000ff;">int</span> hi<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
 SceKernelUtilsMt19937Context ctx;
 <span style="color: #0000ff;">int</span> rs = <span style="color: #0000dd;">10</span> + <span style="color: #0000dd;">rand</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> % <span style="color: #000000;">&#40;</span><span style="color: #0000dd;">500</span> - <span style="color: #0000dd;">10</span><span style="color: #000000;">&#41;</span> + <span style="color: #0000dd;">1</span>;
 sceKernelUtilsMt19937Init<span style="color: #000000;">&#40;</span>&amp;ctx, rs<span style="color: #000000;">&#41;</span>; <span style="color: #ff0000;">//SEED TO TIME</span>
 u32 rand_val = sceKernelUtilsMt19937UInt<span style="color: #000000;">&#40;</span>&amp;ctx<span style="color: #000000;">&#41;</span>;
 rand_val = lo + rand_val % hi;
 <span style="color: #0000ff;">return</span> <span style="color: #000000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&#41;</span>rand_val;
<span style="color: #000000;">&#125;</span>
&nbsp;</pre>
<p>Although it looks more complex than the one in VB.NET, which is:</p>
<pre class="vbnet">&nbsp;
<span style="color: #0600FF;">Function</span> Random<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> Lowerbound <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Long</span>, <span style="color: #FF8000;">ByVal</span> Upperbound <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Long</span><span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">Randomize</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
Random = <span style="color: #0600FF;">Int</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">Rnd</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> * Upperbound<span style="color: #000000;">&#41;</span> + Lowerbound
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span>
&nbsp;</pre>
<p>The basic formula is the same</p>
<p>Here are some things we should declare</p>
<pre class="cpp">&nbsp;
 <span style="color: #0000ff;">int</span> squaresin; <span style="color: #ff0000;">//the total amount of vertical squares in play</span>
 <span style="color: #0000ff;">int</span> arrowDirs<span style="color: #000000;">&#91;</span><span style="color: #0000dd;">1000</span><span style="color: #000000;">&#93;</span>; <span style="color: #ff0000;">//The directions for movement of the vertical squares</span>
 <span style="color: #0000ff;">int</span> Hsquaresin; <span style="color: #ff0000;">//the total amount of horizonal squares in play</span>
 <span style="color: #0000ff;">int</span> HarrowDirs<span style="color: #000000;">&#91;</span><span style="color: #0000dd;">1000</span><span style="color: #000000;">&#93;</span>; <span style="color: #ff0000;">//The directions for movement of the horizontal squares</span>
Rectangle squares<span style="color: #000000;">&#91;</span><span style="color: #0000dd;">1000</span><span style="color: #000000;">&#93;</span>; <span style="color: #ff0000;">//declare the array of vertical squares</span>
Rectangle Hsquares<span style="color: #000000;">&#91;</span><span style="color: #0000dd;">1000</span><span style="color: #000000;">&#93;</span>; <span style="color: #ff0000;">//declare the array of horizontal squares</span>
Color red = RGB<span style="color: #000000;">&#40;</span><span style="color: #0000dd;">255</span>, <span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">0</span><span style="color: #000000;">&#41;</span>; <span style="color: #ff0000;">//declare various colours</span>
Color green = RGB<span style="color: #000000;">&#40;</span><span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">255</span>, <span style="color: #0000dd;">0</span><span style="color: #000000;">&#41;</span>;
Color blue = RGB<span style="color: #000000;">&#40;</span><span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">255</span><span style="color: #000000;">&#41;</span>;
Color yellow = RGB<span style="color: #000000;">&#40;</span><span style="color: #0000dd;">255</span>, <span style="color: #0000dd;">255</span>, <span style="color: #0000dd;">0</span><span style="color: #000000;">&#41;</span>;
&nbsp;</pre>
<p><strong>Note: There is no such thing as a boolean in C++, integers must be used. 1 represents true, 0 represents false.</strong></p>
<p>Next, lets use some methods to create the squares easily:</p>
<pre class="cpp">&nbsp;
	<span style="color: #0000ff;">void</span> addVertRectangle<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">int</span> Vertrectnum<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
     squares<span style="color: #000000;">&#91;</span>Vertrectnum<span style="color: #000000;">&#93;</span>.<span style="color: #00eeff;">x</span> = GetRandomNum<span style="color: #000000;">&#40;</span><span style="color: #0000dd;">1</span>, <span style="color: #0000dd;">480</span><span style="color: #000000;">&#41;</span>; <span style="color: #ff0000;">//random number between 1 and the dimensions of the psp screen</span>
     squares<span style="color: #000000;">&#91;</span>Vertrectnum<span style="color: #000000;">&#93;</span>.<span style="color: #00eeff;">y</span> = GetRandomNum<span style="color: #000000;">&#40;</span><span style="color: #0000dd;">1</span>, <span style="color: #0000dd;">272</span><span style="color: #000000;">&#41;</span>;
     squares<span style="color: #000000;">&#91;</span>Vertrectnum<span style="color: #000000;">&#93;</span>.<span style="color: #00eeff;">width</span> = <span style="color: #0000dd;">5</span>;
     squares<span style="color: #000000;">&#91;</span>Vertrectnum<span style="color: #000000;">&#93;</span>.<span style="color: #00eeff;">height</span> = <span style="color: #0000dd;">5</span>;
     squares<span style="color: #000000;">&#91;</span>Vertrectnum<span style="color: #000000;">&#93;</span>.<span style="color: #00eeff;">mycolor</span> = green; <span style="color: #ff0000;">//vertical squares are green</span>
   <span style="color: #000000;">&#125;</span>
&nbsp;
     <span style="color: #0000ff;">void</span> addHorizRectangle<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">int</span> Horizrectnum<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
     Hsquares<span style="color: #000000;">&#91;</span>Horizrectnum<span style="color: #000000;">&#93;</span>.<span style="color: #00eeff;">x</span> = GetRandomNum<span style="color: #000000;">&#40;</span><span style="color: #0000dd;">1</span>, <span style="color: #0000dd;">480</span><span style="color: #000000;">&#41;</span>;
     Hsquares<span style="color: #000000;">&#91;</span>Horizrectnum<span style="color: #000000;">&#93;</span>.<span style="color: #00eeff;">y</span> = GetRandomNum<span style="color: #000000;">&#40;</span><span style="color: #0000dd;">1</span>, <span style="color: #0000dd;">272</span><span style="color: #000000;">&#41;</span>;
     Hsquares<span style="color: #000000;">&#91;</span>Horizrectnum<span style="color: #000000;">&#93;</span>.<span style="color: #00eeff;">width</span> = <span style="color: #0000dd;">5</span>;
     Hsquares<span style="color: #000000;">&#91;</span>Horizrectnum<span style="color: #000000;">&#93;</span>.<span style="color: #00eeff;">height</span> = <span style="color: #0000dd;">5</span>;
     Hsquares<span style="color: #000000;">&#91;</span>Horizrectnum<span style="color: #000000;">&#93;</span>.<span style="color: #00eeff;">mycolor</span> = blue; <span style="color: #ff0000;">//horizontal squares are blue</span>
  <span style="color: #000000;">&#125;</span>
&nbsp;</pre>
<p>Put these within the main method. </p>
<p>Within our game loop we will need a few things. Here is the start of the game loop and the things we need within it. This is not the full code:</p>
<pre class="cpp">&nbsp;
&nbsp;
<span style="color: #0000ff;">while</span><span style="color: #000000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
SceCtrlData pad;
sceCtrlSetSamplingCycle<span style="color: #000000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #000000;">&#41;</span>;
pspDebugScreenSetXY<span style="color: #000000;">&#40;</span><span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">2</span><span style="color: #000000;">&#41;</span>;
sceCtrlReadBufferPositive<span style="color: #000000;">&#40;</span>&amp;pad, <span style="color: #0000dd;">1</span><span style="color: #000000;">&#41;</span>;
Refresh<span style="color: #000000;">&#40;</span>RGB<span style="color: #000000;">&#40;</span><span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
pspDebugScreenClear<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;</pre>
<p>Here is the code that goes within the game loop. This part handles movements and bouncing off the walls. (In the VB.NET and Java version, it works the excact same way!)</p>
<pre class="cpp">&nbsp;
<span style="color: #0000ff;">int</span> j;
   <span style="color: #0000ff;">for</span> <span style="color: #000000;">&#40;</span>j = <span style="color: #0000dd;">0</span>; j &lt; squaresin; j ++<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
       <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>arrowDirs<span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span> == <span style="color: #0000dd;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
    squares<span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span>.<span style="color: #00eeff;">x</span> = squares<span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span>.<span style="color: #00eeff;">x</span> - <span style="color: #0000dd;">1</span>;
<span style="color: #000000;">&#125;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&#123;</span>
      squares<span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span>.<span style="color: #00eeff;">x</span> = squares<span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span>.<span style="color: #00eeff;">x</span> + <span style="color: #0000dd;">1</span>;
      <span style="color: #000000;">&#125;</span>
      <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>squares<span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span>.<span style="color: #00eeff;">x</span> &lt;= <span style="color: #0000dd;">5</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
      arrowDirs<span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span> = <span style="color: #0000dd;">1</span>;
      <span style="color: #000000;">&#125;</span>
      <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>squares<span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span>.<span style="color: #00eeff;">x</span> &gt;= <span style="color: #000000;">&#40;</span><span style="color: #0000dd;">480</span> - squares<span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span>.<span style="color: #00eeff;">width</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
      arrowDirs<span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span> = <span style="color: #0000dd;">0</span>;
      <span style="color: #000000;">&#125;</span>
    Draw<span style="color: #000000;">&#40;</span>squares<span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> k;
   <span style="color: #0000ff;">for</span> <span style="color: #000000;">&#40;</span>k = <span style="color: #0000dd;">0</span>; k &lt; Hsquaresin; k ++<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
       <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>HarrowDirs<span style="color: #000000;">&#91;</span>k<span style="color: #000000;">&#93;</span> == <span style="color: #0000dd;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
    Hsquares<span style="color: #000000;">&#91;</span>k<span style="color: #000000;">&#93;</span>.<span style="color: #00eeff;">y</span> = Hsquares<span style="color: #000000;">&#91;</span>k<span style="color: #000000;">&#93;</span>.<span style="color: #00eeff;">y</span> - <span style="color: #0000dd;">1</span>;
<span style="color: #000000;">&#125;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&#123;</span>
      Hsquares<span style="color: #000000;">&#91;</span>k<span style="color: #000000;">&#93;</span>.<span style="color: #00eeff;">y</span> = Hsquares<span style="color: #000000;">&#91;</span>k<span style="color: #000000;">&#93;</span>.<span style="color: #00eeff;">y</span> + <span style="color: #0000dd;">1</span>;
      <span style="color: #000000;">&#125;</span>
      <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>Hsquares<span style="color: #000000;">&#91;</span>k<span style="color: #000000;">&#93;</span>.<span style="color: #00eeff;">y</span> &lt;= <span style="color: #0000dd;">5</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
      HarrowDirs<span style="color: #000000;">&#91;</span>k<span style="color: #000000;">&#93;</span> = <span style="color: #0000dd;">1</span>;
      <span style="color: #000000;">&#125;</span>
      <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>Hsquares<span style="color: #000000;">&#91;</span>k<span style="color: #000000;">&#93;</span>.<span style="color: #00eeff;">y</span> &gt;= <span style="color: #000000;">&#40;</span><span style="color: #0000dd;">272</span> - Hsquares<span style="color: #000000;">&#91;</span>k<span style="color: #000000;">&#93;</span>.<span style="color: #00eeff;">height</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
      HarrowDirs<span style="color: #000000;">&#91;</span>k<span style="color: #000000;">&#93;</span> = <span style="color: #0000dd;">0</span>;
      <span style="color: #000000;">&#125;</span>
    Draw<span style="color: #000000;">&#40;</span>Hsquares<span style="color: #000000;">&#91;</span>k<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;</pre>
<p>Heres a quick example on adding new shapes by clicking the O button:</p>
<pre class="cpp">&nbsp;
 <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>pad.<span style="color: #00eeff;">Buttons</span> &amp; PSP_CTRL_CROSS<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
                                   squaresin++;
                                   Hsquaresin++;
                                   addVertRectangle<span style="color: #000000;">&#40;</span>squaresin<span style="color: #000000;">&#41;</span>;
                                   addHorizRectangle<span style="color: #000000;">&#40;</span>Hsquaresin<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;</pre>
<p>Finally, finish the game loop with:</p>
<pre class="cpp">&nbsp;
&nbsp;
  pspDebugScreenClear<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #ff0000;">//Necessary</span>
 sceDisplayWaitVblankStart<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
 flipScreen<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #000000;">&#125;</span>
&nbsp;</pre>
<p>This tutorial may be a bit hard to follow. When the game is fully done the full source and a longer tutorial will be posted. In a few days a tutorial will be up on getting your development environment set up to begin programming for the PSP.</p>
<p>I will like to thank <a href="http://forums.qj.net/member.php?u=126875">thecobra</a> over at the qj.net forums for helping me get started in PSP programming!</p>
<p><center><script type="text/javascript"><!--
google_ad_client = "pub-5038888073735758";
google_ad_slot = "5539939403";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</center></p>
]]></content:encoded>
			<wfw:commentRss>http://bncapps.com/shape-objects-in-c-psp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
