Jump to content

  • Log In with Google      Sign In   
  • Create Account

[C++][SDL] SDL_LoadBMP won't load the bitmap


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Bartek Sowka   Members   -  Reputation: 100

Like
0Likes
Like

Posted 03 April 2012 - 01:35 PM

Ok so my code is:
#include "SDL/SDL.h"
#include <iostream>
using namespace std;
int main( int argc, char* args[] )
{
    SDL_Surface* hello = NULL;
    SDL_Surface* screen = NULL;
    //Start SDL
    SDL_Init( SDL_INIT_EVERYTHING );
    screen = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );
    hello = SDL_LoadBMP("block.bmp");
    SDL_BlitSurface(hello,NULL,screen,NULL);
    SDL_Flip(screen);
    SDL_Delay(2000);
    SDL_FreeSurface( hello );
    //Quit SDL
    SDL_Quit();
    return 0;
}

I have the block.bmp in the same directory as my exe (Debug)

Oh and I'm using Code::Blocks.

Can anyone help me?

Sponsor:

#2 Bartek Sowka   Members   -  Reputation: 100

Like
0Likes
Like

Posted 03 April 2012 - 01:39 PM

Ok so I made the image bigger and it worked...
The Image I had before was 32x32, when I stretched it to 128x128 it worked!
How can I display 32x32 bmps then?

#3 JustinDaniel   Members   -  Reputation: 137

Like
0Likes
Like

Posted 03 April 2012 - 11:07 PM

I dont see any problem there.

If i am right, Try passing the coordinates, width and height to the fourth parameter of the SDL_BlitSurface() function.

SDL_Rect rect = { 0, 0, 32, 32 };
SDL_BlitSurface(hello, NULL, screen, &rect);


#4 rip-off   Moderators   -  Reputation: 8728

Like
0Likes
Like

Posted 04 April 2012 - 02:33 AM

JustinDaniel's guess is likely incorrect. SDL_BlitSurface accepts NULL as the final parameter, and will act as if you had passed it a rectangle encompassing the whole destination surface.

What is likely happening is that the image is failing to load. SDL will help you determine why: use SDL_GetError() to get a human-readable description of the problem. Currently you have no error checking, handling or reporting. This is what your program should look like:

#include <iostream>
#include <cstdlib>

#include "SDL.h"

int main( int argc, char* args[] )
{
    if(SDL_Init( SDL_INIT_EVERYTHING ) < 0)
    {
        std::cerr << "Failed to initialise SDL: " << SDL_GetError() << '\n';
        return 1;
    }
    std::atexit(&SDL_Quit);

    SDL_Surface *screen = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );
    if(!screen)
    {
        std::cerr << "Failed to set video mode: " << SDL_GetError() << '\n';
        return 1;
    }

    const char *filename = "block.bmp";
    SDL_Surface *hello = SDL_LoadBMP(filename);
    if(!hello)
    {
        std::cerr << "Failed to load \'" << filename << "\': " << SDL_GetError() << '\n';
        return 1;
    }

    if(SDL_BlitSurface(hello,NULL,screen,NULL) < 0)
    {
        std::cerr << "Failed to blit surface to screen: " << SDL_GetError() << '\n';
        return 1;
    }

    if(SDL_Flip(screen) < 0)
    {
        std::cerr << "Failed to flip screen: " << SDL_GetError() << '\n';
        return 1;
    }

    SDL_Delay(2000);

    SDL_FreeSurface( hello );
    return 0;
}
Note that almost every SDL call can return an error. You should check for and handle or report these errors. I've simply logged them to stderr here.

Running this program on my computer, which has no "block.bmp", gives the following error:

Failed to load 'block.bmp': Couldn't open block.bmp

Try running this program using your original image, and see what the output is.

My guess is that your image should not be in the Debug folder, but rather in the project root, which is where the "working" directory is typically set by an IDE. Running the executable directly from the file system is different, of course. To diagnose this kind of error, try to see if the file loads using an absolute path (C:\path\to\project\block.bmp). If the smaller image loads correctly, then the problem is that the working directory isn't where you expect it to be.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS