{"id":110,"date":"2011-09-16T15:24:44","date_gmt":"2011-09-16T15:24:44","guid":{"rendered":"http:\/\/www.drmop.com\/?p=110"},"modified":"2014-07-16T14:42:13","modified_gmt":"2014-07-16T14:42:13","slug":"marmalade-sdk-up-and-running-in-a-jiffy-the-main-game-loop","status":"publish","type":"post","link":"http:\/\/www.drmop.com\/index.php\/2011\/09\/16\/marmalade-sdk-up-and-running-in-a-jiffy-the-main-game-loop\/","title":{"rendered":"Marmalade SDK Tutorial &#8211; Up and Running in a Jiffy &#8211; The Main Game Loop"},"content":{"rendered":"<p>This tutorial is part of the Marmalade SDK tutorials collection. To see the tutorials index\u00a0<a title=\"Marmalade SDK tutorials index\" href=\"http:\/\/www.drmop.com\/?page_id=91\">click here<\/a><\/p>\n<p><a title=\"Marmalade SDK tutorials index\" href=\"http:\/\/www.drmop.com\/?page_id=91\"><\/a>Ok, so you downloaded and installed the Marmalade SDK, took a quick scan over the help files and thought \u201cHmm, this is a biggy, where do I start?\u201d When we start something new we all feel like complete and utter noobs. Al you want to do is get a \u201chello world\u201d application up and running so you can have some kind of sense of achievement and feel comfortable with the process of updating code, compiling it and seeing it actually working. With this in mind, this tutorial will help you accomplish just that. On the other hand, if you just want the source code to the tutorial then you can <a title=\"Marmalade SDK Basic MKB Tutorial Source Code\" href=\"http:\/\/www.drmop.com\/wp-content\/uploads\/2011\/09\/BasicMKB.zip\">download it here<\/a>.<\/p>\n<p>By the way, if you haven\u2019t already done so then <a title=\"download a free version of of the Marmalade SDK\" href=\"ttps:\/\/www.madewithmarmalade.com\/download\" target=\"_blank\">http:\/\/www.madewithmarmalade.com\/download<\/a> and grab a copy of the free trial version of the Marmalade SDK and install it.<\/p>\n<p>We will begin with a few Marmalade basics so you don\u2019t get lost during the tutorial.<\/p>\n<h2><strong>Project files (MKB\u2019s)<\/strong><\/h2>\n<p>To begin with, Marmalade uses the concept of project files to organise your source files, data files and deployment settings. The MKB file is basically a text file that when opened up will generate a Visual Studio or XCode project containing your source files etc.<\/p>\n<p>The MKB file is split into a number of sections, each with a heading name followed by curly braces containing the sections data. Here&#8217;s an example of a section:<\/p>\n<pre>\r\n<blockquote>\r\n\r\nsubprojects\r\n{\r\n    iw2d\r\n}<\/blockquote>\r\n<\/pre>\n<p>For now you only need to be concerned with the following basic sections of the MKB:<\/p>\n<ul>\n<li>options \u2013 Options to pass to the build system<\/li>\n<li>includepath \u2013 Tells marmalade where to look for your header files<\/li>\n<li>subprojects \u2013 This section tells Marmalade which parts of the SDK you would like to use. Here you put the names of the SDK parts you would like to access from your code, for example, the 2D API is called Iw2D<\/li>\n<li>files \u2013 This section tells Marmalade which editable files you would like including in your generated project, such as source files, XML files, configuration files etc..<\/li>\n<li>assets \u2013 This section tells Marmalade which assets you would like to include when deploying your app or game, assets include files such as bitmaps, audio files, meshes, fonts, data files etc.. You can define assets in groups if you need a particular set of assets specific platforms<\/li>\n<li>deployments \u2013 This section allows you to define certain parameters on a per target platform basis. We won\u2019t be covering this section in this tutorial but its worth mentioning.<\/li>\n<\/ul>\n<p>Ok, now we have explained a few things about MKB files, here is a very basic one to get you started:<\/p>\n<pre>\r\n<blockquote>\r\n\r\n#!\/usr\/bin\/env mkb\r\noptions\r\n{\r\n}\r\n\r\nsubprojects\r\n{\r\n    iw2d\r\n}\r\n\r\nincludepath\r\n{\r\n    .\/source\r\n}\r\n\r\nfiles\r\n{\r\n    [Source]\r\n    (source)\r\n    Main.cpp\r\n}\r\n\r\nassets\r\n{\r\n}<\/blockquote>\r\n<\/pre>\n<p>To generate a project from this file you need to:<\/p>\n<ul>\n<li>Create a folder on your hard drive, lets call it BasicMKB<\/li>\n<li>Create a text file called BasicMKB.mkb and add the above lines to it<\/li>\n<li>Create a sub folders called source<\/li>\n<li>Add a Main.cpp file to the source folder<\/li>\n<li>Double click the BasicMKB.mkb file to have the Marmalade SDK generate your Visual Studio or XCode project<\/li>\n<\/ul>\n<p>Notes:<\/p>\n<ul>\n<li>Marmalade will generate a data folder containing two .icf files and a build folder, you do not need to be concerned with these for the time being.<\/li>\n<li>You should not add additional source files via XCode or Visual Studio. instead, edit the MKB file, add your source files then re-launch the MKB file regenerate the project.<\/li>\n<\/ul>\n<p>Now we are finally have the basics of creating a Marmalade SDK project out of the way, we now need something to compile and run.<\/p>\n<h2><strong>A very basic game loop<\/strong><\/h2>\n<p>In this section we will cover creating a very basic game loop that checks for the user quitting and clears the screen.<\/p>\n<p>Ok, re-using the previous example, you need to edit the Main.cpp file that you created and add the following code:<\/p>\n<pre>\r\n<blockquote>\r\n\r\n<span style=\"color: #008000;\">\/\/ Marmalade headers<\/span>\r\n#include \"s3e.h\"\r\n#include \"Iw2D.h\"\r\n#include \"IwGx.h\"\r\n\r\nint main()\r\n{\r\n    <span style=\"color: #008000;\">\/\/ Initialise Marmalade graphics system and Iw2D module<\/span>\r\n    IwGxInit();\r\n    Iw2DInit();\r\n\r\n    <span style=\"color: #008000;\">\/\/ Set the default background clear colour<\/span>\r\n    IwGxSetColClear(0x40, 0x40, 0x40, 0);\r\n\r\n    <span style=\"color: #008000;\">\/\/ Main Game Loop<\/span>\r\n    while (!s3eDeviceCheckQuitRequest())\r\n    {\r\n        <span style=\"color: #008000;\">\/\/ Update keyboard system<\/span>\r\n        s3eKeyboardUpdate();\r\n        if (s3eKeyboardGetState(s3eKeyAbsBSK) &amp; S3E_KEY_STATE_DOWN)    <span style=\"color: #008000;\">\/\/ Back key is used to exit on some platforms<\/span>\r\n            break;\r\n\r\n        <span style=\"color: #008000;\">\/\/ Update pointer system<\/span>\r\n        s3ePointerUpdate();\r\n\r\n        <span style=\"color: #008000;\">\/\/ Clear the screen<\/span>\r\n        IwGxClear(IW_GX_COLOUR_BUFFER_F | IW_GX_DEPTH_BUFFER_F);\r\n\r\n        <span style=\"color: #008000;\">\/\/ Update the game<\/span>\r\n\r\n        <span style=\"color: #008000;\">\/\/ Render the games view<\/span>\r\n\r\n        <span style=\"color: #008000;\">\/\/ Show the surface<\/span>\r\n        Iw2DSurfaceShow();\r\n\r\n        <span style=\"color: #008000;\">\/\/ Yield to the operating system<\/span>\r\n        s3eDeviceYield(0);\r\n    }\r\n\r\n    <span style=\"color: #008000;\">\/\/ Shut down Marmalade graphics system and the Iw2D module<\/span>\r\n    Iw2DTerminate();\r\n    IwGxTerminate();\r\n\r\n    return 0;\r\n}<\/blockquote>\r\n<\/pre>\n<p>You can download a zip archive containing the project MKB and source code <a title=\"Marmalade SDK Basic MKB Tutorial Source Code\" href=\"http:\/\/www.drmop.com\/wp-content\/uploads\/2011\/09\/BasicMKB.zip\">from here<\/a><\/p>\n<p>If you build and run this example you will be presented with a grey screen. yes I know its a bit boring, but we will cover drawing sprites and other interesting topics very soon.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial is part of the Marmalade SDK tutorials collection. To see the tutorials index\u00a0click here Ok, so you downloaded and installed the Marmalade SDK, took a quick scan over the help files and thought \u201cHmm, this is a biggy, where do I start?\u201d When we start something new we all feel like complete and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,13,29,43,44,42,28,3,45,1],"tags":[47,718,48,715,49,46],"class_list":["post-110","post","type-post","status-publish","format-standard","hentry","category-airplay-sdk","category-android-app-development","category-blackberry-playbook","category-blackberry-playbook-app-development","category-game-and-app-development","category-ios-app-development","category-marmalade-sdk","category-programming","category-samsung-bada-development","category-uncategorized","tag-basic-game-loop","tag-marmalade-sdk","tag-mkb-project-file","tag-programming","tag-source-code","tag-tutorial"],"_links":{"self":[{"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/posts\/110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/comments?post=110"}],"version-history":[{"count":25,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/posts\/110\/revisions"}],"predecessor-version":[{"id":135,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/posts\/110\/revisions\/135"}],"wp:attachment":[{"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/media?parent=110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/categories?post=110"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/tags?post=110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}