Starting with SDL, can't load BMP :(

Apprentice
Posts: 6
Joined: 2007.01
Post: #1
hi guys, I am starting on SDL ( using C ) . I followed some tutorials, but I haven't been able to load an image.


This is the code ( Using Xcode )

Code:
#include "stdlib.h"
#include "SDL/SDL.h"

int main(int argc, char *argv[])
{
    SDL_Surface *screen;    //This pointer will reference the backbuffer
    SDL_Surface *image;    //This pointer will reference our bitmap sprite
    SDL_Surface *temp;    //This pointer will temporarily reference our bitmap sprite
    SDL_Rect src, dest;    //These rectangles will describe the source and destination regions of our blit
    
    //We must first initialize the SDL video component, and check for success
    if (SDL_Init(SDL_INIT_VIDEO) != 0) {
        printf("Unable to initialize SDL: %s\n", SDL_GetError());
        return 1;
    }
    
    //When this program exits, SDL_Quit must be called
    atexit(SDL_Quit);
    
    //Set the video mode to fullscreen 640x480 with 16bit colour and double-buffering
    screen = SDL_SetVideoMode(640, 480, 16, SDL_DOUBLEBUF | SDL_FULLSCREEN);
    if (screen == NULL) {
        printf("Unable to set video mode: %s\n", SDL_GetError());
        return 1;
    }
    
    //Load the bitmap into a temporary surface, and check for success
    temp = SDL_LoadBMP("image.bmp");
    if (temp == NULL) {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }
    
    //Convert the surface to the appropriate display format
    image = SDL_DisplayFormat(temp);
    
    //Release the temporary surface
    SDL_FreeSurface(temp);
    
    //Construct the source rectangle for our blit
    src.x = 0;
    src.y = 0;
    src.w = image->w;    //Use image->w to display the entire width of the image
    src.h = image->h;    //Use image->h to display the entire height of the image
    
    //Construct the destination rectangle for our blit
    dest.x = 100;        //Display the image at the (X,Y) coordinates (100,100)
    dest.y = 100;
    dest.w = image->w;    //Ensure the destination is large enough for the image's entire width/height
    dest.h = image->h;
    
    //Blit the image to the backbuffer
    SDL_BlitSurface(image, &src, screen, &dest);
    
    //Flip the backbuffer to the primary
    SDL_Flip(screen);
    
    //Wait for 2500ms (2.5 seconds) so we can see the image
    SDL_Delay(2500);
    
    //Release the surface
    SDL_FreeSurface(image);
    
    //Return success!
    return 0;
}

when I go for compile and run, the screen turns black, and no image loads... then I get

"[Session started at 2007-01-16 20:17:27 -0300.]
Unable to load bitmap: Couldn't open image.bmp

program has exited with status 1."

I placed the image.bmp in the same directory as main.c, that's inside the proyect folder... can someone help me ? Sad
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
You need to place the image next to the built application, for SDL.
Quote this message in a reply
Apprentice
Posts: 6
Joined: 2007.01
Post: #3
Oh, that's great, ill check it out tomorrow.

Quick question... if I want to build a self contained package ( so I don't use external folders for images and all that stuff... ) what should I do ?
Quote this message in a reply
Member
Posts: 283
Joined: 2006.05
Post: #4
There's something you can change in SDLMain.c ... I can't remember what, but I think it's pretty obvious.
Quote this message in a reply
Apprentice
Posts: 6
Joined: 2007.01
Post: #5
Where should I place the SDLMain.c ???

I downloaded the extras pack from the SDL main URL, but I just installed the Xcode templates... don't have any idea of where to install the SDL main stuff
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #6
Just include SDLMain.m and SDLMain.h in your project, in the usual manner.
Quote this message in a reply
Apprentice
Posts: 6
Joined: 2007.01
Post: #7
Oh ok , great, I think they come included in the templates Smile
Quote this message in a reply
Apprentice
Posts: 6
Joined: 2007.01
Post: #8
Guys, about the initial post... I cant load the image Sad

I am working with Xcode, and when I hit compile and run... all I get is a black screen, and then my error message that I added in

Code:
    //Load the bitmap into a temporary surface, and check for success
    temp = SDL_LoadBMP("image.bmp");
    if (temp == NULL) {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }

I get

libro has exited with status 1.
[Session started at 2007-01-17 19:15:51 -0300.]
Unable to load bitmap: Couldn't open image.bmp


( all of this in Xcode... I don't know what is wrong... it must be something really stupid I think... I took the image.bmp to the project directory )

Sad , if anyone can help me , it would be great!

I really want to get into this, Smile


#########EDIT#######

Guys I managed to load the image specifying the whole path like this

Code:
//Load the bitmap into a temporary surface, and check for success
    temp = SDL_LoadBMP("/Volumes/MacHD/Users/USER/progra/SDL/libro/image.bmp");
    if (temp == NULL) {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }

Is there an easier way of doing this instead of always specifying the whole path ??

Thanks!!!
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #9
place the image next to the built application.

Somewhere like proga/SDL/libro/build/Debug, from memory. Take a look and find the actual application.
Quote this message in a reply
Member
Posts: 90
Joined: 2006.11
Post: #10
O_o. All that code just to place an image on the screen? Crazy... I will be in there soon enough, though....
Quote this message in a reply
Member
Posts: 37
Joined: 2006.08
Post: #11
leRiCl Wrote:O_o. All that code just to place an image on the screen? Crazy... I will be in there soon enough, though....

You should see how much code it takes to set up an OpenGL context in Cocoa, load a PNG with libpng, and draw it from a Lua script. SDL is cake.
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2014.01
Post: #12
Quote:place the image next to the built application.

I right-clicked on my product in Xcode and chose "Show in Finder" then I moved my image.bmp there, to /Users/.../Library/Developer/Xcode/DerivedData/XcodeCpp-.../Build/Products/Debug. But I'm still unable to load the image. Have I misunderstood "built application"? Where should image.bmp be? How do I get my image to load?
Quote this message in a reply
⌘-R in Chief
Posts: 1,260
Joined: 2002.05
Post: #13
That depends on what the current directory is set to, which depends on the version of SDL you're using and the SDLMain.m file if you have one.
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2014.01
Post: #14
I set my "Working Directory" via Product > Scheme > Edit Scheme... > Run CocoaSDL.app > Working Directory > Use custom...:
/Users/<usernameremoved>/Documents/Xcode Projects/CocoaSDL/CocoaSDL.

I put my image.bmp in that directory.

Problem solved. Thank you!

Using:
IDE: Xcode 5
Quote this message in a reply
⌘-R in Chief
Posts: 1,260
Joined: 2002.05
Post: #15
You need to set your working directory in code. The default SDL 1.2 file SDLMain.m has some code to set it to some place (I can't recall where). I'm not sure if SDL2 tries to set it.

Typically, in the end all of your game's data should be packaged into the bundle's Resources folder (or a subfolder within Resources). During development you can use a folder *next* to the .app bundle. When the app is built and is off in ~/Lib/Dev/X/.../BuildProducts somewhere, you would need to have that data folder next to the .app which is a nuisance, so you can add symlink pointing to where that is back in your project folder, so you have greater convenience, such as changing files and never having to worry if they were copied into the app bundle appropriately.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  How can I load a PNG without premultiplied alpha? Prime 6 7,585 Feb 15, 2005 08:52 PM
Last Post: arekkusu
  need help getting quicktime to load textures xDexx 8 4,696 May 7, 2003 03:57 PM
Last Post: xDexx
  Help deciding how to load textures? Mars_999 1 2,337 Feb 20, 2003 10:09 PM
Last Post: henryj
  Where can I get a library to load 3D Models? Mars_999 4 4,284 Aug 25, 2002 06:05 AM
Last Post: ggadwa