Sign in to follow this  

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

This topic is 2081 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok so my code is:
[CODE]
#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;
}
[/CODE]

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

Oh and I'm using Code::Blocks.

Can anyone help me?

Share this post


Link to post
Share on other sites
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.

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

Share this post


Link to post
Share on other sites
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:
[code]

#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;
}
[/code]
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:
[quote]
Failed to load 'block.bmp': Couldn't open block.bmp
[/quote]
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.

Share this post


Link to post
Share on other sites

This topic is 2081 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this