Advertisement
Guest User

Untitled

a guest
Apr 10th, 2012
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.46 KB | None | 0 0
  1. #include <application.h>
  2.  
  3. #include <stdexcept>
  4.  
  5. #include <opengl/types.h>
  6.  
  7. #include <QHBoxLayout>
  8. #include <QScrollArea>
  9. #include <QToolBar>
  10. #include <QMenuBar>
  11. #include <QLayout>
  12. #include <QDir>
  13. #include <QSettings>
  14. #include <QTime>
  15. #include <QGLWidget>
  16. #include <QGLFormat>
  17. #include <QFontDatabase>
  18. #include <QTranslator>
  19. #include <QLocale>
  20. #include <QLibraryInfo>
  21. #include <QFileDialog>
  22.  
  23. namespace noggit
  24. {
  25.   application::application (int& argc, char** argv)
  26.     : QApplication (argc, argv)
  27.     , _settings (NULL)
  28.   {
  29. #ifndef _DEBUG
  30.     RegisterErrorHandlers();
  31. #endif
  32.     InitLogging();
  33.  
  34.     _settings = new QSettings (this);
  35.  
  36.     QTranslator* qtTranslator (new QTranslator (this));
  37.     qtTranslator->load ("qt_" + QLocale::system().name(), QLibraryInfo::location (QLibraryInfo::TranslationsPath));
  38.     installTranslator (qtTranslator);
  39.  
  40.     QTranslator* appTranslator (new QTranslator (this));
  41.     appTranslator->load ("noggit_" + QLocale::system().name());
  42.     installTranslator (appTranslator);
  43.  
  44.     qsrand (QTime::currentTime().msec());
  45.  
  46.     initialize_video();
  47.     Menu* map_selection_menu (new Menu (NULL));
  48.     connect (map_selection_menu, SIGNAL (create_world_view_request ()), SLOT (create_main_window()));
  49.     map_selection_menu->show();
  50.   }
  51.  
  52.   application::~application()
  53.   {
  54.   }
  55.  
  56.   class dummy_gl_widget : public QGLWidget
  57.   {
  58.   public:
  59.     dummy_gl_widget (const QGLFormat& format)
  60.       : QGLWidget (format)
  61.     {
  62.       updateGL();
  63.     }
  64.   protected:
  65.     virtual void initializeGL()
  66.     {
  67.       const GLenum err (glewInit());
  68.       if( GLEW_OK != err )
  69.       {
  70.         LogError << "GLEW: " << glewGetErrorString (err) << std::endl;
  71.         throw std::runtime_error ("unable to initialize glew.");
  72.       }
  73.  
  74.       //! \todo Fallback for old and bad platforms.
  75.       if (!glGenBuffers) glGenBuffers = glGenBuffersARB;
  76.       if (!glBindBuffer) glBindBuffer = glBindBufferARB;
  77.       if (!glBufferData) glBufferData = glBufferDataARB;
  78.  
  79.       LogDebug << "GL: Version: " << glGetString (GL_VERSION) << std::endl;
  80.       LogDebug << "GL: Vendor: " << glGetString (GL_VENDOR) << std::endl;
  81.       LogDebug << "GL: Renderer: " << glGetString (GL_RENDERER) << std::endl;
  82.     }
  83.   };
  84.  
  85.   void application::initialize_video()
  86.   {
  87.     if (!QGLFormat::hasOpenGL())
  88.       LogError << "Your system does not support OpenGL. Sorry, this application can't run without it." << std::endl;
  89.  
  90.     QGLFormat format;
  91.     format.setStencilBufferSize (1);
  92.     format.setDepthBufferSize (16);
  93.     format.setAlphaBufferSize (8);
  94.  
  95.     if (_settings->value ("antialiasing").toBool())
  96.     {
  97.       format.setSampleBuffers (true);
  98.       format.setSamples (4);
  99.     }
  100.  
  101.     _dummy_gl_widget = new dummy_gl_widget (format);
  102.   }
  103.  
  104.   void application::create_main_window()
  105.   {
  106.     MainWindow* main_window(_dummy_gl_widget, _settings);
  107.       const int xResolution (_settings->value ("resolution/x").toInt());
  108.       const int yResolution (_settings->value ("resolution/y").toInt());
  109.       main_window->show();
  110.       main_window->resize (xResolution, yResolution);
  111.     }
  112.   }
  113.  
  114.   application& app()
  115.   {
  116.     return* reinterpret_cast<application*> (qApp);
  117.   }
  118.  
  119.   MainWindow::MainWindow(QGLWidget* _dummy_gl_widget, QSettings* _settings)
  120.   {
  121.     setWindowTitle("Application");
  122.  
  123.     QWidget* centralWidget (new QWidget);
  124.     setCentralWidget(centralWidget);
  125.  
  126.     MapView* map_view(new MapView ( _dummy_gl_widget, centralWidget));
  127.  
  128.     QScrollArea* glWidgetArea (new QScrollArea);
  129.     glWidgetArea->setWidget(map_view);
  130.     glWidgetArea->setWidgetResizable(true);
  131.     glWidgetArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  132.     glWidgetArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  133.     glWidgetArea->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
  134.     glWidgetArea->setMinimumSize(480, 480);
  135.  
  136.     QHBoxLayout* widgetLayout (new QHBoxLayout);
  137.     widgetLayout->addWidget(_dummy_gl_widget);
  138.     centralWidget->setLayout(widgetLayout);
  139.   }
  140.  
  141.   QToolBar* MainWindow::tool_bar()
  142.   {
  143.       return _toolBar;
  144.   }
  145. }
  146.  
  147. int main (int argc, char* argv[]);
  148.  
  149. #ifdef Q_WS_WIN
  150. int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  151. {
  152.   return main (__argc, __argv);
  153. }
  154. #endif
  155.  
  156. int main (int argc, char* argv[])
  157. {
  158.   try
  159.   {
  160.     application application (argc, argv);
  161.  
  162.     return application.exec();
  163.   }
  164.   catch (const std::exception& e)
  165.   {
  166.     LogError << "Unrecoverable error: " << e.what() << std::endl;
  167.   }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement