{"id":4030,"date":"2016-07-30T14:20:43","date_gmt":"2016-07-30T13:20:43","guid":{"rendered":"http:\/\/positech.co.uk\/cliffsblog\/?p=4030"},"modified":"2016-07-30T15:45:10","modified_gmt":"2016-07-30T14:45:10","slug":"sample-c-game-loop-democracy-3","status":"publish","type":"post","link":"https:\/\/www.positech.co.uk\/cliffsblog\/2016\/07\/30\/sample-c-game-loop-democracy-3\/","title":{"rendered":"Sample C++ game loop : Democracy 3"},"content":{"rendered":"<p>When I first started coding, especially when I first did C++, there was a lot of confusion about where exactly the &#8216;game&#8217; was, and more specifically, what a main game loop looks like. Because these days, all you hip kids code in Unity or Gamemaker or MyFirstIDE or some other colorful beginners IDE for the under 5s*, you never actually see your game loop, and have no idea where it is, let alone what it does., However, us real-men who code from the ground up have to write one. Here is mine from the PC version of <a href=\"http:\/\/www.positech.co.uk\/democracy3\">Democracy 3<\/a>. There are few comments, because in my godlike knowledge, I understand it all at a glance.<\/p>\n<pre>======================================================================================\r\nvoid APP_Game::GameProc()\r\n{\r\n\u00a0\u00a0 \u00a0HRESULT result = GetD3DEngine()-&gt;GetDevice()-&gt;TestCooperativeLevel();\r\n\u00a0\u00a0 \u00a0if(FAILED(result))\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0if (result == D3DERR_DEVICELOST )\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Sleep( 50 );\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0else if(result == D3DERR_DEVICENOTRESET)\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0if (!GetD3DEngine()-&gt;Restore())\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/ Device is lost still\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Sleep( 50 );\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return;\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Restore3D();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0else\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Sleep( 50 );\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0GTimer looptimer;\r\n\u00a0\u00a0 \u00a0looptimer.StartTimer();\r\n#ifndef _GOG_\r\n\u00a0\u00a0 \u00a0GetSteam()-&gt;Process();\r\n#endif \/\/_GOG_\r\n\u00a0\u00a0 \u00a0GetInput()-&gt;Process();\r\n\u00a0\u00a0 \u00a0GUI_GetCursor()-&gt;SetCursorStyle(GUI_Cursor::DEFAULT); \/\/reset each frame...\r\n\u00a0\u00a0 \u00a0if(BActive)\r\n\u00a0\u00a0 \u00a0{\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0GUI_GetMusic()-&gt;Process();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0GUI_GetSounds()-&gt;Process();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0if(PCurrentMode)\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0PCurrentMode-&gt;ProcessInput();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0SIM_GetThreadManager()-&gt;ProcessFrame();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0GetTextureHistory()-&gt;Reset();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0LOCKRENDERTHREAD;\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0GetD3DEngine()-&gt;BeginRender();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0if(PCurrentMode)\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0PCurrentMode-&gt;Draw();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0GUI_GetTransition()-&gt;Draw();\r\n\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0GetD3DEngine()-&gt;EndRender();\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0RenderFont();\r\n#ifdef _DEBUG\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0if(GetInput()-&gt;KeyDown(VK_LSHIFT))\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0GetTextureHistory()-&gt;Draw();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}\r\n#endif \/\/_DEBUG\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0GetD3DEngine()-&gt;Flip();\u00a0\u00a0 \u00a0\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0RELEASERENDERTHREAD;\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0looptimer.Update();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0if(looptimer.GetElapsed() &lt; 16.0f)\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0looptimer.Update();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0if(looptimer.GetElapsed() &lt; 16.0f)\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Sleep(0);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0else\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0ReleaseResources();\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0if(BRestartPending)\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0BRestartPending = false;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0SilentRestart();\r\n\u00a0\u00a0 \u00a0}\r\n}\r\n\r\n======================================================================================<\/pre>\n<p><em>*yeah I&#8217;m mocking different IDEs. deal with it :D This is sarcasm. There is no proven link between masculinity and choice of game development environment.**<\/em><\/p>\n<p><em>**yet.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When I first started coding, especially when I first did C++, there was a lot of confusion about where exactly the &#8216;game&#8217; was, and more specifically, what a main game loop looks like. Because these days, all you hip kids code in Unity or Gamemaker or MyFirstIDE or some other colorful beginners IDE for the<\/p>\n<p class=\"text-right\"><span class=\"screen-reader-text\">Continue Reading&#8230; Sample C++ game loop : Democracy 3<\/span><a class=\"btn btn-secondary continue-reading\" href=\"https:\/\/www.positech.co.uk\/cliffsblog\/2016\/07\/30\/sample-c-game-loop-democracy-3\/\">Continue Reading&#8230;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-4030","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/www.positech.co.uk\/cliffsblog\/wp-json\/wp\/v2\/posts\/4030","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.positech.co.uk\/cliffsblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.positech.co.uk\/cliffsblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.positech.co.uk\/cliffsblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.positech.co.uk\/cliffsblog\/wp-json\/wp\/v2\/comments?post=4030"}],"version-history":[{"count":3,"href":"https:\/\/www.positech.co.uk\/cliffsblog\/wp-json\/wp\/v2\/posts\/4030\/revisions"}],"predecessor-version":[{"id":4033,"href":"https:\/\/www.positech.co.uk\/cliffsblog\/wp-json\/wp\/v2\/posts\/4030\/revisions\/4033"}],"wp:attachment":[{"href":"https:\/\/www.positech.co.uk\/cliffsblog\/wp-json\/wp\/v2\/media?parent=4030"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.positech.co.uk\/cliffsblog\/wp-json\/wp\/v2\/categories?post=4030"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.positech.co.uk\/cliffsblog\/wp-json\/wp\/v2\/tags?post=4030"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}