======================================================================= SINGLE SPRITE MOVEMENT PROGRAM ======================================================================= This document has been prepared to accompany the 'Single Sprite Movement Program'. Its aim is to provide information on how the movement of the sprite was achieved. If you have followed Ira Rainey's Sprite Tutorial and have a sprite on the screen, but want to now how to go about moving it, this text document is for you. The author realises that this document will probably not be of much use to many Yaroze users, but there may be somebody out there who needs it. =============== SPRITE MOVEMENT =============== The following 7 steps will take you through the process of achieving sprite movement. Of course the way you have written your sprite display code may mean that what is below might not work, but to try is to learn, or something. 1) The first thing you need to do is include the 'pad.h' file into your code. To do this, find the pad.h file (Sony provided it on the PC CD) and then copy it into the directory where your main.c file is. Then, in your main.c file, one of the first lines should be: #include 2) The next thing you need to do is to set some global controller-related variables. These are used to set up the joypads and provided buffers where any input on them can be stored. Before you reach any of your funtions, include the following two lines: volatile u_char *pad1Buffer, *pad2Buffer; u_long padCheck; 3) Once your controller variables are set up, you need to include two pad reading functions. You can either do this as shown in main.c contained within this zip file where they are declared as prototype functions before the main function and then placed at the end of the code, or put them both before the main function. The latter will be described here. First you need to include a function used for reading the PlayStation joypads: static u_long ReadPads(long) { return(~(*(pad1Buffer+3)|(pad1Buffer+2)<<8|(*pad2Buffer+3)<<16|*pad2Buffer+2)<<24)); } This is a Sony function and I have no idea what it actually means, but just accept for now that it reads the joypad inputs. The next function you need should be placed into your code directly after the above. It is used to check the status of the joypads: static long InitPads() { padCheck = ReadPads(1); if(padCheck & PADselect) return(99); } Once you have entered both of these, remembering to place them before the main() function, you can move on. 4) At this point you should make sure that in your code you have an InitSprite() function, or similar. If you have followed Ira Rainey's tutorial, you can carry on. If unsure, check the InitSprite() function in the main.c code provided. 5) Right, onto your main() function. After you have set up the PlayStation's graphics routines, initialised the Ordering Tables, and called your InitSprite() function, you need set the address for your joypad buffers. To do this include the following line: GetPadBuf( &pad1Buffer, &pad2Buffer); 6) Now in your code should come the main program loop. This loop continually sets up the frame buffers and displays them to the screen until select is pressed on the joypad. Below you will find the joypad aspects of the main loop presented for you. An explanation of these will follow in the next step. while((InitPads()) != 99) { // In the following PADL refers to the directional pad if(padCheck & PADLup) sprite.y -=5; if(padCheck & PADLdown) sprite.y +=5; if(padCheck & PADLleft) sprite.x -=5; if(padCheck & PADLright) sprite.x +=5; // In the following PADR refers to the 4 symbol buttons if(padCheck & PADRleft) sprite.scalex -=20; if(padCheck & PADRright) sprite.scalex +=20; if(padCheck & PADRup) sprite.scaley -=20; if(padCheck & PADRdown) sprite.scaley +=20; // PADL1 and PADR1 refer to the top two front buttons if(padCheck & PADL1) sprite.rotate -=ONE; if(padCheck & PADR1) sprite.rotate +=ONE; ..... The dots at the end of the above code represent where the rest of the main loop should be written. See the main loop in main.c for details. You have probably already got the rest of the main loop code in your existing sprite code. 7) Well that is really all there is to moving a sprite around. If you follow the above steps in your code you should now be sitting, pad in hand, wizzing your sprite around the screen, rotating it like a madman. I'll just explain what the above main loop code does. Basically, the 'if' statements are using the 'padCheck' variable to read an input on the joypad. The code in the brackets next to the 'if' statements can be read as: if('there is a pad input' & 'that input is equal to a button') The code following each of these statement tells our sprite to move in relation to what was pressed. If left was pressed our sprite is told to decrease its x axis value by 5, if down was pressed our sprite is told to increase its y value by 5, etc. How does this work? I shall tell you. In the InitSprite() function I mentioned in step 4, our sprite is given certain properties. It is given an x axis position on the screen by the code: sprite.x = 150; // For example It is given a scale ratio by the code: sprite.scalex = ONE; There are many more properties our sprite is given, but these are just a sample to show you the area of code you should be looking at. Now, the code we placed into the main program loop in the last step reads an input from the joypad and, depending on what the input was, alters these sprite properties. By altering the properties of our sprite, movement is achieved on screen. To help you understand the main loop code a little more I shall read through a random selection of the 'if' statements: if('there is a pad input' & 'that input is the UP button') decrease by 5 the y position on screen of our sprite; if('there is a pad input' & 'that input is the CROSS button') increase the y axis scale value by 20; if('there is a pad input' & 'that input is the L1 button') decrease the rotational value of the sprite by ONE; By placing these 'if' statements in to the main loop, where the screen updating code is also located, every alteration made to the properties of our sprite is placed on screen. And that is how a sprite is moved. I hope this short document has proved to be of some use to you. My reason for writing it is simply because I know how much trouble I went through in order to learn how to get a sprite move. I found that the hardest part of the whole process was just knowing where to start. I just wish I could have had a document like this when I was trying to get my sprite moving at 3:00am. =============== CONTACT DETAILS =============== If you have any comments, problems, or suggestions regarding the program or this text document, please feel free to e-mail me at the following address: stevenlewis@birminghamhome.freeserve.co.uk ======================================================================= Downloaded From: STEVE'S YAROZE SITE http://www.netyaroze-europe.com/~slewis/ =======================================================================