Rss

Archives for : Code

#1GAM for March: Learning experience

I am not going to be completing my #1GAM project, unfortunately. I do have an executable download for Mac and could wrap up the Android/iOS stuff, but it’s all just so meh that it’s not really worth sharing. I ended up spending a lot of time learning about and then modifying the game framework to get it to do what I needed it to do, and if I continue working on March’s game, I won’t be able to start on nor finish April’s (and then May will get pushed, and so on).

Instead of completing the game, I set out to learn how to do basic things like build for multiple platforms, play audio (more like, where to put the files and where NOT to put the files), animate sprites in phases (turn-based game so pre-movement and post-movement), save and load files (framework didn’t support multiple files), and configurable keyboard controls (framework didn’t support keyboard at all). All sorts of basics.

This might count as an excuse, I dunno. I’m calling March a learning experience. Now that these basics are out of the way I think I’m truly ready for the #1GAM April challenge.

Simple Python script to strip EXIF data from a JPEG

I needed a way to strip EXIF data from JPEGs in Python. All of the methods I found relied on external C/C++ libraries or shell execs which, unfortunately, meant I could not use ‘em — I can’t install external libraries or exec in my environment.

Luckily I found an easy to understand description of the EXIF spec and used it to make this python script:

import struct
def stripExif(image):
	# http://www.media.mit.edu/pia/Research/deepview/exif.html
	# Read the jpeg blocks, remove APP1 (FFE1..)
 
	begin_exif = image.find(b'\xff\xe1')
	if begin_exif >= 0:
		# JPEG header (FFD8) and anything between FFD8 and FFE1
		# needs to be preserved.
		ret = image[0:begin_exif]
 
		# Two bytes after FFE1 is the size of APP1.
		exif_size = struct.unpack('>H', image[begin_exif+2:begin_exif+4])[0]
 
		# Skip exif_size bytes plus 2 bytes (for the size itself).
		ret += image[begin_exif+exif_size+2:]
 
		# All done.
		return ret
 
	# No EXIF. Just return it.
	return image

It’s easy to use:

f = open('foo.jpg')
image = f.read()
f.close()

outputImage = stripExif(image)
f = open('output.jpg', 'w')
f.write(outputImage)
f.close()

Sure hope this is useful for someone out there. I spent a lot of time searching and coming up empty.

The libgdx/box2d Jumper tutorial

I have been too busy to finish the tutorial, and I’m not sure when I will get around to it. I know I’ve been leaving a bunch of people hanging for months, and for that I apologize.

However, I have good news. There’s better, neater software available for building graphics and fixtures for libgdx/box2d games: The Box2D Editor. The same person has created a better texture packer GUI and is working on a physics level editor. Way better than anything I was going to be showing you here!

Check out Aurelien Ribon’s Dev Blog.

libgdx, box2d, tiled maps: full working example, part 2/3

This is part 2 of a 3 part post demonstrating how to make a simple side scrolling game using libgdx and box2d. See part 1.

Updated 2011-09-19: Heh, now uses libgdx 0.9.2. No longer has its own Box2DDebugRenderer, figured out a better way using the new Matrix4 argument to renderer. Improved the controls by removing linear damping and replacing it with friction. (I am learning as I go with this, for what it is worth.) Discovered that I made a rather embarrassing mistake in leaving out rudimentary on-screen controls for Android devices. They are still pretty cruddy, though. Maybe I’ll come up with something better, or maybe someone else will and will contribute it? :) Either way is fine.

Updated 2011-09-18: Now uses libgdx 0.9.1.

In part 1 you saw how to make what is essentially a tiled map viewer. After you are through with this part you will have a character that you can move around the screen in a “natural” way, or at least a familiar way. As in part 1, download JumperTutorialProjects.zip for part 2 and follow along as the code is explained.

Continue Reading >>

libgdx, box2d, tiled maps: full working example, part 1/3

This will be a 3 part post demonstrating how to make a simple side scrolling game using libgdx (official project blog), a cross-platform library that allows you to simultaneously build for the desktop and for Android devices.

Updated 2011-09-19: Updated to use libgdx 0.9.2. No code changes required.

Updated 2011-09-18: Updated to use libgdx 0.9.1 (for real this time…) which meant renaming the pack file, TiledMapRenderer to TileMapRenderer, moving and renaming the tiles and pack file (and reindexing) due to new requirements from libgdx.

Updated 2011-06-12: Fixed some graphical glitches reported by Ed (in part 2′s comments). Thanks for all of your help Ed! Additionally, the tutorial is now based on libgdx 0.9.1 (it might have been libgdx 0.9.0 at the time, sorry folks) instead of an arbitrary nightly build, and it uses a modified TexturePacker in place of “manual” pack file generation.

This example is for anyone interested in making Android games, but is unsure where to start. By the end of part 3 you will have a basic Mario-like side-scrolling game with realistic physics, and (hopefully) you will be able to take this example and build something fun.

I’ve learned a few things since writing my previous post on using Tiled Map Editor with libgdx that I’ll apply in this series, things that make building a simple tiled-map game a lot easier. All code found here is released under the Apache 2.0 license, meaning, basically, you can use it for whatever you want. (IANAL, read the license for details.)

This is a much more involved tutorial than you have seen before on this site. There are a lot of different components used here and I tried to avoid cutting corners during explanations.

Continue Reading >>

perl, string compare, dprof

This may be old news for people more familiar with perl internals.

dpk@dpk1:~$ perl -version | head -2

This is perl, v5.8.8 built for i486-linux-gnu-thread-multi
dpk@dpk1:~$ cat test1.pl
#!/usr/bin/perl

use strict;

my $x = 0;
while ($x < 100)
{
  &doit();
  $x++;
}

sub doit() {
  my $foo = 'foobar'; my $x = 0; my $y = 0;
  while ($x < 1000000)
  {
    if ($foo eq 'foofoo')
    {
      $y++;
    }
    $x++;
  }
}
dpk@dpk1:~$ perl -d:DProf test1.pl;dprofpp
Total Elapsed Time = 52.07938 Seconds
  User+System Time = 52.07938 Seconds
Exclusive Times
%Time ExclSec CumulS #Calls sec/call Csec/c  Name
 99.9   52.07 52.070    100   0.5207 0.5207  main::doit
 0.00       - -0.000      1        -      -  strict::bits
 0.00       - -0.000      1        -      -  strict::import
 0.00       - -0.000      1        -      -  main::BEGIN

And then:

dpk@dpk1:~$ cat test2.pl
#!/usr/bin/perl

use strict;

my $x = 0;
while ($x < 100)
{
  &doit();
  $x++;
}

sub doit() {
  my $foo = 'foobar'; my $x = 0; my $y = 0;
  while ($x < 1000000)
  {
    if ('foofoo' eq $foo)
    {
      $y++;
    }
    $x++;
  }
}
Total Elapsed Time = 51.31948 Seconds
  User+System Time = 51.30948 Seconds
Exclusive Times
%Time ExclSec CumulS #Calls sec/call Csec/c  Name
 100.   51.31 51.310    100   0.5131 0.5131  main::doit
 0.00       - -0.000      1        -      -  strict::bits
 0.00       - -0.000      1        -      -  strict::import
 0.00       - -0.000      1        -      -  main::BEGIN

I’ve repeated this little test multiple times, with different several different inputs, but the result is always the same. if ('string' eq $variable) is faster than if ($variable eq 'string')

Annoying Firefox Bug

There are sites out there (in the tired “Rick Roll” category) that make it hard to close a tab, by adding hooks that allow for dozens or hundreds of alert dialog boxes to appear, preventing the tab from closing. One is http://www.internetisseriousbusiness.com. Don’t go to this site, unless you want to see the bug in action.

I posted a bug on bugzilla, after doing a search. I didn’t see any, but as there are hundreds of thousands of other bugs on there, I didn’t want to spend hours hunting down an exact match, so I just went ahead with it. Turns out I should have searched for “rick roll,” rather than something more specific such as “javascript alert exit loop” or similar. Ah, well, such is the folly of search engines.

So, it got merged in to another bug that’s over 7 years old. Surprisingly, it has only gathered 60 votes in that incredible period of time. I’m asking you, the loyal dpk dot net reader, to use one of your 10000 votes on this bug, and to help raise awareness of this issue, so that it might be squashed.

Looking at the code, it seems like it might involve simply adding another button to the alert dialog box, that would just kill the tab’s javascript context. But, I don’t really know how to do all that. It kinda looks like there’s different alert box code for each “platform.” I dunno. I’m no mozilla pro.

Quality with a capital P

The pdf extension library shipped with PHP-4.4.7 doesn’t work, and it’s a known “non-bug”. I’m not sure why they still ship the broken library. I installed the PECL version, via pear (my first time trying that), and that was pretty painless. It created a pdf.so and put it in some long /usr/local/lib path. So I figured, now I can just load it with:

dl("/usr/local/lib/php/extensions/no-debug-non-zts-20020429/pdf.so");

right? Wrong:

Warning: dl(): Unable to load dynamic library './/usr/local/lib/php/extensions/no-debug-non-zts-20020429/pdf.so' - Cannot open ".//usr/local/lib/php/extensions/no-debug-non-zts-20020429/pdf.so" in /root/test.php on line 7

I have “./” as my extension_dir in php.ini. For some reason, the code for dl doesn’t recognize that I’m specifying a full path, though. The fix, according to posters on http://www.php.net/dl, is to prepend a sufficient number of ../’s to the path before calling dl() !

The resulting code (with helpful dpk-comments) is:
<?php
// This is required, and is astounding.
$dotdots = preg_replace ('//([^/]+)/', '../', dirname(__FILE__));
$dlpath = $dotdots . '/usr/local/lib/php/extensions/no-debug-non-zts-20020429/pdf.so';
dl($dlpath);
// My brain still hurts.
$p = PDF_new();
?>