Path: chuka.playstation.co.uk!scea!peter_alau@playstation.sony.com From: Darco Newsgroups: scea.yaroze.freetalk,scee.yaroze.freetalk.english Subject: Net Yaroze Tutorial Preview Date: Sun, 20 Sep 1998 18:54:53 -0400 Organization: SCEA News Server Lines: 258 Message-ID: <360587BD.5668FFB0@bigfoot.com> NNTP-Posting-Host: 104.dialup.datasys.net Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------17B145C5B2A127067EAAFA7E" X-Mailer: Mozilla 4.05 [en] (Win95; U) Xref: chuka.playstation.co.uk scea.yaroze.freetalk:972 scee.yaroze.freetalk.english:2412 This is a multi-part message in MIME format. --------------17B145C5B2A127067EAAFA7E Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Ok, here's a preview of what the Net Yaroze tutorial will be like. (It's at the bottom of this message) Your suggestions are welcome. This is Lesson 2 from Chapter 1. It isn't finished, but you can still get an idea for how it will flow and all. The graphic at the top of the page for sure won't load, but in the finished version it will. There are two sections in this lesson... One goes over printing "Hello, World!" to the computer, and the other goes over printing "Hello, World!" to the TV screen. This is done step-by-step, explaining every line -- what it does, why it works, and anything else in between. The last part of this tutorial isn't finished... So don't bitch about it. :) Just tell me what you think about the layout. Do you like it? Does the color-coding of the code examples help? (Color coding the code examples is done by hand and is a BITCH to do. But if it significantly helps with readability, I don't mind doing it) And most importantly, if you were a newbie, do you think you could learn from it? 'Darco --------------17B145C5B2A127067EAAFA7E Content-Type: text/html; charset=us-ascii; name="lesson02.html" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="lesson02.html" Content-Base: "file:///C|/WINDOWS/Desktop/Net%20Yaroz e%20Tutorial/chapter1/lesson02.html" Net Yaroze Tutorials (Index Page)  
Lesson 2
Writing your First PlayStation Program
 
Ok, Now in this lesson we will go over writing your first Net Yaroze program. Now, if you are familure with C, you know that the first program is the ever-supercomplicated "Hello, World!" program. (I'm being sarcastic with the ever-supercomplicated thing...) It's simple, even stupid. But I'll go over and explain how everything works, why it works, and food for thought. So...
 
Printing "Hello, World!" on the Computer 
First off, the first line in every C file in your game should be this:
#include <libps.h>
This includes the header that enables you to use all of the Net Yaroze functions. Without it, you can't do anything. So make sure that it's there! If you've programed in C, you are probably use to including the header file stdio.h. Just think of libps.h the equivilant of stdio.h.

Next, we'll write the main() subroutine.  main(), as you should already know, is where your program will start. This is called the Entry Point. (Technical Note: The entry point is actually not main(), but for our intents and purposes we can assume it to be so.) So, here's what we got:

void main(void)
{
}
Now, if you just ran this as it is, it would do absolutely nothing -- the moment it executes it returns to the program that ran it. Not very useful. But what we have now is a 'skeleton' for which we can add code.

In traditional development environments, when you call the subroutine printf(), it types whatever you told it to directly to the screen. However, this is not how this function operates on the PlayStation. Instead of printing to the screen, it prints it to the serial port on the back on the PlayStation. So, if we add printf() to main() . . .

void main(void)
{
    printf("Hello, World!\n");
}
. . . This will print the text "Hello, World!" to the serial port, to your computer, and then you will see this on the computer screen. This makes printf() very useful for debuging -- You can dump all sorts of useful information to the serial port. If you don't quite see the usefulness now, you will see it later on.

Before I go on, I might help out some of the C newbies here. If you are reading the code above and wondering what the heck "\n" means, let me explain. It is essentially pressing the enter key... So the next time you run printf() it will print on the next line, rather than starting off where the previous printf() left off.

Printing "Hello, World!" on the TV Screen 
Ok, so now you've written a program that when you run it, it prints a message to the serial port, and then exits. So now, let's go over printing something to the screen... And this is where things might get a little bit more complex, so bear with me.

First, let's put some things into perspective. Printing a string ("Hello, World!" is a string) to the computer is relitively easy, think about it. All we were doing was sending several characters to the serial port.

Printing a string to the screen would at first seem to be a similarly easy task as well. However, this is not the case. There are many things that we have to set up first before we can print a string to the screen. And after setting up these systems, we can then procede to print something to the screen, but this is still a little more complicated than just dumping the string to the serial port.

If all we did was print "Hello, World!" to the screen and then exited, then you would probably not see anything. (That is unless you were using CodeWarrior) So, lets set our goals for our program:

First, let's write out Startup Routine. A startup routine is the routine which initilizes all of the systems required for your program to execute properly. You COULD put all of your startup functions in your main() subroutine, but that's messy. So, we start by adding this code before the main() subroutine.
void startup(void)
{
}
Then make this the first line in your main() subroutine...
startup();
And now, simply put all of your startup calls into the startup() subroutine. So now, to recap, your program code should look something like this :
#include <libps.h>

void startup(void)
{
}

void main(void)
{
    startup();
 
    // Game code goes here
}

Just a reminder... A subroutine should always be before a subroutine that uses it. (For example, startup() is used in main(), so startup() needs to be before main())

So now, we need to add our startup calls tostartup(). The first thing we need to do is tell the GPU what type of display we are using. Are we using PAL(Europe, Australia), or are we using NTSC(United States, Canada, Japan)? I'm in the United States, so my first line would be

SetVideoMode(MODE_NTSC);
If you are in Europe or Australia, you would put
SetVideoMode(MODE_PAL);
The next thing we want to do is tell the GPU is what screen resolution we want, by way of the GsInitGraph() subroutine. This subroutine does several things other than just setting the video mode, but we can go over that in another lesson. The first two values are the width and the height of the display, in pixels. The rest you don't have to worry about right now. So let's set our screen resolution to 320x240 by making this the next line in startup():
GsInitGraph(320, 240, 4, 0, 0);
Once this routine is called, the GPU automaticly "turns off" the screen by default. Not sure why it does this, but we can turn it back on by adding this:
SetDispMask(1);
Keep in mind that the frame buffer IS NOT CLEARED YET. So, we need to clear the screen or we will have that nasty green brick screen as our background. So, we need to first insert this as the first line in startup().
RECT screen_c;
What this does is create a rectangle structure called screen_c. Now we need to tell it what we are going to clear. So insert these lines also:
screen_c.x = 0; // We want to start clearing the screen at the
screen_c.y = 0; //  top, left hand corrner of the screen.

screen_c.w = 1023; // The video memory is 1024 pixels wide...
screen_c.h = 511; // ...and 512 pixels tall.

We now have a structure describing all of the video memory. Now how do we clear it?

With ClearImage(), of course! The first thing we need to tell it is what region we want to clear. Then we simply tell it what color to clear it, using Red Green and Blue values respectively. So now we add this line to the end of startup()

ClearImage(&screen_c, 0, 0, 0);
// The last three values are the color to clear to.
Now, to recap, this is what startup() should currently look like:
void startup(void)
{
   RECT screen_c;

   screen_c.x = 0; // We want to start clearing the screen at the
   screen_c.y = 0; //  top, left hand corrner of the screen.
   screen_c.w = 1023; // The video memory is 1024 pixels wide...
   screen_c.h = 511; // ...and 512 pixels tall.
 
   SetVideoMode(MODE_NTSC);
   // SetVideoMode(MODE_PAL);
 
   GsInitGraph(320,240,4,0,0);

   SetDispMask(1);

   ClearImage(&screen_c, 0, 0, 0);
   // The last three values are the clear color.

   FntLoad(320,0);
   FntOpen(16,16,260,200,0,512);
}

  --------------17B145C5B2A127067EAAFA7E--