PSP Hacks - Forums

Go Back   PSP Hacks - Forums > PSP Community > PSP Programming & Development

Notices

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 12-01-2007, 09:20 PM
NIGathan NIGathan is offline
Programmer
PSP Ninja
 

Join Date: Jul 2006
Location: ATX
Posts: 658
NIGathan Has a Beginner Reputation
Default

I got a new psp and decided to write some more tutorials!


How to make an impressive gfx menu with only a few simple lines.

This is the probably the easiest and shortest way to make a good gfx menu. I tried making it into a function but i never got it to work right so instead i just put it in the main loop.

here is an example using the NIGaShootuh menu:

Code:
#include <oslib/oslib.h>

PSP_MODULE_INFO("NIGaMenuMaker", 0, 1, 1);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);

OSL_IMAGE *menuback, *sel;

int menu = 1; //Determines when the menu is active//
int menusel = 1; //Determines which selection is selected in the menu//
int first_sel_x = 335; //The x position of the first selection//
int first_sel_y = 50; //The y position of the first selection//
int selimagex; //Determines the starting x postion of the selection//
int selimagey; //Determines the starting y postion of the selection//
int num_of_sel = 3;//Determines the amount of selections//
int x_dis_bet_sel = 0;//The x distance between two selections//
int y_dis_bet_sel = 30;//The y distance between two selections//

int main()
{   
    oslInit(0);

    oslInitGfx(OSL_PF_8888, 2);
    
    oslInitConsole();

    oslInitAudio();
    
    menuback = oslLoadImageFile("menuback.png", OSL_IN_RAM, OSL_PF_8888);
    sel = oslLoadImageFile("sel.png", OSL_IN_RAM, OSL_PF_8888);
    
    while (!osl_quit)
    {          
        oslStartDrawing();

        oslReadKeys();
        
        if (menu == 1) oslDrawImage(menuback);
        if (menu == 1) oslDrawImage(sel);
        
        if (menu == 1) sel->x = selimagex; //Sets the selection coordinates//
        if (menu == 1) sel->y = selimagey; //Sets the selection coordinates//
        
        if (menu == 1) selimagex = first_sel_x+(x_dis_bet_sel*menusel); //Determines where the selection image is drawn for each selection//
        if (menu == 1) selimagey = first_sel_y+(y_dis_bet_sel*menusel); //Determines where the selection image is drawn for each selection//
        
        if (menu == 1 && osl_keys->pressed.down) menusel++; //Moves the selection down//
        if (menu == 1 && osl_keys->pressed.up) menusel--; //Moves the selection up//
        
        if (menu == 1 && menusel > num_of_sel) menusel = 1; //Sets the selection back to the top//
        if (menu == 1 && menusel < 1) menusel = num_of_sel; //Sets the selection back to the bottom//
        
        oslEndDrawing();

        oslSyncFrame();    

        oslAudioVSync();
    }

    oslEndGfx();
    oslQuit();
    return 0;
}
The notes next to each line should explain what it does. If you have any questions just ask.

Now to make this into your own menu the only things you have to change are:

change first_sel_x to the x position of the first selection.
change first_sel_y to the y position of the first selection.
change num_of_sel to the amount of selections in your menu.
change x_dis_bet_sel to the x distance between each selection in your menu. (ussually 0)
change y_dis_bet_sel to the y distance between each selection in your menu.

here is the download link for this example. (inculdes eboot, source, and menu images)
http://www.mediafire.com/?f02wsp19tmv
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||

How to check if a sound file is playing. Used to make playlist or check when a sound file has finished playing.

use this function to determine if a sound file is playing.
Code:
oslGetSoundChannel(OSL_SOUND *s);
the parameter is the sound file your checking.
when it equals -1 the sound is not playing.

here is the function used in an example from NIGaShootuh. It shows how to make a playlist.

Code:
            tunes = oslGetSoundChannel(music);
	    tunes1 = oslGetSoundChannel(music1);
	    tunes2 = oslGetSoundChannel(music2);
	    tunes3 = oslGetSoundChannel(music3);
	    tunes4 = oslGetSoundChannel(music4);
	    tunes5 = oslGetSoundChannel(music5);
	    tunes6 = oslGetSoundChannel(music6);
	    
	    if (tunes == -1 && cursong == 1) songcheck = 1, play = 1;
	    if (tunes1 == -1 && cursong == 2) songcheck = 2, play = 1;
	    if (tunes2 == -1 && cursong == 3) songcheck = 3, play = 1;
	    if (tunes3 == -1 && cursong == 4) songcheck = 4, play = 1;
	    if (tunes4 == -1 && cursong == 5) songcheck = 5, play = 1;
	    if (tunes5 == -1 && cursong == 6) songcheck = 6, play = 1;
	    if (tunes6 == -1 && cursong == 7) songcheck = 0, play = 1;
	    
	    if (songcheck == 0 && play == 1) oslPlaySound(music, 0), play = 0, cursong = 1;
	    
	    if (songcheck == 1 && play == 1) oslPlaySound(music1, 0), play = 0, cursong = 2;
	    
	    if (songcheck == 2 && play == 1) oslPlaySound(music2, 0), play = 0, cursong = 3;
	    
	    if (songcheck == 3 && play == 1) oslPlaySound(music3, 0), play = 0, cursong = 4;
	    
	    if (songcheck == 4 && play == 1) oslPlaySound(music4, 0), play = 0, cursong = 5;
	    
	    if (songcheck == 5 && play == 1) oslPlaySound(music5, 0), play = 0, cursong = 6;
	    
	    if (songcheck == 6 && play == 1) oslPlaySound(music6, 0), play = 0, cursong = 7;
	    
	    if (osl_keys->pressed.L) songcheck++, play = 1;
	    
	    if (songcheck > 6) songcheck = 0;
tunes through tunes6 are the variables used to check if the certain song is active.

cursong makes the next song start playing when a song is over.

you can also press L to skip a song.

songcheck determines which song is playing.

play activates the current song.
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||

How to make a decent shooting system.

Here is an example of the shooting system i wrote for NIGaShootuh.

Code:
	    if (gamebegin == 1 && osl_keys->held.cross && shoot == 0 && alive == 1 && pause == 0 || osl_keys->held.cross && shoot == 0 && alive == 3 && pause == 0) shoot = 1;
	    if (gamebegin == 1 && shoot == 1 && bullet01shoot == 0 && bullet02shoot == 0 && bullet03shoot == 0 && bullet04shoot == 0) shoot = 3, bullet01shoot = 1;
	    if (gamebegin == 1 && shoot == 1 && bullet01shoot == 1 && bullet02shoot == 0 && bullet03shoot == 0 && bullet04shoot == 0) shoot = 3, bullet02shoot = 1;
	    if (gamebegin == 1 && shoot == 1 && bullet01shoot == 1 && bullet02shoot == 1 && bullet03shoot == 0 && bullet04shoot == 0) shoot = 3, bullet03shoot = 1;
	    if (gamebegin == 1 && shoot == 1 && bullet01shoot == 1 && bullet02shoot == 1 && bullet03shoot == 1 && bullet04shoot == 0) shoot = 3, bullet04shoot = 1;
	    if (gamebegin == 1 && shoot == 1 && bullet01shoot == 0 && bullet02shoot == 1 && bullet03shoot == 0 && bullet04shoot == 0) shoot = 3, bullet03shoot = 1;
	    if (gamebegin == 1 && shoot == 1 && bullet01shoot == 0 && bullet02shoot == 0 && bullet03shoot == 1 && bullet04shoot == 0) shoot = 3, bullet04shoot = 1;
	    if (gamebegin == 1 && shoot == 1 && bullet01shoot == 0 && bullet02shoot == 0 && bullet03shoot == 0 && bullet04shoot == 1) shoot = 3, bullet01shoot = 1;
	    if (gamebegin == 1 && shoot == 1 && bullet01shoot == 0 && bullet02shoot == 0 && bullet03shoot == 1 && bullet04shoot == 1) shoot = 3, bullet01shoot = 1;
	    if (gamebegin == 1 && shoot == 1 && bullet01shoot == 0 && bullet02shoot == 1 && bullet03shoot == 1 && bullet04shoot == 1) shoot = 3, bullet01shoot = 1;
	    if (gamebegin == 1 && shoot == 1 && bullet01shoot == 0 && bullet02shoot == 1 && bullet03shoot == 1 && bullet04shoot == 0) shoot = 3, bullet04shoot = 1;
	    if (gamebegin == 1 && shoot == 3) shoottime++;
	    if (gamebegin == 1 && bullet01shoot == 1 && pause == 0) bullet01->x += 6;
	    if (gamebegin == 1 && bullet02shoot == 1 && pause == 0) bullet02->x += 6;
	    if (gamebegin == 1 && bullet03shoot == 1 && pause == 0) bullet03->x += 6;
	    if (gamebegin == 1 && bullet04shoot == 1 && pause == 0) bullet04->x += 6;
	    if (gamebegin == 1 && bullet01shoot == 1) oslDrawImage(bullet01);
	    if (gamebegin == 1 && bullet02shoot == 1) oslDrawImage(bullet02);
	    if (gamebegin == 1 && bullet03shoot == 1) oslDrawImage(bullet03);
	    if (gamebegin == 1 && bullet04shoot == 1) oslDrawImage(bullet04);
	    if (gamebegin == 1 && bullet01->x >= 500) bullet01shoot = 0;
	    if (gamebegin == 1 && bullet02->x >= 500) bullet02shoot = 0;
	    if (gamebegin == 1 && bullet03->x >= 500) bullet03shoot = 0;
	    if (gamebegin == 1 && bullet04->x >= 500) bullet04shoot = 0;
	    if (gamebegin == 1 && shoottime >= 25) shoot = 0, shoottime = 0;
	    if (gamebegin == 1 && bullet01shoot == 0) bullet01->x = pship->x, bullet01->y = pship->y;
	    if (gamebegin == 1 && bullet02shoot == 0) bullet02->x = pship->x, bullet02->y = pship->y;
	    if (gamebegin == 1 && bullet03shoot == 0) bullet03->x = pship->x, bullet03->y = pship->y;
	    if (gamebegin == 1 && bullet04shoot == 0) bullet04->x = pship->x, bullet04->y = pship->y;
basically what this does is use 4 images as the bullets and when they arent active there x and y coordinates equal those of the player and is not being drawn to the screen. when x is pressed it checks if you are alive and if the time between each shot is full and activates the next bullet. if all 4 bullets are active then it waits for a bullet to be deactivated. each time a bullet touches an enemy or goes off of the screen it gets deactivated. when a bullet is active it gets drawn and is constantly moving forward untill it gets deactivated. each bullet has its own variable to determine if it is active or not. if shoot equals 1 then the next deactivated bulletis activated and shoot equals 3. before you can shoot again shoot has to equal 0, but before it can equal 0 shoottime has to has to be done counting.
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||

How to disable the callbacks. Used for disabling the Home screen when you press the Home button.

When your initializing oslib with this line:

Code:
oslInit(0);
just change that 0 to a 1 and the callbacks wont be loaded :)
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||

How to change color and alpha levels for images. Used for adding color overlays or transparency.

Code:
oslDrawImage(background); //Notice that i drew this image before i changed the alpha levels

oslSetAlpha(OSL_FX_ALPHA|OSL_FX_COLOR, RGBA(255,0,0,127)); // This will make every image drawn after this have a red tone and be half transparent.
oslDrawImage(top);
oslSetAlpha(OSL_FX_ALPHA, 255); //This resets the effect back to normal so the effect is only applied to what is between the two oslSetAlpha lines.

//Now you can continue drawing everything like normal.
oslSetAlpha(OSL_FX_ALPHA|OSL_FX_COLOR, RGBA(255,0,0,127)); the OSL_FX_ALPHA allows you to set the alpha level. The | (OR) operator allows you to use two at once. The OSL_FX_COLOR allows you to apply a RGB color tone on top of the image.
You can also use all of these for different effects:

OSL_FX_NONE: no effect.

OSL_FX_FLAT: Identical to OSL_FX_NONE, except that if alpha is null, the pixel will not be drawn.

OSL_FX_RGBA: Takes the alpha channel in account for color computation.

OSL_FX_ALPHA: Alpha blend (normal transparency). The coefficient is multiplicated by the source color to define if your plane is more or less transparent.

OSL_FX_ADD: Adds the source and destination, useful for “ghost” planes. You can also specify the coefficient, which is multiplicated by the source value before the addition. If you want simple addition, pass 0xff as coefficient.

OSL_FX_SUB: Substracts the source and destination. Useful for “mask” planes.

OSL_FX_COLOR: Add (with an OR) to other effects so that you can define the transparency for each RGBA component.
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||

How to generate random integers using the Mersenne Twister.

Code:
#include <oslib/oslib.h>
#include <psputils.h>
#include <stdio.h>
#include <stdlib.h>

PSP_MODULE_INFO("Mersenne Twister Example", 0, 1, 1);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);

int GetRandomNum(int lo, int hi)
{
 SceKernelUtilsMt19937Context ctx;
 sceKernelUtilsMt19937Init(&ctx, time(NULL)); //SEED TO TIME
 u32 rand_val = sceKernelUtilsMt19937UInt(&ctx);
 rand_val = lo + rand_val % hi;
 return (int)rand_val;
}

int myrandnum; //used to hold the value of the random number

int generate = 0;

int main()
{   

    oslInit(0);

    oslInitGfx(OSL_PF_8888, 1);

    oslInitConsole();
    
    while (!osl_quit)
    {  
    
    oslStartDrawing();
    
    generate++;
    
    if (generate > 20) myrandnum = GetRandomNum(0, 123), generate = 0;
    
    oslPrintf_xy (480/2,272/4, "random number = %d   ", myrandnum);
    
    oslEndDrawing();

    oslSyncFrame();    
    
    }

    oslEndGfx();
    oslQuit();
    return 0;
}
First of all there a few includes you need for the Mersenne Twister to work:

#include <psputils.h>
#include <stdio.h>
#include <stdlib.h>

im not possitive about the psputils.h or stdlib.h but i do know stdio is necessary, so i just include all of them just to be safe.

int GetRandomNum(int lo, int hi)
{
SceKernelUtilsMt19937Context ctx;
sceKernelUtilsMt19937Init(&ctx, time(NULL)); //SEED TO TIME
u32 rand_val = sceKernelUtilsMt19937UInt(&ctx);
rand_val = lo + rand_val % hi;
return (int)rand_val;
}


This is required to set up the Mersenne Twister. Don't mess with anything in there. Except maybe change the "GetRandomNum" name to make it easier to type.

int myrandnum; //used to hold the value of the random number

int generate = 0;


Like the comment suggests the myrandnum variable is what Mersenne Twister sets the random number to.

"generate" is used as a timer to change the random number each time it counts higher than 20, to show the randomness.

if (generate > 20) myrandnum = GetRandomNum(0, 123), generate = 0;

This waits until generate is greator then 20 and tells the Mersenne Twister to generate a new random number between 0 and 123. Then sets generate back to 0 so it can continue counting. To make the random number equal a number between different highs and lows just change the first parameter to the low and the second to the high.

oslPrintf_xy (480/2,272/4, "random number = %d ", myrandnum);

This simply displays the random number on the screen that was generated by the Mersenne Twister. Notice the empty spaces after the "%d", these are needed to clear out any extra numbers.

For example, if the first random number is a two digit number and the second number is only single digit then the screen with change the first number to the new number but the number after that will also be shown. So in other words if the first number is 38 and the second number is 6 then after the second number is 6 it will display 68 because the screen is blank so nothing is erased. The spaces take care of this.
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||

How to use Windows fonts in your app/game.

First you need to locate the font2osl.exe file. It should be included with the OSLib download.

Note: When you install oslib the installer does not move any of the tools into your dev environment, so if you cant find it just download it again and go to the "Tools" directory and you should see the program.

Now that you have located the converter make sure you move it somewhere within your pspdev setup. I just copied the whole Tools folder right to the root of my environment.

Run pspdev.bat and go into the directory where font2osl.exe is. So if you copied it to the root like i did just type:

Code:
cd tools
now type the following:

Code:
font2osl -convert "YourWindowsFont" fontSize "YourFontName.bmp" "YourFontName.txt"
Now if you look in the directory you should notice two new files for your font, a .bmp and a .txt file.

Once you confirm that they were created type the following line into pspdev.bat:

Code:
font2osl -create "YourFontName.bmp" "YourFontName.txt" "YourFinalFontName.oft"
Note: Of course you need to replace YourFontName with the font you want to use and fontSize with the font size you want to use. You do not need the physical font file as long as it is currently installed.

You should now have a .oft file for your font! oft is a file format recognized as a font file by oslib.

Now to use your newly created font. First you need to set a pointer for your font so use this line for that:

Code:
OSL_FONT *font;
Then use this to load your font:

Code:
oslLoadFontFile("font.oft");
Now put this line somewhere in your loop where you write text to the screen:

Code:
oslSetFont(font);
Congratulations! You have now writtin text with a custom font!

||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||

If you have any questions or need help or anything feel free to ask.

Ill probably write some more tuts later, I'm also taking requests.
Reply With Quote
 

 



Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 10:23 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©