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