C++ SDL problem
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: C++ SDL problem

  1. #1
    Join Date
    Nov 2009
    Posts
    14

    C++ SDL problem

    Having using visual studio compiler with Lazy Foo's SDL tutorial 3.
    http://lazyfoo.net/SDL_tutorials/les...508e/index.php

    using exact same code he does but added the stdafx.h so i could use <string>. But when i run it it crashes each time. Also have the SDL_Image.dll and SDL.dll in the folder with look.png. So i dont think its a code problem either. Its a win32 console application with precompiled headers.
    Code:
    #include "stdafx.h"
    #include "SDL.h"
    #include "SDL_image.h"
    
    #include <string>
    
    //Screen attributes
    const int SCREEN_WIDTH = 640;
    const int SCREEN_HEIGHT = 480;
    const int SCREEN_BPP = 32;
    
    //The surfaces
    SDL_Surface *image = NULL;
    SDL_Surface *screen = NULL;
    
    SDL_Surface *load_image( std::string filename )
    {
        //The image that's loaded
        SDL_Surface* loadedImage = NULL;
    
        //The optimized image that will be used
        SDL_Surface* optimizedImage = NULL;
    
        //Load the image using SDL_image
        loadedImage = IMG_Load( filename.c_str() );
    
        //If the image loaded
        if( loadedImage != NULL )
        {
            //Create an optimized image
            optimizedImage = SDL_DisplayFormat( loadedImage );
    
            //Free the old image
            SDL_FreeSurface( loadedImage );
        }
    
        //Return the optimized image
        return optimizedImage;
    }
    
    void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
    {
        //Rectangle to hold the offsets
        SDL_Rect offset;
    
        //Get offsets
        offset.x = x;
        offset.y = y;
    
        //Blit the surface
        SDL_BlitSurface( source, NULL, 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( "PNG test", NULL );
    
        //If everything initialized fine
        return true;
    }
    
    void clean_up()
    {
        //Free the surface
        SDL_FreeSurface( image );
    
        //Quit SDL
        SDL_Quit();
    }
    
    int main( int argc, char* args[] )
    {
        //Initialize
        if( init() == false )
        {
            return 1;
        }
    
        //Load the image
        image = load_image( "look.png" );
    
        //If there was a problem in loading the image
        if( image == NULL )
        {
            return 1;
        }
    
        //Apply the surface to the screen
        apply_surface( 0, 0, image, screen );
    
        //Update the screen
        if( SDL_Flip( screen ) == -1 )
        {
            return 1;
        }
    
        //Wait 2 seconds
        SDL_Delay( 2000 );
    
        //Free the surface and quit SDL
        clean_up();
    
        return 0;
    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,098

    Re: C++ SDL problem

    Run it in the debugger.

  3. #3
    Join Date
    Apr 2008
    Posts
    726

    Re: C++ SDL problem

    ok, so where does it crash? did you run it in a debugger?

  4. #4
    Join Date
    Nov 2009
    Posts
    14

    Re: C++ SDL problem

    it opens up the cmd and attempts to load a image for 1/2 second but its blank than crashes says program stopped responding. When i run it in debugger it prompts error
    [CODE]First-chance exception at 0x00000000 in SDLLesson3.exe: 0xC0000005: Access violation.
    Unhandled exception at 0x00000000 in SDLLesson3.exe: 0xC0000005: Access violation.
    The program '[6288] SDLLesson3.exe: Native' has exited with code 0 (0x0).CODE]

  5. #5
    Join Date
    Apr 2008
    Posts
    726

    Re: C++ SDL problem

    thats nearly no help whatsoever

    If you have gone thourgh it in the debugger, then why dont you show us the line where it crashes?

    you have probably got a pointer that is not pointing to an object, or the thing it points to has been deleted.

  6. #6
    Join Date
    Nov 2009
    Posts
    14

    Re: C++ SDL problem

    Quote Originally Posted by Amleto View Post
    thats nearly no help whatsoever

    If you have gone thourgh it in the debugger, then why dont you show us the line where it crashes?

    you have probably got a pointer that is not pointing to an object, or the thing it points to has been deleted.
    my bad line 25

    Code:
       loadedImage = IMG_Load( filename.c_str() );

  7. #7
    Join Date
    Apr 1999
    Posts
    27,427

    Re: C++ SDL problem

    Quote Originally Posted by fireXmist View Post
    my bad line 25

    Code:
       loadedImage = IMG_Load( filename.c_str() );
    What's in the file could be invalid or something the library doesn't expect, causing the crash.

    Secondly the guy who came up with this sample should know to pass strings by const reference, not by value:
    Code:
    SDL_Surface *load_image( const std::string& filename )
    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Nov 2009
    Posts
    14

    Re: C++ SDL problem

    the file is a png img, and shouldnt be unexpected. Also its part of Lazy Foo's tutorials for SDL which works for other people. Was thinking it was more something i did with compiler. I think he uses different complier than me and some code might be different because i have to change headers each tutorials from "SDL/SDL.h" to "SDL.h" and include the stdax.h for <string>.

    When i run the program it does not give a error message but my windows says the program crashed. Does not crash with a message. Image below of how the crash looks like


  9. #9
    Join Date
    Nov 2009
    Posts
    14

    Re: C++ SDL problem

    And for more curious people this is how it looks when i go to debug

    http://img691.imageshack.us/img691/56/debug.jpg

    This is how the folder looks

    http://img12.imageshack.us/img12/5031/folderxl.jpg

    oh and didnt realize it but i accidently had library msvcrt ignored, but when i remove it off ignore this happens. Guessing this is where my problem is. But i installed exact things the tutorial said to.

    MSVCRTD.lib(cinitexe.obj) : warning LNK4098: defaultlib 'msvcrt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library

  10. #10
    Join Date
    Apr 1999
    Posts
    27,427

    Re: C++ SDL problem

    Quote Originally Posted by fireXmist View Post
    And for more curious people this is how it looks when i go to debug
    Then I think it's time for you to debug into the SDL library call itself and find out exactly what is the issue.

    Is there source code to this library? If so, do you have a debug build of the DLL? If yes, then it should be no problem to debug into that call.

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Nov 2009
    Posts
    14

    Re: C++ SDL problem

    Thank you for your guys help. Got the problem solved now. Didnt know i had to put in pnb dll also.
    Last edited by fireXmist; November 13th, 2009 at 02:22 AM.

  12. #12
    Join Date
    Apr 1999
    Posts
    27,427

    Re: C++ SDL problem

    Quote Originally Posted by fireXmist View Post
    Thank you for your guys help. Got the problem solved now. Didnt know i had to put in pnb dll also.
    Well, that isn't a well written library if it crashes your code due to a missing DLL.

    Regards,

    Paul McKenzie

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center