i'm wrote a simple tic-tac-toe game for the psp, using sdl, but i found that the positions of the "X" and the "O" did not correspond to the correct ones, i use a 2*2 matrix and indexes "j" and "i" to run the matrix and draw the symbols, sometimes i got the same symbol on two different positions (like for example [1][0] and [0][2]), even when the matrix has only one valor
This is my code
it's a little messy, (lots of commented code), but it still compile.
Code: Select all
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_mixer.h>
#include <pspkernel.h>
#define PSP_BUTTON_UP (8)
#define PSP_BUTTON_DOWN (6)
#define PSP_BUTTON_LEFT (7)
#define PSP_BUTTON_RIGHT (9)
#define PSP_BUTTON_X (2)
SDL_Surface *screen = NULL;
SDL_Joystick *joystick = NULL;
int done = 0;
int exit_callback(int arg1, int arg2, void *common){
done = 1;
return 0;
}
int CallbackThread(SceSize args, void *argp) {
int cbid;
cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
sceKernelRegisterExitCallback(cbid);
sceKernelSleepThreadCB();
return 0;
}
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 initSDL(){
SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
joystick = SDL_JoystickOpen(0);
screen = SDL_SetVideoMode(480, 272, 24, SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF);
if(screen == NULL){
fprintf(stderr, "No se pudo iniciar el mode de pantalla: %s\n", SDL_GetError());
return 0;
}
if(Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT,2,1024)){
fprintf(stderr,"Unable to open audio!\n");
return 0;
}
return 1;
}
SDL_Surface* loadImage(char *filename){
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = IMG_Load(filename);
if(loadedImage != NULL){
optimizedImage = SDL_DisplayFormat(loadedImage);
SDL_FreeSurface(loadedImage);
}else{
fprintf(stderr, "No se encontro la imagen\n");
done = 1;
}
return optimizedImage;
}
void applySurface(int x, int y, SDL_Surface *source, SDL_Surface *destination){
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(source, NULL, destination, &offset);
}
int getRealxPos(int x){
switch(x){
case 0:
return 74;
case 1:
return 189;
case 2:
return 303;
}
return 0;
}
int getRealyPos(int y){
switch(y){
case 0:
return 16;
case 1:
return 95;
case 2:
return 175;
}
return 0;
}
/*
int initMatrix(int gameMatrix[2][2]){
int i = 0, j = 0;
for(i=0;i<2;i++)
for(j=0;j<2;j++){
gameMatrix[i][j] = 0;
}
return 0;
}
int drawMarks(int gameMatrix[2][2],SDL_Surface *X, SDL_Surface* O, SDL_Surface* src_buffer){
int i = 0, j = 0;
for(i=0;i<2;i++)
for(j=0;j<2;j++){
if(gameMatrix[i][j] == 1){
applySurface(getRealxPos(i) + 19, getRealyPos(j) + 30,X,src_buffer);
}else if(gameMatrix[i][j] == 2){
applySurface(getRealxPos(i) + 19, getRealyPos(j) + 30,O,src_buffer);
}
}
return 0;
}
*/
int main(int argc, char *argv[]){
SetupCallbacks();
int xPos = 0;
int yPos = 0;
int i = 0, j = 0;
int gameMatrix[2][2];
for(i=0;i<3;i++)
for(j=0;j<3;j++){
gameMatrix[i][j] = 0;
}
int turn = 1;
gameMatrix[0][2] = 0;
gameMatrix[1][0] = 1;
if(!initSDL()){
done = 1;
}
SDL_Event event;
SDL_Surface* background = loadImage("backgroundGato.png");
SDL_Surface* src_buffer = loadImage("backgroundGato.png");
SDL_Surface* marker = loadImage("frame.bmp");
SDL_Surface* X = loadImage("cruz.bmp");
SDL_Surface* O = loadImage("bola.bmp");
applySurface(0,0,background,screen);
do{
SDL_Delay(100);
applySurface(0,0,background,src_buffer);
applySurface(getRealxPos(xPos),getRealyPos(yPos),marker,src_buffer);
if(SDL_JoystickGetButton(joystick, PSP_BUTTON_RIGHT)){
if(xPos < 2){
++xPos;
}
}
if(SDL_JoystickGetButton(joystick, PSP_BUTTON_LEFT)){
if(xPos > 0){
--xPos;
}
}
if(SDL_JoystickGetButton(joystick, PSP_BUTTON_DOWN)){
if(yPos < 2){
++yPos;
}
}
if(SDL_JoystickGetButton(joystick, PSP_BUTTON_UP)){
if(yPos > 0){
--yPos;
}
}
while(SDL_PollEvent (&event)){
if(event.type == SDL_JOYBUTTONDOWN){
if(event.jbutton.button == PSP_BUTTON_X){
if(turn == 1){
gameMatrix[xPos][yPos] = 1;
}else if(turn == 2){
gameMatrix[xPos][yPos] = 2;
}
turn = (turn==1) ? 2: 1;
}
}
}
//for(i=0;i<3;++i){
//for(j=0;j<3;++j){
i =0;
while(i < 3){
j = 3;
//while (j < 3){
if(gameMatrix[i][j] == 1){
applySurface(getRealxPos(i) + 19, getRealyPos(j) + 30,X,src_buffer);
}else if(gameMatrix[i][j] == 2){
applySurface(getRealxPos(i) + 19, getRealyPos(j) + 30,O,src_buffer);
}
//j = j+1;
//}
i = i+1;
}
//}
//}
/*
if (gameMatrix[0][0] == 1){
applySurface(getRealxPos(0) + 19, getRealyPos(0) + 30,X,src_buffer);
}
if (gameMatrix[0][1] == 1){
applySurface(getRealxPos(0) + 19, getRealyPos(1) + 30,X,src_buffer);
}
if (gameMatrix[0][2] == 1){
applySurface(getRealxPos(0) + 19, getRealyPos(2) + 30,X,src_buffer);
}
if (gameMatrix[1][0] == 1){
applySurface(getRealxPos(1) + 19, getRealyPos(0) + 30,X,src_buffer);
}
if (gameMatrix[1][1] == 1){
applySurface(getRealxPos(1) + 19, getRealyPos(1) + 30,X,src_buffer);
}
if (gameMatrix[1][2] == 1){
applySurface(getRealxPos(1) + 19, getRealyPos(2) + 30,X,src_buffer);
}
if (gameMatrix[2][0] == 1){
applySurface(getRealxPos(2) + 19, getRealyPos(0) + 30,X,src_buffer);
}
if (gameMatrix[2][1] == 1){
applySurface(getRealxPos(2) + 19, getRealyPos(1) + 30,X,src_buffer);
}
if (gameMatrix[2][2] == 1){
applySurface(getRealxPos(2) + 19, getRealyPos(2) + 30,X,src_buffer);
}
*/
//drawMarks(gameMatrix,X,O,src_buffer);
applySurface(0,0,src_buffer,screen);
SDL_Flip(screen);
}while(!done);
SDL_Quit();
sceKernelExitGame();
return 0;
}
the code compiles without warnings, this is my makefile
Code: Select all
TARGET = gatox
PSPSDK = $(shell psp-config --pspsdk-path)
PSPBIN = $(PSPSDK)/../bin
OBJS = gatox.o
JOY = YES
CFLAGS = -Wall -Wno-long-long -O2 -G0 -DJOY_$(JOY)
CFLAGS += $(shell $(PSPBIN)/sdl-config --cflags)
#LIBS = -lSDL_mixer -lvorbisidec -lSDL $(shell $(PSPBIN)/sdl-config --libs)
LIBS = -lSDL_gfx -lSDL_image -lSDL_mixer -lSDL_ttf -lvorbisidec -lfreetype -lpng -ljpeg -lz -lm $(shell $(PSPBIN)/sdl-config --libs)
EXTRA_TARGETS = EBOOT.PBP
include $(PSPSDK)/lib/build.mak
thxs for your time
Drako
PD: I apologize for my english, is noy my first languaje.