Jump to content
  • Advertisement
Sign in to follow this  
crazycode

problem with SDL_LoadBMP

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

I'm attempting to load an image into SDL, but for some reason its not loading the image even though its in the same directory as main... I'm using code::blocks.. heres my code..

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

using namespace std;

int main(int argc, char* args[])
{
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
//initialize SDL
cerr << "SDL_Init Failed: " << SDL_GetError() << endl;
exit(1);
}

//Set the video mode

SDL_Surface* display;
display = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);

if(display == NULL)
{
cerr<< "SDL_SetVideoMode() Failed: " << SDL_GetError() << endl;
exit(1);
}

//set title

SDL_WM_SetCaption("SDL Tutorial", "");

//load image
SDL_Surface* image;
image = SDL_LoadBMP("a.bmp");
if (image == NULL)
{
cerr << "SDL_LoadBMP() Failed: " << SDL_GetError() << endl;
exit(1);
}

//main loop
SDL_Event event;

while(1)
{
if(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
break;
}
}/*
if (SDL_BlitSurface(image, NULL, display, NULL) != 0)
{
cerr << "SDL_BlitSurface() Failed: " << SDL_GetError() << endl;
exit(1);
}
SDL_Flip(display);*/
}


SDL_Quit();

return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
okay so for some reason this code I have above doesnt work(from here: http://gameprogrammingtutorials.blogspot.com/2010/01/sdl-tutorial-series-part-4-how-to-load.html) but lazy foos does.. and they look very similar to me. :huh: Owell my problems fixed.. Any idea why its like this?

Share this post


Link to post
Share on other sites
What does SDL_GetError() print? These problems are typically caused by the image being in the wrong location. Your IDE starts your application, and it has a chance to set its working directory. The working directory is the one all non-absolute paths are loaded relative to. I don't use Code::Blocks so I don't know its convention for where it sets the working directory.

Can you get Lazy Foo's code to load your image? This way you would know the image isn't corrupt or in some unknown format.

Share this post


Link to post
Share on other sites
Are you saying that your program displays nothing? You have your drawing code commented out. Or do you mean nothing is printed to the console? Have you tried capturing the output to a file or using the debugger to examine the return value of SDL_GetError()?

Please give a full explanation of the behaviour you are seeing.

Share this post


Link to post
Share on other sites
crazycode: You shouldn't use exit() like that. Just return from main. Otherwise, you are circumventing RAII concepts (google it, it is absolutely essential knowledge for non-bad C++). Also, you are not cleaning up resources properly.

Share this post


Link to post
Share on other sites

crazycode: You shouldn't use exit() like that. Just return from main. Otherwise, you are circumventing RAII concepts (google it, it is absolutely essential knowledge for non-bad C++). Also, you are not cleaning up resources properly.


I know that. I'm just looking at SDL tutorials now.. I'm coming from XNA, and I was wondering about the exit function... thought it was oddly placed. The problem is I've ran into the code above and for some reason it doesn't work loading the files... I think I'm going to just follow lazy foo for now on.. :)

Share this post


Link to post
Share on other sites
You'll run into this problem again unless you figure out the root cause. Please look at my last post and answer some of the questions raised there if you are interested in solving this problem.

Share this post


Link to post
Share on other sites
sorry.. accidently skipped over your post.. the draw code is commented out because i thought that was where the problem was coming from, but it wasn't. And there is nothing being printed to the console as far a errors
t

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!