source: flair-src/trunk/lib/FlairSimulator/src/Gui_impl.cpp@ 8

Last change on this file since 8 was 8, checked in by Sanahuja Guillaume, 6 years ago

simulator

File size: 10.4 KB
Line 
1// created: 2013/03/27
2// filename: Gui.cpp
3//
4// author: Guillaume Sanahuja
5// Copyright Heudiasyc UMR UTC/CNRS 7253
6//
7// version: $Id: $
8//
9// purpose: classe definissant une Gui
10//
11/*********************************************************************/
12#ifdef GL
13
14#include "Gui_impl.h"
15#include "Gui.h"
16#include "Simulator.h"
17#include "GenericObject.h"
18#include "Model.h"
19#include "Model_impl.h"
20#include "AnimPoursuite.h"
21#include <Object.h>
22#include <Euler.h>
23#include <irrlicht.h>
24#include <unistd.h>
25#include <sstream>
26
27using namespace irr;
28using namespace irr::video;
29using namespace irr::core;
30using namespace irr::scene;
31using namespace irr::gui;
32using namespace flair::core;
33using namespace flair::simulator;
34
35class MyEventReceiver : public IEventReceiver
36{
37public:
38 // This is the one method that we have to implement
39 virtual bool OnEvent(const SEvent& event)
40 {
41 // Remember whether each key is down or up
42 if (event.EventType == EET_KEY_INPUT_EVENT )
43 KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
44
45 //Send all other events to camera
46 if (event.EventType == EET_MOUSE_INPUT_EVENT && camera)
47 //return camera->OnEvent(event);
48 return camera->MouseMoved(event,cursorControl->getRelativePosition());
49
50 if(model)
51 return model->OnEvent(event);
52
53 return false;
54 }
55
56 // This is used to check whether a key is being held down
57 virtual bool IsKeyDown(EKEY_CODE keyCode)
58 {
59 if(KeyIsDown[keyCode]==true)
60 {
61 KeyIsDown[keyCode]=false;
62 return true;
63 }
64 else
65 {
66 return false;
67 }
68 }
69
70 MyEventReceiver(ICursorControl* cursorControl)
71 {
72 this->cursorControl=cursorControl;
73 camera=NULL;
74 model=NULL;
75 for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
76 KeyIsDown[i] = false;
77 }
78 void SetCamera(AnimPoursuite* camera)
79 {
80 this->camera=camera;
81 }
82 void SetModel(Model* model)
83 {
84 this->model=model;
85 }
86
87private:
88 // We use this array to store the current state of each key
89 bool KeyIsDown[KEY_KEY_CODES_COUNT];
90 AnimPoursuite* camera;
91 Model* model;
92 ICursorControl* cursorControl;
93};
94
95Gui_impl::Gui_impl(Gui* self,int app_width, int app_height,int scene_width, int scene_height,std::string media_path,E_DRIVER_TYPE driver_type)
96{
97 this->self=self;
98 dbtFile_w=NULL;
99 dbtFile_r=NULL;
100 this->media_path=media_path;
101 this->scene_width=scene_width;
102 this->scene_height=scene_height;
103
104 device =createDevice(driver_type, dimension2d<u32>(app_width, app_height),16, false, false, false);
105 receiver=new MyEventReceiver(device->getCursorControl());
106 device->setEventReceiver(receiver);
107 device->getLogger()->setLogLevel(ELL_NONE);
108
109 device->getCursorControl()->setVisible(false);
110 device->setResizable(false);
111
112 //font = device->getGUIEnvironment()->getBuiltInFont();
113 driver = device->getVideoDriver();
114 smgr = device->getSceneManager();
115
116 smgr->setAmbientLight(video::SColorf(1,1,1));
117
118
119 /*
120 env = device->getGUIEnvironment();
121IGUISkin* skin = env->getSkin();
122 font = env->getFont("./fonthaettenschweiler.bmp");
123
124 if (font)
125 skin->setFont(font);
126
127 // create menu
128
129 IGUIContextMenu* menu = env->addMenu();
130 menu->setMinSize(core::dimension2du(640,20));
131 menu->addItem(L"File", -1, true, true);
132 menu->addItem(L"View", -1, true, true);
133 menu->addItem(L"Camera", -1, true, true);
134 menu->addItem(L"Help", -1, true, true);
135
136 // disable alpha
137
138 for (s32 i=0; i<gui::EGDC_COUNT ; ++i)
139 {
140 video::SColor col = env->getSkin()->getColor((gui::EGUI_DEFAULT_COLOR)i);
141 col.setAlpha(255);
142 env->getSkin()->setColor((gui::EGUI_DEFAULT_COLOR)i, col);
143 }
144 */
145}
146
147Gui_impl::~Gui_impl()
148{
149 //printf("del Gui_impl\n");
150 device->drop();
151
152 delete receiver;
153 // printf("del Gui_impl ok\n");
154}
155
156void Gui_impl::setMesh(std::string file,vector3df position,vector3df rotation,vector3df scale)
157{
158 IAnimatedMesh* mesh = smgr->getMesh(file.c_str());
159
160 if (!mesh) {
161 // model could not be loaded
162 self->Err("Model %s could not be loaded\n",file.c_str());
163 return;
164 }
165
166
167 node = smgr->addOctreeSceneNode(mesh->getMesh(0), 0, -1, 1024);
168 node->setPosition(position);
169 rotation+=irr::core::vector3df(90,0,Euler::ToDegree(getSimulator()->Yaw()));
170 node->setRotation(rotation);
171 for(int i=0; i<node->getMaterialCount();i++){
172 node->getMaterial(i).TextureLayer->TextureWrapU = video::ETC_REPEAT;
173 node->getMaterial(i).TextureLayer->TextureWrapV = video::ETC_REPEAT;
174 }
175 //Ceillig
176 //node->getMaterial(0).getTextureMatrix(0).setTextureScale(scale.X/2.0,scale.Z/2.0);
177 //Walls
178 node->getMaterial(1).getTextureMatrix(0).setTextureScale(1/(scale.Y/2.5),1/(scale.Z/2.5));
179 //Floor
180 node->getMaterial(2).getTextureMatrix(0).setTextureScale(1/(scale.X/20.0),1/(scale.Z/20.0));
181
182 node->setScale(scale);
183 //selector
184 selector = smgr->createOctreeTriangleSelector(node->getMesh(), node, 128);
185 node->setTriangleSelector(selector);
186}
187
188void Gui_impl::RunGui(std::vector<Model*> models,std::vector<GenericObject*> objects)
189{
190 int lastFPS = -1;
191 int cam_id=0;
192
193 receiver->SetCamera(models.at(cam_id)->pimpl_->animator);
194 receiver->SetModel(models.at(cam_id));
195
196 for(size_t i=0;i<models.size();i++)
197 {
198 models.at(i)->Draw();
199 }
200
201 for(size_t i=0;i<models.size();i++)
202 {
203 models.at(i)->pimpl_->MetaTriangleSelector()->addTriangleSelector(selector);
204 for(size_t j=0;j<objects.size();j++)
205 {
206 models.at(i)->pimpl_->MetaTriangleSelector()->addTriangleSelector(objects.at(j)->TriangleSelector());
207 }
208 for(size_t j=0;j<models.size();j++)
209 {
210 if(i==j) continue;
211 models.at(i)->pimpl_->MetaTriangleSelector()->addTriangleSelector(models.at(j)->pimpl_->TriangleSelector());
212 }
213 }
214
215 selector->drop(); // As soon as we're done with the selector, drop it.*/
216
217 //wait all models to be started
218 for(size_t i=0;i<models.size();i++)
219 {
220 models.at(i)->pimpl_->SynchronizationPoint();
221 }
222
223 setWindowCaption(models.at(0),0);
224
225 while(device->run())
226 {
227 if(dbtFile_r!=NULL)//rejeu
228 {
229 takeScreenshot();//on enregistre l'image precedente
230 road_time_t time;
231 road_timerange_t tr = 0;
232 if(read_hdfile(dbtFile_r,(void*)dbtbuf,&time,&tr)!=0)
233 {
234 vector3df vect;
235 char *buf=dbtbuf;
236 for(size_t i=0;i<models.size();i++)
237 {
238 models.at(i)->ReaddbtBuf(buf);
239 buf+=models.at(i)->dbtSize();
240 }
241 }
242 else
243 {
244 //Printf("fin play\n");
245 close_hdfile(dbtFile_r);
246 dbtFile_r=NULL;
247 free(dbtbuf);
248 for(size_t i=0;i<models.size();i++)
249 {
250 models.at(i)->pimpl_->Resume();
251 }
252 }
253 }
254 else//mode normal
255 {
256 for(size_t i=0;i<models.size();i++)
257 {
258 models.at(i)->pimpl_->UpdatePos();
259 }
260 }
261
262 driver->beginScene(true, true, video::SColor(255,200,200,200));
263
264 //vue poursuite
265 smgr->setActiveCamera(models.at(cam_id)->pimpl_->camera);
266 driver->setViewPort(core::rect<s32>(0,0,scene_width,scene_height));
267 smgr->drawAll();//commente voir plus bas
268 /*
269 env->drawAll();
270if (font)
271{
272 font->draw(L"This demo shows that Irrlicht is also capable of drawing 2D graphics.",
273 core::rect<s32>(130,10,300,50),
274 video::SColor(255,255,255,255));
275}
276else
277{
278 printf("err\n");
279}
280device->setWindowCaption(L"toto");*/
281
282 if(dbtFile_r==NULL)//mode normal
283 {
284 for(size_t i=0;i<models.size();i++)
285 {
286 models.at(i)->pimpl_->CheckCollision();
287 }
288 }
289
290 for(size_t i=0;i<models.size();i++)
291 {
292 models.at(i)->ProcessUpdate(NULL);
293 }
294
295 //on fait ca ici, devrait etre un peu plus haut
296 //mais a priori souci avec models.at(i)->pimpl_->CheckCollision(); (setelipsoid?)
297 smgr->setActiveCamera(models.at(cam_id)->pimpl_->camera);
298 driver->setViewPort(core::rect<s32>(0,0,scene_width,scene_height));
299 smgr->drawAll();
300
301 driver->endScene();
302
303 int fps =driver->getFPS();
304 //printf("fps %i\n",fps);
305 if (lastFPS != fps)
306 {
307 setWindowCaption(models.at(cam_id),fps);
308 lastFPS = fps;
309 }
310
311 if(receiver->IsKeyDown(KEY_PRIOR))
312 {
313 cam_id++;
314 if(cam_id>=(int)models.size()) cam_id=0;
315 receiver->SetCamera(models.at(cam_id)->pimpl_->animator);
316 receiver->SetModel(models.at(cam_id));
317 setWindowCaption(models.at(cam_id),fps);
318 }
319 if(receiver->IsKeyDown(KEY_NEXT))
320 {
321 cam_id--;
322 if(cam_id<0) cam_id=models.size()-1;
323 receiver->SetCamera(models.at(cam_id)->pimpl_->animator);
324 receiver->SetModel(models.at(cam_id));
325 setWindowCaption(models.at(cam_id),fps);
326 }
327
328 //enregistrement DBT
329 if(receiver->IsKeyDown(KEY_KEY_R) && dbtFile_w==NULL)
330 {
331 dbtFile_w = inithdFile((char*)"./record.dbt",UAV,dbtSize(models));
332 dbtbuf=(char*)malloc(dbtSize(models));
333 }
334 if(receiver->IsKeyDown(KEY_KEY_S) && dbtFile_w!=NULL)
335 {
336 close_hdfile(dbtFile_w);
337 dbtFile_w=NULL;
338 free(dbtbuf);
339 //rt_printf("stop rec\n");
340 }
341 if(dbtFile_w!=NULL)
342 {
343 Time time=GetTime();
344 vector3df vect;
345 char *buf=dbtbuf;
346
347 for(size_t i=0;i<models.size();i++)
348 {
349 models.at(i)->WritedbtBuf(buf);
350 buf+=models.at(i)->dbtSize();
351 }
352
353 write_hdfile(dbtFile_w,dbtbuf,(road_time_t)(time/1000),(road_timerange_t)(time%1000),dbtSize(models));
354 }
355
356 //lecture dbt
357 if(receiver->IsKeyDown(KEY_KEY_P) && dbtFile_r==NULL)
358 {
359 dbtFile_r = open_hdfile((char*)"./record.dbt",READ_MODE);
360 dbtbuf=(char*)malloc(dbtSize(models));
361 //on suspend les models pour ne pas interferer
362 for(size_t i=0;i<models.size();i++)
363 {
364 models.at(i)->pimpl_->Suspend();
365 }
366 }
367 if(receiver->IsKeyDown(KEY_KEY_S) && dbtFile_r!=NULL)
368 {
369 //rt_printf("stop play\n");
370 close_hdfile(dbtFile_r);
371 dbtFile_r=NULL;
372 free(dbtbuf);
373 //on resume les models
374 for(size_t i=0;i<models.size();i++)
375 {
376 models.at(i)->pimpl_->Resume();
377 }
378 }
379
380 }
381
382 receiver->SetCamera(NULL);
383 receiver->SetModel(NULL);
384
385}
386
387void Gui_impl::setWindowCaption(Object* object, int fps)
388{
389 std::ostringstream text;
390 text << "Cam: " << object->ObjectName().c_str() << ", Kbd: " << object->ObjectName().c_str() << ", FPS: " << fps;
391
392 device->setWindowCaption(stringw(text.str().c_str()).c_str());
393}
394
395void Gui_impl::takeScreenshot(void)
396{
397 static int cpt=0;
398 //get image from the last rendered frame
399 IImage* const image = driver->createScreenShot();
400 if (image) //should always be true, but you never know. ;)
401 {
402 //construct a filename, consisting of local time and file extension
403 c8 filename[64];
404 //snprintf(filename, 64, "screenshot_%u.png", device->getTimer()->getRealTime());
405 snprintf(filename, 64, "screenshot_%u.png", cpt);
406 cpt++;
407 //write screenshot to file
408 if (!driver->writeImageToFile(image, filename))
409 device->getLogger()->log(L"Failed to take screenshot.", ELL_WARNING);
410
411 //Don't forget to drop image since we don't need it anymore.
412 image->drop();
413 }
414}
415
416size_t Gui_impl::dbtSize(std::vector<Model*> models)
417{
418 size_t size=0;
419 for(size_t i=0;i<models.size();i++)
420 {
421 size+=models.at(i)->dbtSize();
422 }
423
424 return size;
425}
426#endif //GL
Note: See TracBrowser for help on using the repository browser.