dialog-draw/test/test.cpp

Go to the documentation of this file.
00001 /*
00002  * test.cpp
00003  *
00004  * Copyright (C) 2009  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  * Program to test the dialog-draw library.
00008  */
00009 
00010 // includes --------------------------------------------------------------------
00011 #include <iostream>
00012 #include <fstream>
00013 
00014 #include "common/wave_ex.h"
00015 #include "datahash/datahash.h"
00016 #include "dialog/dialog.h"
00017 #include "dialog/request.h"
00018 #include "dialog-draw/dialog-draw.h"
00019 #include "glut-font/glut-font.h"
00020 #include "perf/perf.h"
00021 #include "util/file.h"
00022 #include "wave-glut/camera.h"
00023 #include "wave-glut/glut_2d.h"
00024 #include "wave-glut/wave-glut.h"
00025 
00026 
00027 
00028 static const int s_defaultWidth                 = 800;
00029 static const int s_defaultHeight                = 600;
00030 
00031 static float s_dist                             = 10.0;
00032 
00033 
00034 ////////////////////////////////////////////////////////////////////////////////
00035 //
00036 //      static helper methods
00037 //
00038 ////////////////////////////////////////////////////////////////////////////////
00039 
00040 static void
00041 addDialog
00042 (
00043 IN dialog::Manager * mgr,
00044 IN dialog::Host * host
00045 )
00046 {
00047         ASSERT(mgr, "null");
00048         ASSERT(host, "null");
00049 
00050         dialog::ContainerRequest crRoot;
00051 
00052         dialog::ContainerRequest cr(dialog::ContainerRequest::eAxis_Horizontal);
00053 
00054 
00055         dialog::ContainerRequest cr2;
00056         cr2.addLabel("Sample dialog");
00057         cr2.addButton("submit1", "button-1");
00058         cr2.addButton("submit2", "button-2");
00059         cr2.addTextbox("text1", "foo", 12);
00060 
00061         cr.addContainer(cr2);
00062 
00063         dialog::ContainerRequest cr3;
00064         cr3.addLabel("Sample d2");
00065         cr3.addButton("submit3", "button-3");
00066 
00067         cr.addContainer(cr3);
00068 
00069         crRoot.addLabel("Start of dialog");
00070         crRoot.addContainer(cr);
00071         crRoot.addLabel("End of dialog");
00072 
00073         smart_ptr<Datahash> data = crRoot.getDatahash();
00074         ASSERT(data, "null");
00075 
00076         mgr->createDialog("d1", 32, 32, data, host);
00077 }
00078 
00079 
00080 
00081 class TestHost : public glut::Host,
00082                 public dialog::Host {
00083 public:
00084         TestHost(IN const char * file) { m_fontFile = file; }
00085         ~TestHost(void) throw() { }
00086 
00087         // dialog::Host class interface methods --------------------------------
00088         void notifySubmit(IN const char * id,
00089                                 IN const Datahash * hash) {
00090                         DPRINTF("Submit event, id='%s'", id);
00091                 }
00092 
00093         // glut::Host class interface methods ----------------------------------
00094         void init(void) {
00095                 // create DES key(!)
00096                 smart_ptr<crypto::DESKey> des = crypto::DESKey::create();
00097                 ASSERT(des, "failed to create DES encryption key");
00098 
00099                 // get font stream
00100                 std::string parentDir;
00101                 GetParentDirectory(m_fontFile.c_str(), parentDir);
00102                 smart_ptr<nstream::Manager> mgr =
00103                     nstream::getFilesystemManager(parentDir.c_str());
00104                 ASSERT(mgr, "null");
00105                 smart_ptr<nstream::Stream> fontStream =
00106                     nstream::openNamedStream(mgr, GetFilename(m_fontFile.c_str()));
00107                 ASSERT(fontStream, "null");
00108 
00109                 // get font
00110                 m_fontMgr = glut::FontManager::create();
00111                 ASSERT(m_fontMgr, "null");
00112 
00113                 // set up dialog drawer data
00114                 dialog::ogl_dialog_draw_t odd;
00115                 odd.font = m_fontMgr->getFont(fontStream);
00116                 ASSERT(odd.font, "failed to acquire OpenGL font");
00117                 ASSERT(odd.isValid(),
00118                     "should have valid OpenGL dialog draw data");
00119 
00120                 // create dialog drawer
00121                 smart_ptr<dialog::Drawer> drawer =
00122                     dialog::createOpenGLDrawer(odd);
00123                 ASSERT(drawer, "null");
00124 
00125                 // create dialog manager
00126                 m_dialogMgr = dialog::Manager::create(des, drawer);
00127                 ASSERT(m_dialogMgr, "failed to create dialog manager");
00128 
00129                 // construct a sample dialog
00130                 addDialog(m_dialogMgr, this);
00131 
00132                 // other initialization
00133                 glShadeModel(GL_SMOOTH);
00134 
00135                 glEnable(GL_LIGHT0);
00136 
00137                 glFrontFace(GL_CCW);
00138                 glEnable(GL_CULL_FACE);
00139                 glCullFace(GL_BACK);
00140         }
00141 
00142         void display(IN int w, IN int h) {
00143                 perf::time_t now = perf::getNow();
00144                 static glut::fps_t s_fps(now);
00145                 s_fps.tick(now);
00146 
00147                 glEnable(GL_LIGHTING);
00148                 glEnable(GL_LIGHT0);
00149                 glEnable(GL_DEPTH_TEST);
00150                 glEnable(GL_TEXTURE_2D);
00151                 glEnable(GL_CULL_FACE);
00152                 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00153 
00154                 // clear buffers (takes no effect until drawing much later)
00155                 glClearColor(0.35, 0.35, 0.35, 1.0);
00156                 glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
00157 
00158                 // set up camera, viewer
00159                 glut::camera_t camera;
00160                 camera.setAspect(w, h);
00161                 glut::Viewer viewer;
00162                 viewer.setPosition(point3d_t(0, 0, -s_dist));
00163 
00164                 // set up glut for drawing
00165                 glut::setOpenGLProjection(camera);
00166                 glut::setOpenGLViewer(viewer);
00167 
00168                 // set up lighting
00169                 float lightAmbient[] = { 0.8, 0.8, 0.8, 1.0 };
00170                 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lightAmbient);
00171 
00172                 // rotate object
00173                 static float angle = 0.0;
00174                 angle += 0.5;
00175                 glMatrixMode(GL_MODELVIEW);
00176                 glPushMatrix();
00177                 glRotatef(angle, 0.0, 1.0, 0.0);
00178 
00179                 // draw object
00180                 glutSolidSphere(4.0, 16, 16);
00181                 glPopMatrix();
00182 
00183                 glDisable(GL_LIGHTING);
00184                 glDisable(GL_LIGHT0);
00185                 glDisable(GL_DEPTH_TEST);
00186                 glDisable(GL_TEXTURE_2D);
00187                 glDisable(GL_CULL_FACE);
00188 
00189                 glut::push2D(w, h);
00190 
00191                 // dialog
00192                 m_dialogMgr->display(w, h);
00193 
00194                 // output FPS
00195                 glut::Font * font = glut::getDefaultFont();
00196                 char buffer[128];
00197                 sprintf(buffer, "%d fps", (int) s_fps.getFPS());
00198                 font->display(1, 12, 0, buffer);
00199                 sprintf(buffer, "%d x %d", w, h);
00200                 font->display(w - 100, 12, 0, buffer);
00201 
00202                 glut::pop2D();
00203         }
00204 
00205         eBehavior tick(void) {
00206                 return eContinue;
00207         }
00208 
00209         int shutdown(void) {
00210                 perf::dumpTimingSummary(std::cout);
00211                 return 0;
00212         }
00213 
00214         eBehavior mouseMove(IN int x, IN int y) {
00215                         m_dialogMgr->cursor(x, y);
00216                         return eContinue;
00217                 }
00218 
00219         eBehavior mouseButton(IN int button, IN int state, IN int x, IN int y) {
00220                         DPRINTF("button = %d,    state = %d", button, state);
00221                         m_dialogMgr->button(button, state, x, y);
00222                         return eContinue;
00223                 }
00224 
00225         eBehavior keyboard(IN int key, IN int mods) {
00226                 if ('=' == key)
00227                         s_dist += 1.0;
00228                 if ('-' == key)
00229                         s_dist -= 1.0;
00230         
00231                 if (s_dist < 1.0)
00232                         s_dist = 1.0;
00233 
00234                 if ('l' == key)
00235                         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
00236 
00237                 if ('s' == key)
00238                         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00239 
00240                 if (27 == key)
00241                         return eExit;
00242 
00243                 m_dialogMgr->keyboard(key, mods);
00244 
00245                 return eContinue;
00246         }
00247 
00248 private:
00249         std::string                             m_fontFile;
00250         smart_ptr<dialog::Manager>              m_dialogMgr;
00251         smart_ptr<glut::FontManager>            m_fontMgr;
00252 };
00253 
00254 
00255 
00256 ////////////////////////////////////////////////////////////////////////////////
00257 //
00258 //      entry point
00259 //
00260 ////////////////////////////////////////////////////////////////////////////////
00261 
00262 int
00263 main
00264 (
00265 IN int argc,
00266 IN const char * argv[]
00267 )
00268 {
00269         ASSERT(2 == argc,
00270             "usage: dialog-draw-test <path-to-font>");
00271         const char * fontFile = argv[1];
00272         DPRINTF("Using font: %s", fontFile);
00273         try {
00274                 // main loop
00275                 smart_ptr<glut::Host> host = new TestHost(fontFile);
00276                 ASSERT(host, "out of memory");
00277                 glut::start(argc, argv,
00278                         s_defaultWidth, s_defaultHeight,
00279                         "wave-glut dialog draw test",
00280                         NULL,
00281                         host);
00282 
00283         } catch (std::exception& e) {
00284                 DPRINTF("Exception: %s", e.what());
00285         }
00286 
00287         // only reach here on error
00288         return 1;
00289 }
00290