Android Game Loop: J2ME style

This post shows an empty basic game loop in Android, similar to how we used to make game loops on the J2ME platform.

The View implements Runnable and starts a render thread itself. An interesting thing is that we can’t invalidate the View from its own thread, as invalidate must be called from the main UI thread only. Instead we use postInvalidate which sends a message to the UI thread’s message queue. The consequences are that drawing is not performed immediately, but only when the UI thread handles the message.

SimpleXML Parse Tags with Different Namespaces

It’s a little bit tricky to parse and use XML data when tags have different namespaces. We would have to use the namespace string when accessing the objects like below with $dc_namespaces:

XML with two different namespaces

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<!-- xmldata.xml -->
<rootnode xmlns="http://mydomain.com/2010">
    <node>
        <title>This is the first Node</title>
        <meta>
            <dc:language>en</dc:language>
        </meta>
    </node>
    <node>
        <title>This is the second Node</title>
        <meta>
            <dc:language>en</dc:language>
        </meta>
    </node>
</rootnode>

PHP code to access the parsed object:

1
2
3
4
5
6
7
8
9
<?php
$dc_namespace = 'http://purl.org/dc/elements/1.1/';

$data = simplexml_load_file('xmldata.xml');
foreach($data as $node)
{
    $title = $node->title;
    $lang  = $node->meta->children($dc_namespace)->language;
}

Create a jQuery function

A simple example for creating a jQuery function called setPosition which can be used to set the top and left CSS properties of an element.

1
2
3
4
5
6
jQuery.fn.setPosition = function(x, y) {
   this.css("position", "absolute");
   this.css("top", y);
   this.css("left", x);
   return this;
}

The function can be used like this:

1
$('#myid').setPosition(100, 200);

PHP SOAP Request Authentication

tip
How to authenticate the call to a SOAP web service with PHP 5+

Below is the authenticated request we want to build. It carries a username and password authentication header.

SOAP XML request

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <SOAP-ENV:Header>
    <m:SOAPAuthenticationHeader xmlns:m="http://www.mydomain.com">
      <m:username>myusername</m:username>
      <m:password>mypassword</m:password>
    </m:SOAPAuthenticationHeader>
  </SOAP-ENV:Header>
  <SOAP-ENV:Body>
    <m:WebServiceMethodName xmlns:m="http://www.mydomain.com">
      <m:param1>some value</m:param1>
      <m:param2>some value</m:param2>
      <m:param3>some value</m:param3>
    </m:WebServiceMethodName>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Use Oracle DB from PHP

tnsnames.ora configuration excerpt:

1
2
3
4
5
MYSERVICE =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = tcp)(HOST = database_hostname_or_ip.com)(PORT = 1521))
(CONNECT_DATA =
(SERVICE_NAME=myservice)))

Connect and execute a prepared statement from PHP code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
$oracledb["host"] = "MYSERVICE"; # service name in the tnsnames.ora file
$oracledb["user"] = "myuser"; # username
$oracledb["pass"] = "mypass"; # password
$oracledb["library"] = "OCI";

$connect_id = ocilogon($oracledb["user"], $oracledb["pass"], $oracledb["host"]);

$query = "SELECT * FROM table";
$statement = ociparse($connect_id, $query);
ociexecute($statement);

$result = array();
while(ocifetchinto($statement, $tmp, OCI_ASSOC + OCI_RETURN_NULLS + OCI_RETURN_LOBS))
{
  array_push($result, $tmp);
}

ocifreestatement($statement);

var_dump($result); # result is here

Attitude Matters Too

note
Excerpt from “The Art of Unix Programming” by Eric Steven Raymond

“When you see the right thing, do it — this may look like more work in the short term, but it’s the path of least effort in the long run. If you don’t know what the right thing is, do the minimum necessary to get the job done, at least until you figure out what the right thing is. To do the Unix philosophy right, you have to be loyal to excellence. You have to believe that software design is a craft worth all the intelligence, creativity, and passion you can muster. Otherwise you won’t look past the easy, stereotyped ways of approaching design and implementation; you’ll rush into coding when you should be thinking. You’ll carelessly complicate when you should be relentlessly simplifying — and then you’ll wonder why your code bloats and debugging is so hard. To do the Unix philosophy right, you have to value your own time enough never to waste it. If someone has already solved a problem once, don’t let pride or politics suck you into solving it a second time rather than re-using. And never work harder than you have to; work smarter instead, and save the extra effort for when you need it. Lean on your tools and automate everything you can.

Software design and implementation should be a joyous art, a kind of high-level play. If this attitude seems preposterous or vaguely embarrassing to you, stop and think; ask yourself what you’ve forgotten. Why do you design software instead of doing something else to make money or pass the time? You must have thought software was worthy of your passion once…. To do the Unix philosophy right, you need to have (or recover) that attitude. You need to care. You need to play. You need to be willing to explore.”