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

Notices

Reply
 
Thread Tools Display Modes
  #1  
Old 08-07-2008, 07:25 AM
Blade_punk's Avatar
Blade_punk Blade_punk is offline
Moderator
PSP Monk
 
Join Date: Nov 2006
Location: Candy Mountain
Posts: 2,686
Blade_punk will become famous soon enoughBlade_punk will become famous soon enough
Default

Hey,
Sabre requested some info on reading and writing to a file earlier today so i decided to write this tutorial up.

Source+binary can be downloaded here:
http://darksabre.site40.net/uploader...e%20IO.rar

It was coded quickly but it shouldn't be too bad. I used sceIo functions instead of the virtaul filesystem ones because I found that the ones built into OSLib use stdio.h.

Also, a quick function i forgot to add:
Code:
char write_buffer[128*1024];
void write_file(const char *readpath, const char *writepath)
{
	int fdin;
	int fdout;
	fdin = sceIoOpen(readpath, PSP_O_RDONLY, 0777);
	if(fdin >= 0)
	{
		int bytesRead = 1;
		fdout = sceIoOpen(writepath, PSP_O_WRONLY | PSP_O_CREAT | PSP_O_TRUNC, 0777);
		bytesRead = sceIoRead(fdin, write_buffer, sizeof(write_buffer));
		while((bytesRead > 0) && (fdout >= 0))
		{
			sceIoWrite(fdout, write_buffer, bytesRead);
			bytesRead = sceIoRead(fdin, write_buffer, sizeof(write_buffer));
		}

		if(fdout >= 0)
		{
			sceIoClose(fdout);
		}

		if(fdin >= 0)
		{
			sceIoClose(fdin);
		}
	}
}
I take no credit for that function, it was made by Slasher.

Sorry for the late posting by the way, Sabre. I got caught up doing things.

-----------------

Simple Menu Source

I also made a simple menu source to help out some of the newbies. Its relatively simple to understand and isn't too bad. It contains 2 loops, one within the other. It blits the menu then enters the second loop and waits for a button to be pressed. If the user presses a button it breaks the loop and updates the menu so it isnt constantly printing.

Link:
http://darksabre.site40.net/uploader...es/MenuTut.rar

------------------

Splitting your source into multiple files

Well everyone knows it can get hard working out of one file, your main.c. Finding functions can be difficult and everything gets harder when navigating through it. In this tutorial I'll give you a quick rundown on how to break your program up into separate files so its waaaay easier to manage.
First of all, say we have a source file called 'Add.c" that contains:
Code:
int plus(int a, int b)
{
	return a+b;
}
We begin by creating a new source file called 'Add.h'. This is going to contain info about our source (*.c) file. Add the very top of the file we add:
Code:
#ifndef __ADD__
#define __ADD__
This begins our header. Now we simply put the first line of our function:
Code:
int plus(int a, int b);//Add a ';'
Here you add all of the 'descriptions' i guess you could call them of your functions from your source file like what we've just done. Finish of the file by adding:
Code:
#endif
So our whole Add.h file is:
Code:
#ifndef __ADD__
#define __ADD__

int plus(int a, int b);//Add a ';'

#endif
From here add
Code:
Add.h
to the top of Add.c and any other file thats going to be using functions from Add.c. After that simply put 'Add.o' to your OBJS line in your makefile and your finished.

Binary + source:
http://darksabre.site40.net/uploader...tingSource.rar

------------------

Heres a really basic batch file that can be pretty handy for quick testing if your not using PSPLink:
Code:
make clean
make
MOVE .\EBOOT.PBP *Your PSP's drive letter*:\PSP\GAME\*Name of your program*\EBOOT.PBP
make clean
cmd
Simply copy that into an empty text file and save as something like compile.bat. Now after you've edited your source and want to test on your PSP, just open the file or type 'compile'
into a command prompt and it will compile your program, copy the eboot.pbp to your PSP and then clean up everything. Saves you going My Computer, *PSP drive*, PSP, GAME .etc constantly on only copies what you need.

How to do the same thing on Linux/Cygwin thanks to Dark_Sabre:
Quote:
Originally Posted by Dark_Sabre View Post
another script for making and copying like your windows one, but for ubuntu, change "projectname" to the name of your project and /media/disk/ to the name of your psp.

Code:
#!/bin/bash
make clean
make
cp -r ../projectname /media/disk/PSP/GAME/
umount /media/disk/
save as "make.sh" or anything with a ".sh" extension. It may work with other distro's of linux, but I only have ubuntu.
Yay for first ubuntu script :)

you should just be able to use this in terminal if the permissions are right (777)

Code:
./make.sh
or as I just tried, just double click on it.
------------------
Fading In and Out
Presentation is a big part of a program if your aiming to make the best one around. Simply using fading can make your app look waaaay better. Here are the functions:
Code:
int transp;//Fo transparency
OSL_COLOR *fade;//To hold the colour of the rectangle blitted

void fadeOut(OSL_IMAGE* bg)//Name and params taken
{
	for(transp = 0; transp < 255; transp += 3)//Sets the transparency to 0 aka invisible. Then it increases by 3 until it equals 255 aka completely visible
	{
		oslStartDrawing();//Start drawing
		fade = RGBA(0, 0, 0, transp);//Set the colour of the rectangle. Sets the alpha to what transp currently is
		oslDrawImage(bg);//Draws an image in the background
		oslDrawFillRect(0, 0, 480, 272, fade);//Draws the rectangle over the screen
		oslEndDrawing();//Finish drawing
		oslEndFrame();//End the current frame
    	oslSyncFrame();//Sync everything
	}
}
void fadeIn(OSL_IMAGE* bg)//Name and params taken
{
	for(transp = 255; transp > 0; transp -= 3)//Sets the transparency to 255 aka completely visible. Then it decreases by 3 until it equals 255 aka invisible
	{
		oslStartDrawing();//Start drawing
		fade = RGBA(0, 0, 0, transp);//Set the colour of the rectangle. Sets the alpha to what transp currently is
		oslDrawImage(bg);//Draws an image in the background
		oslDrawFillRect(0, 0, 480, 272, fade);//Draws the rectangle over the screen
		oslEndDrawing();//Finish drawing
		oslEndFrame();//End the current frame
    	oslSyncFrame();//Sync everything
	}
}
They're made for OSLib but can be easily ported
------------------
Triangle function
Heres an easy to use triangle function for OSLib.
Code:
void oslDrawFillTri(int x0, int y0, int x1, int y1, int x2, int y2, OSL_COLOR color)
{
		OSL_LINE_VERTEX* vertices;

		vertices = (OSL_LINE_VERTEX*)sceGuGetMemory(3 * sizeof(OSL_LINE_VERTEX));
		
		//Goes counter clockwise
		vertices[0].color = color;
		vertices[0].x = x0;
		vertices[0].y = y0;
		vertices[0].z = 0;

		//The middle point
		vertices[1].color = color;
		vertices[1].x = x1;
		vertices[1].y = y1;
		vertices[1].z = 0;

		vertices[2].color = color;
		vertices[2].x = x2;
		vertices[2].y = y2;
		vertices[2].z = 0;

		sceGuDisable(GU_TEXTURE_2D);
		sceGuDrawArray(GU_TRIANGLE_STRIP, GU_COLOR_8888|GU_VERTEX_16BIT|GU_TRANSFORM_2D, 3, 0, vertices);
		sceGuEnable(GU_TEXTURE_2D);
}
------------------

Simple Shooting Source

Contains a basic shooting for upto 5 bullets. Pretty easy to read and modify
http://darksabre.site40.net/uploader...hootingTut.rar
------------------

Basic Platformer Source
Here's some old source of mine. I coded this about 4 or 5 months ago as a basic C++ OOP sample for myself. Has basic velocity usage.
http://www.sendspace.com/file/f0y3z0
__________________
The PSP Programming and Development Mega Thread
-Rock Station-
PSN: Xsjado7
Quote:
Why don't you try walking on your hands! Then you could use your feet for hi-5's and eating sandwiches. You know, all the important stuff!

Last edited by Blade_punk; 01-06-2009 at 02:35 AM.
Reply With Quote
  #2  
Old 08-07-2008, 09:50 AM
pirata nervo pirata nervo is offline
Moderator
PSP Titan
 
Join Date: Mar 2007
Location: www.consoleworld.net
Posts: 5,008
pirata nervo is on a distinguished road
Default

pretty cool tutorial blade punk ;)
__________________

Upgrade your PSP Slim or FAT now!
NervOS Official Forum
Reply With Quote
  #3  
Old 08-07-2008, 01:35 PM
InsertWittyName InsertWittyName is offline
Programmer
PSP Newbie
 
Join Date: Jun 2008
Posts: 41
InsertWittyName User Has a Beginner Reputation
Default

I would recommend removing all the oslib dependent parts in the file io tutorial so anyone can use it and add error checking, you don't do any!
__________________
Reply With Quote
  #4  
Old 08-07-2008, 05:31 PM
Blade_punk's Avatar
Blade_punk Blade_punk is offline
Moderator
PSP Monk
 
Join Date: Nov 2006
Location: Candy Mountain
Posts: 2,686
Blade_punk will become famous soon enoughBlade_punk will become famous soon enough
Default

Quote:
Originally Posted by InsertWittyName
I would recommend removing all the oslib dependent parts in the file io tutorial so anyone can use it and add error checking, you don't do any!
Oh my, i didnt did I :o lol, i didnt even realise. I'll add that in and remove the OSLib code when I re-do it tonight
__________________
The PSP Programming and Development Mega Thread
-Rock Station-
PSN: Xsjado7
Quote:
Why don't you try walking on your hands! Then you could use your feet for hi-5's and eating sandwiches. You know, all the important stuff!
Reply With Quote
  #5  
Old 08-07-2008, 11:57 PM
Dark_Sabre's Avatar
Dark_Sabre Dark_Sabre is offline
Programmer of The Month
PSP Ninja
 
Join Date: Jul 2007
Location: Tauranga, New Zealand
Posts: 720
Dark_Sabre User Has a Beginner Reputation
Default

Thank you blade punk, like I said, your always there to help :)
__________________

ah... it feels good to have a sig again....

SabrOS Progress: Currently working on GUI
Latest release
Reply With Quote
  #6  
Old 08-08-2008, 03:15 AM
Brick Brick is offline
Programmer
PSP Smarty
 
Join Date: Mar 2008
Posts: 145
Brick User Has a Beginner Reputation
Default

I also suggest allocating memory for read/write buffers
dynamically.

Also , you might want to end up writting a small "library",that will look like
this:

Code:
void openFile(SceUID file_handle,const char* filename,flags..?)
void closeFile(file_handle);
void writeFile(file_handle,const char* buf);
void readFile(file_handle,char** buf);
Usage:

Code:
SceUID file;
openFile(file...,"text.txt",..)

char* readbuf=NULL;

readFile(file,readbuf);
free(readbuf)//allocated dynamically by "readFile" function
closeFile(file)

//write something:
openFile(file...,"text.txt",..)
writeFile(file,"1..23..4\n");
writeFile(file,"hey!...");
closeFile(file);
Reply With Quote
  #7  
Old 08-08-2008, 03:21 AM
Blade_punk's Avatar
Blade_punk Blade_punk is offline
Moderator
PSP Monk
 
Join Date: Nov 2006
Location: Candy Mountain
Posts: 2,686
Blade_punk will become famous soon enoughBlade_punk will become famous soon enough
Default

I was thinking about making a library to simplify it. Might work on it after I've uploaded the commented source

Thanks for the feedback

Edit:
Source now commented and doesnt use OSLib (check out the size difference in the file!).

Edit 2:
I got bored so i added a tutorial on how to split up your source files. Explained really badly but should give you an idea
__________________
The PSP Programming and Development Mega Thread
-Rock Station-
PSN: Xsjado7
Quote:
Why don't you try walking on your hands! Then you could use your feet for hi-5's and eating sandwiches. You know, all the important stuff!
Reply With Quote
  #8  
Old 08-09-2008, 01:04 AM
Blade_punk's Avatar
Blade_punk Blade_punk is offline
Moderator
PSP Monk
 
Join Date: Nov 2006
Location: Candy Mountain
Posts: 2,686
Blade_punk will become famous soon enoughBlade_punk will become famous soon enough
Default

Binary and source code uploaded for source splitting tut

Edit:
Updated first post with a basic batch file for compiling your program, copying it to your PSP and then cleaning up everything
__________________
The PSP Programming and Development Mega Thread
-Rock Station-
PSN: Xsjado7
Quote:
Why don't you try walking on your hands! Then you could use your feet for hi-5's and eating sandwiches. You know, all the important stuff!
Reply With Quote
  #9  
Old 08-09-2008, 07:34 AM
pirata nervo pirata nervo is offline
Moderator
PSP Titan
 
Join Date: Mar 2007
Location: www.consoleworld.net
Posts: 5,008
pirata nervo is on a distinguished road
Default

did you put this in the mega thread?
__________________

Upgrade your PSP Slim or FAT now!
NervOS Official Forum
Reply With Quote
  #10  
Old 08-09-2008, 07:37 AM
Blade_punk's Avatar
Blade_punk Blade_punk is offline
Moderator
PSP Monk
 
Join Date: Nov 2006
Location: Candy Mountain
Posts: 2,686
Blade_punk will become famous soon enoughBlade_punk will become famous soon enough
Default

Its in there but under other stuff.

I guess I'll move it up to tutorials now

Edit:
Added fading functions
__________________
The PSP Programming and Development Mega Thread
-Rock Station-
PSN: Xsjado7
Quote:
Why don't you try walking on your hands! Then you could use your feet for hi-5's and eating sandwiches. You know, all the important stuff!
Reply With Quote
Reply

 



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:04 PM.


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