Skip to content
Tenshi Hinanawi edited this page Feb 12, 2013 · 1 revision

We've done some programming for the PSP by now, but it's a little boring. I mean, to make a GAME you need user interaction. So, let's add that. In this tutorials we'll cover button input, and two fundamental aspects of programming, loops and if statements. So let's get started. Add the stuff that you know already. But add one extra include.

[CODE]//Button Input Example //December 3, 2007

#include <pspdebug.h> #include <pspkernel.h> #include <pspcallbacks.h> #include <pspctrl.h> #define printf pspDebugScreenPrintf

PSP_MODULE_INFO("Button Input",0,1,1);[/CODE]

Now, in case you couldn't guess, the extra include is for button input. Ctrl standing for control. Now, let's start our main loop.

[CODE]int main() { pspDebugScreenInit(); pspDebugScreenClear(); SetupCallbacks(); SceCtrlData pad;[/CODE]

Understand all that? No, of course not. Let me explain. 'SceCtrlData' is no differe nt from 'int', 'float' or 'char'. They're ALL variable types. So we just created a variable named 'pad' with the type of 'SceCtrlData'. We didn't give it a value, because it will be assigned later. Now, let's put in a new concept, loops.

[CODE] while(1) {[/CODE]

Now, one thing you should recognize is the starting curly bracket. Which means we're starting a new block of code. But, what exactly does a loop do. Well, a loop happens over and over again, just like a real loop (circle). So it starts at the starting curly bracket and executes all of the code until the ending curly bracket. When it hits the end, it goes back to the beginning and starts again. Now, let me explain the '1' now. Anything you put in the parentheses is compared to the number 1, and as long as it's equal to one, the loop continues. So, take this for example.

int var = 1; while(var) {

The PSP would compare 1 to var. So if var is equal to 1, it'll continue, if it's not equal to 1, the loop will stop. Get it? Good. Now, some more new code.

[CODE] sceCtrlPeekBufferPositive(&pad, 1);[/CODE]

What this does, every time the loop starts again (a few hundred times per second) it will read the PSP buttons. And it stores the button pressed inside of 'pad' our variable that we made a little while ago. So if you press up, then 'pad' will be equal to 'up'. Now, some MORE new code.

[CODE] if (pad.Buttons & PSP_CTRL_UP) { printf("You pressed Up\n"); }[/CODE]

Now, this shouldn't be too hard to understand. Remember how every loop, the button we press gets stored into 'pad'? Well, now, we're using that variable. If the button we press is UP, then it will print that text. Notice how the 'if' statement has curly brackets too. This shouldn't too hard to understand. Let's do one more.

[CODE] if (pad.Buttons & PSP_CTRL_DOWN) { printf("You pressed Down\n"); }[/CODE]

Get it? Now, let's throw in some change.

[CODE] if (pad.Buttons & PSP_CTRL_RIGHT) { printf("You pressed Right\n"); } if (pad.Buttons & PSP_CTRL_LEFT) { printf("You pressed Left\n"); }[/CODE]

Ooh, tricky. You can put it all on the same line. But this is only for short ones. For long ones, use multiple lines.

[CODE] if (pad.Buttons & PSP_CTRL_CROSS) { printf("You pressed "); printf("Cross\n"); }[/CODE]

It may seem like I'm tricking you, but I'm just showing you the way.

[CODE] if (pad.Buttons & PSP_CTRL_CIRCLE) printf("You pressed Circle\n"); if (pad.Buttons & PSP_CTRL_TRIANGLE) printf("You pressed Triangle\n"); if (pad.Buttons & PSP_CTRL_SQUARE) printf("You pressed Square\n");[/CODE]

Now do you see all the different ways we can use it? They may come in handy. Anyway, here's the rest of the buttons.

RTRIGGER LTRIGGER HOME SELECT NOTE

Now, we do one last thing. First we put a closing bracket FOR THE LOOP.

[CODE] }[/CODE]

Now, we put one FOR OUR MAIN FUNCTION. There's a difference.

[CODE] return 0; }[/CODE]

Don't forget the return value.

Now, you can use the same Makefile as before, just edit the title. Hopefully everything I wrote works. Since I'm PSPless, I can't test it. Let me know of any errors.

BA Logo

Bibliotheca Anonoma

BASLQC Wiki

  • Introduction - A quick intro to the rationale and ideals of this guide, and modding in general.
  • Essential Maker Skills - Essential Skills that every maker should have.
  • Archivist Tools - All the tools an internet archivist needs under their belt.

General Guides

  • Android Development Codex - All kinds of Android smartphones.
  • Business Class Laptops - Computers that last: The ThinkPad, the HP EliteBook, the Dell Precision/Latitude.
  • EBook Readers - A good eInk screen remains the best way to read literature comfortably.
  • Game Consoles - Homebrew development scenes have made it possible to unlock the full power of your game console's computer chip.
  • Graphing Calculators - This is the last vestige of the age of 80's Home Computers; where programs were simple and graphics were minimal.
  • Home Server - Why buy cloud storage when you can build your own cloud? For media streaming, torrenting, and VPN access (to bypass blocked internet).
  • Routers - Amazingly, your ordinary router probably runs Linux on it's little embedded CPU.
  • Authentication Wallets - Manage your plethora of accounts, passwords, and RSA public keys using a GPG-encrypted wallet.
  • LEGO Mindstorms - The easiest way to build functional robots and machines; using good ol' LEGO and Technic bricks.
  • Vintage Computers - Amiga, Apple ][, and all the other random home computers that defined the 1980s.

Research

  • Genetic Programming - Programs that mutate and evolve by themselves, like Genes. It's a very difficult concept to grasp, but a very powerful method that transcends math or algorithms.

Content Guidelines

  • General Guidelines - The ideals that you should uphold while working with and editing this guide.
  • Device Guide Templates - Templates and general guidelines for creating customized guides for a device.
  • Linux - Run a full desktop OS on your little mobile device; research is being made to make it comfortable to use in the mobile space.

Reference

  • Glossary - Contains all the crazy acronyms and word soup that you'll need to wade through when using this guide.
Clone this wiki locally