   GameLoop question...
  Hi! I am new to game development, and after some trials and tutorials I want to attempt my first ''serious'' game.. and with serious I just mean ''well done'' and not big or good...I am developing under .NET framework in C# and Managed DirectX, thought that is not much relevant... The first problem I encounter is with the game loop... I am used to see the typical game loop inside the main form class, but i just cannot believe real games are made like that.. It seems simply not right to mix game logic with presentation. Thats why i deceided to move my game code to a new class called CGameEngine, with instantiates the form and holds a reference to it.In my GameLoop() method there is the classic:while(!endGame){   // Check inputs   // Act accordingly    // Update Screen.. sounds.. etcs...   Application.DoEvents();}My problem comes woth the Application.DoEvents() things.. I think it is a must to have it, so the application can recieve normal windows messages, but if I put it inside the loop everytime the code reaches that point, it starts working in its own loop, so it doesn''t work....Any idea how to implement my gameLoop? Changes to my gameEngine? Another question.. should the game loop run on a sepparate thread, or will that generate too much overhead?Sorry for a so long post in my very first attempt, but i am willing to do something ''well done'' and hope you can help.. thanks people in advance.   To Iterate is human, To Recurse Divine... 
  I think its safe to assume Win32 here?Heres what the typical game loop will look like: while(true){if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){if(msg.message == WM_QUIT)break;TranslateMessage(&msg);DispatchMessage(&msg);} Render(); GetInput(); // etc... etc...    }Basically what that does, is only handle messages when there actually is a message to be handled.  If there isn''t one, it doesn''t affect performance at all. ;quote: I think its safe to assume Win32 here?quote:I am developing under .NET framework in C#   and Managed DirectXquote:Any idea how to implement my gameLoop? Changes to my gameEngine?I think it makes more sense for your main form to have an instance of the game engine and then just call engine.update() every frame.quote:Another question.. should the game loop run on a sepparate thread, or will that generate too much overhead?There really is no point to that because most of the time the main form''s thread would be idling, wasting precious cpu time.   --------"Hey, what about those baggy pants you kids are wearin'' these days? Aren''t they hip and cool and poppin'' fresh?"-Peter Griffin"Everytime I see an M followed by a dollar sign I just say "oh god" and go to the next post."-Neurokaotixthe D programming language|google|msdn|XLib reference manual;  I read right over that line apparently.  >_< ;  Definatly far from pro myself, hell I can''t even seem to finish a game all the way, buut my loops normally look a little something like: int main(){    GAMECLIENT client;    client = new GAMECLIENT();}and then inside my constructor: GAMECLIENT::GAMECLIENT(){    // initialize n all that grandness    gameLooper();}Then the loop has all the regular stuff you might do in a loop.Anyone know if this is a good way of doing it? I feel similar to TheObscured about doing things well for once.   -AfroFire ;  don''t worry about that yet  just finish your gamenot everything in .net is the same as in windows ;  The fact is that what I am trying to avoid is just letting my main form call Enigne.Upsate() or something like that. It seems to me that it should be the Engine the absolute ruler, the one who gives orders xD Now seriously, leaving the form to deciede when to call the Engine.Update() thing means the timer has to be in the form, wich is putting game logic in the presentation. I want to avoid that... I don''t mean i want this to be easily portable for linux/Mac/anything, because I don''t want to sacrifice performance agains that, but well, I think a good interface/Logic/Data separation is a must for future, bigger projects.I wish there was a way in .NET like the Win32 sample shown here.. peeking the message queue and responding to that just in case there is a need to, that seems right way.. i am going to look a bit into it.. thanks a lot to everybody. ;  AfroFire.. just like that is how I want to approach. My main just handles de creation of my main game engine, and in the constructor of it, I create my Form (which handles all direct3D stuff) and i create too the devices for the mouse and keyboard. After all the creation is done, I call the gameLoop.The only design difference is that i don''t call the gameLoop method INSIDE the constructor, I have a public mathod to run the loop.. it doesn''t seem right to me that the constructor begins executing the loop, but that''s unrrelevant I suppose.PS: Sorry my english, I forgot to say this before ---   To Iterate is human, To Recurse Divine... ;  It's actually a bit different to do it with .NET because Application is in System.Windows.Forms, and TranslateMessage(), DispatchMessage() are global, so you don't have direct access like you do in the Win32 API.What I did was I made a Scene class that has the game loop in it, and has a function like this... public int Run(){int result;if(!isReady){result = Init();if(result != 0)return result;}bool isRunning = true;while(isRunning){UpdateScene();RenderScene();window.DoEvents();if(!window.IsValid)isRunning = false;}return 0;}UpdateScene() and RenderScene() would be overrided by derived scenes that do the actual work.window is an interface that looks like this: public interface ISceneWindow{void DoEvents();Device GetDevice{get;}bool IsValid{get;}}The main display form signature looks like this:public class WorldViewForm : System.Windows.Forms.Form, ISceneWindowAnd it has these functions derived from the interface: public void DoEvents(){Application.DoEvents();}public Device GetDevice{get{return device;}}public bool IsValid{get{return Created;}}It's probably not the very best way, but I hope that at least gives you a hint!   [edited by - reiplusplus on February 3, 2004 8:59:04 PM];  Thanks reiplusplus, nice trick with the ISceneWindow and it''s DoEvents... I am going to change my engine to try this new approach If you ever come to Spain you can count with a free beer :D Thanks to all the people who answered too this post.. see you!   To Iterate is human, To Recurse Divine... 