Thread: I can't make bmp image files show up in an SDL window

  1. #1
    Registered User
    Join Date
    May 2009
    Location
    UK
    Posts
    3

    I can't make bmp image files show up in an SDL window

    Hi, first post

    I did a search for this but either my search was too specific and yielded no results or too general and I couldn't find any relevant threads, so I apologize if this has already been posted.

    Secondly, I hope this is the correct section to post this in.

    I have been re-learning C++ for the past few days, I originally started learning C++ a while back with the intention of writing small games but never got very deep into learning an API, so I am starting on SDL now.

    My problem is that I have been going through tutorials (Lazy Foo'), and on the second tutorial he writes about how to use .bmp images.
    I can compile and run the code just fine with no errors or warnings, but when the program runs the new SDL window only shows black, no images.

    I have made sure that the .bmp files are in the same folder as the .exe file and had double checked spelling in my code (I copied the code from Lazy Foo' by hand as i went along)

    Anyway, any help would be highly appreciated as i've checked my code over several times.

    I even downloaded his source code, copied it into my IDE and still got the same results.
    Because of this I'm not sure if it has anything to do with the code itself, or if I'm just putting the .bmp files in the wrong place (They are supposed to go with the .exe right?)

    This is my copy of the Lazy Foo' code.
    (If it makes any difference at all I am using Visual C++ 2008)

    Code:
    #include "SDL.h"
    #include <string>
    
    const int SCREEN_WIDTH = 640;
    const int SCREEN_HEIGHT = 480;
    const int SCREEN_BPP = 32;
    
    SDL_Surface *message = NULL;
    SDL_Surface *background = NULL;
    SDL_Surface *screen = NULL;
    
    SDL_Surface *loadImage(std::string filename) {
    	SDL_Surface *loadedImage = NULL;
    	SDL_Surface *optimizedImage = NULL;
    	
    	loadedImage = SDL_LoadBMP(filename.c_str());
    	
    	if(loadedImage != NULL) {
    		optimizedImage = SDL_DisplayFormat(loadedImage);
    		SDL_FreeSurface(loadedImage);
    	}
    	
    	return optimizedImage;
    }
    
    void applySurface(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[]) {
    	if(SDL_Init(SDL_INIT_EVERYTHING) == -1) {
    		return 1;
    	}
    	
    	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
    	
    	if(screen == NULL) {
    		return 1;
    	}
    	
    	SDL_WM_SetCaption("SDL Window", NULL);
    	
    	message = loadImage("message.bmp");
    	background = loadImage("background.bmp");
    	
    	applySurface(0, 0, background, screen);
    	applySurface(180, 140, message, screen);
    	
    	if(SDL_Flip(screen) == -1) {
    		return 1;
    	}
    	
    	SDL_Delay(2000);
    	
    	SDL_FreeSurface(message);
    	SDL_FreeSurface(background);
    	
    	SDL_Quit();
    	
    	return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You could put them in the project root directory (where your .sln file is).

    Further, you should check for errors at all stages.
    Those functions probably return NULL, and maybe even set some additional error status.
    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.

  3. #3
    Registered User
    Join Date
    May 2009
    Location
    UK
    Posts
    3
    Well, I just checked for errors again and found none, but then i opened up the folder where the .exe file is and ran it straight from that and it worked.

    I'm a little confused as to why it worked when I clicked directly on the .exe but didn't work when I ran without debugging from the IDE.

    Thankyou for your reply

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    When run from the IDE, the "current directory" is usually the project root directory (1 level above the .exe).

    You're looking in the wrong place (which good error checking and diagnosis would have told you).
    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.

  5. #5
    Registered User
    Join Date
    May 2009
    Location
    UK
    Posts
    3
    The reason I was confused is because I did exactly as you said, and put the .bmp files into the same directory as the .sln file but still gave the same results.
    (Sorry I forgot to mention that in my last post.)

    Since I'm in the early stages of learning I'm not very good with my own error checking yet.
    The reason I thought there were no errors was because after the message and background pointers were assigned to the .bmp files I tried this.

    Code:
    if(background == NULL) {
    	return 1;
    }
    if(message == NULL) {
    	return 1;
    }
    But I had forgotten to put in the "return 1;" parts ¬_¬

    I predict many more face palm moments as I learn.

  6. #6
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by Lucas89
    I predict many more face palm moments as I learn.
    Hehe, oh those facepalm moments. To quote a well know snack maker, "Once you pop, the fun don't stop."
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 08:23 AM
  2. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  3. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM
  4. Winamp Vis if anyone can help
    By Unregistered in forum Windows Programming
    Replies: 6
    Last Post: 01-27-2002, 12:43 AM
  5. how to make 2 text window
    By bluexrogue in forum C Programming
    Replies: 2
    Last Post: 09-15-2001, 08:51 PM