简体   繁体   中英

Segmentation fault while using SDL

[SOLVED]I'm following this tutorial (video) and I'm at this very moment. I had no problems with compiling, but when it comes to running the program it's just flashing for a second and turning off. So I've run the debugger and I found a segmentation fault from SDL_DisplayFormat in my load_image function, when I comment off lines where I load images it works ok, but I can't find the reason of the problem.

[SOLUTION] I haven't fill the .bmp files that is why the SDL_DisplayFormat didn't work. Once filed with drawning, everything started to work.

Here is my code:

#include "game.h"
/*TODO 
*stworzyć plik z blokami "blocks.bmp"
*stworzyć plik  z tlem "background.bmp"
*uzywac jako tla do obrazkow 000,255,255
*
*/
game::game()
{
    //zaladowanie ekranu
    SDL_Init(SDL_INIT_EVERYTHING);
    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGTH, 32, SDL_SWSURFACE);
    //ladowanei obrazkow

    //ustawianie polozenia i rozmiaru kamery
    camera.x = camera.y = 0;
    camera.w = SCREEN_WIDTH;
    camera.h = SCREEN_HEIGTH;
    //inicjalizacja kierunku w kotrym sie poruszamy
    direction[0] = direction[1] = 0;
    running = true;
    block = (load_image("blocks.bmp"));
    background = (load_image("background.bmp"));
}
game::~game()
{
    SDL_FreeSurface(block);
    SDL_FreeSurface(background);
    SDL_Quit();
}
SDL_Surface* game::load_image (const char* filename)
{
    SDL_Surface* tmp = SDL_LoadBMP(filename);
    SDL_Surface* tmp2 = SDL_DisplayFormat(tmp);
    //do odkomentowania kolorkey na razie zeby sprawdzac kolizje
    //  SDL_SetColorKey(tmp2, SDL_SRCCOLORKEY, SDL_MapRGB(screen->format, 0x00, 0xff, 0xff)
    SDL_FreeSurface(tmp);
    return tmp2;
}
void game::handleEvents()
{
    SDL_Event event;
    while(SDL_PollEvent(&event))
    {
        switch(event.type)
        {
            case SDL_QUIT:
                running = false;
                return;
            case SDL_KEYDOWN:
                switch(event.key.keysym.sym)
                {
                    case SDLK_LEFT:
                        direction[0] = 1;
                        break;
                    case SDLK_RIGHT:
                        direction[1] = 1;
                        break;
                }
                break;
            case SDL_KEYUP:
                switch(event.key.keysym.sym)
                {
                    case SDLK_LEFT:
                        direction[0] = 0;
                        break;
                    case SDLK_RIGHT:
                        direction[1] = 0;
                        break;
                }
                break;
        }
    }
}
void game::start()
{
    while(1)
    {
        handleEvents();
        SDL_Flip(screen);
    }
}

One advise up front: Provide complete, minimal code including compile instructions. Yours doesn't have a main() function and the class definition is missing too (game.h). After reconstructing this and running it in a debugger, I find that the first SDL_LoadBMP() returns NULL (maybe that's the same at yours, but here it's definitely the case that I don't have the according files), so I have no clue if you have the same problem or not.

Now, what you can try to fix this is to check returnvalues. In this case, try this:

SDL_Surface* tmp = SDL_LoadBMP(filename);
if(!tmp)
    throw std::runtime_error("SDL_LoadBMP() failed");

Using a unique_ptr or some custom wrapper class is also a good advice in order to not leak resources.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM