// array to store pieces
struct board_t
{
int piece;
int color;
} board[8][8];
// location of spaces being moved to/from
int rowfrom, colfrom;
int rowto,colto;
int main(int argc, char *argv[])
{
// store location of cursor, initially at board[0][0]
int xpos=27,ypos=5;
// store current board location
int row=0,col=0;
...SDL init...
if (event.type == SDL_KEYDOWN)
{
if (event.key.keysym.sym == SDLK_ESCAPE) { done = 1; }
keys = SDL_GetKeyState(NULL);
// increment cursor position and board position
if (keys[SDLK_UP]) {ypos -=32; row--;}
if (keys[SDLK_DOWN]) {ypos +=32; row++;}
if (keys[SDLK_LEFT]) {xpos -=32; col--;}
if (keys[SDLK_RIGHT]){xpos +=32; col++;}
// check for boundaries of board
if (ypos < 5) {ypos = 5;}
if (ypos > 229) {ypos = 229;}
if (xpos < 27) {xpos = 27;}
if (xpos >251) {xpos = 251;}
if (row < 0) {row=0;} if (col < 0) {col=0;}
if (row > 7) {row=7;} if (col < 7) {col=7;}
int checkmove = 0;
if (keys[SDLK_RETURN])
{
// first keypress; piece to be moved
if (checkmove = 0)
{
rowfrom = row;
colfrom = col;
checkmove = 1;
}
// second keypress, square to be moved to
if (checkmove =1)
{
rowto = row;
colto = col;
MovePiece();
checkmove = 0;
}
}
}
}
MoveCursor(xpos,ypos);
DrawBoard();
SDL_Flip(screen);
}
return 0;
}
int MoveCursor (int x,int y)
{
DrawIMG (back,x-32,y-32,96,96,x-32,y-32);
DrawIMG(cursor,x,y,32,32,32,32);
}
void MovePiece ()
{
// should place contents of first space into second space; doesn't work
board[rowto][colto].piece = board[rowfrom][colfrom].piece;
board[rowto][colto].color = board[rowfrom][colfrom].color;
board[rowfrom][colfrom].piece = EMPTY;
}
SDL Segmentation Error
I'm using an 8x8 array to store chess pieces. I wrote a function to overwrite the value of one piece with another, but it doesn't work and sometimes causes the app to crash with "Fatal signal: Segmentation Fault (SDL Parachute Deployed)." I think the problem is somewhere in how I'm trying to get input to move pieces, but I'm not sure where to start.
Here's the relevant functions/code:
At a glance I don't see anything that would cause seg faults. However your 'if (checkmove =1)' line, and the 'if (checkmove =0)' line, both use the assignment operator instead of the equality operator. Maybe that has an effect.
Thanks for pointing that out, but it didn't change anything.
I guess I'll just go back and try to do the movement a different way.
I guess I'll just go back and try to do the movement a different way.
What about these lines:
if (ypos < 5) {ypos = 5;}
if (ypos > 229) {ypos = 229;}
if (xpos < 27) {xpos = 27;}
if (xpos >251) {xpos = 251;}
if (row < 0) {row=0;} if (col < 0) {col=0;}
if (row > 7) {row=7;} if (col < 7) {col=7;}
shouldn't it be
if (row > 7) {row=7;} if (col > 7) {col=7;}
if (ypos < 5) {ypos = 5;}
if (ypos > 229) {ypos = 229;}
if (xpos < 27) {xpos = 27;}
if (xpos >251) {xpos = 251;}
if (row < 0) {row=0;} if (col < 0) {col=0;}
if (row > 7) {row=7;} if (col < 7) {col=7;}
shouldn't it be
if (row > 7) {row=7;} if (col > 7) {col=7;}
Quote:Original post by renegade weapon
That makes me feel really stupid, and solved most of the problem. Thanks for the help guys.
What's the rest of the problem? If it still gives segmentation faults, when you load your images (with SDL_LoadBMP() or whatever), make sure that the pointer it returns is not NULL, otherwise when you try to draw it you'll get a segmentation fault (IIRC). That seems to be a fairly common cause of segmentation faults in SDL.
The segmentation fault disappeared when I changed
if (row > 7) {row=7;} if (col < 7) {col=7;}
to
if (row > 7) {row=7;} if (col > 7) {col=7;}.
After I did this, the movement function still wouldn't swap the squares. After a little digging around, I found that the checkmove variable was inside the game loop; it was being reset every frame. Because of this, every instance of SDLK_RETURN was regarded as the first, resulting in not enough information being passed to the movement function. After moving checkmove out of the loop, everything runs fine.
if (row > 7) {row=7;} if (col < 7) {col=7;}
to
if (row > 7) {row=7;} if (col > 7) {col=7;}.
After I did this, the movement function still wouldn't swap the squares. After a little digging around, I found that the checkmove variable was inside the game loop; it was being reset every frame. Because of this, every instance of SDLK_RETURN was regarded as the first, resulting in not enough information being passed to the movement function. After moving checkmove out of the loop, everything runs fine.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement