• Advertisement
Sign in to follow this  

[SOLVED] SDL can't find image.

This topic is 1785 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

Hello,

 

I started using SDL today and I am using this tutorial. I tried to run the program in the tutorial, but it just shows a black screen:

#include "SDL.h"

int main( int argc, char* args[] )
{
    // the images
	SDL_Surface *hello = NULL;
	SDL_Surface *screen = NULL;

	SDL_Init( SDL_INIT_EVERYTHING );

	screen = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );

	hello = SDL_LoadBMP( "hello.bmp" );

	SDL_BlitSurface( screen, NULL, hello, NULL );

	SDL_Flip( screen );

	SDL_Delay( 2000 );

	SDL_FreeSurface( hello );

	return 0;
}

 

I placed the hello.bmp in the folder with the vsxproj file. What am I doing wrong?

Share this post


Link to post
Share on other sites
Advertisement

I placed the hello.bmp in the folder with the vsxproj file. What am I doing wrong?

Place it in the folder with your .exe file.

Share this post


Link to post
Share on other sites


I placed the hello.bmp in the folder with the vsxproj file. What am I doing wrong?

Place it in the folder with your .exe file.
Tried that too, also tried using the SDL_image lib. Did not work either.

Share this post


Link to post
Share on other sites
// set program icon
SDL_WM_SetIcon( SDL_LoadBMP("hello.bmp"), NULL );

 

try this to see if it is an image problem or a screen problem

 

// if screen didn't load
if ( screen == NULL )
{

	// set caption to "screen not working"
	SDL_WM_SetCaption( "screen not working", NULL );
}

 

also try this to check if the screen loads. you can do the same for the hello SDL_Surface

Edited by hikarihe

Share this post


Link to post
Share on other sites

Also, try setting the following flag on your Screen initialization: SDL_DOUBLEBUF

 

With that and SDL_HWSURFACE you will be able to use SDL_Flip() properly.

screen = SDL_SetVideoMode( 640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF );
Edited by kuramayoko10

Share this post


Link to post
Share on other sites

You are using the blitscreen the opposite way : http://sdl.beuc.net/sdl.wiki/SDL_BlitSurface

SDL_BlitSurface takes 4 arguments, first one is the source surface or the surface you want to place ontop of your screen ( the image ) 

The important part is that the 3th argument is the destination argument or the surface you are drawing too ( the screen)

 

 

as a side node If the secound argument is NULL, the entire surface is copied.,if the 4th argument is NULL, then the destination position (upper left corner) is (0, 0).

 

In 1 word your function should look like SDL_BlitSurface( hello, NULL screen, NULL );

Share this post


Link to post
Share on other sites

Alex_kir is correct when he says you mistakenly swapped the 'surface to draw' and 'surface to draw on' parameters. (Nice catch, btw! I hadn't noticed)
 
But in addition to that, you should check the return results of your function calls, especially when loading resources:

SDL_Surface *image =  SDL_LoadBMP(fileName);
if(!image)
{
    std::cout << "SDL_LoadBMP() failed: Failed to load the image '" << fileName << "'.\nSDL_LoadBMP() : " << SDL_GetError() << std::endl;
    return;
}

 

It wouldn't have caught your current bug, but it would've told you whether or not the problem was with loading the image.

Share this post


Link to post
Share on other sites

Thank you for all the help, it's really nice to see a community like this! It was a hard to find mistake that I switched the screen and hello surfaces in that function. I will try to fix it when I am home.
Thanks again!

 

Fixed it!

 

This is the code I have now and it works.

#include "SDL.h"
#include "SDL_image.h"
#include <iostream>

int main( int argc, char* args[] )
{
    // the images
	SDL_Surface *hello = NULL;
	SDL_Surface *screen = NULL;

	SDL_Init( SDL_INIT_EVERYTHING );

	SDL_WM_SetCaption( "Rocket 0.0.1" , NULL );

	// create the window
	screen = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );

	hello = IMG_Load( "image.png" );
	if( !hello ) {
		std::cout << "ERROR: " << SDL_GetError() << std::endl;
	}

	SDL_BlitSurface( hello, NULL, screen, NULL );

	// update the screen
	SDL_Flip( screen );

	SDL_Delay( 2000 );

	// unload the image
	SDL_FreeSurface( hello );

	return 0;
}

 

The first problem was I had my image in the wrong directory and the second one was the SDL_BlitSurface problem.

Thanks everybody!

Edited by Dennisvb

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement