Hi,
I'm making this video game in C.
The game contains a player, enemies and bullets. These objects,
which are arrays of structures, are initialized, updated and drawn
in a game loop. Now I am unsure on where to declare these things.
I could declare them in main() like this
#define NUM_ENEMIES 10
#define NUM_BULLETS 20
int main(void)
{
struct *player player1;
struct enemy enemies[NUM_ENEMIES];
struct bullet bullets[NUM_BULLETS];
init_player(pla yer1);
init_enemies(en emies, NUM_ENEMIES);
init_bullets(bu llets, NUM_BULLETS);
while (1) {
update_...
draw_...
}
}
Or, I could declare them globally and make main() a little "cleaner"
by calling the functions with void parameters and let the functions
handle the objects and the count by themselves.
I don't know which one to choose, the second way seems a little
more modular, since I move the internal (should it be internal?)
stuff out of main.
But OTOH, I've read that local variables are better
than global. But again, if I put the handling functions in an other
file, and have static linkage, they won't be global, right?
I would be grateful if someone could give me some points on how
to do.
Thanks! 18 2268
"jaso" <as@email.com > wrote in message Hi, I'm making this video game in C. The game contains a player, enemies and bullets. These objects, which are arrays of structures, are initialized, updated and drawn in a game loop. Now I am unsure on where to declare these things.
I could declare them in main() like this
#define NUM_ENEMIES 10 #define NUM_BULLETS 20
int main(void) { struct *player player1; struct enemy enemies[NUM_ENEMIES]; struct bullet bullets[NUM_BULLETS];
init_player(pla yer1); init_enemies(en emies, NUM_ENEMIES); init_bullets(bu llets, NUM_BULLETS);
while (1) { update_... draw_... } }
Or, I could declare them globally and make main() a little "cleaner" by calling the functions with void parameters and let the functions handle the objects and the count by themselves.
I don't know which one to choose, the second way seems a little more modular, since I move the internal (should it be internal?) stuff out of main. But OTOH, I've read that local variables are better than global. But again, if I put the handling functions in an other file, and have static linkage, they won't be global, right?
I would be grateful if someone could give me some points on how to do.
This is a common problem.
The game needs a "world" and many quite low level functions need to operate
on this world.
For instance the gas-cloud baddy doesn't do its stuff whilst the hurricane
baddy is on screen, or else you need the game to handle gas clouds being
blow about by high winds. This sort of consideration destroys modularity.
So the answer is to declare a single header file, "game.h" and put all your
structures into that. Then have a single pointer to a "GAME" instance. It
doesn't really matter if this is global or passed to every function. Every
file include "game.h".
--
Buy my book 12 Common Atheist Arguments (refuted)
$1.25 download or $7.20 paper, available www.lulu.com/bgy1mm
Malcolm skrev: This is a common problem. The game needs a "world" and many quite low level functions need to operate on this world. For instance the gas-cloud baddy doesn't do its stuff whilst the hurricane baddy is on screen, or else you need the game to handle gas clouds being blow about by high winds. This sort of consideration destroys modularity.
So the answer is to declare a single header file, "game.h" and put all your structures into that. Then have a single pointer to a "GAME" instance. It doesn't really matter if this is global or passed to every function. Every file include "game.h".
So you mean that I have a pointer to my world, and the
functions operate on this "GAME" pointer, am I getting this right?
What kind of type is a "GAME" instance?
On 2006-06-04, jaso <as@email.com > wrote: Malcolm skrev: This is a common problem. The game needs a "world" and many quite low level functions need to operate on this world. For instance the gas-cloud baddy doesn't do its stuff whilst the hurricane baddy is on screen, or else you need the game to handle gas clouds being blow about by high winds. This sort of consideration destroys modularity.
So the answer is to declare a single header file, "game.h" and put all your structures into that. Then have a single pointer to a "GAME" instance. It doesn't really matter if this is global or passed to every function. Every file include "game.h".
So you mean that I have a pointer to my world, and the functions operate on this "GAME" pointer, am I getting this right? What kind of type is a "GAME" instance?
Create a GAME struct (I will call it "world" because I think that's what you
want):
struct world
{
/* All necessary world data here */
};
struct world *current_level = load_first_leve l();
The /only/ global you will need is a pointer to a world struct. This pointer
would be changed only by functions which move you from level to level, and any
other global variables would be accessed as such: current_level->global_var_her e
Having a struct or two to contain all globals makes fairly clean code.
--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
If we would just let the poachers into the zoo, we'd
have less squashed people and more fancy pianos!
Andrew Poelstra wrote:
.... snip ... Create a GAME struct (I will call it "world" because I think that's what you want):
struct world { /* All necessary world data here */ };
Ridiculous. Well designed to make the end result unmaintainable.
--
"Our enemies are innovative and resourceful, and so are we.
They never stop thinking about new ways to harm our country
and our people, and neither do we." -- G. W. Bush.
"The people can always be brought to the bidding of the
leaders. All you have to do is tell them they are being
attacked and denounce the pacifists for lack of patriotism
and exposing the country to danger. It works the same way
in any country." --Hermann Goering.
CBFalconer wrote: Andrew Poelstra wrote: ... snip ... Create a GAME struct (I will call it "world" because I think that's what you want):
struct world { /* All necessary world data here */ };
Ridiculous. Well designed to make the end result unmaintainable.
How come? Why will it be unmaintainable?
Could you please tell me how to design it well? It would
be nice to know beforehand, so it won't be a nightmare to fix it
when I understand I took the wrong decision.
Thank you
jaso said: CBFalconer wrote: Andrew Poelstra wrote: ... snip ... Create a GAME struct (I will call it "world" because I think that's what you want):
struct world { /* All necessary world data here */ };
Ridiculous. Well designed to make the end result unmaintainable.
How come? Why will it be unmaintainable?
What usually happens is that the coupling between modules is drastically
increased by the existence of file scope data.
You're a maintenance programmer. You're fairly new to the project. You are
good at your job, but you don't know the code base terribly well. You're
debugging. This is a nasty one, you're really stuck, and you're stepping
through the code. All is well so far, and you get to this bit:
world.foo = 42;
seemingly_innoc ent_function();
baz = (world.foo + 117) / 35;
It seems obvious that baz should now have the value 4, but your debugger
tells you it has the value 6.
Finally, you twig that seemingly_innoc ent_function() must have updated
world.foo somehow. So you go digging around, and discover to your horror
that seemingly_innoc ent_function() is a wrapper around several thousand
lines. Nothing for it but to dig in and look for references to world.foo -
or maybe there's a world.p that is used to point to any of various world.*
objects - so maybe you have to look for that, too, and the whole mess gets
very wearying indeed. And you will find it almost impossible to decouple
seemingly_innoc ent_function() for use in other programs.
When you pass the data a function needs via its parameters, it makes it far
easier to track changes to the values of objects, and far easier to
separate out code for later re-use in a different context.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Richard Heathfield wrote: [snip]
When you pass the data a function needs via its parameters, it makes
it far easier to track changes to the values of objects, and far easier to separate out code for later re-use in a different context.
Ok, now I understand why to avoid the world structure. Thanks!
So, generally, the best way is to declare all structures separately in a
"high layer" like the game loop, and to pass all needed structures
all the way down to the lowest level functions?
jaso said: Richard Heathfield wrote: > [snip] > > When you pass the data a function needs via its parameters, it makes it far > easier to track changes to the values of objects, and far easier to > separate out code for later re-use in a different context. >
Ok, now I understand why to avoid the world structure. Thanks! So, generally, the best way is to declare all structures separately in a "high layer" like the game loop, and to pass all needed structures all the way down to the lowest level functions?
Yes, sort of. Ideally, each function should only be given the information it
actually needs for doing its job. So try to arrange your structures in such
a way as to minimise the amount of superfluous information you hand to your
functions. And localise your data as much as possible. If you must define a
structure in main(), okay - if you must, you must. But at least investigate
the possibility of making it more local than that.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
In article <Zo************ ********@bt.com >,
Richard Heathfield <in*****@invali d.invalid> wrote: Yes, sort of. Ideally, each function should only be given the information it actually needs for doing its job. So try to arrange your structures in such a way as to minimise the amount of superfluous information you hand to your functions. And localise your data as much as possible.
None of this means that you shouldn't have a structure that gives
access to the entire state. It may well be useful to do this so that
you can easily pass it to a function that, say, saves the game. But
don't make it a monolithic structure - have it contain pointers to
other structures that make sense in themselves: lists of objects or
people for example.
And as has been said, don't make it a global variable. Apart from
the possibility of hidden modification, maybe one day you will want to
make a multi-player version of the game in which there are several
such structures.
-- Richard This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: BlackHawke |
last post by:
My Name is Nick Soutter, I am the owner of a small game company, Aepox Games
(We're in the middle of a name change from "Lamar Games"),
www.lamargames.net.
Our first commercial game, Andromeda Online (www.andromedaonline.net) is
going into beta soon. It runs on an evaluation edition of SQL Server 2000
(our intention is, when it launches, we earn the money to buy a copy before
the evaluation expires).
|
by: theodp |
last post by:
--> From http://www.techdirt.com/articles/20040406/1349225.shtml
Microsoft Patents Saving The Name Of A Game
Contributed by Mike on Tuesday, April 6th, 2004 @ 01:49PM
from the yeah,-that's-non-obvious dept.
theodp writes "As if there weren't enough dodgy patents, here's an
excerpt from one granted to Microsoft Tuesday for a 'Method and
apparatus for displaying information regarding stored data in a gaming
system': 'When saving a game,...
|
by: the_philospher |
last post by:
I am a beginner programmer learning c++. i have read some useful
information about programming games (which is my goal) on gamedev.net,
particularly "how do i program games"
http://www.gamedev.net/reference/design/features/makegames/.
the article suggests first programming a tetris clone, then moving on
to "breakout", "pacman", and a super mario style game..
My question is, how long should it realistically take me to be able to...
|
by: enki |
last post by:
I am writing a game and I am having trouble with moving the character
on the map.
Here is what I have right now. It involves win32 programming but that
not my problem. I would like some suggestions how I can make my code
better. If you need more code I will post it. I am in early
devlopment of the game.
map.h:
|
by: Brandon J. Van Every |
last post by:
Anyone know of any "good" open source C# game projects out there? Something
that actually has a game engine and some content done, so I can just fiddle
with it and do interesting / goofy things. I hesitate to state genre
preferences. I've been all over SourceForge and it's slim pickings in C#
land, at least for projects that have actually gotten something done. I'll
be doing my last round of sifting and searching tonight, then I'm...
| |
by: Valerie |
last post by:
Ok. Help. the quarter is ending soon and I have no interest in creating
games. I think that's why i have a mental block on this particular lab
assignment.
I'm using the same book as Steven Smith is using when One Handed Man helped
him.
And, I don't even know where to continue.
The application is a tic tac toe game.
|
by: Chu |
last post by:
Hello All-
I'm writing a game using asp.net (c#). The game is a browser-based Role
Playing Game. I'm trying to determine the best way to design the game
with scalability in mind.
The Player class will hold info about the player, such as hit points,
weapons and armor that are equipped, and other stats specific to the
player. Each page on the site will require some or all of the info
stored in the player object.
|
by: v4vijayakumar |
last post by:
In computer based, two player, board games, how to make computer play?
Are there any formal ways to _teach_ computer, to choose best possible
move?
I know this is kind of off-topic here. Please redirect me, if there
are more appropriate newsgroup.
Many thanks.
|
by: swasa |
last post by:
We at AJ Aquare are engaged in design and development of PC-based,
online and mobile games. Our Game designers are passionate towards
creating games that people long to have. Our designs can drive
millions of players to your site.
sources: http://www.ajsquare.com/games/game_design.php?game=design
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |