SDL issue

It compiles fine with no errors or warnings, but then crashes when I open it. Any ideas? (trying to get it to display the png, which IS in the right place). All my dll's are linked right as well. Just opens for a split second then closes.


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
/*This source code copyrighted by Lazy Foo' Productions (2004-2009) and may not
be redestributed without written permission.*/

//The headers
#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;
}
Last edited on
Have you tried stepping through it to see where it crashes at?
I know im gonna get yelled at for being a noob here, but no... Not sure how to in vc++... Im kind of in over my head a little, but im determined to learn it. How do you step through in vc++? I'll try and work on it while waiting for a reply.
Are you saying the executable crashes when launched? What error are you getting?
No error, thats the problem... it just.. closes.
Debugging 101:

Find out where these options are in VS and remember where they are (* means they're probably assigned to a hotkey -- possibly one of the F keys):

* Add/Remove breakpoint
* Run
- Step Into (button looks like {braces} with an arrow pointing inside of it)
- Step Over (button looks like {braces} with an arrow pointing around/over it)


Adding a breakpoint on a line means the program will break (or "pause") when code execution reaches that line of code. When this happens VS will bring itself to the front and have a green triangular arrow (iirc) pointing to the line of code being executed next.

When the program is broken like this, you can examine contents of your variables via the "Watch" feature (look around in the menus for it). Simply add the variable name to the Watch list and it will report the contents of that variable. You can also use breakpoints to simply know when code is being reached and when. Or to find out if code is ever being reached at all.

To get out of the program, press Run and the program will continue as normal.

Step Into and Step Over can be used to step through code to see how execution is flowing. This can be useful if a routine is broken somewhere, and you want to find out where. You can step through it line by line, keeping an eye on your watches, until you see the exact line where things are going wrong.

Both Step Into and Step Over advance to the next line of code, however if there's a function call, Step Into will step into the function, and Step Over will step over it (obviously).

---------------------

How to apply this to your problem:

put a breakpoint near the start of main, and just start stepping through everything until the program ends unexepectedly. Then you'll find out where the problem is.
Oh, I found out the problem.. the png is not loading.. commented out
1
2
3
4
5
    //If there was a problem in loading the image
    if( image == NULL )
    {
        return 1;
    }


and it works.. as a black screen..
/bmp files work... png's don't, but i set it up (I think) so it should...
well, the problem is I was missing some special files. I guess I should be happy that i made even the bmp work >.> Im not, but I should be. Next step is a moving image, then one that moves by command... wish me luck lol
well, the problem is I was missing some special files


Which files were you missing?
I have same problem too but I solved with this way:
Measure look.png is in the same folder of executable file.
If you are windows user,measure SDL.dll SDL_image.dll libpng12-0.dll is in the same folder of executable file.
If you are linux user,measure you have libsdl and libsdl_image
Oh, I found out the problem.. the png is not loading.. commented out
.........
I guess I should be happy that i made even the bmp work >.> Im not

If there is black screen in both .png and .bmp,that most of chance is image file missing,measure look.png is in the same folder of executable file.
Last edited on
Topic archived. No new replies allowed.