Lesson 03
A Programming Primer
A crash course in the basics of C programming for the PSP.
Now for the third and final section of Lesson 03. If you haven't read the first or
second parts, rewind and come back later.
The next thing we need to do is increase our counter by one so that the next time through the loop, the number displayed is one higher. It wouldn't
really be a counter if we didn't do this, now would it? We could do this in one of two ways. The first, less commmon way to do this would be by
setting "counter = counter+1;" this works perfectly well, but there is an easier way. To increment a variable by one, just use the "++" operator:
Now we need to insert a short pause to make the "HOME" button work. Here we will be utilizing a "for" loop. This loop is just a tad bit different than the "while" loop. It takes three parameters. The first is the initialization; it will set the variable for the start of the loop. The second is the control statement, which does the same thing as the control statement in the "while" loop. And the third is what you want to happen to your variable at the end of each loop.
This will execute our line of code 5 times (when i = 0, 1, 2, 3, and 4).
And finally, we need to end the code block that the loop runs through with a simple ending bracket:
Now for the final little bit. This is the code that will run after the "break" statement executes. We want to display a simple message, and the final count of our counter. So we'll clear the screen and use two more "printf" statements like we did in the loop, but with different text. Add:
The Makefile needs to be changed to reflect our new program. Go into it and change the title to one that you think suits the new program. I named mine "Counter Program." And also change your target to "count." Now go ahead and build your program, and give it a test run. You should now have the basic skills you need to create your own programs (text based ones at least). You can check out some other C programming tutorials for information on how you can use more complex logic structures (if/if else/else), loops, functions, pointers, etc. There are a few differences with programming for the PSP, but the basic stuff is all straight-up C. Good luck, and have fun!
Update: Lesson 04 - Simple Image Processing is now up!
Be sure to add the feed to your RSS Aggregator (or Google Homepage, or Firefox Live Bookmark) to stay updated with the latest tutorials.
If you have enjoyed this tutorial and have a spare buck or two, please consider donating to the author. Or, if you have a website, link to this tutorial series (helping spread the word means more homebrew for all!).
If there's a calling, I will consider making more tutorials. Please contact me with your feedback on the tutorials and on what you'd like to see in the next lessons. My AIM is Yeldarb2k3, and my e-mail is Yeldarb [at] Barbdwyer [dot] com. Also, if you are looking for someone to design you a website, please contact me through my site, Barbdwyer Web Design.
counter++;
It does the same thing as "counter = counter +1;" but it's a bit more elegant, don't you think?Now we need to insert a short pause to make the "HOME" button work. Here we will be utilizing a "for" loop. This loop is just a tad bit different than the "while" loop. It takes three parameters. The first is the initialization; it will set the variable for the start of the loop. The second is the control statement, which does the same thing as the control statement in the "while" loop. And the third is what you want to happen to your variable at the end of each loop.
for(i=0; i<5; i++) {
sceDisplayWaitVblankStart();
}
sceDisplayWaitVblankStart();
}
This will execute our line of code 5 times (when i = 0, 1, 2, 3, and 4).
And finally, we need to end the code block that the loop runs through with a simple ending bracket:
}
Now for the final little bit. This is the code that will run after the "break" statement executes. We want to display a simple message, and the final count of our counter. So we'll clear the screen and use two more "printf" statements like we did in the loop, but with different text. Add:
pspDebugScreenClear();
printf("Counter Finished.");
printf("Final Count: %i", counter);
And we're done with our code! Now for a couple of cosmetic changes from Lesson 02.printf("Counter Finished.");
printf("Final Count: %i", counter);
The Makefile needs to be changed to reflect our new program. Go into it and change the title to one that you think suits the new program. I named mine "Counter Program." And also change your target to "count." Now go ahead and build your program, and give it a test run. You should now have the basic skills you need to create your own programs (text based ones at least). You can check out some other C programming tutorials for information on how you can use more complex logic structures (if/if else/else), loops, functions, pointers, etc. There are a few differences with programming for the PSP, but the basic stuff is all straight-up C. Good luck, and have fun!
Update: Lesson 04 - Simple Image Processing is now up!
Be sure to add the feed to your RSS Aggregator (or Google Homepage, or Firefox Live Bookmark) to stay updated with the latest tutorials.
If you have enjoyed this tutorial and have a spare buck or two, please consider donating to the author. Or, if you have a website, link to this tutorial series (helping spread the word means more homebrew for all!).
If there's a calling, I will consider making more tutorials. Please contact me with your feedback on the tutorials and on what you'd like to see in the next lessons. My AIM is Yeldarb2k3, and my e-mail is Yeldarb [at] Barbdwyer [dot] com. Also, if you are looking for someone to design you a website, please contact me through my site, Barbdwyer Web Design.
