Game Programming and Development Tools

2D Game Development Snippets (C/C++) – dartsman

dartsman

Member

Posts: 484
From: Queensland, Australia
Registered: 03-16-2006
There seems to be a fair bit of conversation over 2D topics, and I just thought I'd start this thread for people to ask questions for others to come along and answer them (providing an explanation and source snippet).

Lets keep this C/C++ only, as for one, I don't really know anything else, and for 2, it seems that people who are asking about 2D libraries/topics are generally using C or C++.

To start it off, I'd like to show the fundamental game loop:

initialize();

while (game running)
{
logic();
render();
}

shutdown();


- Initialize()
* Here is were you setup your game. This is usually things like creating the managers that your game will use to even just creating the blank tic-tac-toe 3x3 array.

- while (game running)
* This is our 'game loop', we want to continually loop through this as long as the game has not met a 'ending' condition. An ending condition could be such things as the player exiting the game, or the player meeting the 'win' condition.

- Logic()
* This is where we perform the game logic, this is such things as AI, movement, physics updating, network updating, etc. This can also include calculating of what should be included in the render which is coming. There can be quiet a lot of extra detail here, and I can definitely explain a lot more topics here, this is just the foundation. Just ask...

- Render()
* Drawing of the scene, going through the objects in the scene to then render them. Again, there can be quiet a lot of extra detail here, and I can definitely explain a lot more topics here, this is just the foundation. Just ask...

- Shutdown()
* This is called to destroy any created information. This should also include the deallocation of memory allocated. Typically destroying anything created within the Initialize(), and anything created at runtime (runtime allocation should be avoided however).


So please, ask away... what things would you like to know? I would also hope that some of the other experienced members would be able to offer up their help too... no flaming/idle chit-chat (keep this thread neat and clean), though do point out errors, don't worry too much about speed things, unless something is done really bad, or it is asked for...

------------------
Junior Programmer www.auran.com
Quality Assurance Lead www.rebelplanetcreations.com

crazyishone

Member

Posts: 1685
From:
Registered: 08-25-2004
Ah, most excellent. Now I won't have to plague foreign lands with my noob questions.

For starters, I'd like to ask about the shutdown() part.
I use c++, and I'm curious...how much of this "garbage collection" is really up to me? I'm not looking for an easy way out or anything, just wondering if I should be on the lookout in the future.

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

TwoBrothersSoftware

Member

Posts: 141
From: Janesville, Wi USA`
Registered: 08-05-2006
quote:
Originally posted by crazyishone:
Ah, most excellent. Now I won't have to plague foreign lands with my noob questions.

For starters, I'd like to ask about the shutdown() part.
I use c++, and I'm curious...how much of this "garbage collection" is really up to me? I'm not looking for an easy way out or anything, just wondering if I should be on the lookout in the future.


If you're game creates objects - you need to make sure you destroy them, Otherwise memory leaks add up quicks.

60 frames a second * 2 K graphic not destroyed but recreated means 7 megs a minuite of memory loss, over 1/2 hour of gaming means 210 megs of lost memory - and lag will happen on machines with less than 1/2 gig of ram.

And that's a 2k graphic not being destroyed each frame.


steveth45

Member

Posts: 536
From: Eugene, OR, USA
Registered: 08-10-2005
When I make games in C++, I generally use STL-style containers that automatically allocate memory as needed on the heap and then free that memory when the containers go out of scope.


{
std::vector<int> vInt;
for(int i = 0; i < 10; i++)
vInt.push_back(i); // vInt allocates more memory as needed

/* do some other things */

} // <- vInt automatically frees memory when it goes out of scope here

Python does ref-counting, so when an object is no longer being referenced, then it gets garbage collected automatically.

------------------
+---------+
|steveth45|
+---------+

Briant

Member

Posts: 742
From: Stony Plain, Alberta, Canada
Registered: 01-20-2001
Good basic game loop Dartsman, but you should add one thing:

while (game running)
{
getinput();
logic();
render();
}


A game usually isn't fun if you can't interact with it.

------------------
Brian

"OOP programmers have a lot of class"

Check out this webhost! Fantastic prices, features and support!

dartsman

Member

Posts: 484
From: Queensland, Australia
Registered: 03-16-2006
thanks BrianT, though I can't really take any credit for it.. it is after all the standard game loop... good point.. I usually have the input from the user within the logic... I left out mentioning that the logic would also check for the input...

Logic() can be thought of as gathering everything you need to render the scene... so pretty much there won't be a render until all logic has been processed. Logic can be a bottle neck within a game more so then rendering when bad logic coding such as deep nested loops and the like are used.

Crazy: nothing is garbage collected in C++ (in the Java 'Garbage Collection' sense).. sure when you shut the game down, all the memory which was used will cease (please correct me if I'm wrong..). You should definitely look out for any thing which is allocated (new/malloc) and delete it once you've finished...

One thing to note, if there is too much run-time allocation within your game, you might want to think about allocating chunks at a time, as allocation is a slow process.

------------------
Junior Programmer www.auran.com
Quality Assurance Lead www.rebelplanetcreations.com

[This message has been edited by dartsman (edited April 17, 2007).]

David Lancaster

Member

Posts: 276
From: Adelaide, Australia
Registered: 05-22-2006
If only I had the genius Jon has...
jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
C++ doesn't automatically free all heap memory when it's shut down. using the 'new' or 'malloc' keywords will steal a chunk of memory and won't give it back until you free/delete it or reboot the system. and worse: you probably won't even know it still has it. hence the term "memory leak"

Edit: managed C++ DOES have garbage collection (Managed DirectX) so it's a bit safer i would think. Oh, and coding with Ogre3D is really nice because it tells you about all of your memory leaks in a file after you shut the program down.

[This message has been edited by jestermax (edited April 18, 2007).]

Lazarus

Member

Posts: 1668
From: USA
Registered: 06-06-2006
quote:
Originally posted by David Lancaster:
If only I had the genius Jon has...

If only I could create games like the ones David has...

jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
quote:
Originally posted by David Lancaster:
If only I had the genius Jon has...

If i only had a brain

------------------
Visit my portfolio (and check out my projects):
http://Jestermax.googlepages.com/

Lazarus

Member

Posts: 1668
From: USA
Registered: 06-06-2006
quote:
Originally posted by jestermax:
If i only had a brain


If I only wasn't a spam bot.

jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
quote:
Originally posted by steveth45:
When I make games in C++, I generally use STL-style containers that automatically allocate memory as needed on the heap and then free that memory when the containers go out of scope.

<BLOCKQUOTE><table width=80% border=0 bgcolor="#FFFFF" CELLPADDING="2" CELLSPACING="2"><TR><TD><font size="3" face="Courier" color="#000000"><pre>
{
std::vector<int> vInt;
for(int i = 0; i < 10; i++)
vInt.push_back(i); // vInt allocates more memory as needed

/* do some other things */

} // <- vInt automatically frees memory when it goes out of scope here
</pre></font></TD></TR></TABLE></BLOCKQUOTE>

Python does ref-counting, so when an object is no longer being referenced, then it gets garbage collected automatically.


oh yeah, STL is amazing for it's containers, however be VERY careful when storing pointers in it. It does NOT deallocate the pointer memory. So either create a class that will deallocate the pointed to memory upon deconstruction/element removal or use 'Smart Pointers' from the Boost library (or write your own)

------------------
Visit my portfolio (and check out my projects):
http://Jestermax.googlepages.com/

SSquared

Member

Posts: 654
From: Pacific Northwest
Registered: 03-22-2005
quote:
Originally posted by jestermax:
Oh, and coding with Ogre3D is really nice because it tells you about all of your memory leaks in a file after you shut the program down.

Visual Studio will also display memory leaks when running a DEBUG version in the Debugger. Upon closing down the application, a list of memory leaks are written to the output tab. Double-clicking will take you right to where the memory is allocated and you can figure out for yourself when to delete.

As far as I know, this is only available when compiling with MFC. You need to add the following in your CPP file:

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

If you don't use this, you will still see memory leaks, but the output is generally all in hexadecimal.

jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
MFC??? ewwwwwww. i use win32 for windowing . good tip though.

------------------
Visit my portfolio (and check out my projects):
http://Jestermax.googlepages.com/

dartsman

Member

Posts: 484
From: Queensland, Australia
Registered: 03-16-2006
ah stuff it, I'm done with this thread... I said no spam posts ("no flaming/idle chit-chat (keep this thread neat and clean)"), and I meant it.. thanks guys for ruining it...

------------------
Junior Programmer www.auran.com
Quality Assurance Lead www.rebelplanetcreations.com

samw3

Member

Posts: 542
From: Toccoa, GA, USA
Registered: 08-15-2006
Darts,
maybe you could write your ideas up as an article? I'm sure it would be an interesting read. Then post a forum link to discuss it. I have been lurking on this thread and was also disappointed to see it dissolve into a pointer allocation and memory leaks tangent. .
Just to let you know I'm interested in your topic!

God bless!

------------------
Sam Washburn

[This message has been edited by samw3 (edited April 18, 2007).]

dartsman

Member

Posts: 484
From: Queensland, Australia
Registered: 03-16-2006
thanks samw3... same here.. if people didn't take it off course, it might have worked...

I'll give the article a thought, otherwise put it onto a forum which I'm an admin in (such as http:\\www.projectnresource.com) so I can easily delete unwanted posts...

though, if someone wishes to ask a question about how something is done, please PM me... you shouldn't have to suffer due to some people being stupid...

------------------
Junior Programmer www.auran.com
Quality Assurance Lead www.rebelplanetcreations.com

steveth45

Member

Posts: 536
From: Eugene, OR, USA
Registered: 08-10-2005
quote:
Originally posted by samw3:
I have been lurking on this thread and was also disappointed to see it dissolve into a pointer allocation and memory leaks tangent. .


I don't think talking about memory leaks was particularly idle chit-chat, but David's post, and responses from jestermax and laz were. Things like initialization and clean-up are important parts of the game loop, hence the initialize() and shutdown() calls in dartsman's example.

Darts, you don't have to give up, try appealing to the moderators to clean up the spam, that's what they are there for. Also, you have pretty lame attitude, calling people stupid.

------------------
+---------+
|steveth45|
+---------+

[This message has been edited by steveth45 (edited April 18, 2007).]

Calin

Member

Posts: 358
From: Moldova
Registered: 12-04-2006
quote:
Originally posted by steveth45:
[QUOTE]Originally posted by samw3:
[b]I have been lurking on this thread and was also disappointed to see it dissolve into a pointer allocation and memory leaks tangent. .


I don't think talking about memory leaks was particularly idle chit-chat, but David's post, and responses from jestermax and laz were. Things like initialization and clean-up are important parts of the game loop, hence the initialize() and shutdown() calls in dartsman's example.

Darts, you don't have to give up, try appealing to the moderators to clean up the spam, that's what they are there for. Also, you have pretty lame attitude, calling people stupid.

[/B][/QUOTE]

Ditto, looks like this thread could use some 'Moderator justice'.

------------------
Check my libraries (C# assemblies):
http://calinnegru.googlepages.com/downloads