validity/visibility of vars

Discussion in 'C++' started by Michael Sgier, Oct 4, 2005.

  1. Hi folks

    class X
    {
    private:
    CTexture *modelTex; // texture data
    }


    CTexture* X::Load()
    {
    return modelTex;
    }


    program doing other things and then returning and calling:

    anotherfunctionofclassX{
    glBindTexture(GL_TEXTURE_2D, modelTex->texID);
    }

    here modelTex doesn't produce an error but it's empty. Why I thought
    that in classes you don't need to pass vars. but even after changing Load()
    to return a pointer to CTexture, when I return to the class modelTex is
    empty. What has happened to modelTex? What do i need to do?
    THANKS and regards
    Michael
    Michael Sgier, Oct 4, 2005
    #1
    1. Advertising

  2. Michael Sgier wrote:
    >
    > Hi folks
    >
    > class X
    > {
    > private:
    > CTexture *modelTex; // texture data
    > }
    >
    > CTexture* X::Load()
    > {
    > return modelTex;
    > }
    >
    > program doing other things and then returning and calling:
    >
    > anotherfunctionofclassX{
    > glBindTexture(GL_TEXTURE_2D, modelTex->texID);
    > }
    >
    > here modelTex doesn't produce an error but it's empty. Why I thought
    > that in classes you don't need to pass vars. but even after changing Load()
    > to return a pointer to CTexture, when I return to the class modelTex is
    > empty. What has happened to modelTex? What do i need to do?
    > THANKS and regards
    > Michael


    It is impossible to figure out what went wrong using only those snippets.
    What does 'modelTex is empty' mean? Is it a 0 pointer?
    The problem is most likely in the code you didn't show, somewhere in
    "program doing other things and then returning and calling"


    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Oct 4, 2005
    #2
    1. Advertising

  3. Hello Karl
    nobody's perfect as i'm still only trying to understand C++. It's a
    basic question on how to deal with variables.
    I guess yes modelTex is now a O pointer. It exists but has all 0 values.
    It exists because it was declared in x class? In the Load function of X
    it got values assigned..why those values were lost? Is that
    misconception of basics or somewhere a fault in the program. Basically
    how do i need to proceed to keep the assigned values to modelTex, for
    later use in the program?
    THANKS and regards
    Michael
    Michael Sgier, Oct 4, 2005
    #3
  4. Michael Sgier wrote:
    >
    > Hello Karl
    > nobody's perfect as i'm still only trying to understand C++. It's a
    > basic question on how to deal with variables.
    > I guess yes modelTex is now a O pointer. It exists but has all 0 values.
    > It exists because it was declared in x class? In the Load function of X
    > it got values assigned..why those values were lost?


    I don't know, because I C A N N O T S E E Y O U R C O D E.

    > Is that
    > misconception of basics or somewhere a fault in the program.


    I don't know, because I C A N N O T S E E Y O U R C O D E.

    Is that so hard to grasp?

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Oct 4, 2005
    #4
  5. Hello
    here's the code i believe to be relevant.
    THANKS for you help.
    Regards Michael


    class CTexture
    {
    public:
    CTexture* LoadTexture(char* filename);
    unsigned int texID;
    }


    class CMD2Model : public CObject
    {
    public:
    CTexture *modelTex; // texture data
    CTexture *tex;
    }


    in md2.cpp:
    //here i want to load the texture. As i've a instance of modelTex im using
    //this? Can i do that better? tex also is declared the same as modelTex
    // this looks weird to me and might be wrong but i want to know about the
    //validity of modelTex too.

    CTexture* CMD2Model::Load(char *modelFile, char *skinFile)
    {
    tex = modelTex->LoadTexture(skinFile);
    modelTex = tex;
    ...
    st.s = (float)stPtr.s / (float)modelTex->width;
    st.t = (float)stPtr.t / (float)modelTex->height;;
    ...
    //so far everything is fine then i return modelTex for further use?
    Where
    // will modelTex be?

    return modelTex;
    }


    this is in texture.cpp:
    /* Texturen laden & generieren */
    CTexture* CTexture::LoadTexture(char* filename)
    {
    CTexture *thisTexture;
    SDL_Surface* img;
    img = SDL_LoadBMP(filename);
    if (!img) {
    printf("Error: %s\n", SDL_GetError());
    exit(1);
    }
    // store BMP data in texture
    // the next line doesn't work. Why?
    // thisTexture->data = img->pixels;

    // if (thisTexture->data == NULL)

    // {

    // free(thisTexture);

    // return NULL;

    // }



    // store texture information

    thisTexture->width = img->w;

    thisTexture->height = img->h;

    thisTexture->palette = NULL;

    thisTexture->scaledHeight = 0;

    thisTexture->scaledWidth = 0;

    // thisTexture->textureType = BMP;
    // the next 2lines look fine to me in the debugger
    glGenTextures(1, &thisTexture->texID);
    glBindTexture(GL_TEXTURE_2D, thisTexture->texID);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

    return thisTexture;
    }


    in entity.cpp:
    //here i want to bind the texture modelTex->texID) but it's 0. it exists
    as declared but all null.

    AnimateModel(0, 39, deltaT*animSpeed);


    that function is again in md2.cpp:
    // desc: animates the model between the keyframes startFrame and endFrame
    int CMD2Model::AnimateModel(int startFrame, int endFrame, float percent)
    {
    ...
    glBindTexture(GL_TEXTURE_2D, modelTex->texID); // here modelTex =
    0 and should not be so
    glBegin(GL_TRIANGLES);
    ...
    return 0;
    }
    Michael Sgier, Oct 4, 2005
    #5
  6. Hi again
    now i reached to here.
    tex is a instance of class CTexture then I want to submit tex as
    parameter to the
    animate function like this:

    the header declaration:
    int AnimateModel(int startFrame, int endFrame, float percent, CTexture
    *tex);

    the function call:
    AnimateModel(0, 39, deltaT*animSpeed, tex);

    here the function and again tex has dissapeared...
    int CMD2Model::AnimateModel(int startFrame, int endFrame, float percent,
    CTexture *tex)
    {
    ...
    glBindTexture(GL_TEXTURE_2D, tex->texID);
    ...

    Thanks for clearing my missconception.
    Regards Michael

    PS: to Karl
    Now you can "S E E Y O U R C O D E." and no answer..
    Is that so hard to grasp?
    Michael Sgier, Oct 5, 2005
    #6
  7. "Michael Sgier" <> wrote in message
    news:43429a2d$0$1151$...

    > here's the code i believe to be relevant.


    I think I see one of your problems.

    [...]

    > class CMD2Model : public CObject
    > {
    > public:
    > CTexture *modelTex; // texture data
    > CTexture *tex;


    That's the definition of tex...

    > }
    >
    >
    > in md2.cpp:
    > //here i want to load the texture. As i've a instance of modelTex im using
    > //this? Can i do that better? tex also is declared the same as modelTex
    > // this looks weird to me and might be wrong but i want to know about the
    > //validity of modelTex too.
    >
    > CTexture* CMD2Model::Load(char *modelFile, char *skinFile)
    > {
    > tex = modelTex->LoadTexture(skinFile);


    There you set it to what LoadTexture returns. Let's hope that LoadTexture
    returns a pointer to a valid object...

    [...]

    > /* Texturen laden & generieren */
    > CTexture* CTexture::LoadTexture(char* filename)
    > {
    > CTexture *thisTexture;


    thisTexture is an uninitialized object at this point. (Points to no object.)

    You should make it point to a dynamic object:

    CTexture * thisTexture = new CTexture();

    [Note: You should better return a smart pointer.]

    > SDL_Surface* img;
    > img = SDL_LoadBMP(filename);
    > if (!img) {
    > printf("Error: %s\n", SDL_GetError());
    > exit(1);
    > }
    > // store BMP data in texture
    > // the next line doesn't work. Why?
    > // thisTexture->data = img->pixels;


    The line above doesn't work in your code because thisTexture is
    uninitialized. You can't access uninitialized objects. With my change, it
    should work.

    [...]

    > return thisTexture;
    > }


    Now you will have issues of object ownerships. You need to make sure that
    what LoadTexture returns is owned by the caller. One way of doing that is
    returning smart pointers instead of plain ones.

    Probably you will need a CMD2Model destructor now, so that the allocated
    CTexture object can be cleaned-up properly:

    CMD2Model::~CMD2Model()
    {
    delete tex;
    }

    If you agree, to preserve symmetry, you might want to write a CMD2Model
    constructor as well...

    Also, you will need to take care of the operator= and the copy
    constructor... (Read about "the rule of three.")

    Ali
    =?iso-8859-1?Q?Ali_=C7ehreli?=, Oct 5, 2005
    #7
  8. Hello Ali
    that was my mistake. I've forgotten that i've:
    // CMD2Model constructor
    CMD2Model::CMD2Model()
    {
    modelTex = new CTexture; // skin/texture
    ....

    As i'm new to C++ my question might be very basic. In the
    CTexture::LoadTexture function I assign values to modelTex.
    There i want to return modelTex for later use. The program
    is doing other things like X-initialising etc and then it
    returns to CMD2Model::AnimateModel which is in the same class.
    I thought that modelTex would remain accessible in this class
    but apparently even be returning modelTex its gone. And i only
    wounder where modelTex has gone? And how i can use it in the
    animate function. Well this surely is very basic...but i haven't
    found it in my book so far.
    So THANKS and regards
    Michael
    PS: oops just remarked that modelTex was returned in the CTexture
    class and I want to use it later in the md2 class. But anyway the
    question is the same. i know how to pass it on but it makes obviously
    no sense to pass it on over 50 or so functions.
    Michael Sgier, Oct 5, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon

    app vars and cache vars

    Jon, Dec 14, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    381
  2. Martin Burger
    Replies:
    3
    Views:
    546
    Roland
    Jun 30, 2005
  3. Steven T. Hatton
    Replies:
    1
    Views:
    407
    Steven T. Hatton
    Dec 13, 2006
  4. Linuxguy123
    Replies:
    7
    Views:
    670
    Paddy O'Loughlin
    Feb 20, 2009
  5. caccolangrifata
    Replies:
    18
    Views:
    385
    Chris Torek
    Jul 22, 2011
Loading...

Share This Page