Hack47.Develop for the PSP


Hack 47. Develop for the PSP

If you think you have the chops to code your own app for the PSP, this hack will show you where to get started.

Do you love your PSP, but you're disappointed that it doesn't have a program for easily viewing all of your recipes? A paint-by-numbers program? An advanced statistical analysis tool? A PSP port of Snood? Why not code your own application?

Unfortunately, the buy-in price to be a Sony-certified programmer for the PSP is a bit beyond most of us mere mortals, so the only real way to get into coding for the PSP is to dive right in to the homebrew scene [Hack #40]. Fortunately, there's lots of very valuable information online for the would-be PSP coder, and there's a very active community of other coders working away in their free time to make the PSP a vibrant homebrew platform.

Homebrew applications can't be loaded on a PSP without some trickery, and it's a given that the most recent (2.0, as of this writing) version of the firmware is immune to such trickery. So, if you want to run true homebrew applications, you're going to need to make sure that you get your hands on a PSP running Version 1.0 or 1.5 of the firmware. If you're a cracking savant, however, please by all means figure out a way to run homebrew on later firmware versionsthe Internet will herald your name far and wide.

But, if you're not inclined to invoke the trickery needed to load homebrew, you're running the latest firmware, or you just want to do some elementary programming, you're not out of luck. The PSP web browser is powered by JavaScript, HTML, and CSS, the technologies that come together to give you Dynamic HTML (DHTML). With these, you can write some simple and attractive applications that run right on your PSP's web browser.

5.8.1. PS2Dev.org

The PS2Dev Network (http://www.ps2dev.org) has a plethora of information for developing for both the PSP and PS2. When you first start clicking around on the site, you'll probably head straight to the PSP Tutorial section (http://ps2dev.org/psp/Tutorials) and find yourself staring at an empty page. Don't panic. Most of the information you would want to find has made its way over to the PS2Dev Network wiki (http://wiki.ps2dev.org/). If you navigate to the Programming FAQ section of the wiki (http://wiki.ps2dev.org/psp:programming_faq), you'll find a section called "How do I get started in PSP programming?" Here you will be confronted with the cost of entry into the world of PSP programming: familiarity with C or C++.

If you are on Windows, you will need to download Cygwin (http://www.cygwin.com/), which will allow you to compile the Unix programs needed to code for the PSP. If you are on Mac OS X or Linux, you should be able to compile the PSPSDK and PSP tool chain, as long as all the necessary dependencies are in place. Both of these tools are available via the PS2Dev Net-work's PSP Project page (http://ps2dev.org/psp/Projects).

5.8.2. ScriptScribbler PSP Programming Tutorials

ScriptScribbler (http://www.scriptscribbler.com) currently offers three tutorials by Brad Dwyer on programming for the PSP, and it looks as though there will continue to be more tutorials forthcoming in the future. The first section of the tutorial (http://www.scriptscribbler.com/psp/tutorials/lesson01.htm) walks you through setting up a development environment on Windows. The second section walks you through creating your first program (http://www.scriptscribbler.com/psp/tutorials/lesson02.htm), a basic "Hello World" program for the PSP. The third section, serving as a programming primer, is currently the most useful (http://www.scriptscribbler.com/psp/tutorials/lesson03.htm), as it serves as a "crash course in the basics of C programming for the PSP."

5.8.3. LuaPlayer

If C and C++ aren't your thing, and you're feeling a little rusty in the coding arena, then you'll most likely want to check out the LuaPlayer (http://www.luaplayer.org/). To work with LuaPlayer, all you need is a PSP running Version 1.0 or 1.5 of the firmware, and a text editor on your computer. The LuaPlayer site features a rather good step-by-step tutorial (http://www.luaplayer.org/tutorial/index.html) to this PSP scripting language that walks you through creating a basic "Hello World" test program, working with images, working with animation, and an introduction to coding for the controls on the PSP. There are several sections not yet complete in the tutorial, but considering the amount of people in the PS2Dev Forums (http://forums.ps2dev.org/viewforum.php?f=21) who are using this tool to easily program for the PSP, you can most likely find some guidance with a few carefully placed questions.

One of the advantages of using Lua is that you can test applications on a computer running Windows before you deploy them to your PSP. This eliminates the need to copy your source code over to your PSP every time you make a change. Instead, you can do all your testing and debugging on your PC, and send it to your PSP when you are ready to play it. For more information on the Windows version of Lua Player, see http://forums.ps2dev.org/viewtopic.php?p=22332#22332.

Here's a simple Lua program that runs on the PSP. You should save it as script.lua, and follow the instructions included with the Lua Player to run it on your PSP. This is a program that draws an @ character onscreen at the specified x/y coordinates. You can move the directional pad to move the character and leave a trail, as shown in Figure 5-27.

Figure 5-27. Snaking around the screen in the Lua Player for Windows


 -- starting positions for the character x = 200 y = 100 -- A nice color color = Color.new(128, 255, 0) -- this flag tells whether the program needs to draw draw_character = true -- loop forever while true do   if draw_character then     -- print a rogue at the x/y coordinates     screen:print(x, y, "@", color)     screen.flip()   end   -- check whether the user pressed the pad, and move accordingly   pad = Controls.read()   draw_character = true   if pad:left() then     x = x - 3   elseif pad:right() then     x = x + 3   elseif pad:up() then     y = y - 3   elseif pad:down() then     y = y + 3   else     draw_character = false   end   -- wait for the next vertical blank   screen.waitVblankStart() end 

5.8.4. JavaScript

The PSP's web browser is no slouch; although it's not as feature-laden as the latest desktop browsers, it holds its own pretty well. If you know a little HTML, CSS, and JavaScript, you can create DHTML applications that run right in the browser. For example, here's a short program that draws a chunky version of the Mandelbrot Set, one of the most famous fractals (objects that exhibit self-similarity at various levels of detail but with an organic irregularity):

 <html> <head><title>Mandelbrot Set</title></head> <body style="width: 480px; height: 272px;"> <script language="JavaScript"> colors = new Array("black", "aqua", "blue", "fuchsia", "gray",     "green", "lime", "maroon", "navy", "olive", "purple",     "red", "silver", "teal", "white", "yellow"); function plot() {   height = 20;   width = 150;   max = 17; // maximum number of iterations.   document.write('<p style="font-size: 8px">');   // imaginary axis from -1.25 to 1.25   for (y = -1.25; y <= 1.25; y += 2.5/height) {     // real axis from -2.25 to .75     for (x = -2.25; x <= .75; x += 3/width) {       a1 = x;       b1 = y;       for (cnt = 1; cnt <= max; cnt++) {         // If the square magnitude of the complex number exceeds         // the limit, break out of the loop. Otherwise, calculate         // and loop around again.         //         a = a1*a1;         b = b1*b1;         if (a + b > 4.0) {           break;         } else {           b1 = 2 * a1 * b1 + y; // imaginary component           a1 = a - b + x; // real component         }       }       if (cnt > max) {         // At this resolution, the point does not appear to be         // outside the Mandelbrot set, so use color 0 (black).         cnt = 0;       }       style = 'background-color: ' + colors[ cnt % 16 ] + ';';       document.write('<span style="' + style + '">&nbsp;</span>');     }     document.write('<br/>');   }   document.write('</p>'); } plot(); </script> </body> </html> 

You can save this as an HTML file on your PSP's memory stick, navigate to it with the 2.0 firmware's web browser [Hack #41], wait a minute or two, and then see the fractal displayed (albeit rather crudely) on your PSP's screen, as shown in Figure 5-28.

Figure 5-28. Viewing the Mandelbrot Set on the PSP


In fact, you could combine DHTML with Perl [Hack #30] to create a read-only copy of your address book, calendar, or favorite recipes on your PSP. Create some attractive styles to present the data, use JavaScript to create navigation features, and use Perl to extract the data and dump it into an HTML file that acts as the back-end database for your styles and scripts. For more information, see the JavaScript & DHTML Cookbook (O'Reilly).

5.8.5. Hacking the Hack

This hack is just an introduction to some of the available information on developing for the PSP. If you really want to delve into PSP programming, you plan to spend many hours talking to other developers on the PS2Dev Forums (http://forums.ps2dev.org/). If you really want to make some primo games and programs for the PSP, you're going to need to seriously perfect your C and C++ coding skills. O'Reilly has several books and online articles about these languages, and you can find them all on our C/C++ programming pages (http://cprog.oreilly.com/).

Brian Jepson and C.K. Sample III




PSP Hacks
PSP Hacks: Tips & Tools for Your Mobile Gaming and Entertainment Handheld
ISBN: 0596101430
EAN: 2147483647
Year: 2006
Pages: 108

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net