• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
ATC

Robust main loop; best practices/ideas...

4 posts in this topic

Anyone can cobble together a decent main loop... but doing one "[i]perfectly[/i]" is a bit of a different story. So before I revisit my main loop implementation for my engine, I decided to open this discussion to get better ideas. I must offer the following features (NOTE: project is in C#):
[list]
[*]Optional variable/fixed time-step (precise enough for realistic physics)
[*]Optional multi-threaded updates
[*]Optional multi-threaded rendering (for D3D11 & OGL)
[*]Thread safety for memory and forms/controls/GUI stuff
[/list]
I already have implemented an extremely hi-resolution timer/clock for the engine. So that is taken care of. But I want to re-implement the entire main loop from start to finish, and create a robust implementation that can satisfy virtually any game or hard-core simulation.

Here are some of the things that concern me...
[list=1]
[*]Proper separation of render and update logic?
[*]Proper way to implement option of fixed vs variable time-step?
[*]Implementation of a Winforms-like "InvokeRequired" property for GUI thread safety?
[/list]

I'd also like to know what might happen if I try to render something from another thread in, say, D3D10 (which doesn't offer multi-threaded rendering like 11)? Does the device interface just perform the rendering on the thread it was created on, or would it crash my engine? What about running my update loop on a totally separate thread from rendering? Is this acceptable or a "bad practice"? Should I instead offer the option to set "game components" to update themselves on separate threads (e.g., physics engine, content loading/streaming, etc as components which run their updates on new threads)? What is the best way to synchronize a very complex multi-threaded environment like this? What problems might I encounter, and what is the best way to work around them? Any further information is greatly appreciated...
0

Share this post


Link to post
Share on other sites
No idea if you've read these yet, but both of these articles may give you some ideas: [url="http://www.koonsolo.com/news/dewitters-gameloop/"]deWiTTERS Game Loop[/url] and [url="http://gafferongames.com/game-physics/fix-your-timestep/"]Fix Your Timestep![/url]. I know a lot of people use variations of what are discussed here.

Regarding all of your desired options, I don't think it's practical to make a single game loop configurable to that degree. If I were going to do such a thing, for, say, a game framework, I would prefer to make separate implementations targeting the different conditions to take advantage of any idiosyncrasies in that environment. Piling everything in one cover-all-the-bases loop is going to be inefficient, but the bigger concern in my book is maintainability. IMO, the [i]perfect[/i] game loop (if there is such a thing) isn't one that is highly configurable, but one that matches the needs of the game.
1

Share this post


Link to post
Share on other sites
In my current project I felt a definite need to invest some of my critical thinking and programming skills into this issue, and the end result was basically a kernel that divided everything up into a grid of states/systems. The X/Y dimensions of this grid are the Systems and States of the engine/kernel.. Thus, systems like Render/Network/Physics, etc. with various 'states' that can be invoked across threads, and done so repeatedly according to a desired time interval.. Eg: Render_Setup, Render_Cycle, Render_Shutdown...

Each system I allow a min-interval and a max-interval. All systems attempt to reach a 'balance' so that their actual execution intervals are within their defined ranges. If one system is running plenty quick for a particlar state (eg: STATE_SETUP, STATE_STANDBY, STATE_INGAME, STATE_SHUTDOWN) and other systems aren't, it will simply increase the wait period until the next execution..

Along with all this is a neat ability to create logic-chains, where one state - depending on the result of each system executing for that state - determines what state to progress to next. So if the STATE_SETUP fails on one of the systems, it will go to STATE_SHUTDOWN.. If the setup for all systems succeeds then it can, otherwise, progress the state to STATE_STANDBY to display a menu, or whatever.

I just create a new thread for each system, unless the system specifies that it should run in the main thread (some things like hardware rendering don't much appreciate occurring outside the main thread) and each thread runs its own main-loop that determines if/when its corresponding system should execute. Integrating multithreading into the system is probably the trickiest part of the whole deal, but the key is to not over-think it..

As far as overhead is concerned, my primary goal when devising this setup was having the ability to control the use of resources by each system so as to provide the end-user an optimal experience as far as responsiveness is concerned, by enabling the core, or 'main loop', to skip the refresh of certain systems which can afford it..

If one system magically takes a bunch of time and causes other systems to fall behind, forcing them to execute well beyond their max-interval, what I do is take the total amount of elapsed time since the last refresh (for each system, individually), divide it up evenly, and execute the system however many times in an attempt to emulate proper execution intervals so as to allow the system to 'catch up'.

I'm not sure how well I explained this, but it works great and I'm happy with it.
1

Share this post


Link to post
Share on other sites
[b]@ radioteeth:[/b]

Yes, you did explain things very well; about as good as one can without "going all out" and supplying diagrams, code, etc... You've given me some very good ideas which I'm going to work into the design and test; and take it from there, tweaking it out for performance and improving the logic itself. So +1 for you and Aldacron, whom I must also thank for his time.

Any others have some insight that might be beneficial for me to hear? My ears will be open! :-)

Thanks for all thus far,

--ATC--
0

Share this post


Link to post
Share on other sites
Ok, I put a little thought into a fixed time-step implementation and I've come up with something like this:

[source lang="csharp"]
void _MSG_LOOP() {

if ( abort )
game.ShutDown();

clock.Step();
float delta = clock.Elapsed.Milliseconds;

if (IsFixedTimeStep)
{
accumMS += delta;
float stepRate = (1 / TargetUPS);

int nSteps = (int)Math.Floor(accumMS / stepRate);
int count = Math.Min(nSteps, MAX_STEPS);

while (count-- > 0)
{
_update(stepRate);
accumMS -= stepRate;
}

_render(stepRate);
}

else
{
_update(delta);
_render(delta);
}
}
[/source]

So what I'm wondering is: Is this correct? the way I should be doing it? Or am I doing something wrong? If so, please explain... I just want to "perfect" this little algorithm before I code out of the real system for the engine...

I'm also considering the idea of having my algorithm compute the "remainder" any time left over after executing nSteps... basically, if the remainder fell within the acceptable delta value range for the physics engine it would then dispatch one more update to [i]clear[/i] the ms accumulator and pass that remainder as the delta value... For example:

if( remainder > min_stepRate && remainder < max_stepRate)
_update( remainder );

Is this advisable or not? Edited by ATC
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0