1 // Object Viewer Qt - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2011 Dzmitry KAMIAHIN (dnk-88) <dnk-88@tut.by>
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2010 Winch Gate Property Limited
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"
25 #include <QtCore/QTimer>
26 #include <QtGui/QResizeEvent>
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>
37 QNLWidget::QNLWidget(QWidget
*parent
)
43 setAttribute(Qt::WA_OpaquePaintEvent
);
44 setAttribute(Qt::WA_NoSystemBackground
);
45 setAttribute(Qt::WA_PaintOnScreen
);
46 setMouseTracking(true);
47 setFocusPolicy(Qt::StrongFocus
);
53 m_mainTimer
= new QTimer(this);
54 connect(m_mainTimer
, SIGNAL(timeout()), this, SLOT(updateRender()));
57 QNLWidget::~QNLWidget()
62 void QNLWidget::init()
65 m_driver
= NL3D::UDriver::createDriver(0, false);
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);
77 void QNLWidget::release()
89 void QNLWidget::setInterval(int msec
)
92 m_mainTimer
->setInterval(msec
);
95 void QNLWidget::setBackgroundColor(NLMISC::CRGBA backgroundColor
)
97 m_backgroundColor
= backgroundColor
;
100 void QNLWidget::updateRender()
105 m_driver
->EventServer
.pump();
109 static sint64 lastTime
= NLMISC::CTime::getPerformanceTime ();
110 sint64 newTime
= NLMISC::CTime::getPerformanceTime ();
111 m_fps
= float(1.0 / NLMISC::CTime::ticksToSecond (newTime
-lastTime
));
114 if (m_initialized
&& !m_driver
->isLost())
116 //_driver->activate();
117 m_driver
->clearBuffers(m_backgroundColor
);
118 Q_EMIT
updatePreRender();
120 Q_EMIT
updatePostRender();
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
)
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();
151 winProc proc
= (winProc
)driver
->getWindowProc();
152 return proc(driver
, message
->hwnd
, message
->message
, message
->wParam
, message
->lParam
);
159 #elif defined(NL_OS_MAC)
161 typedef bool (*cocoaProc
)(NL3D::IDriver
*, const void *e
);
163 bool QNLWidget::macEvent(EventHandlerCallRef caller
, EventRef event
)
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();
173 cocoaProc proc
= (cocoaProc
)driver
->getWindowProc();
174 return proc(driver
, event
);
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();
192 x11Proc proc
= (x11Proc
)driver
->getWindowProc();
193 return proc(driver
, event
);
201 } /* namespace NLQT */