HTML5 and gaming

HTML5 Powered with CSS3 / Styling, Device Access, Graphics, 3D & Effects, Performance & Integration, Semantics, and Offline & Storage

and specifically game development.

So initially I wanted to make a board game (backgammon) with the help of some HTML5 (with game features) library.

Here are some libraries out there with game dev features:

Finally - and after wasting some time researching / building with some libraries - I decided LimeJs would be the one.

LimeJS is a HTML5 game framework based on Closure Library by Google and extends Closure Library by adding many classes useful for game development like: Animation, audio, director (which is the base), scene (inside director), layer (inside a scene), sprite etc. For rendering DOM, canvas and WebGL are supported (latter has been added recently).

The HTML5 framework is compatible with all the major browsers and also supports touch events by touch devices. More in their site with: documentation, demos and a very good guide.

Another idea also was starting without any library - straight with Javascript language.

I also then decided to start with something simpler, as a game / test / experiment: Puzzler, a variation of jigsaw puzzle which I had made some time ago as a windows game (see Old games)

So here it is Puzzler in HTML5: Click to play

And here is the source code at GitHub.

Old games

I uploaded some old games that I had made about a decade++ ago and are still capable of running on modern Windows systems:

I suppose I really lke puzzle games :P

Issues running these games (tested on a Windows Vista system):

Agonia: Help does not work; a visit on this wikipedia greek page (game rules) solves the problem :)

Tetricom: right click on tetricom.exe file before running the game -> properties -> compatibility -> select 'Run in 256 colors'.

I had these games on a Geocities page but now this service is history.

HTML5 demos

A couple of months ago I made 2 demos for Mozilla DemoStudio, taking also part in the monthly contests for August & September.

The context for the first contest was the History API so I created a Calendar. In this you can go to the past and the future and create events using the browser's History. So I made a time machine! why didn't they gave me the first place? :P I also created an improved version that saves events locally, using localStorage and also added mouse support for selecting a day (has some issues in Chrome though).

The second context was the GeoLocation object and I created 'Cities guide'. The demo asks for your location and loads some cities info so you can view the nearest cities, sort by population and view other useful information. You can also visit (or view) other countries cities. Again a great demo! still wondering why didn't I get the first place :P

I didn't took any of the first places in the contests, but I enjoyed writing the demos both and also I learned some useful stuff like:

  • History object in Firefox can have up to 50 items.
  • localStorage: is very easy, just like set - get a cookie.
  • JSON.stringify with a hash table just doesn't work, instead use an object.
  • Javascript has (like many programming languages) a sort function using user-defined comparison function.
  • sort function is faster in Chrome than Firefox.
  • some HTML5 elements like: section, aside, article, header, etc. (didn't really used them; Google maps had issues with section, aside)
  • India is the second country (after United States) that has more cities with 5000 or more population.

Both demos have been tested only in latest Firefox and Chrome.

Canvas is another object I'm trying to learn now, not for a contest this time but maybe a game.

Resizing / converting images in php

One great (third party) tool to resize your images if available is imagemagick's command line tool: convert,  with many options and features. Here is an example:

passthru("convert -normalize -background white -quality 95 -resize x768\> -resize 1280x\> -support 0.8 +repage -interlace plane $tmpFile $destFile");

(will convert an image with maximum width 1024 or maximum height 768)

Now how about if this tool is not available?

Php can also be used to create and manipulate image files if compiled with the GD library (read more here)
So here comes a handy function that can be used for resizing both thumbnails and normal size images:


function ResizeImage($originalImage, $toWidth, $toHeight, $filename, $thumb = false)
{
    // Get the original geometry and calculate scales
    list($width, $height) = getimagesize($originalImage);
    $xscale = $width/$toWidth;
    $yscale = $height/$toHeight;

    // Recalculate new size with default ratio
    $offsetX = 0;
    $offsetY = 0;
    if ($thumb)
    {
        $new_width = $toWidth;
        $new_height = $toHeight;

        $ratioComputed = $width / $height;
        $cropRatioComputed = (float) 1 / (float) 1;

        if ($ratioComputed < $cropRatioComputed)
        { // Image is too tall so we will crop the top and bottom
            $origHeight= $height;
            $height= $width / $cropRatioComputed;
            $offsetY= ($origHeight - $height) / 2;
        }
        else if ($ratioComputed > $cropRatioComputed)
        { // Image is too wide so we will crop off the left and right sides
            $origWidth= $width;
            $width= $height * $cropRatioComputed;
            $offsetX= ($origWidth - $width) / 2;
        }

    }
    else
    {
        if ($yscale>$xscale)
        {
            $new_width = round($width * (1/$yscale));
            $new_height = round($height * (1/$yscale));
        }
        else
        {
            $new_width = round($width * (1/$xscale));
            $new_height = round($height * (1/$xscale));
        }
    }

    // Resize the original image
    $imageResized = imagecreatetruecolor($new_width, $new_height);
    $imageTmp     = imagecreatefromjpeg ($originalImage);
    imagecopyresampled($imageResized, $imageTmp, 0, 0,  $offsetX, $offsetY, $new_width, $new_height, $width, $height);

    return (imagejpeg($imageResized, $filename));
}

PHP usort: sort an array of records using a particular sequence

With PHP's usort function you are sorting an array using a custom function you have made:

function sort_by_title($a,$b)
{

  return strcasecmp($a['title'], $b['title']);

}
$myArray[0]['id'] = 1;
$myArray[0]['title'] = 'Hello';
$myArray[1]['id'] = 5;
$myArray[1]['title'] = 'Foo';
$myArray[2]['id'] = 6;
$myArray[2]['title'] = 'Foo etc';
.... and so on

usort($myArray, 'sort_by_title');

How about sorting the same array using a particular sequence of either ids, titles or whatever field of the record (using an array)?

Here is the solution:

(this example sorts the above array by a sequence that refers to the title field)

function sort_by_seq($a,$b)
{

  $seq = array('Foo', 'Hello', 'Foo etc');
  return array_search($a['title'], $seq) - array_search($b['title'], $seq);

}
usort($myArray, 'sort_by_seq');


Check out also another (sql related) tip for sorting sql result rows:

http://programming.pblogs.gr/general-tips.html which will be updated continuity with new tips.

Firefox add-ons

Back to posts...img1.jpg


So I just made my first Firefox add-on: 'Bookmarks menu'
More on that here.


A good place to start is Mozilla developers center and also "How to develop a Firefox extension"
What you will need is some html, javascript and XUL (XML User Interface Language) knowledge in order to make a Firefox add-on, time and a lot of Firefox restarts.
Well you may avoid the last since there is a helpful add-on called "Extension Developer".


Also recently Mozilla launched Jetpack an early beta of a project that will allow any web developer to build an extension easily as building a web site

Pingbacks

Well markp added pingbacksto our pblogs. It kinda replaced trackbacks.


So checking if it works!

Blocksum

0111.jpgBlocksum is a great freeware puzzle game I discovered a few days ago.

Really addictive!

New pathfinderzzz

After Patfhinder Avatars, now we have a new Pathfinder Signup page!

That page uses real time checking for the user name the user picks does not exist and also for the rest fields required for the sign up process.

DirectX programming

At my previous programming era, I did some DirectX programming
with Borland Delphi (pascal language), building at that time Tetricom (a tetris - TetriNet clone).
Now I decided to get involved with C++ and DirectX (having already some
expirience with C++ at my work).
With joy I learned that Visual C++ is available as a free download!

So I downloaded and installed the following (all free downloads):


After some trials, failures and google searches I managed to compile succesfully my
first DirectX program in C++!