.exe not running outside codeblocks

Hello friends,
I have created a game.It is working fine in codeblocks, but when I am trying to run it outside codeblocks, it opens and closes immediately. I have pasted all the necessary files and dlls in root folder and debug folder. Here is the code:-
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <iostream>
#include <SDL/SDL_mixer.h>
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include <sstream>

const int WINDOWS_Height = 480;
const int WINDOWS_Width = 640;
const int SPRITE_SIZE = 32;
const int BALL_SIZE=15;
const char* WINDOWS_Title = "Ball Collecting Game";

using namespace std;
SDL_Surface *grass, *sprite, *temp, *ball, *scores;
SDL_Rect rcSprite, rcGrass, rcSrc, rcBall, rcScores;
int colorkey, score=0, bc=0;
bool runGame = true;
stringstream pScore;
Mix_Music *mainMusic  = NULL;
Mix_Music *ballCollection  = NULL;;


bool checkCollision(SDL_Rect Sprite1, SDL_Rect Sprite2);
void musicDone();
void handleEvents(SDL_Event event);                                                                                             // Function to handle Events

int main (int argc, char *argv[]){
    if (SDL_Init(SDL_INIT_EVERYTHING) == -1)                                                                                // Initializing SDL and Creating Window
        return false;
    SDL_Surface *screen = SDL_SetVideoMode(WINDOWS_Width, WINDOWS_Height, 0, SDL_SWSURFACE | SDL_DOUBLEBUF);
    SDL_WM_SetCaption(WINDOWS_Title, "Ball Game");

    int audio_rate = 22050;
    Uint16 audio_format = AUDIO_S16; /* 16-bit stereo */
    int audio_channels = 2;
    int audio_buffers = 4096;

    if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) {
        printf("Unable to open audio!\n");
        exit(1);
    }



    temp = SDL_LoadBMP("./sprite.bmp");                                                                //Loading Sprite Sheet
    sprite = SDL_DisplayFormat(temp);
    SDL_FreeSurface(temp);

    SDL_EnableKeyRepeat(70, 70);                                                                    //Enabling Key Repeat

    colorkey = SDL_MapRGB(screen->format, 255, 0, 255);                             //Setting up sprite colorkey
    SDL_SetColorKey(sprite, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey);

    temp = SDL_LoadBMP("./grass.bmp");                                                            //Loading Grass
    grass = SDL_DisplayFormat(temp);
    SDL_FreeSurface(temp);

    temp = SDL_LoadBMP("./ball.bmp");
    ball = SDL_DisplayFormat(temp);
    SDL_FreeSurface(temp);

    colorkey = SDL_MapRGB(screen->format, 192, 192, 192);                             //Setting up ball color-key
    SDL_SetColorKey(ball, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey);

    rcSprite.x = 150;                                                                                           //Setting Position of sprite
    rcSprite.y = 150;

    rcBall.x = rand() % 600 + 30;                                                                                //Random Position of Ball
    rcBall.y = rand() % 450 + 30;

    rcSrc.x =  128;                                                                                             //setting frames for animation
    rcSrc.y = 0;
    rcSrc.w = SPRITE_SIZE;
    rcSrc.h = SPRITE_SIZE;

    rcScores.x = rcScores.y = 0;
    rcScores.h = 20;
    rcScores.w = WINDOWS_Width;

    TTF_Init();
    TTF_Font *times;
    times = TTF_OpenFont("./Lato.ttf", 20);
    SDL_Color white = {2, 2, 2};


        //Game Loop
    while (runGame) {

        SDL_Event event;

        if (SDL_PollEvent(&event)) {                                                                         //Here we call function handle Event
            handleEvents(event);
            if (event.key.keysym.sym == SDLK_q || event.key.keysym.sym == SDLK_ESCAPE) {
                return 0;
            }
        }

         if (mainMusic == NULL) {
            mainMusic = Mix_LoadMUS("./mainSound.wav");
            Mix_PlayMusic(mainMusic, -1);
            Mix_HookMusicFinished(musicDone);
        }

        if (rcSprite.x < 0)                                                                                            //Collision with Corners
            rcSprite.x = 0;
        else if (rcSprite.x > WINDOWS_Width-SPRITE_SIZE)
            rcSprite.x = WINDOWS_Width-SPRITE_SIZE;
        if (rcSprite.y < 20)
            rcSprite.y = 20;
        else if (rcSprite.y > WINDOWS_Height-SPRITE_SIZE)
            rcSprite.y = WINDOWS_Height-SPRITE_SIZE;
        if  (checkCollision(rcBall, rcSprite) == true) {

            rcBall.x = rand() % 600 + 30;
            rcBall.y = rand() % 400 + 30;
            score += 10;
            pScore.str(std::string());
            pScore <<"Score :" << score;
            SDL_Flip(screen);
            ballCollection = Mix_LoadMUS("./ballCollected.wav");
            Mix_PlayMusic(ballCollection, 0);

        }

        scores = TTF_RenderText_Blended(times, pScore.str().c_str(), white);
        for (int x = 0; x < WINDOWS_Width/SPRITE_SIZE; x++) {
            for (int y = 0; y <WINDOWS_Height /SPRITE_SIZE; y++ ) {
                rcGrass.x = x * SPRITE_SIZE;
                rcGrass.y = y  *SPRITE_SIZE ;
                SDL_BlitSurface(grass, NULL, screen, &rcGrass);                    //Drawing Grass on Screen
            }
        }
        SDL_BlitSurface(sprite, &rcSrc, screen, &rcSprite);                                           //Drawing Sprite on Screen

        SDL_BlitSurface(ball, NULL, screen, &rcBall);                                                   //Drawing ball on Screen

        SDL_BlitSurface(scores, NULL, screen, &rcScores);

        SDL_UpdateRect(screen, 0, 0, 0, 0);                                                                 //Updating Screen
    }
    SDL_FreeSurface(screen);
    SDL_FreeSurface(ball);
    SDL_FreeSurface(grass);
    SDL_FreeSurface(sprite);
    TTF_CloseFont(times);
    TTF_Quit();
    musicDone();
    SDL_Quit();

    return 0;
}

void handleEvents(SDL_Event event) {
    bool keysHeld[323] = {false};
    if (event.type == SDL_QUIT) {
        runGame = false;
        return;
    }
    if (event.type == SDL_KEYDOWN){
        keysHeld[event.key.keysym.sym] = true;
    }
    if (event.type == SDL_KEYUP){
        keysHeld[event.key.keysym.sym] = false;
    }
    if (keysHeld[SDLK_UP] ) {
        if(rcSrc.x == 0)
            rcSrc.x = 32;
        else
            rcSrc.x = 0;
        rcSprite.y -= 7;
    }

    if (keysHeld[SDLK_DOWN]) {
        if (rcSrc.x == 128)
            rcSrc.x = 160;
        else
            rcSrc.x = 128;
        rcSprite.y += 7;
    }

    if (keysHeld[SDLK_LEFT]) {
        if (rcSrc.x == 192)
            rcSrc.x = 224;
        else
            rcSrc.x = 192;
        rcSprite.x -= 7;
    }

    if (keysHeld[SDLK_RIGHT]) {
        if (rcSrc.x == 64)
            rcSrc.x = 96;
        else
            rcSrc.x = 64;
        rcSprite.x += 7;
    }
}


bool checkCollision( SDL_Rect A, SDL_Rect B ) {
                                                                                                                    //The sides of the rectangles
    int leftA, leftB;
    int rightA, rightB;
    int topA, topB;
    int bottomA, bottomB;
                                                                                                                    //Calculate the sides of rect A
    leftA = A.x;
    rightA = A.x + A.w;
    topA = A.y;
    bottomA = A.y + A.h;

    leftB = B.x;                                                                                         //Calculate the sides of rect B
    rightB = B.x + B.w;
    topB = B.y;
    bottomB = B.y + B.h;
                                                                                                                //If any of the sides from A are outside of B
    if( bottomA <= topB ) {
        return false;
    }
    if( topA >= bottomB ) {
        return false;
    }
    if( rightA <= leftB ) {
        return false;
    }
    if( leftA >= rightB ) {
        return false;
    }
                                                                                                            //If none of the sides from A are outside B
    return true;
}

void musicDone() {
  Mix_HaltMusic();
  Mix_FreeMusic(mainMusic);
  mainMusic = NULL;
  Mix_FreeMusic(ballCollection);
  ballCollection = NULL;
}

Thank you in advance
All *.bmp, *.ttf and *.wav must be in the same directory as the EXE.
The alternative solution to what modoran posted is to not use relative directories.

This happens by the way because when you run your code through Code::Blocks it uses the location of the ".cbp" project file as it's starting directory by default. You can change this behavior on a per project basis but it is usually easier to keep track of things using one of the other two solutions.
Topic archived. No new replies allowed.