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

## Recommended Posts

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?

##### Share on other sites
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?

##### 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.

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

##### 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:
 #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
[/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.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 21
• 22
• 11
• 25