473,406 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

validity/visibility of vars

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
7 1452
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
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
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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: james | last post by:
I am new to php and need some help getting the session variables into include files. (after-thought, Sorry for the drawn out post but I really, really need help....;) Here's what I'm doing.. ...
3
by: Jukka K. Korpela | last post by:
I have noticed that the meaning of visibility: collapse has been discussed on different forums, but with no consensus on what it really means. Besides, implementations differ. The specification...
12
by: lawrence | last post by:
The following function correctly makes everything invisible but then fails to turn the one chosen DIV back to visible. I imagine I'm getting the syntax of the variable wrong? I've tried this with...
4
by: lawrence | last post by:
Can anyone tell me why this code works in Netscape 7.1 but not in IE??? <SCRIPT type='text/javascript'> function makeVisible(nameOfDiv) {...
8
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an...
3
by: Jon | last post by:
I have a couple of tables I want to load into a dataset and keep around pretty much forever, although they will need to be refreshed every so often. I can either put the dataset into an...
6
by: Protoman | last post by:
I'm writing a program to calc truth tables of arguments to prove that they are logically valid. Currently, I have to tell the program to print a truth table, and check it by hand to prove it's...
9
by: Andy Dingley | last post by:
Here's a chunk of a longer piece of punditry I'm working on, re: the choices between doctypes for authoring and the State of the Union for validity. I've got a bucketload of nasty code and a bunch...
19
RMWChaos
by: RMWChaos | last post by:
Previously, I had used independent JSON lists in my code, where the lists were part of separate scripts. Because this method did not support reuse of a script without modification, I decided to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.