vgfx-opengl-draw/test/test.cpp

Go to the documentation of this file.
00001 /*
00002  * test.cpp
00003  *
00004  * Copyright (C) 2010  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  * Demo program using the OpenGL vector graphics drawer.
00008  */
00009 
00010 // includes --------------------------------------------------------------------
00011 #include <iostream>
00012 #include <fstream>
00013 
00014 #include "glut-demo/glut-demo.h"
00015 #include "vgfx-opengl-draw/vgfx-opengl-draw.h"
00016 
00017 
00018 ////////////////////////////////////////////////////////////////////////////////
00019 //
00020 //      static helper methods
00021 //
00022 ////////////////////////////////////////////////////////////////////////////////
00023 
00024 class TestHost : public glut::DemoHost {
00025 public:
00026         // destructor ----------------------------------------------------------
00027         // public class methods ------------------------------------------------
00028         // glut::DemoHost class interface methods ------------------------------
00029         void display2D(IN int width, IN int height) {
00030                         ASSERT(m_root, "null");
00031                         ASSERT(m_drawer, "null");
00032 
00033                         // initialize OpenGL
00034                         glColor3f(1, 1, 1);
00035 
00036                         // actually draw
00037                         vgfx::rect_t drawRect(0, 0, width, height);
00038                         xform_2d_t T;
00039                         T.setTranslate(m_offset.x, m_offset.y);
00040                         T.scale(m_scale);
00041                         m_root->draw(m_drawer, drawRect, T);
00042                 }
00043 
00044         // static factory methods ----------------------------------------------
00045         static smart_ptr<glut::DemoHost> create(IN std::istream& stream,
00046                                 IN const char * rootId,
00047                                 IN float scale) {
00048                         perf::Timer timer("TestHost::create()");
00049                         ASSERT_THROW(stream.good(), "bad stream?");
00050                         ASSERT(rootId, "null");
00051                         ASSERT(scale, "null?");
00052 
00053                         smart_ptr<TestHost> local = new TestHost;
00054                         ASSERT(local, "out of memory");
00055 
00056                         local->initialize(stream, rootId, scale);
00057 
00058                         return local;
00059                 }
00060 private:
00061         // constructor ---------------------------------------------------------
00062         TestHost(void) throw() {
00063                         m_root = NULL;
00064                         m_scale = 1.0;
00065                 }
00066 
00067         // private helper methods ----------------------------------------------
00068         void initialize(IN std::istream& stream,
00069                                 IN const char * rootId,
00070                                 IN float scale) {
00071                         ASSERT_THROW(stream.good(), "bad stream?");
00072                         ASSERT(rootId, "null");
00073                         ASSERT(!m_root, "already have a root object?");
00074                         ASSERT(scale, "null scale?");
00075 
00076                         m_scale = scale;
00077 
00078                         // create vector graphics object map
00079                         m_objectMap = vgfx::ObjectMap::create();
00080                         ASSERT(m_objectMap, "null");
00081 
00082                         // process input request
00083                         bool want_undo = false;
00084                         std::string undo, diagnostic;
00085                         ASSERT_THROW(vgfx::processRequest(m_objectMap, stream,
00086                             want_undo, undo, diagnostic),
00087                             "Failed to process vgfx request!  Diagnostic: " <<
00088                             diagnostic);
00089 
00090                         DPRINTF("Object map contains %d objects",
00091                             (int) m_objectMap->size());
00092 
00093                         // should be able to find root object in map now
00094                         DPRINTF("Looking up object with id='%s'...", rootId);
00095                         m_root = m_objectMap->findObject(rootId);
00096                         ASSERT_THROW(m_root, "Failed to find object with id= '"
00097                             << rootId << "'");
00098 
00099                         // create an OpenGL-friendly drawer
00100                         m_drawer = vgfx::createOpenGLDrawer();
00101                         ASSERT(m_drawer, "null");
00102 
00103                         // recalculate bounding rects
00104                         xform_2d_t T;
00105                         T.setIdentity();
00106                         T.scale(m_scale);
00107                         m_root->recalcBoundingRect("*", m_drawer, T);
00108 
00109                         // get bounding rect
00110                         vgfx::rect_t r;
00111                         m_root->getBoundingRect(r);
00112                         r.dump("Bounding rect");
00113                         m_offset.x = -r.left;
00114                         m_offset.y = -r.top;
00115                 }
00116 
00117         // private member data -------------------------------------------------
00118         smart_ptr<vgfx::ObjectMap>      m_objectMap;
00119         smart_ptr<vgfx::Drawer>         m_drawer;
00120         vgfx::Primitive *               m_root;
00121         float                           m_scale;
00122         vgfx::point_t                   m_offset;
00123 };
00124 
00125 
00126 
00127 ////////////////////////////////////////////////////////////////////////////////
00128 //
00129 //      entry point
00130 //
00131 ////////////////////////////////////////////////////////////////////////////////
00132 
00133 int
00134 main
00135 (
00136 IN int argc,
00137 IN const char * argv[]
00138 )
00139 {
00140         int retval = 0;
00141         ASSERT(4 == argc,
00142             "Usage: vgfx-draw-test <vgfx_input_file> <root_object_id> <scale>");
00143         const char * filename = argv[1];
00144         const char * rootId = argv[2];
00145         float scale = atof(argv[3]);
00146         DPRINTF("Using vgfx input file: '%s'", filename);
00147         DPRINTF("ID of root object in file: '%s'", rootId);
00148         DPRINTF("Using scale=%f (from '%s')", scale, argv[3]);
00149 
00150         try {
00151                 // open file
00152                 std::ifstream stream(filename);
00153                 ASSERT_THROW(stream.good(), "problems opening file: " <<
00154                     filename);
00155 
00156                 // create host
00157                 smart_ptr<glut::DemoHost> host =
00158                     TestHost::create(stream, rootId, scale);
00159                 ASSERT(host, "null");
00160                 
00161                 // start opengl
00162                 std::string title = "OpenGL - Vector Graphics (vgfx) demo";
00163                 glut::startDemo(argc, argv, title.c_str(), host);
00164 
00165         } catch (std::exception& e) {
00166                 DPRINTF("EXCEPTION: %s", e.what());
00167                 retval = 1;
00168         }
00169 
00170         perf::dumpTimingSummary(std::cerr);
00171 
00172         return retval;
00173 }
00174