Skip to main content
GameDev.net gamedev.net
🔒 Locked

[SOLVED] SDL can't find image.

Started by Dennisvb Feb 28, 2013 at 4:16 PM 6 replies 4.9k views
Original Post
Dennisvb
Dennisvb

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?

Nercury
Nercury

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



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

// 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

kuramayoko10
kuramayoko10

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 );
Programming is an art. Game programming is a masterpiece!
?????????????????
?????????????????

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

Servant of the Lord
Servant of the Lord

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.

Dennisvb
Dennisvb

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!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.