views:

61

answers:

2

Hi everyone, rookie C++ programmer here still.

I'm using VC++ VS2008 compiler and glut library. All working fine and up to date ( I know there's 2010 just cba because of XNA (C#) support reasons)

Ok this time I have a question that is code related but I can make the code work. What I can not do is figure out what is happening under the hood. That's what I'd like to figure out.

Assume all works, because clearly in runtime it works!

Here's the code (minimized to what is actually running to it's bare core to help speed this up)

#include "stdafx.h"
#include "Controller.h"
#include "glut.h"
#include <iostream>

void main(int argc, char** argv) 
{   
    glutInit(&argc, argv);
    //controller_ptr->InitController();  <---- no link to controller-->DrawScene

    //INIT GLUT
    //glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    //glutInitWindowSize(400, 400); //Set the window size
    //glutCreateWindow("Some Program"); 

    //INIT RENDERING
    glEnable(GL_DEPTH_TEST);

    glutDisplayFunc(drawScene);
    glutKeyboardFunc(handleKeypress);
    glutReshapeFunc(handleResize);
    glutMainLoop();
}

You may be thinking, hey you have the init code commented. I do indeed. Guess what , it is still working! Why? I don't really KNOW glut except on BBQ's so ye.... <---- This could be a good point for you to call the fail if any

I have a drawscene class with this method

void DrawScene::initGlut()
{
    //Enables depth culling/front face culling??????  <----- what about that, some   depth thingy was involved is all I'm sure of
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(400, 400); //Set the window size
    glutCreateWindow("Some Program"); 
}

When I comment this and uncomment the one in the main, same result. I comment none, glut says init is done twice, I comment neither, init is not done it all goes boom. So the only logical conclusion would be to say this method is called, right?

There's a bunch of other methods around and classes but there is no instance of the displayescene class nor controller class. I can comment the includes and it will do all the same.

Is there some design part I do not understand in MSbuild or VC++ development which allows methods to sift along some stream or flow that I am unaware of? As far as my knowledge goes, if I do not make an instance of a class it's not relevant, at least it's non-static members. So how can this code being active or inactive in this member have an effect on my program then?

Probably forgetting something incredibly stupid, but I'd rather find out a.s.a.p. anyway, the good part about being a rookie is that I couldn't care less about falling flat on my face and trying again so fire away with the comments ^^

Thank you very much in advance!

EDIT: What the ? Now the console without any change to the code did report once a double call to init with the main init in comment and the one in drawscene not in comment...I am very much confused.

EDIT 2: 14:45 29/08/2010

Ok I found something....

The constructor in drawscene calls the initGlut and initRendering methods of the class. The controller makes a pointer to the heap holding a drawScene object there which causes the methods to run the init code.

The controller however is not included in the main nor is it instantiated. That drawScene code is run before the main starts and can I then conclude that the build itself is the problem so I should avoid init code in constructors?

EDIT 3: 15:02 29/08/2010

Found another , the double call does occur when only the drawScene is the active init code. Could it be that every include of drawscene causes constructor code to run init code and cause the double init?

+1  A: 

Why not stick a break point on your init code and then walk up the call stack to see where it's being called?

Step through it slowly and I would imagine you'll find the source..

Edit: keeping initialisation of things like libraries out of the constructor is good practice. Personally, I would just test that glut has been initialised in the constructor if it's required and throw an exception if it's not detected.

Jon Cage
Why didn't I think of that haha, I will try that right now
Proclyon
Well it's inside the Drawscene BEFORE it hits a single line of code in the main, so that means it's preprocessor right?
Proclyon
No, static object construction is the only thing that runs before main. If you're in a library/DLL other code can run before main.
DeadMG
What about the includes then?Pretty much everything with a # in front is a pre-processor directive right ? I don't know much about preprocessing just that it's always step 1 in the compilation chain. So running on a whimsical thought here, what if all the includes combined with constructor code resulted into a method call? Would that then be pre-main code?
Proclyon
A: 

apperantly those nifty pointers were not part of an object but globals, GG C# VS C++ :P

Global declaration of stuff in cpp files BAD , lesson learned

Proclyon