{"id":24516,"date":"2015-03-04T06:44:04","date_gmt":"2015-03-04T11:44:04","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=24516"},"modified":"2015-03-04T06:47:06","modified_gmt":"2015-03-04T11:47:06","slug":"ideas-about-a-new-programming-language-for-games-annotated-part-4","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=24516","title":{"rendered":"Ideas about a new programming language for games, Annotated: Part 4"},"content":{"rendered":"<p>We finally get to the good stuff. The discussion below contains a lot of the reasons I wanted to do this write-up in the first place.<\/p>\n<p><table class='nomargin' cellspacing='0' width='100%' cellpadding='0' align='center' border='0'><tr><td><iframe loading=\"lazy\" width=\"1024\" height=\"576\" src=\"https:\/\/www.youtube.com\/embed\/TH9VCN6UkyQ\" frameborder=\"0\" allowfullscreen class=\"embed\"><\/iframe><br\/><small><a href='http:\/\/www.youtube.com\/watch?v=TH9VCN6UkyQ'>Link (YouTube)<\/a><\/small><\/td><\/tr><\/table><\/p>\n<h3>1:00:00 Let&#8217;s Make an Array Type.<\/h3>\n<p>Yeeeessss. <\/p>\n<p>This might seem trivial to a lot of people. This is basically just a fix for the <tt>std::vector&lt;int&gt;  name;<\/tt> problem I discussed earlier in this series. We use arrays everywhere in our code. We&#8217;re constantly building lists of data. C++ offers tons of ways to do this, and somehow they all suck. <\/p>\n<p>The lazy way:<\/p>\n<p><!--more--><\/p>\n<pre lang=\"c\" line=\"1\">int    things[10];<\/pre>\n<p>That gives us space to store 10 things, which sucks if the program suddenly needs 11. The array is of fixed size. So let&#8217;s do it the &#8220;right&#8221; way:<\/p>\n<pre lang=\"c\" line=\"1\">int*   things;\r\n\r\nthings = (int*)malloc (sizeof (int)*10);\r\n\/\/(A bunch of stuff happens. And then a long time later...\r\nfree (things);\r\n<\/pre>\n<p>We allocate the memory ourselves. If we need more, we can allocate more. But we need to ask for the memory ourselves, we can&#8217;t use more than we asked for, and we can&#8217;t forget to free it. It&#8217;s prone to mistakes and takes an unreasonable level of work and care for what should be a simple, low-stress task.<\/p>\n<p>And the best<span class='snote' title='1'>Not the best in all cases, but certainly the best for simple lists of unknown size.<\/span> way to do it:<\/p>\n<pre lang=\"c\" line=\"1\">std::vector<int>   things;<\/pre>\n<p>That&#8217;s actually the <del datetime=\"2015-03-02T08:24:54+00:00\">best<\/del> least awful solution so far. This makes a list that can grow or shrink or be re-ordered whenever you like and the compiler will worry about all that stupid memory management crap for you. But because this feature was bolted onto the language<span class='snote' title='2'>Does the standard library count as &#8220;part of the language&#8221;? I honestly don&#8217;t know.<\/span> so late in the lifespan of C\/C++, the syntax is strange, verbose, and ugly. If you make a mistake it will sometimes spew pages of gibberish error messages at you over a simple misplaced symbol. <\/p>\n<p>(Nitpick shield: There are a LOT more ways to store data. Linked lists, hash tables, whatever. But they&#8217;re kind of specialized and have other tradeoffs. These three are enough for us to consider right now.)<\/p>\n<p>Blow is proposing we take this simple idea and express it in a simple way. That&#8217;s it.<\/p>\n<p>I&#8217;m not totally sold on the thing where he moves the brackets around and adds exclamation marks four minutes later. I can&#8217;t tell if I don&#8217;t like it because there&#8217;s something wrong with it or if I don&#8217;t like it because it&#8217;s unfamiliar. But whatever. The important point is that a language could do better than we&#8217;re doing now. (And lots of modern languages do.)<\/p>\n<h3>1:14:00 NO HEADER FILES<\/h3>\n<p>C programs are nothing more than text files. But you generally don&#8217;t want to put your whole program into one text file. It would be gigantic and you&#8217;d never be able to find anything. So for the sake of organization, we put related ideas into the same file. All the code for handling space marines goes in SpaceMarine.cpp and the code for rockets goes in Projectiles.cpp and the code for sound effects goes in Audio.cpp. Or whatever. You can organize it however you like. <\/p>\n<p>So the functions in these files need to talk to each other. Maybe a rocket, when it detects it&#8217;s hit a space marine, needs to make them explode. So it will call SpaceMarineExplode (). So the compiler is working its way through Projectiles.cpp and it sees the use of SpaceMarineExplode (), but it doesn&#8217;t know what to do with it. It hasn&#8217;t read the space marine file<span class='snote' title='3'>IT actually reads each file in isolation. It&#8217;s&#8230; not a great system.<\/span>. It doesn&#8217;t know that SpaceMarineExplode () exists. Maybe I just broke up with my girlfriend Marnie, and in a fit of distress, teen angst, and Freudian slippage I&#8217;ve accidentally typed SpaceMarnieExplode (). How is the compiler supposed to know that one is correct and the other is wrong? What about when the phone rings, I hope it&#8217;s her, then realize it can&#8217;t be her, and won&#8217;t ever be her ever again, and I go face-down on the keyboard and as I&#8217;m sobbing snotty tears I manage to type kbggggggytyutgiiiiiiiii? How does the compiler know that SpaceMarineExplode () is a valid function and kbggggggytyutgiiiiiiiii isn&#8217;t?<\/p>\n<p>Well, in C we have &#8220;prototypes&#8221;. It&#8217;s just a single line of text<span class='snote' title='4'>You HOPE.<\/span> that says, &#8220;SpaceMarineExplode () is a thing. &#8220;It&#8217;s not in THIS text file, but I&#8217;ll define it in one of the other text files.&#8221; It&#8217;s a promise to the compiler that the thing will show up later.<\/p>\n<p>I don&#8217;t know enough about the history of the language, but I imagine this was done because C was invented in 1968 and computers had very little memory, they were very slow, and disk access was glacial. Compiling would take a long time. The last thing you wanted was for the compiler to have to read ALL THOSE TEXT FILES THERE MUST BE NEARLY 100 KILOBYTES OF THEM ARE YOU MAD and create an inventory of everything, and then go through them all AGAIN to do the actual compiling. <\/p>\n<p>To avoid this, we put all those prototypes in another text file called the header file. Like SpaceMarine.h. Then Projectiles.cpp can #include&lt;SpaceMarine.h&gt; which will bring in all those prototypes. Basically, we&#8217;re manually doing that messy &#8220;inventory&#8221; step manually. By typing things. Instead of letting a computer do it. <\/p>\n<p>This is a monumentally horrible way of doing things, and has been for decades. It gets even more fun when one header file needs to read another. So SpaceMarine.h will #include Weapons.h and that file will include Projectiles.h, which turns around and includes SpaceMarine.h, which is the file it&#8217;s already trying to compile. So the compiler goes crazy, locks up, or (if you&#8217;re using a modern compiler because you&#8217;re not a savage) gives you a cryptic error that lets you know about the circular reference without telling you what it is.  There are ways to protect against this, which involves stupid boilerplate code to the effect of &#8220;If you&#8217;ve read this file before, don&#8217;t read it again&#8221;, which probably seems like the kind of busywork that the compiler could probably sort out on its own if this paradigm hadn&#8217;t been designed a year before The Beatles broke up.<\/p>\n<p>So header files suck. They dump onto programmers really tedious busywork that some sort of compiler could easily take off our hands. Other languages don&#8217;t waste your time with this. <\/p>\n<p>So that&#8217;s the presentation. Hope you enjoyed it. Or found it educational. Or whatever. I have to go back to <del datetime=\"2015-03-04T08:55:43+00:00\">complaining about<\/del> <em>using<\/em> C++ to get work done.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We finally get to the good stuff. The discussion below contains a lot of the reasons I wanted to do this write-up in the first place. Link (YouTube) 1:00:00 Let&#8217;s Make an Array Type. Yeeeessss. This might seem trivial to a lot of people. This is basically just a fix for the std::vector&lt;int&gt; name; problem [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[],"class_list":["post-24516","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/24516","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=24516"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/24516\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=24516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=24516"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=24516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}