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
6 // Copyright (C) 2011 Matt RAYKOWSKI (sfb) <matt.raykowski@gmail.com>
8 // This program is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Affero General Public License as
10 // published by the Free Software Foundation, either version 3 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU Affero General Public License for more details.
18 // You should have received a copy of the GNU Affero General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "qnel_widget.h"
26 #include <QtCore/QTimer>
27 #include <QtGui/QResizeEvent>
30 #include <nel/misc/event_server.h>
31 #include <nel/misc/debug.h>
32 #include <nel/3d/u_driver.h>
33 #include <nel/3d/driver_user.h>
38 QNLWidget::QNLWidget(QWidget
*parent
)
44 setMouseTracking(true);
45 setFocusPolicy(Qt::StrongFocus
);
51 m_mainTimer
= new QTimer(this);
52 connect(m_mainTimer
, SIGNAL(timeout()), this, SLOT(updateRender()));
55 QNLWidget::~QNLWidget()
60 void QNLWidget::init()
63 m_driver
= NL3D::UDriver::createDriver(NULL
, false, NULL
);
66 // initialize the nel 3d viewport
67 m_driver
->setDisplay((nlWindow
)winId(), NL3D::UDriver::CMode(width(), height(), 32));
69 // set the cache size for the font manager(in bytes)
70 m_driver
->setFontManagerMaxMemory(2097152);
75 void QNLWidget::release()
87 void QNLWidget::setInterval(int msec
)
90 m_mainTimer
->setInterval(msec
);
93 void QNLWidget::setBackgroundColor(NLMISC::CRGBA backgroundColor
)
95 m_backgroundColor
= backgroundColor
;
98 void QNLWidget::updateRender()
103 m_driver
->EventServer
.pump();
107 static sint64 lastTime
= NLMISC::CTime::getPerformanceTime ();
108 sint64 newTime
= NLMISC::CTime::getPerformanceTime ();
109 m_fps
= float(1.0 / NLMISC::CTime::ticksToSecond (newTime
-lastTime
));
112 if (m_initialized
&& !m_driver
->isLost())
114 //_driver->activate();
115 m_driver
->clearBuffers(m_backgroundColor
);
116 Q_EMIT
updatePreRender();
118 Q_EMIT
updatePostRender();
120 m_driver
->swapBuffers();
125 void QNLWidget::showEvent(QShowEvent
*showEvent
)
127 QWidget::showEvent(showEvent
);
128 m_driver
->activate();
129 m_mainTimer
->start(m_interval
);
132 void QNLWidget::hideEvent(QHideEvent
*hideEvent
)
135 QWidget::hideEvent(hideEvent
);
138 #if defined(NL_OS_WINDOWS)
140 typedef bool (*winProc
)(NL3D::IDriver
*driver
, HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
);
142 bool QNLWidget::winEvent(MSG
*message
, long *result
)
144 if (m_driver
&& m_driver
->isActive())
146 NL3D::IDriver
*driver
= dynamic_cast<NL3D::CDriverUser
*>(m_driver
)->getDriver();
149 winProc proc
= (winProc
)driver
->getWindowProc();
150 return proc(driver
, message
->hwnd
, message
->message
, message
->wParam
, message
->lParam
);
157 #elif defined(NL_OS_MAC)
159 typedef bool (*cocoaProc
)(NL3D::IDriver
*, const void *e
);
161 bool QNLWidget::macEvent(EventHandlerCallRef caller
, EventRef event
)
164 nlerror("You are using QtCarbon! Only QtCocoa supported, please upgrade Qt");
166 if (m_driver
&& m_driver
->isActive())
168 NL3D::IDriver
*driver
= dynamic_cast<NL3D::CDriverUser
*>(m_driver
)->getDriver();
171 cocoaProc proc
= (cocoaProc
)driver
->getWindowProc();
172 return proc(driver
, event
);
179 #elif defined(NL_OS_UNIX)
181 typedef bool (*x11Proc
)(NL3D::IDriver
*drv
, XEvent
*e
);
183 bool QNLWidget::x11Event(XEvent
*event
)
185 if (m_driver
&& m_driver
->isActive())
187 NL3D::IDriver
*driver
= dynamic_cast<NL3D::CDriverUser
*>(m_driver
)->getDriver();
190 x11Proc proc
= (x11Proc
)driver
->getWindowProc();
191 return proc(driver
, event
);
199 } /* namespace NLQT */