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) shutdown();
- while (game running) - Logic() - Render() - Shutdown()
------------------ |
|
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. ------------------ |
|
TwoBrothersSoftware Member Posts: 141 From: Janesville, Wi USA` Registered: 08-05-2006 |
quote: 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.
Python does ref-counting, so when an object is no longer being referenced, then it gets garbage collected automatically. ------------------ |
|
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)
------------------ 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. ------------------ [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: If only I could create games like the ones David has... |
|
jestermax![]() Member Posts: 1064 From: Ontario, Canada Registered: 06-21-2006 |
quote: If i only had a brain ------------------ |
|
Lazarus![]() Member Posts: 1668 From: USA Registered: 06-06-2006 |
quote: If I only wasn't a spam bot. |
|
jestermax![]() Member Posts: 1064 From: Ontario, Canada Registered: 06-21-2006 |
quote: 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) ------------------ |
|
SSquared![]() Member Posts: 654 From: Pacific Northwest Registered: 03-22-2005 |
quote: 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 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 ![]() ------------------ |
|
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... ------------------ |
|
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! ------------------ [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... ------------------ |
|
steveth45![]() Member Posts: 536 From: Eugene, OR, USA Registered: 08-10-2005 |
quote: 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. ------------------ [This message has been edited by steveth45 (edited April 18, 2007).] |
|
Calin![]() Member Posts: 358 From: Moldova Registered: 12-04-2006 |
quote: 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'. ------------------ |