access error (segmentation fault)

My Game below compiles correctly, but when i try to run the program, i get a windows error, When i run DEV-C++ debuger it gives me the messege
An Access error (Segmentation Fault) has been raised in your program


If some one could help me threw this error i would be especialy greatful

(code below)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//headers
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include <string>

//Screen Atributes
const int SCREEN_WIDTH = 400;
const int SCREEN_HEIGHT = 400;
const int SCREEN_BPP = 32;

//Arrays
bool board[36];
bool bluetroops[36];
bool redtroops[36];

//Surfaces
//Board (collum,row)
//a                             //b
SDL_Surface *a1 = NULL;         SDL_Surface *b1 = NULL;
SDL_Surface *a2 = NULL;         SDL_Surface *b2 = NULL;
SDL_Surface *a3 = NULL;         SDL_Surface *b3 = NULL;
SDL_Surface *a4 = NULL;         SDL_Surface *b4 = NULL;
SDL_Surface *a5 = NULL;         SDL_Surface *b5 = NULL;
SDL_Surface *a6 = NULL;         SDL_Surface *b6 = NULL;

//c                             //d
SDL_Surface *c1 = NULL;         SDL_Surface *d1 = NULL;
SDL_Surface *c2 = NULL;         SDL_Surface *d2 = NULL;
SDL_Surface *c3 = NULL;         SDL_Surface *d3 = NULL;
SDL_Surface *c4 = NULL;         SDL_Surface *d4 = NULL;
SDL_Surface *c5 = NULL;         SDL_Surface *d5 = NULL;
SDL_Surface *c6 = NULL;         SDL_Surface *d6 = NULL;

//e                             //f
SDL_Surface *e1 = NULL;         SDL_Surface *f1 = NULL;
SDL_Surface *e2 = NULL;         SDL_Surface *f2 = NULL;
SDL_Surface *e3 = NULL;         SDL_Surface *f3 = NULL;
SDL_Surface *e4 = NULL;         SDL_Surface *f4 = NULL;
SDL_Surface *e5 = NULL;         SDL_Surface *f5 = NULL;
SDL_Surface *e6 = NULL;         SDL_Surface *f6 = NULL;

//Troop Surfaces (playercolor,"troop",troopnumber)
//Player1 (red)                 //Player2 (blue)
SDL_Surface *rtroop1 = NULL;     SDL_Surface *btroop1 = NULL;
SDL_Surface *rtroop2 = NULL;     SDL_Surface *btroop2 = NULL;
SDL_Surface *rtroop3 = NULL;     SDL_Surface *btroop3 = NULL;
SDL_Surface *rtroop4 = NULL;     SDL_Surface *btroop4 = NULL;
SDL_Surface *rtroop5 = NULL;     SDL_Surface *btroop5 = NULL;

//Other Surfaces (misc)
SDL_Surface *border = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *instructions = NULL;

//The event structure
SDL_Event event;

//Functions below!
SDL_Surface *load_image( std::string filename )
{
    //The image that's loaded
    SDL_Surface* loadedImage = NULL;

    //The optimized surface that will be used
    SDL_Surface* optimizedImage = NULL;

    //Load the image
    loadedImage = IMG_Load( filename.c_str() );

    //If the image loaded
    if( loadedImage != NULL )
    {
        //Create an optimized surface
        optimizedImage = SDL_DisplayFormat( loadedImage );

        //Free the old surface
        SDL_FreeSurface( loadedImage );

        //If the surface was optimized
        if( optimizedImage != NULL )
        {
            //Color key surface
            SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
        }
    }

    //Return the optimized surface
    return optimizedImage;
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
    //Holds offsets
    SDL_Rect offset;

    //Get offsets
    offset.x = x;
    offset.y = y;

    //Blit
    SDL_BlitSurface( source, clip, destination, &offset );
}

bool init()
{   
    //Initialize all SDL subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return false;
    }

    //Set up the screen
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
    
    //If there was an error in setting up the screen
    if( screen == NULL )
    {
        return false;
    }
    
    //Set the window caption
    SDL_WM_SetCaption( "Square Wars!", NULL );

    //Returning True if nothing fails
    return true;
}

void cleanup()
{
    //Free surfaces
    
    //troops
    SDL_FreeSurface( rtroop1 );     SDL_FreeSurface( btroop1 );
    SDL_FreeSurface( rtroop2 );     SDL_FreeSurface( btroop2 );
    SDL_FreeSurface( rtroop3 );     SDL_FreeSurface( btroop3 );
    SDL_FreeSurface( rtroop4 );     SDL_FreeSurface( btroop4 );
    SDL_FreeSurface( rtroop5 );     SDL_FreeSurface( btroop5 );
    
    //a                     //b                     //c
    SDL_FreeSurface( a1 );  SDL_FreeSurface( b1 );  SDL_FreeSurface( c1 );
    SDL_FreeSurface( a2 );  SDL_FreeSurface( b2 );  SDL_FreeSurface( c2 );
    SDL_FreeSurface( a3 );  SDL_FreeSurface( b3 );  SDL_FreeSurface( c3 );
    SDL_FreeSurface( a4 );  SDL_FreeSurface( b4 );  SDL_FreeSurface( c4 );
    SDL_FreeSurface( a5 );  SDL_FreeSurface( b5 );  SDL_FreeSurface( c5 );
    SDL_FreeSurface( a6 );  SDL_FreeSurface( b6 );  SDL_FreeSurface( c6 );

    
    //d                     //e                     //f
    SDL_FreeSurface( d1 );  SDL_FreeSurface( e1 );  SDL_FreeSurface( f1 );
    SDL_FreeSurface( d2 );  SDL_FreeSurface( e2 );  SDL_FreeSurface( f2 );
    SDL_FreeSurface( d3 );  SDL_FreeSurface( e3 );  SDL_FreeSurface( f3 );
    SDL_FreeSurface( d4 );  SDL_FreeSurface( e4 );  SDL_FreeSurface( f4 );
    SDL_FreeSurface( d5 );  SDL_FreeSurface( e5 );  SDL_FreeSurface( f5 );
    SDL_FreeSurface( d6 );  SDL_FreeSurface( e6 );  SDL_FreeSurface( f6 );



    
    SDL_Quit();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
void setup(bool board[])
{
        for(int i=0; i<32; i++) // sets up the individual squares for the start of the game
        board[i] = true;
        for(int i=32; i<64; i++)
        board[i] = false;
}

//set the tiles from the bool values
void settiles(bool board[])
{
    if (board[0] == true) a1 = load_image("blue.png");
    else a1 = load_image("red.png");
    
    if (board[1] == true) a2 = load_image("blue.png"); 
    else a2 = load_image("red.png");
    
    if (board[2] == true) a3 = load_image("blue.png");
    else a3 = load_image("red.png");
    
    if (board[3] == true) a4 = load_image("blue.png");
    else a4 = load_image("red.png");
    
    if (board[4] == true) a5 = load_image("blue.png");
    else a5 = load_image("red.png");
    
    if (board[5] == true) a6 = load_image("blue.png");
    else a6 = load_image("red.png");

    
    if (board[6] == true) b1  = load_image("blue.png");
    else b1 = load_image("red.png");
    
    if (board[7] == true) b2  = load_image("blue.png"); 
    else b2 = load_image("red.png");
    
    if (board[8] == true) b3  = load_image("blue.png"); 
    else b3 = load_image("red.png");
    
    if (board[9] == true) b4  = load_image("blue.png"); 
    else b4 = load_image("red.png");
    
    if (board[10] == true) b5  = load_image("blue.png"); 
    else b5 = load_image("red.png");
    
    if (board[11] == true) b6  = load_image("blue.png"); 
    else b6 = load_image("red.png");
        
        
    if (board[12] == true) c1  = load_image("blue.png");
    else c1 = load_image("red.png");
    
    if (board[13] == true) c2  = load_image("blue.png"); 
    else c2 = load_image("red.png");
    
    if (board[14] == true) c3  = load_image("blue.png"); 
    else c3 = load_image("red.png");
    
    if (board[15] == true) c4  = load_image("blue.png"); 
    else c4 = load_image("red.png");
    
    if (board[16] == true) c5  = load_image("blue.png"); 
    else c5 = load_image("red.png");
    
    if (board[17] == true) c6  = load_image("blue.png"); 
    else c6 = load_image("red.png");
    

    if (board[18] == true) d1  = load_image("blue.png");
    else d1 = load_image("red.png");
     
    if (board[19] == true) d2  = load_image("blue.png"); 
    else d2 = load_image("red.png");

    if (board[20] == true) d3  = load_image("blue.png"); 
    else d3 = load_image("red.png");

    if (board[21] == true) d4  = load_image("blue.png"); 
    else d4 = load_image("red.png");

    if (board[22] == true) d5  = load_image("blue.png"); 
    else d5 = load_image("red.png");

    if (board[23] == true) d6  = load_image("blue.png"); 
    else d6 = load_image("red.png");

    
    if (board[24] == true) e1  = load_image("blue.png");
    else e1 = load_image("red.png");

    if (board[25] == true) e2  = load_image("blue.png"); 
    else e2 = load_image("red.png");

    if (board[26] == true) e3  = load_image("blue.png"); 
    else e3 = load_image("red.png");

    if (board[27] == true) e4  = load_image("blue.png"); 
    else e4 = load_image("red.png");

    if (board[28] == true) e5  = load_image("blue.png"); 
    else e5 = load_image("red.png");

    if (board[29] == true) e6  = load_image("blue.png"); 
    else e6 = load_image("red.png");

    
    if (board[30] == true) f1  = load_image("blue.png");
    else f1 = load_image("red.png");

    if (board[31] == true) f2  = load_image("blue.png");
    else f2 = load_image("red.png");
 
    if (board[32] == true) f3  = load_image("blue.png"); 
    else f3 = load_image("red.png");

    if (board[33] == true) f4  = load_image("blue.png"); 
    else f4 = load_image("red.png");

    if (board[34] == true) f5  = load_image("blue.png"); 
    else f5 = load_image("red.png");

    if (board[35] == true) f6  = load_image("blue.png"); 
    else f6 = load_image("red.png");

    
} //damn thats long (thats what she said)

void spawntroops( bool squares[], bool bluetroops[], bool redtroops[])
{
    int spawnloc = 0; //will be set to a random, controlled square
    int bluemen=0;
    int redmen=0;
    srand ( time(NULL) );
    
    for(int x=0; x<36; x++)
    {
        if (bluetroops[x] == true) bluemen++;
        if (redtroops[x] == true) redmen++;
    }

    while (bluemen < 5) //Spawns Blue People
    {
        spawnloc = (rand() % 36);
        if (squares[spawnloc] == true) // checks to see if you control the area
        {
            if(bluetroops[spawnloc] == false) //makes sure its not occupied
            {
                bluetroops[spawnloc] = true; //spawns the troop at the location
                bluemen++;
            }
        }
        
    }
    
    while (redmen < 5) //Spawns Red People
    {
        spawnloc =(  rand() % 36);
        if (squares[spawnloc] == false) //checks to see if red controls area
        {
            if(redtroops[spawnloc] == false)
            {
                redtroops[spawnloc] = true;
                redmen++;
            }
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
void apply_tiles(bool board[])
{
    //a
    apply_surface( 0, 0, a1, screen );
    apply_surface( 50, 0, a2, screen );
    apply_surface( 100, 0, a3, screen );
    apply_surface( 150, 0, a4, screen );
    apply_surface( 200, 0, a5, screen );
    apply_surface( 250, 0, a6, screen );
    
    //b
    apply_surface( 50, 0, b1, screen );
    apply_surface( 50, 50, b2, screen );
    apply_surface( 50, 100, b3, screen );
    apply_surface( 50, 150, b4, screen );
    apply_surface( 50, 200, b5, screen );
    apply_surface( 50, 250, b6, screen );
    
    //c
    apply_surface( 100, 0, c1, screen );
    apply_surface( 100, 50, c2, screen );
    apply_surface( 100, 100, c3, screen );
    apply_surface( 100, 150, c4, screen );
    apply_surface( 100, 200, c5, screen );
    apply_surface( 100, 250, c6, screen );
    
    //d
    apply_surface( 150, 0, d1, screen );
    apply_surface( 150, 50, d2, screen );
    apply_surface( 150, 100, d3, screen );
    apply_surface( 150, 150, d4, screen );
    apply_surface( 150, 200, d5, screen );
    apply_surface( 150, 250, d6, screen );
    
    //e
    apply_surface( 200, 0, e1, screen );
    apply_surface( 200, 50, e2, screen );
    apply_surface( 200, 100, e3, screen );
    apply_surface( 200, 150, e4, screen );
    apply_surface( 200, 200, e5, screen );
    apply_surface( 200, 250, e6, screen );
    
    //f
    apply_surface( 250, 0, f1, screen );
    apply_surface( 250, 50, f2, screen );
    apply_surface( 250, 100, f3, screen );
    apply_surface( 250, 150, f4, screen );
    apply_surface( 250, 200, f5, screen );
    apply_surface( 250, 250, f6, screen );
}        
    
    
//Actual Program below! Work On it!
int main ( int argc, char* args[] )
{
    if (init == false)
    {
        return 1;
    }
    bool quit = false;
    border = load_image("background.png");
    apply_surface(0,0,border,screen);
    setup(board);
    SDL_Delay(5000);
    //settiles(board);
    
    
    cleanup();
}
Topic archived. No new replies allowed.