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