how does the engine treat the global variable.

Started by
4 comments, last by Dave Hunt 18 years, 9 months ago
I want to do a engine.but I have no idea how to deal the global variable IN A ENGINE. in D3D,it seam no global variable.we need create it by ourself. like Direct3DDevice9.in D3D no something like g_device. but in higher - level product - engine. we still create global variable by ourself? if yes,it seem the engine is not good to use.we still create many low - level thing. so in engine, we will have many global variable.and these variable will be not visible to user? or something else solution?
Advertisement
I understand that you think the engine does low level stuff which is not nessecary for a programmer who uses the engine too see, so the programmer should only see the variables there's a good reason for him/her to use?

One way to do this is by encapsulating the engine in a class. A class can have global variables within it which are only global to the member functions of the class (the functions which are defined within the class). The class can also have some variables which are accessible to functions outside (public variables) it by writing something like "className.variableName". I assume you're using C++? In that case here's an example:

//define a classclass Engine{private: //the following varaibles/functions are only accessible within "Engine"	//private member variables:	int numberOfDevices;	int bpp;	//bits per pixel	//private member functions	void AddNewDevice() {numberOfDevices += 1;}public:	//the following variables/functions are accessible outside the class	//public member variables	unsigned int framesPerSecond;	//public member functions	Engine()	{	//this is a class "constructor", it's good for setting default values		numberOfDevices = 0;		bpp = 1;		framesPerSecond = 0;	}	int Initialize(int bitsPerPixel)	{		//set a private variable if it's valid due for its later usage		if (bitsPerPixel > 0 && bitsPerPixel <= 32)			bpp = bitsPerPixel;		else			return -1;	//return -1 to indicate there was an error		//call private function		AddNewDevice();		//everything went OK - return 0		return 0;	}};	//the class definition ends here//declare an instance of the class we defined, the constructor is calledEngine g_engine;int main(){	/*	the members of the class are accessed with the name of the class,		a dot and the name of the member*/	g_engine.Initialize(32);	//access the public variable of the class	g_engine.framesPerSecond = 60;	/*	POINT: now, if we were to access the private member variable bpp		the compiler wouldn't compile the program since it's not allowed*/	int iWantBppButCannot;	//the following line is not allowed!	//iWantBppButCannot = g_engine.bpp;	return 0;}


One can also go even further and make no member variables accessible (private) and make functions that modify or return the values of the variables follow specified rules (like the set value can only be in a special interval or something like that).

I also suggest you read more about this since it's quite large subject and it can help the programmer a lot. Some links:
http://www.cplusplus.com/doc/tutorial/tut4-1.html
http://www.cprogramming.com/tutorial/lesson12.html
thanks for you.
Puting variable to class is a good way.

I see the Direct3D has no variable.so maybe it put it to class.

but no these kinds of class : class Direct3D

so how Direct3D eliminate the global variable in lib.
Please speak readable english.
I cant really understand what your saying, you think DirectX has its own variables for you to use? It doesnt...its just a media lib, you control the variables and such.

BTW, I had a similar question about the global variables in an engine, if I have a DLL and I need some info kept there, if the program isnt in a function of the DLL's, will the DLL be out of scope and the variables be wiped? (im also trying to keep the variables away from the user)
DONT LET THEM DISCRIMINATE! BRING BACK THE BLACK!
I just noticed that derek has written only 172 replies but created 238 new threads so far...
Quote:Original post by Anonymous Poster
I just noticed that derek has written only 172 replies but created 238 new threads so far...


I think he believes the forums here are his personal diary. Here is an example of what I mean.

This topic is closed to new replies.

Advertisement