drawType/main.cpp

Go to the documentation of this file.
00001 /*
00002  * main.cpp
00003  *
00004  * Copyright (C) 2010  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  * Simple program to demonstrate the gamepad-vgfx library (using the vgfx
00008  * vector graphics engine + OpenGL drawer to draw gamepad types).
00009  */
00010 
00011 // includes --------------------------------------------------------------------
00012 #include <iostream>
00013 
00014 #include "gamepad-vgfx/gamepad-vgfx.h"
00015 #include "glut-demo/glut-demo.h"
00016 #include "glut-font/glut-font.h"
00017 #include "perf/perf.h"
00018 #include "vgfx-opengl-draw/vgfx-opengl-draw.h"
00019 
00020 
00021 static const int s_border       = 100;
00022 
00023 static const char * s_defaultLocale             = "en_US.UTF-8";
00024 
00025 
00026 ////////////////////////////////////////////////////////////////////////////////
00027 //
00028 //      static helper methods
00029 //
00030 ////////////////////////////////////////////////////////////////////////////////
00031 
00032 class Host : public glut::DemoHost {
00033 public:
00034         // constructor, destructor ---------------------------------------------
00035         ~Host(void) throw() { }
00036 
00037         // public class methods ------------------------------------------------
00038 
00039         // glut::DemoHost class interface methods ------------------------------
00040 
00041         bool displayAxes(void) { return false; }
00042         bool displayQueuedPolys(void) { return false; }
00043 
00044         void onInit(void) {
00045                         this->initialize(m_dataDir.c_str(), m_typeName.c_str());
00046                 }
00047 
00048         void display2D(int width, int height) {
00049                         m_counter++;
00050                         if (m_counter > 200) {
00051                                 m_counter = 0;
00052                                 ++m_iter;
00053                                 if (m_logical.end() == m_iter) {
00054                                         m_iter = m_logical.begin();
00055                                 }
00056                         }
00057 
00058                         // update current logical control
00059                         const char * logical = m_iter->c_str();
00060                         float intensity = 0.005 * m_counter;
00061                         m_renderer->setIntensity(logical, intensity);
00062                         m_renderer->setPosition(logical, 0.5, intensity);
00063 
00064                         int xBorder = s_border;
00065                         int yBorder = s_border;
00066 
00067                         // show name
00068                         glut::Font * font = glut::getDefaultFont();
00069                         const int bufsize = 1024;
00070                         char buffer[bufsize];
00071                         snprintf(buffer, bufsize, "Current control: %s",
00072                             logical);
00073                         glColor4f(1, 1, 1, 1);
00074                         font->display(xBorder + 2, height - yBorder + 16, 0,
00075                             buffer);
00076 
00077                         // show pretty (localized) name
00078                         const char * pretty = m_i18nMgr->getString(logical);
00079                         snprintf(buffer, bufsize, "Localized name: \"%s\"",
00080                             pretty);
00081                         font->display(xBorder + 2, height - yBorder + 30, 0,
00082                             buffer);
00083 
00084                         m_renderer->draw(xBorder, yBorder,
00085                             width - 2 * xBorder, height - 2 * yBorder);
00086                 }
00087 
00088         // static factory method -----------------------------------------------
00089         static smart_ptr<Host> create(IN const char * dataDir,
00090                                 IN const char * typeName) {
00091                         ASSERT(dataDir, "null");
00092                         ASSERT(typeName, "null");
00093 
00094                         smart_ptr<Host> local = new Host;
00095                         ASSERT(local, "out of memory");
00096                         local->m_dataDir = dataDir;
00097                         local->m_typeName = typeName;
00098 
00099                         return local;
00100                 }
00101 
00102 private:
00103         // private methods -----------------------------------------------------
00104         Host(void) throw() { }
00105         void initialize(IN const char * dataDir,
00106                                 IN const char * typeName) {
00107                         ASSERT(dataDir, "null");
00108                         ASSERT(typeName, "null");
00109 
00110                         // open up the filesystem mgr
00111                         smart_ptr<nstream::Manager> fsMgr =
00112                             nstream::getFilesystemManager(dataDir);
00113                         ASSERT_THROW(fsMgr,
00114                             "Failed to create filesystem manager for data dir: "
00115                             << dataDir);
00116                         smart_ptr<nstream::Folder> folder = fsMgr->getRoot();
00117                         ASSERT(folder, "null");
00118 
00119                         // create the gamepad manager
00120                         m_gamepadMgr = gamepad::Manager::create(folder);
00121                         ASSERT_THROW(m_gamepadMgr, "failed to create");
00122 
00123                         // find our type
00124                         int nTypes = m_gamepadMgr->getTypeCount();
00125                         smart_ptr<gamepad::Type> type;
00126                         for (int i = 0; i < nTypes; ++i) {
00127                                 type = m_gamepadMgr->getType(i);
00128                                 if (!type) {
00129                                         DPRINTF("Manager returned null type?");
00130                                         continue;
00131                                 }
00132 
00133                                 DPRINTF("Is '%s' what we're looking for?",
00134                                     type->getName());
00135                                 if (!strcmp(typeName, type->getName())) {
00136                                         break;  // yes!
00137                                 }
00138                                 type = NULL;    // no
00139                         }
00140 
00141                         // did we find it?
00142                         ASSERT_THROW(type,
00143                             "Failed to find a type with name: " << typeName);
00144 
00145                         // create vector graphics drawer
00146                         smart_ptr<vgfx::Drawer> drawer =
00147                             vgfx::createOpenGLDrawer();
00148                         ASSERT(drawer, "null");
00149 
00150                         m_renderer =
00151                             gamepad::TypeVgfxRenderer::create(type, drawer);
00152                         ASSERT(m_renderer, "null");
00153 
00154                         // get our localization manager
00155                         const char * locale = i18n::getHostLocale();
00156                         m_i18nMgr = gamepad::getTypeLocaleMgr(type, locale,
00157                             s_defaultLocale, folder);
00158                         ASSERT_THROW(m_i18nMgr,
00159                             "failed to create localization mgr");
00160 
00161                         // initialize
00162                         m_counter = 0;
00163                         type->getAllInputs(m_logical);
00164                         DPRINTF("Found %d total logical controls on gamepad",
00165                             (int) m_logical.size());
00166                         m_iter = m_logical.begin();
00167                 }
00168         
00169         // private member data -------------------------------------------------
00170         smart_ptr<gamepad::Manager>     m_gamepadMgr;
00171         smart_ptr<gamepad::TypeVgfxRenderer> m_renderer;
00172         smart_ptr<i18n::Manager>        m_i18nMgr;
00173         VecString                       m_logical;
00174         VecString::iterator             m_iter;
00175         std::string                     m_dataDir;
00176         std::string                     m_typeName;
00177         int                             m_counter;
00178 };
00179 
00180 
00181 ////////////////////////////////////////////////////////////////////////////////
00182 //
00183 //      entry point
00184 //
00185 ////////////////////////////////////////////////////////////////////////////////
00186 
00187 int
00188 main
00189 (
00190 IN int argc,
00191 IN const char * argv[]
00192 )
00193 {
00194         int retval = 0;
00195         ASSERT(3 == argc, "Usage: drawType <data-directory> <type-name>");
00196         const char * dataDir = argv[1];
00197         DPRINTF("Using data directory: '%s'", dataDir);
00198         const char * typeName = argv[2];
00199         DPRINTF("Using type name: '%s'", typeName);
00200 
00201         try {
00202 
00203                 const char * title = "Gamepad Type vgfx Drawer Demo";
00204 
00205                 smart_ptr<glut::DemoHost> host =
00206                     Host::create(dataDir, typeName);
00207                 ASSERT(host, "failed to create demo host");
00208 
00209                 glut::startDemo(argc, argv, title, host);
00210 
00211         } catch (std::exception& e) {
00212                 DPRINTF("Exception!  %s", e.what());
00213                 retval = 1;
00214         }
00215 
00216         perf::dumpTimingSummary(std::cerr);
00217 
00218         return retval;
00219 }
00220