I'm reworking a 2d engine for my game. I was wondering how to declare variables that I can use amost several files. For instance, like xres and yres vars. I was looking at the voxlap engine as an example. I figured how to do external functions to help simplify the game code from the engine code. I noticed that you could assign the xres and yres vars by just using their name without using a function or referencing a class to do it. How do I do it?
TX at
blah.c:
long whatever;
blah2.c:
extern long whatever;
Make sense now?
Silhouette of a Can at
no
maniac1701 at
it depends on if your using c or c++
with c put your variables in a .h file and include that file with every thing that needs to access it
global.h
int xres, yres;
other.c
procedure()
{
if(xres==320)
{
.....
}
}
or if your using c++ it must be done a little differently and you need a src file to declare the variable in addition to a fake declareation in the h file
global.h
#ifndef global_h
#define global_h 1
extern int xres, yres;
#endif
global.cc
int xres, yres;
other.cc
procedure()
{
if(xres==320)
{
.....
}
}
i hope this helps if your interested im working on a 2d engine myself and it already has a working battleship game included and you can see how i used this my self. src and bin available here: http://et791.ni.utoledo.edu/~awillia19/Gecko.zip
Silhouette of a Can at
thanks! :D
Shodan at
Urgs!
Global variables are bad coding style! 8)
GothOtaku at
Shodan said
Urgs!
Global variables are bad coding style! 8)
But oftentimes necessary.
maniac1701 at
in c there are many times they're neccary. for example suppose i need 2 functions and both need to access 10 variables from each other do you really want to make your function accept 10 arguments i certainly don't
on the other hand carefull coding in c++ eliminates the need for global variables b/c all related functions go into the same class and it has its own set of variables. but even then global variables are usefull. it one of those things that as long as your carefull its ok to use.
masterlee at
maniac1701 said
in c there are many times they're neccary. for example suppose i need 2 functions and both need to access 10 variables from each other do you really want to make your function accept 10 arguments i certainly don't
Waht about using a struct and passing a pointer as parameter?
JonoF at
Global variables are hardly bad style or something to be avoided at all costs, but the programmer should really know why (s)he is using one. Globals are basically used for persistent or shared state information across multiple functions. In C, some people like to make global variables static to a module of code and provide functions to read or write those variables, which is achieving the same sort of effect as private data with public accessor function in a C++ class. It's just a question of what you're trying to achieve.
Jonathon
maniac1701 at
you didn't look at my whole post the example of 10 variables to a function was refering to c only not c++. if you look at that post again look at the 2nd paragraph which refers to c++ and i mentioned that a class is a convienient way to avoid this (struct/class are essentially the same thing except for the default private/public settings)
edit:
oh wait i see what your saying now. that will work in some cases but if you have unrelated information it might not be convienient to use a struct for 1 function.
masterlee said
maniac1701 said
in c there are many times they're neccary. for example suppose i need 2 functions and both need to access 10 variables from each other do you really want to make your function accept 10 arguments i certainly don't
Waht about using a struct and passing a pointer as parameter?
BarrenSoul at
Shodan said
Global variables are bad coding style!
no such thing, wether a coding style is bad or not is defined by soour society AKA not a real definition :) it's just not "standard" coding style :) (wanna know something worse? using goto in C++ >:) )
maniac1701 at
what do you have against c++? its a way to organize similar functions and data. for example my bmp class. i say:
bmp b;
b.load("filename.bmp");
b.draw(0,0);
there that draws a bmp file BUT in c it looks more like this:
notice how i have to open the file 2x in this example (less efficiant) where as the c++ code i only open it once. also in the c++ method i don't have to use the complicated malloc statement (its done in the class level but its only done once in ALL of my code)
i conclude that for this example c++ is supurior to c
edit:
wow with smileys enabled the code includes smiley faces ( bpp/8); turns into bpp/smiley; )
I hope you realize each scanwidth is DWord aligned. :P
BarrenSoul at
GASP you didn't know that there are predefined stucts for bmpFH and bmpIH???? ouch :) they are in windows.h structs make life a breeze :) (take a look at my source code in the png comparrison, RGB is a custom struct of 3 bytes R,G,B, and yes microsoft has a predefined one for this also known as RGBTRIPLE, which I could use but I like RGB, mabe I should use typedef... well my code needs work so :D )
typedef struct tagBITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER, *PBITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER{
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER, *PBITMAPINFOHEADER;
i was attempting to give an example where i feel that c++ is better than c (besides in the end there both converted to assembly anyway) but the code clip i showed you i never said what was in the fucntions themselves
this function would set the width and height variables based on the header. and i have code to read the whole header all at once where each variable is the correct size and everything. then i extract the information i need (width height bpp) and discard the rest
BarrenSoul at
wo there little man lets not start a holy war ok? C++ can have C inside of it, so in some ways this makes C++ better, but C++ tends to be a smidge slower if not implimented properly. each has it's own trade offs lets leave it at that and say that you should choose the right langauge for the job :) (oh and FORTRAN is faster then C++, but C++ is catching up)
JonoF at
C++ can be just as fast as C so long as you don't enable or use things that make it slow, like exceptions and run-time type identification for instance. Earlier compilers weren't as efficient at optimising C++ code compared with C, but I don't believe that's a problem these days, so you can use what makes you happy.
Jonathon
BarrenSoul at
Like I said, it all comes down to how you implement them :) you could do something in C++ that's more efficient then the corroponding C code (I can't think of something of the top of my head). I'd preffer to keep them on the same playing field :)
maniac1701 at
yeah not to mention on our ghz bases machines it doesn't make that much of a difference anymore where as 5 years ago on our ? * 100 mhz based machines it did
BarrenSoul at
no kidding, these days those nice 3D games are being produced so EASYILY becaue the amatureish now don't have to worry bout optimizing cause they have extra clock cycles to throw at it :D on the TI-83+ optimizing is priority as is space since it only runs at 6mhz :)