SDL Application will not open

Oct 3, 2012 at 5:56pm
Every time I try to run my application, it will not open. It complies just fine, but it will not run.
Can someone help me figure out where I am going wrong here? I don't see the problem. Other times that I've had this problem it was because I did not put SDL.dll in the project directory, but I have and it still will not run.

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
#include <sdl/sdl.h>
#include <string>

    //Define constant variables, such as screen dimensions, and bpp.
    const int screen_width = 640;
    const int screen_height = 480;
    const int screen_bpp = 32;
    
    //Define all surfaces
    SDL_Surface* screen = NULL;
    SDL_Surface* background = NULL;

    //Function used to load images and convert them from 24 bit to 32 bit
    SDL_Surface *load_image(std::string filename)
    {
     SDL_Surface* loaded_image = NULL;
     SDL_Surface* opt_image = NULL;
     
     loaded_image = SDL_LoadBMP(filename.c_str());
     
                  if (loaded_image != NULL)
                  {
                  opt_image = SDL_DisplayFormat(loaded_image);
                  SDL_FreeSurface(loaded_image);
                  }
     return opt_image;
    }
    
    //Get the x/y coordinates of the image and blit them to the correct surface.
    void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
    {
         SDL_Rect offset;
         offset.x = x;
         offset.y = y;
         SDL_BlitSurface(source, NULL, destination, &offset);
    }

int main(int argc, char* args[])
{
    //Start SDL
    if (SDL_Init(SDL_INIT_EVERYTHING) == -1);
    {
    return 1;                                  
    }
    
    //Draw the window
    screen = SDL_SetVideoMode(screen_width, screen_height, screen_bpp, SDL_SWSURFACE); 
    if (screen == NULL)
    {
    return 1;
    }
    
    //Window caption
    SDL_WM_SetCaption("<caption here>", NULL);
    
    //Load the background image
    SDL_Surface* background = SDL_LoadBMP("C:/resources/background_index/background.bmp");
    
    //apply the background image to the appropriate surface
    apply_surface(0, 0, background, screen);
     
    //Render the screen
    if (SDL_Flip(screen) == -1)
    {
    return 1;                 
    }
    
    //Wait...
    SDL_Delay(2000);
    
    //Clear the background surface
    SDL_FreeSurface(background);
    
    //Clear the screen and quit SDL
    SDL_Quit();
    
    return 0;
}
Last edited on Oct 3, 2012 at 5:56pm
Oct 3, 2012 at 6:13pm
Well you are still using LoadBMP to load your image when you have your load_image function. Not sure if that will affect it. If I get a chance later I'll try it out.
Oct 3, 2012 at 6:16pm
Thank you for catching that lol. Sadly the application still did not run. :/.
Oct 3, 2012 at 6:24pm
Replacing your SDL Initialisation with this seemed to do the job.

1
2
3
4
5
//Initialize all SDL subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return false;
    }

Last edited on Oct 3, 2012 at 6:25pm
Oct 3, 2012 at 6:27pm
I tried that, still did not work. Thanks anyways though.
Oct 3, 2012 at 6:31pm
Do you have your image in the right directory? Make sure they are in the same folder as the exe and the code. I can't remember which is right. Mine didn't run until the image was in the right place.
Oct 3, 2012 at 6:37pm
What IDE do you use ?


by the way
1
2
3
4
5
//Start SDL
    if (SDL_Init(SDL_INIT_EVERYTHING) == -1);//delete the ';' at the end of this line
    {
    return 1;                                  
    }

at the end of line 41 delete the ';'
Oct 3, 2012 at 6:37pm
Moved the background bmp to the project directory with the exe and the source code, it still has the same effect. Project will not run at all. :/.
Oct 3, 2012 at 6:42pm
I've been using Dev-C++, and thanks, it works now. I didn't notice that either. :).
Shouldn't the compiler have picked up on that one though?
Last edited on Oct 3, 2012 at 6:43pm
Oct 3, 2012 at 6:47pm
No, because if (SDL_Init(SDL_INIT_EVERYTHING) == -1); if the condition is true than it will not do anything Nothing To Process and it will continue the execution.
';' indicates the end of an instruction
"{}" groups more instructions

Sorry for bad English
Last edited on Oct 3, 2012 at 6:48pm
Oct 3, 2012 at 6:54pm
Makes sense. Thank you. :).
Topic archived. No new replies allowed.