What am i doing wrong? SDL-C++ program

hello I am trying to open a screen with an image saying hello. It shows no errors on xcode, but when I run the program, the screen pops up black. It is just a black screen, and doesnt show the image saying hello. I heard that the solution is to put the image into the same directory, so I dragged it onto xcode. Still, it shows up black. I really would appreciate all help :). Thanks

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
#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
    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;
}
SDL_LoadBMP will return NULL if it fails to load the image so you could check that in your program to make sure. The file path is relative the current working directory. I'm not sure how xcode set it. You could try putting the image inside the project directory and see if that works better.
Last edited on
Hi, peter87! How can i put it in the directory? thanks for your reply :)
bump
Not sure how XCode works, but most IDE generate a project directory. Something like:

C:\<whatever>\yourprojectname\

This will often be the default current directory in programs when you run them from the IDE. Put your bmp file in that directory.


If that doesn't work you'll have to find out what the current directory is some other way (the WinAPI GetCurrentDirectory function will do this if you're on Windows).
Hi Disch. Tysm for your reply :). I put it in the folder and the inside of folder, and i dragged onto project. Still, it is black when it pops up.. I am frustrated :(. Thanks for your reply though, and any further help would be appreciated. Thanks for all replies.
Double check and make sure it's even a directory problem. Try specifying the full path to your file:

 
hello = SDL_LoadBMP( "C:/path/to/your/fille/hello.bmp" );



EDIT:

once you verify that's working... temporarily put this at the start of your main function so you can see where the current directory is:

1
2
3
4
5
6
7
8
9
10
11
12
#include <Windows.h>

//...

int main()
{
    char curdir[MAX_PATH];
    GetCurrentDirectoryA( MAX_PATH, curdir );

    // ... print 'curdir' to the screen here, or snap the debugger and view its contents
    //    it will contain the exact path that is being treated as the current directory.
    //    that path is the one you will want to put your .bmp file in 
Last edited on
Topic archived. No new replies allowed.