Code: Select all
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspctrl.h>
#include <pspsdk.h>
#include <pspdisplay.h>
#include "ZBadhoc.h"
#include <oslib/oslib.h>
#include <string>
/* Define the module info section */
#define MY_HOMEBREW_MAJOR_VERSION 0
#define MY_HOMEBREW_MINOR_VERSION 42
//PSP_MODULE_USER
//PSP_MODULE_KERNEL
PSP_MODULE_INFO("SMBC", PSP_MODULE_USER, MY_HOMEBREW_MAJOR_VERSION, MY_HOMEBREW_MINOR_VERSION);
PSP_HEAP_SIZE_MAX();
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);
OSL_IMAGE *player_img;
/**Ad-Hoc Only Variables**/
int sceInit(void);
int send; //Data Sent And Received
int receive;
int encore=1; //Adhoc Enabled
ZBremotePsp * pPsp; //Stores Mac Address Of Other PSP
int user; //Host = 1 Guest = 2
int init; //Mode Initiation Variable
char buffer[42+1];
char data[50];
int state = 1;
/**^^^^^^^^^^^^^^^^^^^^^**/
/**OSLIB STUFF**/
int initOSLib(){
oslInit(0);
oslInitGfx(OSL_PF_8888, 1);
oslInitAudio();
oslSetQuitOnLoadFailure(1);
oslSetKeyAutorepeatInit(40);
oslSetKeyAutorepeatInterval(10);
return 0;
}
int endOSLib(){
oslEndGfx();
osl_quit = 1;
return 0;
}
/**^^^^^^^^^^^**/
typedef struct {
int x,y;
int state;
OSL_IMAGE *img;
}Player1;
Player1 p1;
typedef struct {
int x,y;
int state;
OSL_IMAGE *img;
}Player2;
Player2 p2;
void LoadImages(), DrawImages();
//char to int function
bool string2int(char* digit, int& result) {
result = 0;
//--- Convert each digit char and add into result.
while (*digit >= '0' && *digit <='9') {
result = (result * 10) + (*digit - '0');
digit++;
}
//--- Check that there were no non-digits at end.
if (*digit != 0) {
return false;
}
return true;
}
int main( void)
{
scePowerSetClockFrequency(333, 333, 166); //Overclock to full potential
sceInit(); //set exit callback
pspDebugScreenInit();
sceKernelDelayThread(1000 * 1000); //1 Second Delay
//init everything
ZBadhoc::getSingleton();
SceCtrlData pad;
pspDebugScreenClear();
while(encore)
{
if (!user) //If The Player Hasn't Chosen To Host Or To Join
{
pspDebugScreenSetXY(0, 1); //Set Text X & Y
pspDebugScreenPrintf("To Host A Game Press []\n");
pspDebugScreenPrintf("To Join A Game Press /\\\n");
pspDebugScreenPrintf("To Quit Press ()");
}
//what button is pressed
sceDisplayWaitVblankStart();
sceCtrlReadBufferPositive(&pad, 1);
if(pad.Buttons & PSP_CTRL_SQUARE)
{
if (user != 1)
{
user = 1; //Set Player To Host
}
}
if(pad.Buttons & PSP_CTRL_TRIANGLE)
{
if (user != 2) {user = 2;} //Set Player To Guest
}
if(pad.Buttons & PSP_CTRL_CIRCLE) {encore = 0; init = 0; pPsp = 0; user = 0; continue;} //Quit
/**HOST**/
if (user == 1)
{
if (init == 0)
{
pPsp= ZBadhoc::getSingleton().selectAPsp(); //Select A PSP
receive = ZBadhoc::getSingleton().requestConnection(pPsp); //Request Connection
if( pPsp==NULL) {encore = 0; continue;} //If No PSP Is Chosen then Exit
initOSLib();
LoadImages(); //Load Images
init = 1; //End Initiation
}
if (init == 1) //Main Loop After Initiation-
{
/**Main Functions**/
DrawImages();
if (osl_keys->held.right) {p1.x+=4;}
if (osl_keys->held.left) {p1.x-=4;}
if (osl_keys->held.up) {p1.y--;}
if (osl_keys->held.down) {p1.y++;}
/**Ad-Hoc Functions**/
if (receive < 0) {continue;} //If No Signal, then Exit.
sprintf(data,"%i",p1.x); //convert int to char
send = ZBadhoc::getSingleton().sendData(pPsp,data,strlen(data));//send a messge to the other PSP
receive = ZBadhoc::getSingleton().receiveData( pPsp, buffer, 42); //receive data
if (receive > 0) {buffer[receive]=0; string2int(buffer,p2.x);} //convert char to int
receive = 0;
}
}
/**GUEST**/
if (user == 2)
{
if (init == 0)
{
pPsp = ZBadhoc::getSingleton().waitForConnection(); //Wait For Connection
if (pPsp == NULL) {continue;} //If No PSP Is Chosen Then Exit
initOSLib(); //Initiate Old School Library
LoadImages(); //Load Images
init = 1; //End Initiation
}
if (init == 1) //Main Loop After Initiation
{
/**Main Functions**/
DrawImages();
if (osl_keys->held.right) {p2.x+=4;}
if (osl_keys->held.left) {p2.x-=4;}
if (osl_keys->held.up) {p2.y--;}
if (osl_keys->held.down) {p2.y++;}
/**Ad-Hoc Functions**/
if (receive < 0) {continue;} //If No Signal, then Exit.
sprintf(data,"%i",p2.x); //int to char
send = ZBadhoc::getSingleton().sendData(pPsp,data,strlen(data));//send a messge to the other PSP
receive = ZBadhoc::getSingleton().receiveData( pPsp, buffer, 42); //Receive data
if (receive > 0) {buffer[receive]=0; string2int(buffer,p1.x);} //char to int
receive = 0;
}
}
}//end of main while
pspDebugScreenClear();
pspDebugScreenPrintf("Done.");
sceKernelSleepThread();
return 0;
}
/* Exit callback */
int exit_callback(int arg1, int arg2, void *common)
{
sceKernelExitGame();
return (int)0;
}
/* Callback thread */
int CallbackThread(SceSize args, void *argp)
{
int cbid;
cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
sceKernelRegisterExitCallback(cbid);
sceKernelSleepThreadCB();
return 0;
}
int sceInit(void)
{
int thid = 0;
thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
if(thid >= 0)
{
sceKernelStartThread(thid, 0, 0);
}
return thid;
}
void LoadImages()
{
p1.img = oslLoadImageFilePNG("img.png", OSL_IN_RAM,OSL_PF_5551);
p2.img = oslLoadImageFilePNG("img.png", OSL_IN_RAM,OSL_PF_5551);
}
void DrawImages()
{
oslStartDrawing(); //Start Drawing
oslCls(); //Clear Screen
oslReadKeys(); //Read Input Keys
if (user == 1) {oslDrawImageXY(p2.img,p2.x,p2.y); oslDrawImageXY(p1.img,p1.x,p1.y);}
if (user == 2) {oslDrawImageXY(p1.img,p1.x,p1.y); oslDrawImageXY(p2.img,p2.x,p2.y);}
oslEndDrawing(); //End Drawing
oslAudioVSync(); //Synchronize Audio
oslSyncFrame(); //Synchronize Screen
}