Jump to content

C++ - SDL Problem Loading Image

- - - - -

  • Please log in to reply
18 replies to this topic

#1
Wakababu

Wakababu

    Newbie

  • Members
  • PipPip
  • 20 posts
I'm not new to C++, but I am new to SDL. When I tried rendering an image, it continuously fails to load. The problem, I think, is in my use of the function SDL_LoadBMP. Are there any other ways to load an image? Will post source code if requested.

#2
brownhead

brownhead

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
You can load a non-bmp by using the SDL_image extension, though if you're only trying to load a BMP then please post the source code as SDL should be able to handle that natively just fine.

#3
Wakababu

Wakababu

    Newbie

  • Members
  • PipPip
  • 20 posts
Here it is, I'm using Visual Studios 2010 Express(if that changes anything besides the header)
And I really should get an Avatar

#include "SDL.h"


bool done = false;


int main(int argc, char* args[])    {

    //The images

    SDL_Surface* hello = NULL;

    SDL_Surface* screen = NULL;


    SDL_Event event;


    SDL_Init(SDL_INIT_EVERYTHING);


    screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE); //Set Screen


	hello = SDL_LoadBMP( "hello.bmp" );

	if (!hello){done = true;} 


    while (!done){

        while (SDL_PollEvent(&event)){

            // If you try to close the window

            if (event.type == SDL_QUIT){

                done = true;

            }

        }


        SDL_BlitSurface( hello, NULL, screen, NULL ); //Apply image

        SDL_Flip( screen ); //Update the screen

        SDL_Delay( 1 );

    }


    SDL_FreeSurface(hello); 


    SDL_Quit();


    return 0;

}


#4
artificial

artificial

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 624 posts
Instead of...
if(!hello)
... you should write...

if(hello == NULL)

{

   //...

}


And please use code-tags next time. ;)

EDIT: How do you execute your application? Do you launch it out of VS? If so, check whether the image is in the right directory.

Greets,
artificial

Edited by artificial, 22 January 2011 - 01:44 PM.
EDIT

Sometimes words ain't enough to express something. That's why computer scientists use double words.

#5
Wakababu

Wakababu

    Newbie

  • Members
  • PipPip
  • 20 posts
Sorry about the Code-Tag thing, I'm not used to forums.

Changing the if-statement had no affect.

And as for whether or not the image is in the correct directory; I think that's the problem. I have the image beside the .cpp file I'm using... And actually about everywhere else in the Visual Studios file, for good measure. And I do indeed launch it out of VS.

#6
brownhead

brownhead

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
@Artificial: That is preference only, different people prefer a different method. They are both equivalent (I'm not sure if they are equivalent based upon the standard but you will not find a compiler that will give unexpected results without intentional tweaking).

@Wakababu: Please place your code in code tags. Also, what is the result of running your program?

#7
artificial

artificial

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 624 posts

Wakababu said:

Changing the if-statement had no affect.

I know, but it's better style. ;)

Wakababu said:

And as for whether or not the image is in the correct directory; I think that's the problem. I have the image beside the .cpp file I'm using... And actually about everywhere else in the Visual Studios file, for good measure. And I do indeed launch it out of VS.

I personally don't work with VS, so I can't tell you the complete solution. Nevertheless, most IDEs have a working directory which differs from the one your source-files are placed in. Just put your image in every folder you see. :D
Sometimes words ain't enough to express something. That's why computer scientists use double words.

#8
brownhead

brownhead

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
The image must be in the same directory as the executable file. Find your .exe file (likely in a directory named bin or bin/debug, though not altogether familiar with vs's build system) and place the image in the same directory as it and try running the .exe file directly.

#9
Wakababu

Wakababu

    Newbie

  • Members
  • PipPip
  • 20 posts
When I run the program, it simply appears for a frame, then closes (no image). I'm assuming that it's because of the fact that loading the image failed, because whenever I take out the if-statement that checks to see if it loaded properly, it just sits there, doing nothing(no image).

#10
artificial

artificial

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 624 posts

brownhead said:

The image must be in the same directory as the executable file. Find your .exe file (likely in a directory named bin or bin/debug, though not altogether familiar with vs's build system) and place the image in the same directory as it and try running the .exe file directly.

That only works when you don't launch the application out of the IDE (at least this is true for some IDEs). ;)

Other (possible) solution: Put the image and the .exe-file into the same directory and lauch the latter "normally".
Sometimes words ain't enough to express something. That's why computer scientists use double words.

#11
brownhead

brownhead

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
That's very strange if it won't work even with the img file in the same directory. What OS are you running? And what is the file extension of your image?

Edit:

Quote

Other (possible) solution: Put the image and the .exe-file into the same directory and lauch the latter "normally".
:c-penguin:that is indeed exactly what I said.

#12
Wakababu

Wakababu

    Newbie

  • Members
  • PipPip
  • 20 posts
I've put the image into every directory in the VS folder (including beside the .exe) and I've run it through the IDE along with just using the .exe. It was a no-go. :/




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users