![]() |
|
#1
|
||||
|
||||
|
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://www.sendspace.com/file/wdc8br 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);
}
}
}
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://www.sendspace.com/file/6rwyp2 ------------------ 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;
}
Code:
#ifndef __ADD__ #define __ADD__ Code:
int plus(int a, int b);//Add a ';' Code:
#endif Code:
#ifndef __ADD__ #define __ADD__ int plus(int a, int b);//Add a ';' #endif Code:
Add.h Binary + source: http://www.sendspace.com/file/gzx6xq ------------------ 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 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. ------------------ 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
}
}
------------------ 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://www.sendspace.com/file/mz0u3d
__________________
The PSP Programming and Development Mega Thread VDrum downloaded over 10000 times! PSN: Xsjado7
|
|
#2
|
|||
|
|||
|
pretty cool tutorial blade punk ;)
|
|
#3
|
|||
|
|||
|
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!
__________________
|
|
#4
|
||||
|
||||
|
Quote:
__________________
The PSP Programming and Development Mega Thread VDrum downloaded over 10000 times! PSN: Xsjado7
|
|
#5
|
||||
|
||||
|
Thank you blade punk, like I said, your always there to help :)
__________________
----------Current project----------
---------- -----------A shell which will have music, picture, game, text editors etc included! WIP Thread |
|
#6
|
|||
|
|||
|
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); 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); |
|
#7
|
||||
|
||||
|
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 VDrum downloaded over 10000 times! PSN: Xsjado7
|
|
#8
|
||||
|
||||
|
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 VDrum downloaded over 10000 times! PSN: Xsjado7
|
|
#9
|
|||
|
|||
|
did you put this in the mega thread?
|
|
#10
|
||||
|
||||
|
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 VDrum downloaded over 10000 times! PSN: Xsjado7
|
![]() |
|
|
|||
|
|||
|
|
| Thread Tools | |
| Display Modes | |
|
|