Game engine problems - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Game engine problems

I'm making a simple game engine class for a mario-like game. I've been having trouble with ifstream. If I put ifstream levelFile, I get errors, so I put std::ifstream levelFile. But the program quits after initializing SDL, even though I don't include gameengine.h in main.cpp. Does SDL have a problem with ifstream?

Here's my code:

[code]gameengine.h

#ifndef _GAMEENGINE_H_
#define _GAMEENGINE_H_

#include
#include

#include
#include

#include "sprite.h"
#include "image.h"
#include "font.h"

class level {
public:
//// Initialize the game engine ////
bool init();

//// Load the level ////
bool loadLevel(char* filename);
char* pseudoComment(); //For the comments

level();
virtual ~level();
private:
//// For loading the game ////
std::ifstream levelFile; //The file handle

///// Ground ////
SDL_Rect* groundRect;
image groundSurface;

int curRect;

//// Sky ////

image skySurface;

//// Scrolling ////

int scrolling;
};

#endif[/code]

[code]gameengine.cpp

#include "gameengine.h"

#include
#include
#include

#include


#include "image.h"
#include "sprite.h"
#include "font.h"

bool level::init() { //Init the game enginge
curRect = 0;
}

bool level::loadLevel(char* filename) { //Load the level
char* action = new char[30]; //The current action
char* variableChar = new char[30]; //A variable string
int variableInt; //A variable int

levelFile.open(filename); //Open the file

if (!levelFile)
return false; //Oh no! Error!

while (pseudoComment()) { //Get command
if (action=="define") {
levelFile >> variableChar;
if (variableChar=="sky") {
levelFile >> variableChar;
skySurface.loadImage(variableChar);
pseudoComment();
} else if (variableChar=="ground") {
levelFile >> variableChar;
groundSurface.loadImage(variableChar);
}
} else if (action=="createGround") {
levelFile >> variableInt;
groundRect[curRect].x = variableInt;
levelFile >> variableInt;
groundRect[curRect].y = variableInt;
levelFile >> variableInt;
groundRect[curRect].w = variableInt;
levelFile >> variableInt;
groundRect[curRect].h = variableInt;
curRect++;
}
}

levelFile.close();
}

char* level::pseudoComment() {
char* comment; //Storage for the comment

levelFile >> comment; //Is it a start of a comment?
if (comment=="#") { //Yep
int done = 1; //Is it done?
while (done) { //Loop until the end of the comment
levelFile >> comment;
if (comment=="#") //Comment is done!
levelFile >> comment;
return comment;
}
} else { //Nope
return comment;
}
}

[/code]
Best Regards,
CodeKing

Comments

  • MT2002MT2002 Posts: 1,444Member
    SDL should work fine with file streams. I suspect the problem lies
    in the way you are loading the file into char buffers, rather then
    the safer getline() method into an std::string.

    Why are you using char*'s in a C++ program, anyway? There are so many problems with them.
  • MT2002MT2002 Posts: 1,444Member
    Also, the reason using [b]ifstream[/b] gives errors, while [b]std::ifstream[/b] works is because ifstream is part of the std namespace.

    You didnt specify what namespaces to use, hence you have to append namespace::.

    If you add [b]using namespace std;[/b] after [b]#include [/b]
    you will not need to append the [b]std::[/b]. I personally still dont recommend this for larger programs.

Sign In or Register to comment.