Thread: SDL wont display .png image

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    151

    Exclamation SDL wont display .png image

    My program opens, without returning -1, or showing the image, and I was wonderng why this happens.

    here is my code
    Code:
    #include <SDL/SDL.h>
    #include <SDL/SDL_image.h>
    
    SDL_Surface *buffer = 0;
    SDL_Surface *man = 0;
    
    void draw_surface(SDL_Surface* sur, int x, int y){
        SDL_Rect pos;
        pos.x = x;
        pos.y = y;
    
        SDL_BlitSurface(sur, 0, buffer, &pos);
    }
    
    SDL_Surface *draw_img(const char *filename){
        SDL_Surface *img = IMG_Load(filename);
    
        if(man != 0){
            SDL_SetColorKey(img, SDL_SRCCOLORKEY, 0xFFFF00);
        }
        return(img);
    }
    
    int main(int argc, char *argv[]){
        SDL_Init(SDL_INIT_VIDEO);
        buffer = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
    
        man = draw_img("person.png");
    
        if(man == 0){
            return -1;
        }
    
        bool run = true;
        SDL_Event event;
    
        while(run){
            while(SDL_PollEvent(&event)){
            if(event.type == SDL_QUIT){
                run = false;
            }
            }
            draw_surface(man, 0, 0);
        }
    
        SDL_FreeSurface(man);
        SDL_Quit();
        return 1;
    }
    Last edited by bijan311; 04-24-2010 at 11:55 AM.

  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
    Your other SDL functions also return success/fail.
    How about checking them as well?
    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
    Nov 2009
    Posts
    151
    I changed man in
    Code:
    if(man != 0)
    to
    Code:
    if(img != 0);

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    I'm not sure if this would help but, when I go to the executable file in windows explorer it closes automatically

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    46
    Are you sure the image you're trying to load is in the same folder as the program expects it to be? Additionally, to actually get the image to show after you blit it to the 'buffer' surface, remember to call SDL_Flip on it to update the display.(Added that to your code and the picture showed up fine when the file was in the correct spot).

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reducing size of image pixel data to display in window
    By synthetix in forum C Programming
    Replies: 2
    Last Post: 11-16-2009, 05:10 AM
  2. Problem reading tiff image files?
    By compz in forum C++ Programming
    Replies: 9
    Last Post: 10-30-2009, 04:17 AM
  3. display image name
    By shamee_banu in forum Windows Programming
    Replies: 1
    Last Post: 06-29-2007, 12:13 PM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. how do i display a grayscale image ?
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 02-13-2002, 01:11 AM