main.c:
Code: Select all
/* PSP Tic-Tac-Toe - Tic Tac Toe game for the Sony PSP
*
* It is a simple text-based game of Tic-Tac-Toe.
*/
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspdisplay.h>
#include <pspctrl.h>
PSP_MODULE_INFO ( "PSP Tic-Tac-Toe", 0, 1, 1 );
#define printf pspDebugScreenPrintf
/* Exit callback */
int exit_callback(int arg1, int arg2, void *common) {
sceKernelExitGame ();
return 0;
}
/* Callback thread */
int CallbackThread(SceSize args, void *argp) {
int cbid;
cbid = sceKernelCreateCallback ("Exit Callback", exit_callback, NULL);
sceKernelRegisterExitCallback (cbid);
sceKernelSleepThreadCB ();
return 0;
}
/* Sets up the callback thread and returns its thread id */
int SetupCallbacks(void) {
int thid = 0;
thid = sceKernelCreateThread ( "update_thread", CallbackThread, 0x11, 0xFA0, 0, 0 );
if ( thid >= 0 ) {
sceKernelStartThread(thid, 0, 0);
}
return thid;
}
int main() {
pspDebugScreenInit ();
SetupCallbacks ();
char squares[3][3] = {
{' ', ' ', ' '},
{' ', ' ', ' '},
{' ', ' ', ' '}
};
char player = 'X';
int i = 0;
int cursorX = 1;
int cursorY = 1;
SceCtrlData pad;
pspDebugScreenSetXY (0, 33);
printf ("PSP Tic-Tac-Toe v. 0.1b");
while ( 1 ) {
sceCtrlReadBufferPositive (&pad, 1);
if ( (pad.Buttons & PSP_CTRL_UP) && (cursorY > 0) )
--cursorY;
else if ( (pad.Buttons & PSP_CTRL_DOWN) && (cursorY < 2) )
++cursorY;
else if ( (pad.Buttons & PSP_CTRL_LEFT) && (cursorX > 0) )
--cursorX;
else if ( (pad.Buttons & PSP_CTRL_RIGHT) && (cursorX < 2) )
++cursorX;
else if ( (pad.Buttons & PSP_CTRL_CROSS) &&
(squares[cursorX][cursorY] == ' ') ) {
squares[cursorX][cursorY] = player;
if ( player == 'X' )
player = 'O';
else if ( player == 'O' )
player = 'X';
}
pspDebugScreenSetXY (0, 0);
printf ("%c|%c|%c\n", squares[0][0], squares[1][0], squares[2][0]);
printf ("-+-+-\n");
printf ("%c|%c|%c\n", squares[0][1], squares[1][1], squares[2][1]);
printf ("-+-+-\n");
printf ("%c|%c|%c\n", squares[0][2], squares[1][2], squares[2][2]);
pspDebugScreenSetXY (cursorX * 2, cursorY * 2);
pspDebugScreenSetBackColor (0xFF0000FF);
printf ("%c", squares[cursorX][cursorY]);
pspDebugScreenSetBackColor (0xFF000000);
pspDebugScreenSetXY (0, 5);
if ( ((squares[0][1] == squares[1][1]) && (squares[1][1] == squares[2][1])) ||
((squares[0][0] == squares[1][1]) && (squares[1][1] == squares[2][2])) ||
((squares[1][0] == squares[1][1]) && (squares[1][1] == squares[1][2])) ||
((squares[2][0] == squares[1][1]) && (squares[1][1] == squares[0][2])) ||
((squares[0][0] == squares[0][1]) && (squares[0][1] == squares[0][2])) ||
((squares[0][2] == squares[1][2]) && (squares[1][2] == squares[2][2])) ||
((squares[0][0] == squares[1][0]) && (squares[1][0] == squares[2][0])) ||
((squares[2][0] == squares[2][1]) && (squares[2][1] == squares[2][2])) ) {
printf ("%c is the winner!", player);
break;
}
if ( (squares[0][0] != ' ') &&
(squares[0][1] != ' ') &&
(squares[0][2] != ' ') &&
(squares[1][0] != ' ') &&
(squares[1][1] != ' ') &&
(squares[1][2] != ' ') &&
(squares[2][0] != ' ') &&
(squares[2][1] != ' ') &&
(squares[2][2] != ' ') ) {
printf ("Tie game!");
break;
}
for ( i = 0; i < 5; ++i )
sceDisplayWaitVblankStart ();
}
pspDebugScreenClear ();
sceKernelSleepThread ();
return 0;
}
Code: Select all
GET = psp_tictactoe
OBJS = main.o
CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)
EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = PSP Tic-Tac-Toe
PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak