{"id":69,"date":"2021-11-25T23:31:27","date_gmt":"2021-11-25T23:31:27","guid":{"rendered":"https:\/\/writingagame.com\/?p=69"},"modified":"2022-08-05T18:10:17","modified_gmt":"2022-08-05T18:10:17","slug":"chapter-2-hello-android","status":"publish","type":"post","link":"https:\/\/writingagame.com\/index.php\/2021\/11\/25\/chapter-2-hello-android\/","title":{"rendered":"Chapter 2. Hello Android"},"content":{"rendered":"\n<p><strong>Visual Studio, Android, Native activity, C++, OpenGL ES<\/strong><\/p>\n\n\n\n<p>Official Android languages are Java and Kotlin.<\/p>\n\n\n\n<p><strong>Native activity<\/strong> will allow us to write code for Android in C++. Besides, Visual Studio\u2019s Native activity template includes OpenGL ES initialization.<\/p>\n\n\n\n<p>1. Open Visual Studio. Pick &#8220;<strong>Create a new project<\/strong>&#8220;. In templates menu pick Android, then \u201cNative Activity Application (Android)\u201d:<\/p>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/writingagame.com\/img\/b01\/c02\/01.jpg\"><\/p>\n\n\n\n<p>Then \u2013 <strong>Next<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p>2. On <strong>Configure your new project<\/strong> page:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Project name &#8211; <strong>p_android <\/strong>(&#8220;p&#8221; &#8211; for &#8220;platform&#8221;), <\/li><li>Location is wrong. Change it (browse) to \u201cC:\\CPP\\a999hello\u201d, <\/li><li>Place solution in the same directory (just to know where to look for it in the future):<\/li><\/ul>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/writingagame.com\/img\/b01\/c02\/02.jpg\"><\/p>\n\n\n\n<p>Then \u2013 <strong>Create<\/strong>.<\/p>\n\n\n\n<p>Looks like this:<\/p>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/writingagame.com\/img\/b01\/c02\/03.jpg\"><\/p>\n\n\n\n<p>As you can see, under \u201cp_android\u201d solution\/folder it created TWO projects \u2013 \u201cp_android.NativeActivity\u201d and \u201cp_android.Packaging\u201d.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p>3. <strong>IMPORTANT<\/strong>: Default platform (in the top menu) is ARM. Change it to <strong>ARM64<\/strong>.<\/p>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/writingagame.com\/img\/b01\/c02\/04.jpg\"><\/p>\n\n\n\n<p>Google announced already that 32 bit will NOT be supported, so 64 is a must!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p>4. Since we are planning to use OpenGL ES 3.2, we need higher API level (in my case default was 21).<\/p>\n\n\n\n<p>Right-click on <strong>p_android.NativeActivity<\/strong> project -&gt; Properties (on the bottom), <\/p>\n\n\n\n<p>Change Configuration to <strong>All Configurations<\/strong>, Platform: <strong>ARM64<\/strong>, <\/p>\n\n\n\n<p>go to <em>General -&gt; Target API Level<\/em>, Change to <strong>27<\/strong> (it will be quite enough):<\/p>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/writingagame.com\/img\/b01\/c02\/05.jpg\"><\/p>\n\n\n\n<p>Then \u2013<strong> Apply, Ok<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p>5. Also we need to change <em>minSdkVersion <\/em>in the <strong>AndroidManifest <\/strong>to 24 (minimum for ES 3.2) and <em>targetSdkVersion <\/em>to 27 (as decided above):<\/p>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/writingagame.com\/img\/b01\/c02\/06.jpg\"><\/p>\n\n\n\n<p>Also, let&#8217;s change <strong>android:label<\/strong> from &#8220;@string\/app_name&#8221; to &#8220;<strong>OurProject<\/strong>&#8220;.<\/p>\n\n\n\n<p>Then &#8211; <strong>Save All<\/strong>:<\/p>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/writingagame.com\/img\/b01\/c02\/07.jpg\"><\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p>6. Now we can switch from default OpenGL ES 1.0 to desirable OpenGL <strong>ES 3.2<\/strong>. The reference is in <em>pch.h<\/em> file under <em>p_android.NativeActivity<\/em> project. Replace corresponding string to \u201cGLES3\/gl32.h\u201d:<\/p>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/writingagame.com\/img\/b01\/c02\/08.jpg\"><\/p>\n\n\n\n<p>Again, Save All.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p>7. Also need to change linked library. Right-click on \u201cp_android.NativeActivity\u201d project -&gt; Properties, <strong>All Configurations<\/strong>, <strong>ARM64<\/strong>, <em>Linker -&gt; Input<\/em>, open <em>Library Dependencies -&gt; Edit<\/em>.<\/p>\n\n\n\n<p>Manually change GLESv1_CM to <strong>GLESv3<\/strong><\/p>\n\n\n\n<p>Ok, Apply, Ok.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p>To make sure that nothing is broken yet, let\u2019s try to compile and run our program on actual Android device.<\/p>\n\n\n\n<p>For this we will need to enable (if not enabled yet) \u201cUSB debugging\u201d in Settings -&gt; Developer options on the device.<\/p>\n\n\n\n<p>Just in case: On my Samsung S20 phone \u201cDeveloper Options\u201d were not presented in Settings list. To enable them I had to go to Settings -&gt; About phone -&gt; Software information and tap on Build number seven times. After that Developer options supposed to be available, so you can go back to Settings -&gt; Developer options and enable USB debugging.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p>8. Now turn on and unlock your phone and plug it in to your PC with USB cable. Device should ask \u201cAllow USB debugging?\u201d \u2013 allow. After that your device should appear in the list of available devices:<\/p>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/writingagame.com\/img\/b01\/c02\/09.jpg\"><\/p>\n\n\n\n<p>Click on the green arrow to run the program (Configuration: Debug \/ ARM64):<\/p>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/writingagame.com\/img\/b01\/c02\/10.jpg\"><\/p>\n\n\n\n<p>Just as was expected &#8211; errors.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p>9. Ignore poll.h &#8220;errors&#8221;. Important is &#8211; 2 \u201cundefined identifiers\u201d: GL_PERSPECTIVE_CORRECTION_HINT and GL_SMOOTH in main.cpp. Lines 111 and 113 (it&#8217;s for GLES 1.0). <\/p>\n\n\n\n<p>Double-click on the error to move to source code. Just comment these 2 lines out:<\/p>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/writingagame.com\/img\/b01\/c02\/11.jpg\"><\/p>\n\n\n\n<p>Run again (green arrow) \u2013 success! On my S20 it looks like flashing green screen:<\/p>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/writingagame.com\/img\/b01\/c02\/12.jpg\"><\/p>\n\n\n\n<p>For now, let&#8217;s put Android aside and move on to the PC.<\/p>\n\n\n\n<p> VS top menu -&gt; Debug -&gt; Stop Debugging. <\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p class=\"mb-2\">Visual Studio, Android, Native activity, C++, OpenGL ES Official Android languages are Java and Kotlin. Native activity will allow us to write code for Android in C++. Besides, Visual Studio\u2019s Native activity template includes OpenGL ES initialization. 1. Open Visual Studio. Pick &#8220;Create a new project&#8220;. In templates menu pick Android, then \u201cNative Activity Application [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":210,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-69","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cross-platform-3d"],"_links":{"self":[{"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/posts\/69","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/comments?post=69"}],"version-history":[{"count":16,"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/posts\/69\/revisions"}],"predecessor-version":[{"id":1655,"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/posts\/69\/revisions\/1655"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/media\/210"}],"wp:attachment":[{"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/media?parent=69"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/categories?post=69"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/tags?post=69"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}