00001 #ifndef CGLSURFACE_H 00002 #define CGLSURFACE_H 00003 00004 /****************************************************************************\ 00005 * REQUIRED HEADERS * 00006 \****************************************************************************/ 00007 00008 //UI 00009 #include "ui_CGLSurface.h" 00010 //QT 00011 #include <QGLWidget> 00012 #include <QSize> 00013 #include <QMutex> 00014 #include <QWaitCondition> 00015 #include <QPainter> 00016 //OWN 00017 #include "Definitions.h" 00018 #include "Memory/mmgr.h" 00019 #include "Memory/TSmartPointer.h" 00020 #include "Input/IInputHandler.h" 00021 #include "CGLRenderThread.h" 00022 00023 /****************************************************************************\ 00024 * DEFINITIONS * 00025 \****************************************************************************/ 00026 00027 /****************************************************************************\ 00028 * CLASS * 00029 \****************************************************************************/ 00030 class CGLSurface : public QGLWidget 00031 { 00032 Q_OBJECT 00033 00034 friend class CGLRenderThread; 00035 00036 /**************************************************************************\ 00037 * PUBLIC CLASS FUNCTIONS * 00038 \**************************************************************************/ 00039 public: 00040 00041 CGLSurface(QGLFormat & requestFormat,QWidget *parent = 0); 00042 ~CGLSurface(); 00043 00044 //manual update 00045 inline void Update(){Render();} 00046 void StartAnimate(int ms); 00047 void EndAnimate(); 00048 00049 //switch threaded rendering 00050 void EnableThread(bool tf); 00051 void EnableOverlay(bool tf); 00052 00053 //forward input to Handlers 00054 void registerInputHandler(TSmartPointer<IInputHandler> handler); 00055 void unregisterInputHandler(TSmartPointer<IInputHandler> handler); 00056 00057 //make the widgets rendering context current 00058 void lockGLContext(); 00059 void unlockGLContext(); 00060 00061 /**************************************************************************\ 00062 * PROTECTED CLASS FUNCTIONS * 00063 \**************************************************************************/ 00064 protected: 00065 00066 //virtual functions that can be implemented by a deriving class 00067 virtual void onInitialize(); 00068 virtual void onResize(); 00069 virtual void onDraw(); 00070 virtual void onOverlayDraw(QPainter * painter){} 00071 virtual void onAnimate(){} 00072 virtual void onFinishRendering(); 00073 00074 //virtual function for redirecting output 00075 virtual void onLOG(QString &){} 00076 virtual void onLOG(const char*){} 00077 00078 //widget input events 00079 int oldPosX,dx,oldPosY,dy; 00080 virtual void enterEvent(QEvent *event); 00081 virtual void leaveEvent(QEvent *event); 00082 virtual void keyPressEvent(QKeyEvent *event); 00083 virtual void keyReleaseEvent(QKeyEvent *event); 00084 virtual void mouseMoveEvent(QMouseEvent *event); 00085 virtual void mousePressEvent(QMouseEvent *event); 00086 virtual void mouseReleaseEvent(QMouseEvent *event); 00087 virtual void contextMenuEvent(QContextMenuEvent *event); 00088 00089 //can be queried by deriving class 00090 int viewportWidth; 00091 int viewportHeight; 00092 00093 /**************************************************************************\ 00094 * PRIVATE CLASS FUNCTIONS * 00095 \**************************************************************************/ 00096 private: 00097 00098 //Logging 00099 void Log(const char *txt){onLOG(txt);} 00100 00101 //thread sync 00102 void initRendering(); 00103 void finishRendering(); 00104 00105 //render 00106 void Render(); 00107 00108 //widget events 00109 void resizeEvent(QResizeEvent *event); 00110 void paintEvent(QPaintEvent *event); 00111 void closeEvent(QCloseEvent *event); 00112 void showEvent(QShowEvent *event); 00113 void timerEvent(QTimerEvent *){onAnimate();} 00114 00115 /**************************************************************************\ 00116 * PRIVATE CLASS MEMBERS * 00117 \**************************************************************************/ 00118 private: 00119 00120 //widget 00121 Ui::CGLSurface ui; 00122 00123 //renderthread and current inputHandler 00124 CGLRenderThread renderer; 00125 TSmartPointer<IInputHandler> pInputHandler; 00126 00127 //thread sync 00128 QMutex render_mutex; 00129 QWaitCondition render_condition; 00130 00131 //renderOverlay 00132 QPainter * pPainter; 00133 bool bRenderWithOverlay; 00134 00135 //state 00136 bool useRenderThread; 00137 bool initialized; 00138 int timerID; 00139 }; 00140 00141 #endif // CGLSURFACE_H