iDevGames Forums
Starting with SDL, can't load BMP :( - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: Starting with SDL, can't load BMP :( (/thread-3554.html)



Starting with SDL, can't load BMP :( - ngoles - Jan 16, 2007 04:18 PM

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


Starting with SDL, can't load BMP :( - OneSadCookie - Jan 16, 2007 05:03 PM

You need to place the image next to the built application, for SDL.


Starting with SDL, can't load BMP :( - ngoles - Jan 16, 2007 07:13 PM

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 ?


Starting with SDL, can't load BMP :( - maximile - Jan 16, 2007 07:43 PM

There's something you can change in SDLMain.c ... I can't remember what, but I think it's pretty obvious.


Starting with SDL, can't load BMP :( - ngoles - Jan 17, 2007 01:36 PM

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


Starting with SDL, can't load BMP :( - OneSadCookie - Jan 17, 2007 02:28 PM

Just include SDLMain.m and SDLMain.h in your project, in the usual manner.


Starting with SDL, can't load BMP :( - ngoles - Jan 17, 2007 02:55 PM

Oh ok , great, I think they come included in the templates Smile


Starting with SDL, can't load BMP :( - ngoles - Jan 17, 2007 03:17 PM

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!!!


Starting with SDL, can't load BMP :( - OneSadCookie - Jan 17, 2007 04:07 PM

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.


Starting with SDL, can't load BMP :( - leRiCl - Jan 18, 2007 04:44 AM

O_o. All that code just to place an image on the screen? Crazy... I will be in there soon enough, though....


Starting with SDL, can't load BMP :( - djork - Feb 21, 2007 11:25 AM

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.


RE: Starting with SDL, can't load BMP :( - geoffreyhale - Jan 16, 2014 08:33 AM

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?


RE: Starting with SDL, can't load BMP :( - SethWillits - Jan 20, 2014 08:58 AM

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.


RE: Starting with SDL, can't load BMP :( - geoffreyhale - Jan 20, 2014 07:57 PM

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


RE: Starting with SDL, can't load BMP :( - SethWillits - Jan 21, 2014 11:19 AM

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.