Thread: C and SDL: A weird SDL_LoadBMP error

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    5

    C and SDL: A weird SDL_LoadBMP error

    Hi,

    I'm just getting started with using SDL to create graphics. I am using C. But I have come across an error which I simply cannot understand.

    My code is supposed to set up SDL and then set a video mode. It does this successfully. However, it will not load a BMP file. I have tried putting the BMP file in the same directory and on the root of the drive in case something silly was happening, neither was successful.

    Heres the code:

    Code:
    #include <SDL/SDL.h>#include <stdio.h>
    #include <stdlib.h>
    
    
    
    
    int main(int argc, char *argv[]){
    
    
        SDL_Surface *screen;
        SDL_Surface *image;
        SDL_Rect    src, dest;
    
    
        if(SDL_Init(SDL_INIT_VIDEO)!=0){
            return 1;
        }
    
    
        atexit(SDL_Quit);
    
    
        screen=SDL_SetVideoMode(640, 480, 16, SDL_FULLSCREEN);
        if(screen == NULL){
            return 2;
        }
        //SDL_Delay(2000);
    
    
        image=SDL_LoadBMP("image.bmp");
        if(image == NULL){
            return 3;
        }
    
    
        return 0;
    }
    It will _always_ return 3. It's like the file somehow does not exist.

    Any help would be appreciated,
    Thanks!


    EDIT: Now this is getting really silly, even if I remove the "return 3" code and delete the EXE and then recompile, it is still returning 3 in the newly built EXE.
    Last edited by Lloyd; 05-06-2012 at 08:05 PM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Two possibilities:
    *) The compiler's programmer tried to pull out a really bad joke
    *) The program is run from a temporary folder as backup or something

    What compiler are you using?
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    5
    Thanks for the reply.

    I'm using Mingw32. I've looked around at several SDL tutorials and all of them load bitmaps in this way, so I'm puzzled.

    As for the IDE, I'm using Code::Blocks. However, I'm considering moving to another IDE, as I still can't solve the mystery of why it is somehow either recompiling old code, or somehow resurrecting deleted EXE's.
    Last edited by Lloyd; 05-07-2012 at 02:49 AM.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    5
    OK, I have a "possible" solution.

    The program never returns 3 with the following code, and the old EXE doesn't appear to come back from no where. I have never seen anything so stupid before, as far as I'm aware specifying just a file without a path means it will look in the same directory for the file, but apparently this is not the case with SDL. Can anyone explain this?

    By using: image=SDL_LoadBMP("\\image.bmp"); instead of image=SDL_LoadBMP("image.bmp");

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    5
    Scratch that, using \\ instead of just the file name means it will look on the root of the drive for the file, which obviously isn't a very good solution.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You could try printing the result of calling getcwd() / getwd() to show where the process starts looking for filenames.

    If you're running the program within the IDE, then it might be the project root directory.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Code::Blocks has the annoying habit of NOT saving the project files when you press build. As a result, the old code is being compiled. Always press "Save All" before building.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird if/else error
    By taurus in forum C Programming
    Replies: 3
    Last Post: 10-10-2009, 08:43 AM
  2. SDL_LoadBMP file path...
    By manugarciac in forum C++ Programming
    Replies: 2
    Last Post: 04-26-2007, 07:16 AM
  3. Say what? - Weird error.
    By Blackroot in forum C++ Programming
    Replies: 6
    Last Post: 08-15-2006, 11:54 PM
  4. Please help... I am getting a weird error
    By IxTanGxI in forum C Programming
    Replies: 12
    Last Post: 11-15-2005, 04:09 PM
  5. Weird IE error
    By Ravage in forum Tech Board
    Replies: 16
    Last Post: 03-31-2003, 11:46 AM