Results 1 to 7 of 7

Thread: Segmentation Fault while running basic sdl program

  1. #1
    Join Date
    Aug 2011
    Beans
    9

    Segmentation Fault while running basic sdl program

    I was attempting to learn to program with sdl in c++ using lazy foo tutorials(http://lazyfoo.net/) when I got a segfault error on my first program. Im working on a basic program that blits a bmp to the screen but it breaks when loading the image and/or when blitting it to the screen. Im running this on ubuntu server 12.04 32 bit.

    Code:
    /*This source code copyrighted by Lazy Foo' Productions (2004-2012)
    and may not be redistributed without written permission.*/
    
    //Include SDL functions and datatypes
    #include "SDL/SDL.h"
    
    int main( int argc, char* args[] )
    {
        //The images
        SDL_Surface* hello = NULL;
        SDL_Surface* screen = NULL;
    
        //Start SDL
        SDL_Init( SDL_INIT_EVERYTHING );
    
        //Set up screen. Problem Here.
        screen = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );
    
        //Load image
        hello = SDL_LoadBMP( "hello.bmp" );
    
        //Apply image to screen
        SDL_BlitSurface( hello, NULL, screen, NULL );
    
        //Update Screen
        SDL_Flip( screen );
    
        //Pause
        SDL_Delay( 2000 );
    
        //Free the loaded image
        SDL_FreeSurface( hello );
    
        //Quit SDL
        SDL_Quit();
    
        return 0;
    }
    Last edited by smithy545; December 30th, 2012 at 03:55 AM. Reason: Clarification

  2. #2
    Join Date
    Sep 2010
    Location
    Sweden
    Beans
    Hidden!

    Re: Segmentation Fault while running basic sdl program

    Either post your code in code-tags or use pastebin. I, probably including other members, dislike downloading stuff. It makes things much more tedious. Another reason posting the code directly in the thread is good is because you get an overview of the program, and can determine whether or not you're capable of helping.

  3. #3
    Join Date
    Aug 2011
    Beans
    9

    Re: Segmentation Fault while running basic sdl program

    Ya sorry. I put the code in my post.

  4. #4
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,297
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Segmentation Fault while running basic sdl program

    Works fine here, so you will need to provide more details. Have you tried it with other image files and/or on other systems?
    「明後日の夕方には帰ってるからね。」


  5. #5
    Join Date
    Aug 2011
    Beans
    9

    Re: Segmentation Fault while running basic sdl program

    Okay I've narrowed down the problem. I changed the bits per pixel, width and height of the window to 0 in SDL_SetVideoMode and it ran fine so I know it's a problem with that function.
    If SDL_SetVideoMode is like this:
    Code:
    screen = SDL_SetVideoMode( 0, 0, 0, SDL_SWSURFACE );
    It works, but if otherwise it gives a Segmentation Fault error.
    Last edited by smithy545; December 30th, 2012 at 03:10 AM.

  6. #6
    Join Date
    Aug 2011
    Beans
    9

    Re: Segmentation Fault while running basic sdl program

    I fixed my problem. I was in root while trying this and I logged into another account and it worked. Although it did appear in ASCII art format it still worked.

  7. #7
    Join Date
    Dec 2007
    Location
    Gainesville, Florida
    Beans
    Hidden!
    Distro
    Xubuntu 12.04 Precise Pangolin

    Re: Segmentation Fault while running basic sdl program

    Quote Originally Posted by smithy545 View Post
    Okay I've narrowed down the problem. I changed the bits per pixel, width and height of the window to 0 in SDL_SetVideoMode and it ran fine so I know it's a problem with that function.
    If SDL_SetVideoMode is like this:
    Code:
    screen = SDL_SetVideoMode( 0, 0, 0, SDL_SWSURFACE );
    It works, but if otherwise it gives a Segmentation Fault error.
    you should be able to do something like this
    Code:
    screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
    also check for screen being null,
    Code:
    if (screen == null) {
      // say it's borked and exit
    }
    Last edited by pbrane; January 5th, 2013 at 10:32 PM.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •