CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2008
    Posts
    2

    Problem in initializing any external object in displayfunc

    Hi all!

    I am a beginner and currently working on a little hobby Opengl game project and I am having several .h and .cpp files.

    In my main.cpp file I have my glutDisplayFunc(renderScene). I would like to use a "ship" class that is declared in ship.h and the impemetation of the functions in consequently in ship.cpp. One of the constructors in ship.cpp Ship::Ship(string textures){...} takes a string in order to attach a certain texture to the object.

    Since I wanted to have it all neat and nice, I use in main.cpp a func "initObjects" to initialize a ship object via Ship A("usethistexture"). This function initObjects is called directly in main.cpp from main{... initObjects(void);... }

    I would like to use now this object A in my renderscene in main.cpp because in renderscene I use something something like

    glBindTexture ( GL_TEXTURE_2D, A.itsTextures[0] );

    The problem is, that since Ship A is initialized in initObjects it would get out of scope once it leaves initObjects and renderScene doesn't know Ship A therefore.

    My first idea was to make the object external by trying something like in main.h and ship.h: extern Ship A;

    but then I get a compiler error undefined reference to _A.

    Anyway this seemed also strange to me to have all Objects being global.

    Thus I tried to find an alternative and I found a solution which works but is pretty ugly.

    I initialize the object Ship A in renderscene the first time the function is being called, since renderscene is an endlessloop. It works but the code looks ugly and i didn't manage to keep my promise in intitializing the object in initObjects().

    Another thought then was I may pass A by reference to renderscene but this would be also pretty cumbersome to pass everytime an object to renderscene which is created in initObjects.

    My last thought was then to make a new class in main.cpp which holds pointers to all objects. But this seems to me to be a lot of work and kind of exotic and if would declare this new class in newclass.h, I would face exactly the same problem as now.

    I would be grateful for any suggestion how to have Ship A in the scope of renderscene in the cleanest possible way. Thanks.

    Jack

  2. #2
    Join Date
    Aug 2008
    Posts
    2

    Re: Problem in initializing any external object in displayfunc

    Ok here is a strange thing. I sort of solved the problem...

    When I write:

    _main.cpp
    #include stuff and all GL stuff
    #include "ship.h"
    ..
    vector <ship> vec;
    int i
    etc.
    ..
    main {
    Ship A("Texture t");
    vec.push_back(A)
    }

    renderScene{
    ...
    vec.DrawShip();
    }

    and everything works just fine and the textures get loaded and bound correctly.

    However my first straight forward approach wouldnt work. It shows just a blank screen because the Texture doesnt get assigned but everything else and all other member data seems to be okay when I write

    _main.cpp
    include stuff and all GL stuff
    #include "ship.h"
    ..
    Ship A("Texture t");
    int i
    etc.
    ..

    renderScene{
    ...
    A.DrawShip();
    }


    Any suggestions why the second code wouldn't work? I use for my textures a tgaloader. The following two lines are in my Ship class constructor.

    itsTexture = tgaLoadAndBind( "texture_t.tga" , TGA_COMPRESS )
    and then I call
    glBindTexture ( GL_TEXTURE_2D, itsTexture);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured