|
-
July 20th, 2009, 10:44 PM
#1
Segmentation Fault
Code:
#include <allegro.h>
BITMAP *RedBlock;
BITMAP *BlueBlock;
BITMAP *WhiteBlock;
BITMAP *GreenBlock;
BITMAP *BlackBlock;
BITMAP *Background;
BITMAP *Background2;
BITMAP *buffer;
//BlockColor 0 = no block 1 = redblock 2 = blueblock 3 = whiteblock 4 = greenblock
int BlockColor[15][17] = {{0},{0}};
int color;
int BlockPosX [15];
int BlockPosY [17];
int CounterPos = 0;
void NewGame(){
}
int MoveSingleBlock( int Blockx, int Blocky, int color1 ){
if ( color1 == 1 ){
draw_sprite( Background2, BlackBlock, BlockPosX[Blockx], BlockPosY[Blocky] );
draw_sprite( Background2, RedBlock, BlockPosX[Blockx], BlockPosY[Blocky - 1]);
}
if ( color1 == 2 ){
draw_sprite( Background2, BlackBlock, BlockPosX[Blockx], BlockPosY[Blocky] );
draw_sprite( Background2, BlueBlock, BlockPosX[Blockx], BlockPosY[Blocky - 1]);
}
if ( color1 == 3 ){
draw_sprite( Background2, BlackBlock, BlockPosX[Blockx], BlockPosY[Blocky] );
draw_sprite( Background2, WhiteBlock, BlockPosX[Blockx], BlockPosY[Blocky - 1]);
}
if ( color1 == 4 ){
draw_sprite( Background2, BlackBlock, BlockPosX[Blockx], BlockPosY[Blocky] );
draw_sprite( Background2, GreenBlock, BlockPosX[Blockx], BlockPosY[Blocky - 1]);
}
}
void MoveBlocks(){
int x = 17;
int i = 0;
while ( x != 0 ){
while( i != 15 ){
if (BlockColor[i][x - 1] == 0 ){
MoveSingleBlock(BlockPosX[i], BlockPosY[x], BlockColor[i][x]);
}
i = i + 1;
}
x = x - 1;
}
}
void SetBlockPos(){
BlockPosX[0] = 122;
BlockPosX[1] = 148;
BlockPosX[2] = 174;
BlockPosX[3] = 200;
BlockPosX[4] = 226;
BlockPosX[5] = 252;
BlockPosX[6] = 278;
BlockPosX[7] = 304;
BlockPosX[8] = 330;
BlockPosX[9] = 356;
BlockPosX[10] = 382;
BlockPosX[11] = 408;
BlockPosX[12] = 434;
BlockPosX[13] = 460;
BlockPosX[14] = 486;
BlockPosY[0] = 455;
BlockPosY[1] = 429;
BlockPosY[2] = 403;
BlockPosY[3] = 377;
BlockPosY[4] = 351;
BlockPosY[5] = 325;
BlockPosY[6] = 299;
BlockPosY[7] = 273;
BlockPosY[8] = 247;
BlockPosY[9] = 221;
BlockPosY[10] = 195;
BlockPosY[11] = 169;
BlockPosY[12] = 143;
BlockPosY[13] = 117;
BlockPosY[14] = 91;
BlockPosY[15] = 65;
BlockPosY[16] = 39;
}
void newline(){
int i = 0;
while (i != 15){
color = rand()%4;
if (color == 0){
draw_sprite(Background2, RedBlock, BlockPosX[i], BlockPosY[16]);
BlockColor[i][17] = 1;
i = i + 1;
}
if (color == 1){
draw_sprite(Background2, BlueBlock, BlockPosX[i], BlockPosY[16]);
BlockColor[i][17] = 2; //i believe this is line 103
i = i + 1;
}
if (color == 2){
draw_sprite(Background2, WhiteBlock, BlockPosX[i], BlockPosY[16]);
BlockColor[i][17] = 3;
i = i + 1;
}
if (color == 3){
draw_sprite(Background2, GreenBlock, BlockPosX[i], BlockPosY[16]);
BlockColor[i][17] = 4;
i = i + 1;
}
}
}
int main(){
//initialize allegro
allegro_init();
install_timer();
install_keyboard();
install_mouse();
srand(time(NULL));
set_color_depth(16);
set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
//set the block positions
SetBlockPos();
//load needed bitmaps
buffer = create_bitmap( SCREEN_W, SCREEN_H );
Background2 = create_bitmap( SCREEN_W, SCREEN_H );
Background = load_bitmap( "background.bmp", NULL);
RedBlock = load_bitmap( "redblock.bmp", NULL);
WhiteBlock = load_bitmap( "whiteblock.bmp", NULL);
BlueBlock = load_bitmap( "blueblock.bmp", NULL);
GreenBlock = load_bitmap( "greenblock.bmp", NULL);
BlackBlock = load_bitmap( "blackblock.bmp", NULL);
//newline and MoveBlocks timer
install_int_ex(newline, SECS_TO_TIMER(5));
install_int_ex(MoveBlocks, MSEC_TO_TIMER(50));
//make a background i can draw on
draw_sprite(Background2, Background, 0, 0);
//main game loop
while ( !key[KEY_ESC] ){
//acquire screen for writing
acquire_screen();
//draw the background to buffer
draw_sprite(buffer, Background2, 0, 0);
//release screen
release_screen();
blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
rest(80);
}
return 0;
}
END_OF_MAIN();
I cannot figure out where this error is coming from. This is my first full game, so forgive me if it is sloppy at all but I would really appreciate it if you could help me with this problem ASAP. I am using Allegro which already declares all the include files needed for C++. The error always points at line 103, I have the line marked with a comment. Thank you in advanced.
also my operating system is vista
Last edited by noey699; July 20th, 2009 at 10:55 PM.
Reason: forgot to end [code]
-
July 21st, 2009, 02:13 AM
#2
Re: Segmentation Fault
Look at your declaration of BlockColor:
Code:
int BlockColor[15][17] = {{0},{0}};
What is the maximum index you can use (i.e. what index values would you use to access the last element in the array)?
Now look at the line in question (and all the other lines that access BlockColor in the function for that matter).
Code:
BlockColor[i][17] = 2;
What do you notice? The maximum index is BlockColor[14][16], you have gone beyond that, and that is why you have a segmentation fault.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|