Fix game:addSpawnShapesByZone
[ryzomcore.git] / studio / src / plugins / core / Nel3DWidget / nel3d_widget.cpp
blob80e5a8db847ae504607cbe1ab502e1e751cb59b6
1 // Ryzom Core MMORPG framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2013-2014 Laszlo KIS-ADAM (dfighter) <dfighter1985@gmail.com>
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 "nel3d_widget.h"
21 #include "nel/3d/u_driver.h"
22 #include "nel/3d/text_context.h"
23 #include "nel/3d/driver_user.h"
24 #include "nel/misc/rgba.h"
25 #include "nel/misc/path.h"
27 #ifdef NL_OS_WINDOWS
28 #include <Windows.h>
29 #endif
31 #include <QResizeEvent>
33 Nel3DWidget::Nel3DWidget( QWidget *parent ) :
34 NEL3DWIDGET( parent )
36 driver = NULL;
37 textContext = NULL;
39 // Need to set this attribute with a NULL paintengine returned to Qt
40 // so that we can render the widget normally ourselves, without the image
41 // disappearing when a widget is resized or shown on top of us
42 setAttribute( Qt::WA_PaintOnScreen, true );
43 setAttribute( Qt::WA_OpaquePaintEvent, true );
44 setAttribute( Qt::WA_NoSystemBackground, true );
47 Nel3DWidget::~Nel3DWidget()
49 if( driver != NULL )
51 if( textContext != NULL )
53 driver->deleteTextContext( textContext );
54 textContext = NULL;
57 driver->release();
58 delete driver;
59 driver = NULL;
63 void Nel3DWidget::init()
65 nlassert( driver == NULL );
67 driver = NL3D::UDriver::createDriver( 0, false, 0 );
68 driver->setDisplay( winId(), NL3D::UDriver::CMode( width(), height(), 32, true ) );
71 void Nel3DWidget::createTextContext( std::string fontFile )
73 if( driver == NULL )
74 return;
76 std::string font;
78 try
80 font = NLMISC::CPath::lookup( fontFile );
82 catch( ... )
84 nlinfo( "Font %s cannot be found, cannot create textcontext!", fontFile.c_str() );
85 exit( EXIT_FAILURE );
88 if( textContext != NULL )
90 driver->deleteTextContext( textContext );
91 textContext = NULL;
94 textContext = driver->createTextContext( font );
97 void Nel3DWidget::clear()
99 if( driver == NULL )
100 return;
101 driver->clearBuffers( NLMISC::CRGBA::Black );
102 driver->swapBuffers();
105 void Nel3DWidget::showEvent( QShowEvent *evnt )
107 QWidget::showEvent( evnt );
109 if( driver != NULL )
110 driver->activate();
113 void Nel3DWidget::resizeEvent( QResizeEvent *evnt )
115 QWidget::resizeEvent( evnt );
117 Q_EMIT( evnt->size().width(), evnt->size().height() );
120 #if defined ( NL_OS_WINDOWS )
122 typedef bool ( *winProc )( NL3D::IDriver *driver, HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam );
124 bool Nel3DWidget::winEvent( MSG *message, long *result )
126 if( driver != NULL )
128 NL3D::IDriver *iDriver = dynamic_cast< NL3D::CDriverUser* >( driver )->getDriver();
129 if( iDriver != NULL )
131 winProc proc = (winProc)iDriver->getWindowProc();
132 return proc( iDriver, message->hwnd, message->message, message->wParam, message->lParam );
136 return false;
139 #elif defined( NL_OS_MAC )
141 typedef bool ( *cocoaProc )( NL3D::IDriver *, const void *e );
143 bool Nel3DWidget::macEvent( EventHandlerCallRef caller, EventRef event )
145 if( caller )
146 nlerror( "You are using QtCarbon! Only QtCocoa supported, please upgrade Qt" );
148 if( driver != NULL )
150 NL3D::IDriver *iDriver = dynamic_cast< NL3D::CDriverUser* >( driver )->getDriver();
151 if( iDriver != NULL )
153 cocoaProc proc = ( cocoaProc )iDriver->getWindowProc();
154 return proc( iDriver, event );
158 return false;
161 #elif defined( NL_OS_UNIX )
163 typedef bool ( *x11Proc )( NL3D::IDriver *drv, XEvent *e );
165 bool Nel3DWidget::x11Event( XEvent *event )
167 if( driver != NULL )
169 NL3D::IDriver *iDriver = dynamic_cast< NL3D::CDriverUser* >( driver )->getDriver();
170 if( driver != NULL )
172 x11Proc proc = ( x11Proc )iDriver->getWindowProc();
173 return proc( iDriver, event );
177 return false;
179 #endif