Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

JwayneT

Hey, do you guys keep DX stuff global?

This topic is 6438 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In all of MS''s DX examples, like for DDraw. Primary Surfaces and stuff are all global. I was wondering if everyone did that, or if MS did it just for time constraints or something.

Why is it called a hot water heater? Isn't it cold when it goes in the tank?

-=CF=-

Share this post


Link to post
Share on other sites
Advertisement
I got the answer to this from lamothe''s book. It''s because globals are faster then sending in and out variables. Yes everyone does do this. Every single source I''ve seen they are all global, so far.

Share this post


Link to post
Share on other sites
Well, those MS DDraw examples suck anyway. But I keep them global. In fact, I keep almost everything, that is used in many places, global. I''ve got to get rid of that habit .

-Hans [home page] [e-mail]

Share this post


Link to post
Share on other sites
In C++, I tend to create an object to represent the screen etc... so I store the Interface pointers as member variables of the class responsible for them. But C++ is evil... I know. =)

Share this post


Link to post
Share on other sites
With games programming, global variables are a must. They''re actually quicker than declaring a new variable everytime a function runs.

In normal application programming, global variables are a bad habit, but game programming is completely different.

Share this post


Link to post
Share on other sites
Microsoft puts everything global because it keeps things simple for the samples. You''d never do this in real life. If your using C, then maybe but its still better practice to just pass a ptr around. Though i would never recommend C, its such an ugly language. There''s virtually no way to protect your data in C.

I keep everything in classes.

BTW, making everything global is not going to give you any noticable speed. Maybe a few unnoticable nano seconds. But i doubt even 1 FPS more.

Global variables do not belong in games or applications. They are dangerous, unsafe, and not good programming practice. If you are in a position where you must use global variables it is often do to poor design. Of course in C(there''s ugly C again) you are sometimes forced to use them no matter what, but if its a well designed C program there should be minimal globals.

Ifyour working with a team, globals are a big no, no. Nobody wants to maintain code with global variables all over the place.

Global variables don''t make for reuseable code, whereas classes and a robust C-library do.

ECKILLER

Share this post


Link to post
Share on other sites
I completely agree with everything ECKILLER said, except for the whole "c is ugly" statement , even tho I am a C++ advocate.


- Houdini

Share this post


Link to post
Share on other sites
Well, I mostly agree with the "globals==bad" thing. I, however, consistently code one global variable:

ifstream log("out.err");

I then put this in my shared header file:

extern ifstream log;

And finally, right before WinMain() (or main() or whatever) finishes:

log.close();

This way you can log anything you want from anywhere you want. You could also do a conditional compile depending on whether or not DEBUG is defined. If someone has a better way to log stuff to a file from EVERYWHERE, I would love to hear it. Please don''t say that ALL of my functions should have an ifstream& as a parameter.

Maybe I can countenance only passing it to the contructor of every class and then keeping a referrence to it, but that seems kinda silly

Why is this one instance of a global bad?

Share this post


Link to post
Share on other sites
You can hide data in C. Put stuff in different modules and it becomes hidden if you do not declare it as an extern in the H file. If you have a lot of things and you are constantly passing them around I''m sure there would be a speed hit in terms of things being passed onto the stack.



Please state the nature of the debugging emergency.


sharewaregames.20m.com

Share this post


Link to post
Share on other sites
--------------------------------------------
Microsoft puts everything global because it keeps things simple for the samples. You''d never do this in real life. If your using C, then maybe but its still better practice to just pass a ptr around. Though i would never recommend C, its such an ugly language. There''s virtually no way to protect your data in C.

I keep everything in classes.

BTW, making everything global is not going to give you any noticable speed. Maybe a few unnoticable nano seconds. But i doubt even 1 FPS more.

Global variables do not belong in games or applications. They are dangerous, unsafe, and not good programming practice. If you are in a position where you must use global variables it is often do to poor design. Of course in C(there''s ugly C again) you are sometimes forced to use them no matter what, but if its a well designed C program there should be minimal globals.

Ifyour working with a team, globals are a big no, no. Nobody wants to maintain code with global variables all over the place.

Global variables don''t make for reuseable code, whereas classes and a robust C-library do.

ECKILLER
--------------------------------------------

Erm, if C is an ugly language, than C++ is a childish hack put around it. But no, neither are. Of course you can do OOP in C, and you can do very ugly things in C++, or the opposite. Please keep your offensive, insult starting opinions to yourself.

Now that I got that out of my system:

Global variables for DX is probably the best thing to do, unless you have a really good design specification, you may find yourself needing the DX vars in weird places. Simply put, totally eliminating globals from any program would be ridiculous, considering classes are basically global variables anyway. So, if you are gonna wrap the DX functionality into a library, you shouldn''t use globals, but if you are making a Quake-like engine(insanely fast), globals are the way to go.

-----------------------------

A wise man once said "A person with half a clue is more dangerous than a person with or without one."

The Micro$haft BSOD T-Shirt

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!