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