Jump to content
  • Advertisement
Sign in to follow this  
iAmCodeMonkey

help with lazy foo tutorial one...

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

Hey! I am trying out lazy foo's tutorials and for the first one, I cannot get the SDL function SDL_LoadBMP() to load my bitmap image. Is there a specific place to put any bitmap image files to use with SDL? (i.e same directory as the .exe )

My code is as follows, just in case there is a problem with it...

/* include files */
#include <iostream>
#include <string>
#include "defines.h"
#include "SDL.h" /* main SDL header */
#include "SDL_Mixer.h" /* used for audio */
#include "SDL_TTF.h" /* used for true-type fonts */
#include "SDL_Image.h" /* used for graphics */

/* namespaces */
using namespace std;

/* pragma statements */
#pragma once

/* data structures */


/* global variables */
int gameTimer; /* integer varible for game timing */

/* game-state management functions */


/* game-state helper functions */


/* main function */
int main(int argc, char** argv) {

SDL_Surface* gameBitmap = NULL; /* background bitmap image */
SDL_Surface* gameWindow = NULL; /* back buffer used for display */
SDL_Event gameEvent; /* SDL event structure for input */

/* setup SDL, check for success */
if ( SDL_Init(SDL_INIT_EVERYTHING) != 0 ) {
printf("Error! Unable To Initiallize SDL Systems. Program Terminating.\n", SDL_GetError());
return 1;
} /* end if */

/* setup screen, check for success */
gameWindow = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, BPP, SDL_SWSURFACE);
if ( gameWindow == NULL ) {
printf("Error! Unable To Setup Screen Surface. Program Terminating.\n", SDL_GetError());
return 1;
} /* end if */

/* load image, check for success */
gameBitmap = SDL_LoadBMP("test.bmp");
if ( gameBitmap == NULL ) {
printf("Error! %s. Program Terminating.\n", SDL_GetError());
return 1;
} /* end if */

/* apply image to screen */
SDL_BlitSurface(gameBitmap, NULL, gameWindow, NULL);

/* update screen */
SDL_Flip(gameWindow);

/* pause X milliseconds, for now */
SDL_Delay(200);

/* free the loaded image from video memory */
SDL_FreeSurface(gameBitmap);

/* quit SDL */
SDL_Quit();

return 0;
} /* end main() */

Share this post


Link to post
Share on other sites
Advertisement
Yes, it should be in the same directory as the .exe.

To make sure it can find the file, you can use a function like this:


bool fileExists(const std::string &fileName) {
std::ifstream file(fileName.c_str());
return file.is_open();
}



You'll need to #include <fstream> to use it.

Before calling SDL_LoadBMP(), call the function:

if (!fileExists("test.bmp")) {
printf("Failed to find file");
return 1;
}


And if you see the above message, you know the problem is not with SDL.

Share this post


Link to post
Share on other sites
The file needs to be in the current working directory, which may or may not be the directory that contains the executable. (VC++ 2008, for example, sets the working directory to the directory that contains the project file by default, I believe.)

Share this post


Link to post
Share on other sites
Right, sorry about that. When you run the program from VC++, the current working directory is different from when you directly run the .exe file.

The simplest thing to do IMO is to set the current working directory to always be the one that contains the .exe. You can do that by going to Project -> Properties -> Configuration Properties -> Debugging, and in the Working Directory field, enter the directory that contains the .exe.

BTW, if you want to change the directory that contains the .exe, you can do that at Project -> Properties -> Configuration Properties -> General, at the Output Directory field.

Share this post


Link to post
Share on other sites
Quote:
The simplest thing to do IMO is to set the current working directory to always be the one that contains the .exe. You can do that by going to Project -> Properties -> Configuration Properties -> Debugging, and in the Working Directory field, enter the directory that contains the .exe.
What about when you have multiple builds? Wouldn't that necessitate creating and maintaining multiple copies of all data files?

I imagine you could set things up so that all the executables go in the same directory, but the way VC++ (2008) sets things up by default is to create a separate directory for each executable, which seems reasonable.

I've always just used the default project setup, which (as noted previously) sets the current working directory to the project directory, and therefore allows you to use the same data for all builds. I'm sure there are other ways things could be set up, but having to create and maintain multiple copies of the application data should be avoided, IMO.

Share this post


Link to post
Share on other sites
If you execute the exe outside of the IDE (prob Visual Studio?) then the image need to be in the same place as the exe. However if you run the exe through Visual Studio then the image need to be in the Project folder (same place as the project file.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!