svn merge -r 21041:21301 https://svn.blender.org/svnroot/bf-blender/branches/blender2...
[blender.git] / source / gameengine / GamePlayer / common / unix / GPU_Engine.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29    
30 #include <assert.h>
31 #include <unistd.h>
32 #include "GPU_Engine.h"
33 #include "GPC_MouseDevice.h"
34 #include "GPU_Canvas.h"
35 #include "GPU_KeyboardDevice.h"
36 #include "GPU_System.h"
37
38 #include "BLI_blenlib.h"
39 #include "BLO_readfile.h"
40
41 #include "SND_DeviceManager.h"
42
43 #include "NG_NetworkScene.h"
44 #include "NG_LoopBackNetworkDeviceInterface.h"
45 #include "SND_DeviceManager.h"
46 #include "KX_BlenderSceneConverter.h"
47 #include "KX_KetsjiEngine.h"
48
49 #include "GPC_RenderTools.h"
50 #include "GPC_RawImage.h"
51
52 #ifdef HAVE_CONFIG_H
53 #include <config.h>
54 #endif
55
56 void Redraw(GPU_Engine *engine);  // -the- redraw function
57
58 // callback functions
59 /*
60 void RedrawCallback(Widget, XtPointer closure, XEvent *, Boolean *continue_to_dispatch);
61  
62 void KeyDownCallback(Widget w, XtPointer closure, XEvent *event, Boolean *continue_to_dispatch);
63 void KeyUpCallback(Widget w, XtPointer closure, XEvent *event, Boolean *continue_to_dispatch);
64  
65 void ButtonPressReleaseCallback(Widget w, XtPointer closure, XEvent *event, Boolean *continue_to_dispatch);
66 void PointerMotionCallback(Widget w, XtPointer closure, XEvent *event, Boolean *continue_to_dispatch);
67  
68 void TimeOutCallback(XtPointer closure, XtIntervalId *id);
69 */
70
71 GPU_Engine::GPU_Engine(char *customLoadingAnimationURL,
72                 int foregroundColor, int backgroundColor, int frameRate) :
73                 GPC_Engine(customLoadingAnimationURL, foregroundColor, backgroundColor,
74                 frameRate), m_timerTimeOutMsecs(10)
75 {
76 }
77
78
79 GPU_Engine::~GPU_Engine()
80 {
81 }
82
83 /* 
84 bool GPU_Engine::Initialize(Display *display, Window window, int width, int height)
85 {
86         SND_DeviceManager::Subscribe();
87         m_audiodevice = SND_DeviceManager::Instance(); 
88
89         m_keyboarddev = new GPU_KeyboardDevice();
90         m_mousedev = new GPC_MouseDevice();
91                 
92         // constructor only initializes data
93         //      m_canvas = new GPU_Canvas(display, window, width, height);
94         //m_canvas->Init();  // create the actual visual and rendering context
95         //cout << "GPU_Canvas created and initialized, m_canvas " << m_canvas << endl;
96         //AddEventHandlers();  // done here (in GPU_Engine) since the event handlers need access to 'this', ie the engine
97
98         // put the Blender logo in the topleft corner
99         if(m_BlenderLogo != 0)
100                 // adding a banner automatically enables them
101                 m_BlenderLogoId = m_canvas->AddBanner(m_BlenderLogo->Width(), m_BlenderLogo->Height(),
102                                 m_BlenderLogo->Width(), m_BlenderLogo->Height(),
103                                 m_BlenderLogo->Data(), GPC_Canvas::alignTopLeft);
104
105         // put the Blender3D logo in the bottom right corner
106         if(m_Blender3DLogo != 0)
107                 // adding a banner automatically enables them
108                 m_Blender3DLogoId = m_canvas->AddBanner(m_Blender3DLogo->Width(), m_Blender3DLogo->Height(),
109                                 m_Blender3DLogo->Width(), m_Blender3DLogo->Height(),
110                                 m_Blender3DLogo->Data(), GPC_Canvas::alignTopLeft);
111
112 #if 0
113         // put the NaN logo in the bottom right corner
114         if(m_NaNLogo != 0)
115                 // adding a banner automatically enables them
116                 m_NaNLogoId = m_canvas->AddBanner(m_NaNLogo->Width(), m_NaNLogo->Height(),
117                                 m_NaNLogo->Width(), m_NaNLogo->Height(),
118                                 m_NaNLogo->Data(), GPC_Canvas::alignBottomRight);
119 #endif
120         // enable the display of all banners
121         m_canvas->SetBannerDisplayEnabled(true);
122
123         m_rendertools = new GPC_RenderTools();
124
125         m_networkdev = new NG_LoopBackNetworkDeviceInterface();
126         assert(m_networkdev);
127
128         // creation of system needs 'current rendering context', this is taken care
129         // of by the GPU_Canvas::Init()
130         m_system = new GPU_System();
131
132         m_system->SetKeyboardDevice((GPU_KeyboardDevice *)m_keyboarddev);
133         m_system->SetMouseDevice(m_mousedev);
134         m_system->SetNetworkDevice(m_networkdev);
135
136         m_initialized = true;
137
138         return m_initialized;
139 }
140 */
141
142 /* 
143 void GPU_Engine::HandleNewWindow(Window window)
144 {
145         // everything only if it's really a new window
146         if(window != ((GPU_Canvas *)m_canvas)->GetWindow())
147         {
148                 cout << "GPU_Engine::HandleNewWindow(), new window so calling SetNewWindowMakeNewWidgetAndMakeCurrent()" << endl;
149                 // We don't have to remove the event handlers ourselves, they are destroyed by X11
150                 
151                 // make canvas aware of new window, and make it current
152                 ((GPU_Canvas *)m_canvas)->SetNewWindowMakeNewWidgetAndMakeCurrent(window);
153                 
154                 // and add event handlers to new widget
155                 AddEventHandlers();
156                 cout << "GPU_Engine::HandleNewWindow(), event handlers added" << endl;
157         }
158 }
159 */
160 /*
161 void GPU_Engine::AddEventHandlers(void)
162 {
163         Widget widget = ((GPU_Canvas *)m_canvas)->GetWidget();
164
165         // redraw
166         // MUST be the *Raw* event handler, the normal one doesn't work!
167         XtAddRawEventHandler(widget, ExposureMask, FALSE, RedrawCallback, this);
168 #if 0
169         // key down
170         XtAddRawEventHandler(widget, KeyPressMask, FALSE, KeyDownCallback, this);
171         // key up
172         XtAddRawEventHandler(widget, KeyReleaseMask, FALSE, KeyUpCallback, this);
173  
174         // mouse button press
175         XtAddRawEventHandler(widget, ButtonPressMask, FALSE, ButtonPressReleaseCallback, this);
176         // mouse button release
177         XtAddRawEventHandler(widget, ButtonReleaseMask, FALSE, ButtonPressReleaseCallback, this);
178         // mouse motion
179         XtAddRawEventHandler(widget, PointerMotionMask, FALSE, PointerMotionCallback, this);
180 #endif
181 #if 0
182         // time out, not a real timer. New time out will be set in callback
183         m_timerId = XtAppAddTimeOut(XtWidgetToApplicationContext(widget),
184                                         m_timerTimeOutMsecs, TimeOutCallback, this);
185 #endif
186 }
187 */
188
189 void Redraw(GPU_Engine *engine)
190 {
191 /*      if(engine->Running())
192         {
193                 if(engine->Loading())
194                 {
195                         engine->UpdateLoadingAnimation();
196                 }
197  
198                 engine->m_system->DoMainLoopCallback();
199         }*/
200 }
201
202
203 /*+++++++++++++++++++++++++++++++++++++++++++++++++
204  * Callback functions
205  +++++++++++++++++++++++++++++++++++++++++++++++++*/
206 void RedrawCallback(Widget, XtPointer closure, XEvent *, Boolean *continue_to_dispatch)
207 {
208         GPU_Engine *engine = (GPU_Engine *)closure;
209
210         Redraw(engine);
211
212         *continue_to_dispatch = True;
213 }
214
215
216 void KeyDownCallback(Widget, XtPointer closure, XEvent *event, Boolean *continue_to_dispatch)
217 {
218 /*      GPU_Engine *engine = (GPU_Engine *)closure;
219         XKeyEvent *keyEvent = (XKeyEvent *)event;
220  
221         if(engine->Running())
222                 engine->m_system->AddKey(int(keyEvent->keycode), 1);
223
224         *continue_to_dispatch = True;*/
225 }
226  
227  
228 void KeyUpCallback(Widget, XtPointer closure, XEvent *event, Boolean *continue_to_dispatch)
229 {
230 /*      GPU_Engine *engine = (GPU_Engine *)closure;
231         XKeyEvent *keyEvent = (XKeyEvent *)event;
232  
233         if(engine->Running())
234                 engine->m_system->AddKey(int(keyEvent->keycode), 0);
235
236         *continue_to_dispatch = True;*/
237 }
238  
239  
240 void ButtonPressReleaseCallback(Widget, XtPointer closure, XEvent *event, Boolean *continue_to_dispatch)
241 {
242         GPU_Engine *engine = (GPU_Engine *)closure;
243         XButtonEvent *buttonEvent = (XButtonEvent *)event;
244         bool isDown;
245         GPC_MouseDevice::TButtonId button;
246  
247         if(engine->Running())
248         {
249                 // determine type of event, press or release
250                 isDown = false;
251                 if(buttonEvent->type == ButtonPress)
252                 isDown = true;
253                 // determine which button exactly generated this event
254                 switch(buttonEvent->button)
255                 {
256                         case 1:
257                                 button = GPC_MouseDevice::buttonLeft;
258                                 break;
259                         case 2:
260                                 button = GPC_MouseDevice::buttonMiddle;
261                                 break;
262                         case 3:
263                                 button = GPC_MouseDevice::buttonRight;
264                                 break;
265                 }
266                 engine->m_mousedev->ConvertButtonEvent(button,
267                                 isDown, buttonEvent->x, buttonEvent->y);
268         }
269
270         *continue_to_dispatch = True;
271 }
272  
273  
274 void PointerMotionCallback(Widget w, XtPointer closure, XEvent *event, Boolean *continue_to_dispatch)
275 {
276         GPU_Engine *engine = (GPU_Engine *)closure;
277         XButtonEvent *buttonEvent = (XButtonEvent *)event;
278  
279         if(engine->Running())
280         {
281                 engine->m_mousedev->ConvertMoveEvent(buttonEvent->x, buttonEvent->y);
282         }
283
284         *continue_to_dispatch = True;
285 }
286
287 /*
288 void TimeOutCallback(XtPointer closure, XtIntervalId *id)
289 {
290         GPU_Engine *engine = (GPU_Engine *)closure;
291
292         Redraw(engine);
293         // add a new time out since there is no real timer for X (not a simple one like under windows)
294         // TODO Have to get faster timer !
295
296         if(engine->Running())
297                 engine->m_timerId = XtAppAddTimeOut(XtWidgetToApplicationContext(
298                         ((GPU_Canvas *)engine->m_canvas)->GetWidget()),
299                         engine->m_timerTimeOutMsecs, TimeOutCallback,
300                         closure);
301 }
302
303 */