<?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>Herberth Amaral &#187; hello world</title> <atom:link href="http://herberthamaral.com/tag/hello-world/feed/" rel="self" type="application/rss+xml" /><link>http://herberthamaral.com</link> <description>Software development adventures</description> <lastBuildDate>Thu, 26 Aug 2010 13:59:42 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.0.1</generator> <item><title>Usando o SabreAMF com o CakePHP</title><link>http://herberthamaral.com/2009/12/usando-o-sabreamf-com-o-cakephp/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=usando-o-sabreamf-com-o-cakephp</link> <comments>http://herberthamaral.com/2009/12/usando-o-sabreamf-com-o-cakephp/#comments</comments> <pubDate>Tue, 15 Dec 2009 21:25:33 +0000</pubDate> <dc:creator>Herberth Amaral</dc:creator> <category><![CDATA[Flex & Actionscript]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[cakephp]]></category> <category><![CDATA[flex]]></category> <category><![CDATA[hello world]]></category> <category><![CDATA[sabreamf]]></category><guid isPermaLink="false">http://herberthamaral.com/?p=66</guid> <description><![CDATA[O AMF é um formato aberto de comunicação do Flash. É o formato preferido para tais aplicações por possuir diversas vantagens, sendo que a principal delas é a velocidade, já que o AMF não precisa ser deserializado como o XML e o JSON. Por ser um formato aberto, ele pode ser implementado em qualquer linguagem. [...]]]></description> <content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"> <a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fherberthamaral.com%2F2009%2F12%2Fusando-o-sabreamf-com-o-cakephp%2F"> <img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fherberthamaral.com%2F2009%2F12%2Fusando-o-sabreamf-com-o-cakephp%2F&amp;source=HerberthAmaral&amp;style=normal" height="61" width="50" /> </a></div><p>O <a href="http://en.wikipedia.org/wiki/Action_Message_Format" target="_blank">AMF</a> é um formato aberto de comunicação do Flash. É o formato preferido para tais aplicações por possuir diversas vantagens, sendo que a principal delas é a velocidade, já que o AMF não precisa ser deserializado como o XML e o JSON.</p><p>Por ser um formato aberto, ele pode ser implementado em qualquer linguagem. Há várias implementações para o PHP, sendo que as mais notáveis são o AMFPHP, Zend_AMF e o SabreAMF.</p><p>Neste tutorial, eu mostrarei como desenvolver aplicações em PHP com o framework CakePHP que se comunicam com o Flash (usando a plataforma Flex 3) através da biblioteca SabreAMF.</p><h4>1- Baixando e instalando os componentes</h4><p>1 &#8211; Baixe os CakePHP 1.2.5 no <a href="http://www.cakephp.org" target="_blank">site do CakePHP</a> e o SabreAMF no <a href="http://code.google.com/p/sabreamf/downloads/list" target="_blank">Google Code</a>.</p><p>2- <a href="http://book.cakephp.org/pt/view/308/Installing-CakePHP" target="_blank">Instale o CakePHP</a> de forma que ele fique disponível em http://localhost/cakeflex.</p><p>3 &#8211; Descompacte o SabreAmf em app/vendors/SabreAMF</p><h4>2 &#8211; Colocando os dois para trabalhar</h4><p>Para fazer com que o CakePHP trabalhe de forma transparente com o SabreAMF, é necessário<a href="http://book.cakephp.org/view/64/Creating-Components"> criar um componente para o CakePHP</a>.</p><p>Para tal, vamos criar o nosso componente: (salve-o em app/controllers/components/sabre_amf.php)</p><div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br /> <br /> <a href="http://www.php.net/set_include_path"><span style="color: #990000;">set_include_path</span></a><span style="color: #009900;">&#40;</span>APP<span style="color: #339933;">.</span><span style="color: #0000ff;">'vendors'</span> <span style="color: #339933;">.</span> PATH_SEPARATOR <span style="color: #339933;">.</span> <a href="http://www.php.net/get_include_path"><span style="color: #990000;">get_include_path</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> App<span style="color: #339933;">::</span><span style="color: #004000;">import</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Vendor'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'SabreAMF_CallbackServer'</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'file'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'SabreAMF/CallbackServer.php'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> <br /> <span style="color: #000000; font-weight: bold;">class</span> SabreAmfComponent <span style="color: #000000; font-weight: bold;">extends</span> Object <span style="color: #009900;">&#123;</span><br /> <br /> &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> startup<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span><span style="color: #000088;">$controller</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$controller</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">action</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'gateway'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$_cakeController</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$_cakeController</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$controller</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Configure<span style="color: #339933;">::</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'debug'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$controller</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">autoRender</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$server</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SabreAMF_CallbackServer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$server</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">onInvokeService</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'amfCallBack'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$server</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">exec</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/exit"><span style="color: #990000;">exit</span></a><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$controller</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">amfExclude</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">!</span><a href="http://www.php.net/in_array"><span style="color: #990000;">in_array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$controller</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">action</span><span style="color: #339933;">,</span><span style="color: #000088;">$controller</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">amfExclude</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/exit"><span style="color: #990000;">exit</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /> <br /> &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> amfCallBack<span style="color: #009900;">&#40;</span><span style="color: #000088;">$service</span><span style="color: #339933;">,</span> <span style="color: #000088;">$method</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$_cakeController</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_cakeController</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/strpos"><span style="color: #990000;">strpos</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$method</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;_&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/method_exists"><span style="color: #990000;">method_exists</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_cakeController</span><span style="color: #339933;">,</span> <span style="color: #000088;">$method</span><span style="color: #009900;">&#41;</span> &nbsp;and<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">!</span><a href="http://www.php.net/in_array"><span style="color: #990000;">in_array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_cakeController</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">action</span><span style="color: #339933;">,</span><span style="color: #000088;">$_cakeController</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">amfExclude</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/call_user_func_array"><span style="color: #990000;">call_user_func_array</span></a><span style="color: #009900;">&#40;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$_cakeController</span><span style="color: #339933;">,</span> <span style="color: #000088;">$method</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Action nao encontrada.&quot;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Nome de metodo invalido.&quot;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Controller nao encontrado.&quot;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$res</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /> <span style="color: #009900;">&#125;</span><br /> <span style="color: #000000; font-weight: bold;">?&gt;</span></div></div><p>O que nós fizemos acima foi redirecionar todas as chamadas à action &#8216;gateway&#8217; do controller para nosso gateway do SabreAMF.</p><p>Agora, um exemplo de como utilizar isso em um controller:</p><div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br /> <span style="color: #666666; font-style: italic;">// app/controllers/teste_amf_controller.php</span><br /> <span style="color: #000000; font-weight: bold;">class</span> TesteAmfController <span style="color: #000000; font-weight: bold;">extends</span> AppController<br /> <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$components</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SabreAmf'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> <br /> &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> hello<span style="color: #009900;">&#40;</span><span style="color: #000088;">$nome</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$ret</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Olá, &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$nome</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;! agora são: &quot;</span><span style="color: #339933;">.</span><a href="http://www.php.net/date"><span style="color: #990000;">date</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;H:i:s&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; horas&quot;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> &nbsp;<span style="color: #000088;">$ret</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /> <span style="color: #009900;">&#125;</span><br /> <span style="color: #000000; font-weight: bold;">?&gt;</span></div></div><p>Nosso server side não estaria completo sem o nosso model. No meu caso, como é só uma demonstração, eu não quero criar uma tabela no banco de dados. Portanto, eu crio o meu model dessa forma:</p><div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br /> <span style="color: #666666; font-style: italic;">// app/models/teste_amf.php</span><br /> <span style="color: #000000; font-weight: bold;">class</span> TesteAmf <span style="color: #000000; font-weight: bold;">extends</span> AppModel<br /> <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$useTable</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span><br /> <span style="color: #009900;">&#125;</span><br /> <span style="color: #000000; font-weight: bold;">?&gt;</span></div></div><p>Para demonstrar nosso código funcionado, nada melhor que uma aplicação Flex. Segue abaixo, um exemplo de como a configuração criada acima pode ser usada no Flex:</p><pre>
&lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"&gt;

	&lt;mx:RemoteObject
		endpoint="http://localhost/cakeflex/teste_amf/gateway"
		id="rmoTeste"
		source=""
		destination="minhaApp"
		result="rmoTesteResult(event)"
		fault="rmoTesteFail(event)"
		showBusyCursor="true"
		/&gt;

        &lt;mx:Button label="Hello!" click="rmoTeste.hello('Herberth')" /&gt;
        &lt;mx:Script&gt;
		&lt; ![CDATA[
			import mx.rpc.events.FaultEvent;
			import mx.rpc.events.ResultEvent;
			import mx.controls.Alert;

			public function rmoTesteResult(e:ResultEvent):void
			{
				Alert.show(e.result.toString());
			}

			public function rmoTesteFail(e:FaultEvent):void
			{
				Alert.show("Fail!");
			}
		]]&gt;
	&lt;/mx:Script&gt;
&lt;/mx:Application&gt;
</pre><h4>4 &#8211; Conclusão</h4><p>O resultado final deve ficar parecido com isso:</p><p><img class="alignnone size-full wp-image-81" title="hello_flex" src="http://herberthamaral.com/wp-content/uploads/2009/12/hello_flex.png" alt="hello_flex" width="525" height="272" /></p><p>É possível retornar tipos mais complexos para serem usados em tipos mais complexos (tipo AdvancedDataGrid), mas irei abordar tal aplicação mais futuramente.</p><h4>5 &#8211; Referências</h4><ul><li><a href="http://blog.non-style.com/2008/11/cakephp-airsabreamfcakephp.html">http://blog.non-style.com/2008/11/cakephp-airsabreamfcakephp.html</a></li></ul><p>É isso aí pessoal. Até a próxima!</p><p>Sem posts relacionados.</p>]]></content:encoded> <wfw:commentRss>http://herberthamaral.com/2009/12/usando-o-sabreamf-com-o-cakephp/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Hello World de Python + OpenGL</title><link>http://herberthamaral.com/2009/11/hello-world-de-python-opengl/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=hello-world-de-python-opengl</link> <comments>http://herberthamaral.com/2009/11/hello-world-de-python-opengl/#comments</comments> <pubDate>Wed, 25 Nov 2009 02:51:32 +0000</pubDate> <dc:creator>Herberth Amaral</dc:creator> <category><![CDATA[Python]]></category> <category><![CDATA[hello world]]></category> <category><![CDATA[opengl]]></category> <category><![CDATA[unimontes]]></category><guid isPermaLink="false">http://herberthamaral.com/?p=10</guid> <description><![CDATA[Há quase um ano eu comecei a estudar Python e gostei de muitas características da linguagem, como o fato de ser de altíssimo nível (very high level), sintaxe clara e elegante, poderosa, portável,  super rápida,fácil aprendizado e um dos melhores: tipagem forte e dinâmica. Gostei tanto que comecei a fazer alguns trabalhos de faculdade (mais [...]]]></description> <content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"> <a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fherberthamaral.com%2F2009%2F11%2Fhello-world-de-python-opengl%2F"> <img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fherberthamaral.com%2F2009%2F11%2Fhello-world-de-python-opengl%2F&amp;source=HerberthAmaral&amp;style=normal" height="61" width="50" /> </a></div><p>Há quase um ano eu comecei a estudar <a title="Python" href="http://www.python.org" target="_blank">Python </a>e gostei de muitas características da linguagem, como o fato de ser de altíssimo nível (<em>very high level</em>), sintaxe clara e elegante, poderosa, portável,  super rápida,fácil aprendizado e um dos melhores: tipagem forte e dinâmica.</p><p>Gostei tanto que comecei a fazer alguns trabalhos de faculdade (mais notavelmente trabalhos nas áreas de Banco de Dados, Inteligência Artificial e, recentemente, Computação Gráfica) usando Python. Para vários trabalhos eu encontrei <em>snippets </em> de código, mas não foi tão fácil para CG usando OpenGL. Por isso, resolvi postar esse pequeno exemplo de como usar Python (na versão 2.6.4) + OpenGL.</p><p>Primeiro, baixe o PyOpenGL em <a href="http://pypi.python.org/pypi/PyOpenGL">http://pypi.python.org/pypi/PyOpenGL</a>, descompacte e execute os comandos para instalar o pacote:´</p><div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$python setup.py build<br /> $sudo python setup.py install</div></div><p>Lembrando que se você estiver no Windows, o executável do Python deverá estar no seu PATH.</p><p>Instalado o pacote, é hora de fazer um teste. Abra o shell do Python e digite isso:</p><div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #ff7700;font-weight:bold;">import</span> OpenGL</div></div><p>Se não aparecer nenhuma mensagem de erro, o pacote do OpenGL foi instalado com sucesso. Agora é hora de uma pequena demonstração de uso do PyOpenGL:</p><div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">from</span> OpenGL.<span style="color: #dc143c;">GL</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span><br /> <span style="color: #ff7700;font-weight:bold;">from</span> OpenGL.<span style="color: black;">GLUT</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span><br /> <br /> <span style="color: #ff7700;font-weight:bold;">def</span> display<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:<br /> &nbsp; glClear <span style="color: black;">&#40;</span>GL_COLOR_BUFFER_BIT<span style="color: black;">&#41;</span><br /> &nbsp; glBegin <span style="color: black;">&#40;</span>GL_POLYGON<span style="color: black;">&#41;</span><br /> &nbsp; glVertex2f<span style="color: black;">&#40;</span><span style="color: #ff4500;">0.4</span>,<span style="color: #ff4500;">0.4</span><span style="color: black;">&#41;</span><br /> &nbsp; glVertex2f<span style="color: black;">&#40;</span><span style="color: #ff4500;">0.01</span>,<span style="color: #ff4500;">0.01</span><span style="color: black;">&#41;</span><br /> &nbsp; glVertex2f<span style="color: black;">&#40;</span><span style="color: #ff4500;">0.3</span>,<span style="color: #ff4500;">0.6</span><span style="color: black;">&#41;</span><br /> &nbsp; glEnd<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br /> &nbsp; glFlush<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br /> glutInit <span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br /> glutInitDisplayMode <span style="color: black;">&#40;</span>GLUT_SINGLE | GLUT_RGB<span style="color: black;">&#41;</span><br /> glutInitWindowSize <span style="color: black;">&#40;</span><span style="color: #ff4500;">250</span>,<span style="color: #ff4500;">250</span><span style="color: black;">&#41;</span><br /> glutInitWindowPosition <span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>,<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span><br /> glutCreateWindow <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Testando o PyGraphs&quot;</span><span style="color: black;">&#41;</span><br /> glClearColor <span style="color: black;">&#40;</span><span style="color: #ff4500;">0.0</span>, <span style="color: #ff4500;">0.0</span>, <span style="color: #ff4500;">0.0</span>, <span style="color: #ff4500;">0.0</span><span style="color: black;">&#41;</span><br /> glOrtho <span style="color: black;">&#40;</span><span style="color: #ff4500;">0.0</span>, <span style="color: #ff4500;">1.0</span>, <span style="color: #ff4500;">0.0</span>, <span style="color: #ff4500;">1.0</span>, -<span style="color: #ff4500;">1.0</span>, <span style="color: #ff4500;">1.0</span><span style="color: black;">&#41;</span><br /> glutDisplayFunc<span style="color: black;">&#40;</span>display<span style="color: black;">&#41;</span><br /> glutMainLoop <span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div></div><p>A API do OpenGL pro Python segue as mesmas convenções da API para C/C++. Então, todo e qualquer exemplo que você achar na Web em C/C++ é compatível com o Python (as vezes com algumas pequenas alterações de tipos, mas o nome das funções continua o mesmo).</p><p>Se tudo estiver ok, você deverá ver algo parecido com a seguinte figura:</p><p><img class="alignnone size-full wp-image-16" title="pygraph" src="http://herberthamaral.com/wp-content/uploads/2009/11/pygraph.png" alt="pygraph" width="267" height="289" /></p><p>Alguns links úteis:</p><ul><li><a href="http://www.opengl.org/sdk/docs/man/">http://www.opengl.org/sdk/docs/man/</a></li><li><a href="http://www.inf.pucrs.br/~manssour/OpenGL" target="_blank">www.inf.pucrs.br/~manssour/OpenGL</a></li><li><a href="http://nehe.gamedev.net" target="_blank">nehe.gamedev.net</a></li></ul><p>Acho que é isso. Até a próxima!</p><p>Sem posts relacionados.</p>]]></content:encoded> <wfw:commentRss>http://herberthamaral.com/2009/11/hello-world-de-python-opengl/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Hello world!</title><link>http://herberthamaral.com/2009/11/hello-world/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=hello-world</link> <comments>http://herberthamaral.com/2009/11/hello-world/#comments</comments> <pubDate>Sat, 21 Nov 2009 03:27:03 +0000</pubDate> <dc:creator>Herberth Amaral</dc:creator> <category><![CDATA[News]]></category> <category><![CDATA[hello world]]></category> <category><![CDATA[new]]></category><guid isPermaLink="false">http://herberthamaral.com/?p=1</guid> <description><![CDATA[Buenas! Finalmente eu cumpri a promessa de criar meu blog. Já estava na hora, né? Aqui eu quero fazer jus à palavra/expressão Web Log: pretendo postar sobre meu cotidiano, principalmente a parte envolvendo tecnologia. Atualmente o meu foco é desenvolvimento para a Web e isso inclui tecnologias como JavaScript, .NET, Flex, PHP, Python e seus respectivos frameworks [...]]]></description> <content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"> <a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fherberthamaral.com%2F2009%2F11%2Fhello-world%2F"> <img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fherberthamaral.com%2F2009%2F11%2Fhello-world%2F&amp;source=HerberthAmaral&amp;style=normal" height="61" width="50" /> </a></div><p>Buenas!</p><p>Finalmente eu cumpri a promessa de criar meu blog. Já estava na hora, né? <img src='http://herberthamaral.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /></p><p>Aqui eu quero fazer jus à palavra/expressão <strong>Web Log: </strong>pretendo postar sobre meu cotidiano, principalmente a parte envolvendo tecnologia.</p><p>Atualmente o meu foco é desenvolvimento para a Web e isso inclui tecnologias como JavaScript, .NET, Flex, PHP, Python e seus respectivos frameworks que eu estudo: jQuery, ASP.NET MVC, PureMVC, CodeIgniter e CakePHP, Django.</p><p>Além de tecnologias, metodologias de desenvolvimento têm feito parte do meu dia-a-dia. Então é possível que eu fale muito sobre Scrum, XP, Lean e vários conceitos agregados:  TDD, Pair Programming, Continuous Integration, User Stories e etc.</p><p>Hope you enjoy! <img src='http://herberthamaral.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /></p><p>Sem posts relacionados.</p>]]></content:encoded> <wfw:commentRss>http://herberthamaral.com/2009/11/hello-world/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> </channel> </rss>