• 13
• 16
• 27
• 9
• 9

help with lazy foo tutorial one...

This topic is 2803 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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 */
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 on other sites
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 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 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 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 on other sites
Well, this sucks. I tried putting the image file into the same directory as the exe, but to no avail. Ugh! lol

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 on other sites
I have tried both of the above methods.

Share on other sites
Try specifying the full path as in "C:\\data\\test.bmp" then relative location shouldn't be an issue.