New lua versions
[ryzomcore.git] / studio / src / plugins / example / qnel_widget.cpp
blobd0821eb6ba4da124a3f8d22f6e7716b4c8f35d13
1 // Object Viewer Qt - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2011 Dzmitry KAMIAHIN (dnk-88) <dnk-88@tut.by>
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2010 Winch Gate Property Limited
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "qnel_widget.h"
22 // STL includes
24 // Qt includes
25 #include <QtCore/QTimer>
26 #include <QtGui/QResizeEvent>
28 // NeL includes
29 #include <nel/misc/event_server.h>
30 #include <nel/misc/debug.h>
31 #include <nel/3d/u_driver.h>
32 #include <nel/3d/driver_user.h>
34 namespace NLQT
37 QNLWidget::QNLWidget(QWidget *parent)
38 : QNeLWidget(parent),
39 m_driver(NULL),
40 m_initialized(false),
41 m_interval(25)
43 setAttribute(Qt::WA_OpaquePaintEvent);
44 setAttribute(Qt::WA_NoSystemBackground);
45 setAttribute(Qt::WA_PaintOnScreen);
46 setMouseTracking(true);
47 setFocusPolicy(Qt::StrongFocus);
49 init();
50 #ifdef Q_OS_LINUX
51 makeCurrent();
52 #endif
53 m_mainTimer = new QTimer(this);
54 connect(m_mainTimer, SIGNAL(timeout()), this, SLOT(updateRender()));
57 QNLWidget::~QNLWidget()
59 release();
62 void QNLWidget::init()
64 // create the driver
65 m_driver = NL3D::UDriver::createDriver(0, false);
66 nlassert(m_driver);
68 // initialize the nel 3d viewport
69 m_driver->setDisplay((nlWindow)winId(), NL3D::UDriver::CMode(width(), height(), 32));
71 // set the cache size for the font manager(in bytes)
72 m_driver->setFontManagerMaxMemory(2097152);
74 m_initialized = true;
77 void QNLWidget::release()
79 m_mainTimer->stop();
80 delete m_mainTimer;
81 if (m_initialized)
83 m_driver->release();
84 delete m_driver;
85 m_driver = NULL;
89 void QNLWidget::setInterval(int msec)
91 m_interval = msec;
92 m_mainTimer->setInterval(msec);
95 void QNLWidget::setBackgroundColor(NLMISC::CRGBA backgroundColor)
97 m_backgroundColor = backgroundColor;
100 void QNLWidget::updateRender()
102 if (isVisible())
104 if (m_initialized)
105 m_driver->EventServer.pump();
106 Q_EMIT updateData();
108 // Calc FPS
109 static sint64 lastTime = NLMISC::CTime::getPerformanceTime ();
110 sint64 newTime = NLMISC::CTime::getPerformanceTime ();
111 m_fps = float(1.0 / NLMISC::CTime::ticksToSecond (newTime-lastTime));
112 lastTime = newTime;
114 if (m_initialized && !m_driver->isLost())
116 //_driver->activate();
117 m_driver->clearBuffers(m_backgroundColor);
118 Q_EMIT updatePreRender();
120 Q_EMIT updatePostRender();
121 // swap 3d buffers
122 m_driver->swapBuffers();
127 void QNLWidget::showEvent(QShowEvent *showEvent)
129 QWidget::showEvent(showEvent);
130 m_driver->activate();
131 m_mainTimer->start(m_interval);
134 void QNLWidget::hideEvent(QHideEvent *hideEvent)
136 m_mainTimer->stop();
137 QWidget::hideEvent(hideEvent);
140 #if defined(NL_OS_WINDOWS)
142 typedef bool (*winProc)(NL3D::IDriver *driver, HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
144 bool QNLWidget::winEvent(MSG *message, long *result)
146 if (m_driver && m_driver->isActive())
148 NL3D::IDriver *driver = dynamic_cast<NL3D::CDriverUser *>(m_driver)->getDriver();
149 if (driver)
151 winProc proc = (winProc)driver->getWindowProc();
152 return proc(driver, message->hwnd, message->message, message->wParam, message->lParam);
156 return false;
159 #elif defined(NL_OS_MAC)
161 typedef bool (*cocoaProc)(NL3D::IDriver *, const void *e);
163 bool QNLWidget::macEvent(EventHandlerCallRef caller, EventRef event)
165 if(caller)
166 nlerror("You are using QtCarbon! Only QtCocoa supported, please upgrade Qt");
168 if (m_driver && m_driver->isActive())
170 NL3D::IDriver *driver = dynamic_cast<NL3D::CDriverUser *>(m_driver)->getDriver();
171 if (driver)
173 cocoaProc proc = (cocoaProc)driver->getWindowProc();
174 return proc(driver, event);
178 return false;
181 #elif defined(NL_OS_UNIX)
183 typedef bool (*x11Proc)(NL3D::IDriver *drv, XEvent *e);
185 bool QNLWidget::x11Event(XEvent *event)
187 if (m_driver && m_driver->isActive())
189 NL3D::IDriver *driver = dynamic_cast<NL3D::CDriverUser *>(m_driver)->getDriver();
190 if (driver)
192 x11Proc proc = (x11Proc)driver->getWindowProc();
193 return proc(driver, event);
197 return false;
199 #endif
201 } /* namespace NLQT */