Bob561917

Hi,
As most of you know, I have downloaded a chess game called BrutalChess for the purpose of learning how to build a chess game. The application type is a console application and also uses the SDL Library and FreeType library. So far, I have sort of finally got the program to run except for one error at runtime. The error is a vector subscript out of range. I found what was causing the problem which is due to a font error that the FreeType Library uses, and the application was designed poorly for error checking. So in other words, the program will continue to run even if any of the files that are needed did not load. There are several load file problems, but what causes the "vector subscript out of range" error to happen, is when the font file does not load. So I am wondering if there is a way to terminate the program in a C++ console application as soon as a file does not load




If you need to see how this error happens, here are the steps marked in bold...

this is the start of the error problem.
Code Snippet

bool drawGLScene(GameCore * core)
{
// These are to calculate our fps.
static int T0 = 0;
static int Frames = 0;

// Clear The Screen And The Depth Buffer.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

//this is the class function call that starts the error problem.

core->draw();

// Draw it to the screen.
SDL_GL_SwapBuffers();

// Gather our frames per second.
Frames++;
int t = SDL_GetTicks();
if (t - T0 >= 5000) {
float sec = (float)((t-T0)/1000.0);
float fps = Frames / sec;
// cout << Frames << " frames in "<< sec << " seconds = "<< fps << " FPS\n";
T0 = t;
Frames = 0;
}

return true;
}


this is what the GameCore:Big Smileraw() class function looks like...
Code Snippet

void GameCore::draw()
{
//When m_loaded is true, the current 3D scene gets drawn, and when it is false, it draws the splash screen.

if(m_loaded) {
/// .... in this area, the code is not necessory for my error.
} else {
glLoadIdentity();

// it is inside this class function call that causes the "vector subscript out of range" error.

drawLoadingScreen();
// Extra delay during the loading screen to avoid choking the CPU
SDL_Delay(25);
}
}


this is what the GameCore:Big SmilerawLoadingScreen class function looks like this...
Code Snippet

void GameCore::drawLoadingScreen()
{
if(!m_preloaded)

//This class function will try to load the needed preload files, including the font used for this application. However, if any of the files do not load the game still continues to run which is why I get that subscript error.

preload();


glTranslated(0,0,-106);

// Drawing the Brutal Chess Logo
m_logotexture.use();
glEnable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING);
glEnable(GL_BLEND);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_QUADS);
glNormal3d(0,0,-1);
glTexCoord2d(0.0,0.0);
glVertex3f(-35.0,25.0,0.0);
glTexCoord2d(0.0,1.0);
glVertex3f(-35.0,-10.0,0.0);
glTexCoord2d(1.0,1.0);
glVertex3f(35.0,-10.0,0.0);
glTexCoord2d(1.0,0.0);
glVertex3f(35.0,25.0,0.0);
glEnd();
glDisable(GL_BLEND);
glEnable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);

// Alpha value to create a pulsing effect
double c = 0.6+0.4*cos(0.15*SDL_GetTicks()*3.1415/180.0);

// Drawing the Loading text
glEnable(GL_BLEND);
glColor4f(1.0, 1.0, 1.0, c);
glTranslated(28, -18, 0);
glScaled(1/10.0, 1/10.0, 1/10.0);
//It is this line that causes the "vector subscript out of range error to happen if the font has not been loaded.

FontLoader::print(-28,-7,"Loading...");
glScaled(10, 10, 10);
glDisable(GL_BLEND);

// Spinning pawn
glRotatef(SDL_GetTicks()/2.5,0,1,0);
glRotatef(30,0,0,1);

m_loadpawn.draw();
}


this is what the GameCore:Stick out tonguereload() class function looks like this...
Code Snippet

bool GameCore::preload()
{
if(m_loadpawn.load("../models/pawn.obj")) {
// add debugging
}

else {
cerr << "Failed to load pawn." << endl;
return false;
}

m_loadpawn.findNorms();
m_loadpawn.setScale(m_loadpawn.scale()*12);

if(FontLoader::loadFont("sans", "../fonts/ZEROES__.TTF", 32)) {
// add debugging
}

else {
cerr << "Failed to load fonts." << endl;
return false;
}

if(m_logotexture.load("../art/brutalchesslogo.png")) {
// add debugging
}

else {
cerr << "Failed to load logo" << endl;
return false;
}

m_logotexture.loadGL();
m_preloaded = true;
return true;
}


now, as you can see in the GameCore:Stick out tonguereload class function, it does make the condition to return false if any of those files do not load. However, the problem lies in that after preload is called inside of the GameCore:Big SmilerawLoadingScreen(), it still continues to run the program instead of terminating the program.


Re: Visual C++ Express Edition need help on terminating program

einaros

You may be returning false from preload, but you're never checking preload's return value -- let alone attempting to exit the application. Returning false along does not terminate your application. You can throw an exception and catch it at the absolute origin of your application, or handle return values all the way back and terminate the application gracefully. The less pretty alternative is to call an hard abort function (such as exit) if e.g. preload fails.





Re: Visual C++ Express Edition need help on terminating program

Bob

einaros wrote:
You may be returning false from preload, but you're never checking preload's return value -- let alone attempting to exit the application. Returning false along does not terminate your application.

einaros,
I think I did not make myself clear to you. I tried to explain in my original post that this program was downloaded from the internet which means that I did not create or write this program. Also, I know that the program is not checking preload's return value which is why I am here to ask what are my possibilities for terminating the program when a file does not load properly. True, I could keep on returning false until it reaches the original function call, but there has to be a cleaner way of doing this without having all these return false, return false and so on.

einaros wrote:
You can throw an exception and catch it at the absolute origin of your application...


Also, what do you mean by catching it at the absolute origin of your application Do you mean that I should check for these errors before any other initailization is called in the int main function such as in this example....

Code Snippet

int main()
{
if(preload())
// continue my code
else
cout << "The program has encountered an error.";

return 0;
}







Re: Visual C++ Express Edition need help on terminating program

einaros

Bob561917 wrote:
einaros wrote:
You may be returning false from preload, but you're never checking preload's return value -- let alone attempting to exit the application. Returning false along does not terminate your application.

einaros,
I think I did not make myself clear to you. I tried to explain in my original post that this program was downloaded from the internet which means that I did not create or write this program. Also, I know that the program is not checking preload's return value which is why I am here to ask what are my possibilities for terminating the program when a file does not load properly. True, I could keep on returning false until it reaches the original function call, but there has to be a cleaner way of doing this without having all these return false, return false and so on.


Yes, use exceptions.

Bob561917 wrote:

einaros wrote:
You can throw an exception and catch it at the absolute origin of your application...


Also, what do you mean by catching it at the absolute origin of your application Do you mean that I should check for these errors before any other initailization is called in the int main function such as in this example....

Code Snippet

int main()
{
if(preload())
// continue my code
else
cout << "The program has encountered an error.";

return 0;
}





No, I mean

int main()
{
GameCore gc;
try
{
// do whatever with the gc object to make it run
}
catch(SomeReallySeriousException& e)
{
// possibly show an error message from the exception object, or just exit
}
}

SomeReallySeriousException would of course be a class of your making, probably capable of passing a string. If you're unfamiliar with exceptions as a whole, see e.g. http://www.cplusplus.com/doc/tutorial/exceptions.html.





Re: Visual C++ Express Edition need help on terminating program

Bob

einaros,
Thank you so much for explaining the exception rule. Also, I will take a look at the link you have provided since I am fairly new to exceptions.