Jump to content

  • Log In with Google      Sign In   
  • Create Account


[SOLVED] SDL can't find image.


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
7 replies to this topic

#1 Dennisvb   Members   -  Reputation: 160

Like
0Likes
Like

Posted 28 February 2013 - 10:16 AM

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?



Sponsor:

#2 Nercury   Crossbones+   -  Reputation: 766

Like
0Likes
Like

Posted 28 February 2013 - 12:24 PM

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.

#3 Dennisvb   Members   -  Reputation: 160

Like
0Likes
Like

Posted 28 February 2013 - 01:02 PM


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.

#4 hikarihe   Members   -  Reputation: 243

Like
1Likes
Like

Posted 28 February 2013 - 01:12 PM

// 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, 28 February 2013 - 01:17 PM.


#5 kuramayoko10   Members   -  Reputation: 386

Like
1Likes
Like

Posted 28 February 2013 - 02:43 PM

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, 28 February 2013 - 02:47 PM.

Programming is an art. Game programming is a masterpiece!

#6 Alex_kir   Members   -  Reputation: 254

Like
3Likes
Like

Posted 28 February 2013 - 03:22 PM

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 );



#7 Servant of the Lord   Crossbones+   -  Reputation: 16667

Like
1Likes
Like

Posted 28 February 2013 - 04:10 PM

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.


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


#8 Dennisvb   Members   -  Reputation: 160

Like
1Likes
Like

Posted 01 March 2013 - 05:21 AM

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, 01 March 2013 - 06:31 AM.





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