By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,089 Members | 1,152 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,089 IT Pros & Developers. It's quick & easy.

validity/visibility of vars

P: n/a
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
Oct 4 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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
kb******@gascad.at
Oct 4 '05 #2

P: n/a
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
Oct 4 '05 #3

P: n/a
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
kb******@gascad.at
Oct 4 '05 #4

P: n/a
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[i].s = (float)stPtr[i].s / (float)modelTex->width;
st[i].t = (float)stPtr[i].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_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,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;
}
Oct 4 '05 #5

P: n/a
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?
Oct 5 '05 #6

P: n/a
"Michael Sgier" <sg***@nospam.ch> wrote in message
news:43**********************@news.sunrise.ch...
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

Oct 5 '05 #7

P: n/a
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.
Oct 5 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.