SDL Segmentation Fault

Hello,

I keep getting this when I run my program outside codeblocks. I know what the problem means but not sure where it lies. Thanks in advance for you help!

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
#ifdef __cplusplus
    #include <cstdlib>
#else
    #include <stdlib.h>
#endif
#ifdef __APPLE__
#include <SDL/SDL.h>
#else
#include <SDL.h>
#endif

// Surfaces



// Set Variables

    //Player
    int playerPosX = 0;
    int playerPosY = 0;

    //Engine



bool running = true;

void addSurface(int x, int y, SDL_Surface* source, SDL_Surface* destination) {

    SDL_Rect rect;
    rect.x = x;
    rect.y = y;

    SDL_BlitSurface(source, NULL, destination, &rect);

}

void addSprite(int srcX, int srcY, int dstX, int dstY, int spriteWidth, int spriteHeight, SDL_Surface* source, SDL_Surface* destination) {

SDL_Rect src;
src.x = srcX;
src.y = srcY;
src.w = spriteWidth;
src.h = spriteHeight;

SDL_Rect dst;
dst.x = dstX;
dst.y = dstY;
dst.w = spriteWidth;
dst.h = spriteHeight;

SDL_BlitSurface(source, &src, destination, &dst);

}

int main ( int argc, char* argv[] )
{

// Initialize Engine Stuff

    // Start SDL Video Mode
    SDL_Init( SDL_INIT_VIDEO );



    // Create Window
    SDL_Surface *screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);
    SDL_WM_SetCaption("Quest: Adventure ", NULL);


    // Engine Settings



    // Loading Files

        // Main
        SDL_Surface *background = SDL_LoadBMP("res/main/background.bmp");

        // Player
        SDL_Surface *player = SDL_LoadBMP("res/player/playerDown.bmp");

        //Objects






    // Sprite Color Settings
    SDL_SetColorKey(player, SDL_SRCCOLORKEY, SDL_MapRGB(player->format, 255, 0, 255));


    //game loop
    while (running == true) {

        SDL_Event event;

        if (SDL_PollEvent(&event)) {

                if(event.type == SDL_QUIT)
                {
                    running = false;
                }

               if(event.type == SDL_KEYDOWN) { // Control Code



                    if(event.key.keysym.sym == SDLK_DOWN) {
                        playerPosY += 20;
                        player = SDL_LoadBMP("res/player/playerDown.bmp");
                        SDL_SetColorKey(player, SDL_SRCCOLORKEY, SDL_MapRGB(player->format, 255, 0, 255));
                    }
                    if(event.key.keysym.sym == SDLK_UP) {
                        playerPosY -= 20;
                        player = SDL_LoadBMP("res/player/playerUp.bmp");
                        SDL_SetColorKey(player, SDL_SRCCOLORKEY, SDL_MapRGB(player->format, 255, 0, 255));
                    }
                    if(event.key.keysym.sym == SDLK_RIGHT) {
                        playerPosX += 20;
                        player = SDL_LoadBMP("res/player/playerRight.bmp");
                        SDL_SetColorKey(player, SDL_SRCCOLORKEY, SDL_MapRGB(player->format, 255, 0, 255));
                    }
                    if(event.key.keysym.sym == SDLK_LEFT) {
                        playerPosX -= 20;
                        player = SDL_LoadBMP("res/player/playerLeft.bmp");
                        SDL_SetColorKey(player, SDL_SRCCOLORKEY, SDL_MapRGB(player->format, 255, 0, 255));
                    }


                }

        }

        //Apply Surfaces

            // Main
            addSurface(0,0, background, screen);

            // Objects


            //Player Settings
            addSprite(0, 0, playerPosX, playerPosY, 64, 64, player, screen);

            //Menus







        //Update the Screen
        SDL_Flip(screen);


    }

    // The End
    SDL_Quit();
    return 0;
}
File paths are relative to the current working directory. When you run it from code::blocks it probably uses a different working directory. If you run the program by double clicking on it the current working directory is probably the same directory as the executable file is located. In that case you either need to change the paths in the program or make sure that res is located in the same directory as the executable file.
YOU ARE A LIFE SAVER THANKS!!!!

I was being wierd and thought it all saved in the EXE like resources do! I now need to go back and add everything I thought was stopping it!

:)
Topic archived. No new replies allowed.