forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Tic-Tac-Toe C game error

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
drako



Joined: 14 Sep 2007
Posts: 2

PostPosted: Fri Sep 14, 2007 6:38 pm    Post subject: Tic-Tac-Toe C game error Reply with quote

Hi! Everybody

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:

#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;
}



i tryed several things to try to find the solution, but none of them worked, when "gameMatrix [1][0] = 1" the valor of gameMatrix[0][2] also changes to 1 or it behaves as if it had that value

the code compiles without warnings, this is my makefile

Code:

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




i would appreciate if someone could give me any pointers about how to solve this problem

thxs for your time

Drako

PD: I apologize for my english, is noy my first languaje.
Back to top
View user's profile Send private message
CpuWhiz



Joined: 04 Jun 2007
Posts: 42

PostPosted: Fri Sep 14, 2007 7:16 pm    Post subject: Reply with quote

Short answer: int gameMatrix[3][3];

Long answer: When you allocate an array the numbers in the brackets is the total count, so for a 3x3 matrix you allocate it as [3][3] but use the numbers 0, 1, and 2 to access the elements. [1][0] and [0][2] are the same because you allocated a 2x2 instead of a 3x3. A index of [0][2] would be the 3rd item in that first row, but since there is only 2 items for that row it spills over into the next place in memory - the first item in the 2nd row, or the location of [1][0]. You mention a 2x2 in your opening paragraph but access elements like it's a 3x3 array. Likewise if you tried to access [2][2] you would be accessing past the allocated memory for the array and write to who knows what it memory. Doing that could and should give you a segmentation fault and a crash, but it might not. Fix the matrix to allocate a 3x3 as I showed above and this problem should go away.
Back to top
View user's profile Send private message
drako



Joined: 14 Sep 2007
Posts: 2

PostPosted: Sat Sep 15, 2007 5:03 am    Post subject: Reply with quote

that was it!, and i was thinking that it was something more complicated jaja, thxs for your reply and the explanation, it was very clear. And i also think that errors like that should raise a segmentation fault

Drako
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group