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:


#ifndef _GAMEENGINE_H_
#define _GAMEENGINE_H_



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

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

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

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

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

int curRect;

//// Sky ////

image skySurface;

//// Scrolling ////

int scrolling;



#include "gameengine.h"



#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; //Open the file

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

while (pseudoComment()) { //Get command
if (action=="define") {
levelFile >> variableChar;
if (variableChar=="sky") {
levelFile >> variableChar;
} else if (variableChar=="ground") {
levelFile >> 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;


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;

Best Regards,


  • 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.
  • 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.

Howdy, Stranger!

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


In this Discussion