{"id":15633,"date":"2012-04-15T22:53:51","date_gmt":"2012-04-16T03:53:51","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=15633"},"modified":"2012-04-15T22:53:51","modified_gmt":"2012-04-16T03:53:51","slug":"the-gui-problem","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=15633","title":{"rendered":"The GUI Problem"},"content":{"rendered":"<p>You might remember that I wrote <a href=\"?p=9562\">a program for laying out comic strips<\/a>. I stopped using it when I stopped working on Stolen Pixels to become <a href=\"http:\/\/shamusyoung.com\/author\/?page_id=6\">an author<\/a>. I think about Comic Press now and again, feeling vaguely guilty that I&#8217;ve got this useful chunk of software sitting on my hard drive, basically going to waste. I should clean it up and release it to the public. Or do some tipjar-based development on it. Or something. <\/p>\n<p>And then I remember that Comic Press is in this oddball limbo state.  It was written in Visual Studio 6, which came out in 1998. It uses a ton of non-portable Windows code. I can&#8217;t even compile it now that I&#8217;ve migrated to Microsoft Visual C++ 2010 Express. I need to update all of the dialog and menu code to get it running. But if I&#8217;m going to do that, I should fix this interface to make it a little more portable. But if I&#8217;m going to do that&#8230;<\/p>\n<p>Hm.<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/comic_press1.jpg' class='insetimage'   alt='comic_press1.jpg' title='comic_press1.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>You might remember that a couple of years ago I wrote <a href=\"?p=9557\">a rant on how much of a monumental pain in the ass it is to use someone else&#8217;s library<\/a>, particularly in C++. I wrote that when I was looking for a GUI system to work with OpenGL. I just revisited the issue this week, and re-familiarized myself with all the annoyances I&#8217;d forgotten. <\/p>\n<p>A GUI system is what enables you to add standard controls to your program.  Buttons, menus, checkboxes, file open dialogs, scrollbars, edit windows, and so on. See, in Windows (or Linux, MacOS, etc.) the operating system can do all of that for you. With a few lines of code I can make a button&#8230;<\/p>\n<p><!--more--><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/windows_dialog.jpg' class='insetimage'   alt='windows_dialog.jpg' title='windows_dialog.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>I can tell the system, &#8220;stick a button right here&#8221;.  Then Windows (the operating system) will handle everything else.  It will draw the button, making sure it matches the theme used by this particular computer.  It will make the button look &#8220;pressed&#8221; or &#8220;disabled&#8221; at the proper times. It handles hotkey interactions. It tracks mouse movements and clicks to see where the user is clicking, and it will send me a little message if the user ever gets around to activating the button so I can take the appropriate action.  I do not hold up the Windows API as some great work of technological engineering.  It&#8217;s old and awkward and clunky and <a href=\"?p=1959\">not terribly efficient<\/a>. But it&#8217;s there, and it saves all of us Windows programmers from needing to write our interface code from scratch.  <\/p>\n<p>The problem is that we lose all of those features once we&#8217;re talking about a window that renders 3D stuff. For example, if I&#8217;m rendering a 3D scene in OpenGL:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gui2.jpg' class='insetimage'   alt='gui2.jpg' title='gui2.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Basically, you can&#8217;t do this:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gui1.jpg' class='insetimage'   alt='gui1.jpg' title='gui1.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>I could make a Windows-style window to sit in <em>front<\/em> of the 3D window, but that&#8217;s not the same thing. It&#8217;s the difference between using the chat window in the corner of your World of Warcraft screen and just having (say) a Notepad window positioned over that same area of the screen.  It doesn&#8217;t work. <\/p>\n<p>So once you&#8217;re doing the 3D graphics thing, you&#8217;re stuck making interfaces yourself. This is a lot of really fiddly work.  Most people don&#8217;t even realize how crazy complicated a scrollbar really is.  There&#8217;s buttons at the top and bottom for moving up and down a &#8220;line&#8221; at a time. There&#8217;s the scroll area, which should let the user jump to the given point in whatever thing they&#8217;re viewing. Then there&#8217;s the shuttle control itself, which changes size based on the ratio of the total document and the potion that is immediately visible. There&#8217;s interactions between the document and the scrollbar as the document changes size. Using the mousewheel on the document should move the scrollbar, and using the arrow keys on the scrollbar should move the document. This adds up to days of work, <em>just for a scrollbar<\/em>. Add in the time you&#8217;ll need for text windows, button bars, progress bars, sliders, spinner controls, and all the other things we take for granted, and you have months of work. <\/p>\n<p>Months!<\/p>\n<p>For an interface!<\/p>\n<p>A lot of this probably explains why game interfaces are so primitive. <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gui3.jpg' class='insetimage'   alt='gui3.jpg' title='gui3.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>I don&#8217;t imagine we&#8217;ll be seeing a text box with a scroll bar in Minecraft anytime soon. I&#8217;m betting Notch had to program his interface from scratch. If I made an interface by hand, this is exactly what it would look like: Buttons and sliders. Those are far easier to implement than other types of controls. Note how the difficulty selector is a button with four states: Peaceful, Easy, Normal, Hard. In a full-featured windowed environment, usability standards would normally call for a drop-down box in this context. <\/p>\n<p>Over the years, a lot of enterprising developers have taken a stab at solving this problem.  They do this by making a library, which is a big ol&#8217; lump of code that you can add to your project.  Some of the notable ones are:<\/p>\n<p><a href=\"http:\/\/www.cegui.org.uk\/\">Crazy Eddie&#8217;s GUI system<\/a><br \/>\n<a href=\"http:\/\/glgooey.sourceforge.net\/\">GLGooey<\/a><br \/>\n<a href=\"http:\/\/turska.sourceforge.net\/\">Turska<\/a><br \/>\n<a href=\"http:\/\/glam.sourceforge.net\/\">GLAM<\/a><br \/>\n<a href=\"http:\/\/glui.sourceforge.net\/\">GLUI<\/a><br \/>\n<a href=\"http:\/\/www.bramstein.com\/projects\/gui\/\">OpenGL GUI<\/a><br \/>\n<a href=\"http:\/\/libufo.sourceforge.net\/\">LibUFO<\/a><\/p>\n<p>Some of these were abandoned before they were finished. Some are poorly documented. A lot of them are just hard to use. Some are stuck looking like Windows 95. Some are difficult to compile, or have challenging dependencies. <\/p>\n<p>Ideally, a library should be as simple as possible. If you make a library to handle the physics in a game environment, you shouldn&#8217;t include a bunch of crap for generating sound effects. Yes, the programmer using your library will probably need sound effects, but they might already have a sound system.  That extra sound code stuff will be all the more stuff that they have to compile. It will create more dependencies and make the library that much harder to learn, integrate, and debug.<\/p>\n<p>OpenGL is a perfectly focused library. It does rendering, and only rendering. It doesn&#8217;t know how to load images. It doesn&#8217;t talk to your file system for saving and loading data. It has no physics, no particle engine, no support for 3D models. No sound, no joystick support, no special effects. It&#8217;s concerned with the low-level, nitty-gritty details of drawing polygons. If you want to load in a texture map, you load in the image (JPG, PNG, BMP, TIFF, etc) yourself (you&#8217;ll need <a href=\"?p=12293\">another library<\/a> for doing that) and just feed OpenGL the raw image data. It doesn&#8217;t concern itself with where images come from, where they reside on disk, or what they&#8217;re named. <\/p>\n<p>I said it&#8217;s a perfectly <em>focused<\/em> library. I did not say it was a perfect library.  It concentrates on doing one thing, and doing it well. If you need more you can get other libraries to do other things.  If you want a lot more help you can use one of the many game engines built on top of OpenGL. But the core is there, uncluttered, if that&#8217;s what you need.  <\/p>\n<p>It&#8217;s fine for me to sit here and make broad statements about keeping libraries simple, but in the real world this kind of platonic ideal is actually really hard to achieve. The problem here is that you need a lot of stuff to make a proper GUI. You need fonts. (You can&#8217;t have an interface without fonts!) You need to be able to load images.  (Because those interface buttons have to be made of <em>something<\/em>.) You need to process keyboard and mouse input. (Otherwise, what&#8217;s the point?) It needs access to the rendering systems.  (So that it can draw its controls.) This means your &#8220;interface&#8221; library needs hooks into the filesystem, rendering, an image loader, peripheral I\/O, and a font loader. <\/p>\n<p>This is a nightmare. A lot of those things are messy, non-portable systems, which means they will vary from one operating system to the next. It means the library will need to include lots of other libraries, which will lead back to <a href=\"?p=9557\">my original rant<\/a>.<\/p>\n<p><a href=\"http:\/\/awesomium.com\/\">Awesomium<\/a> is about the most promising development so far.  It&#8217;s a library that lets you stick a fully functional web window into OpenGL. So you could put your interface into web page form, stick it on a polygon, and render it that way. The downside is that HTML doesn&#8217;t have native support for all types of interface controls. (Progress bars and sliders are two important ones.)  You can create these manually using Javascript, but then you&#8217;re back to hand-coding your own interface elements.<\/p>\n<p>It&#8217;s a tough problem, which is why I keep walking away from it every time I revisit Comic Press.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You might remember that I wrote a program for laying out comic strips. I stopped using it when I stopped working on Stolen Pixels to become an author. I think about Comic Press now and again, feeling vaguely guilty that I&#8217;ve got this useful chunk of software sitting on my hard drive, basically going to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[],"class_list":["post-15633","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\/15633","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=15633"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/15633\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=15633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=15633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=15633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}