TORCS  1.3.10
The Open Racing Car Simulator
Loading...
Searching...
No Matches
grscreen.cpp
Go to the documentation of this file.
1/***************************************************************************
2
3 file : grscreen.cpp
4 created : Thu May 15 22:11:03 CEST 2003
5 copyright : (C) 2003 by Eric Espi�
6 email : eric.espie@torcs.org
7 version : $Id$
8
9 ***************************************************************************/
10
11/***************************************************************************
12 * *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * (at your option) any later version. *
17 * *
18 ***************************************************************************/
19
25
26#include <plib/ssg.h>
27
28#include <tgfclient.h>
29#include <portability.h>
30#include "grutil.h"
31#include "grmain.h"
32#include "grscene.h"
33#include "grshadow.h"
34#include "grskidmarks.h"
35#include "grcar.h"
36#include "grboard.h"
37#include "grcarlight.h"
38
39#include "grscreen.h"
40
42{
43 id = myid;
44 curCar = NULL;
45 curCam = NULL;
46 mirrorCam = NULL;
47 dispCam = NULL;
48 boardCam = NULL;
49 bgCam = NULL;
50 board = NULL;
51 curCamHead = 0;
52 drawCurrent = 0;
53 active = 0;
56 mirrorFlag = 1;
57 memset(cams, 0, sizeof(cams));
58 viewRatio = 1.33;
59 cars = 0;
60}
61
63{
64 unsigned int i;
65 class cGrCamera *cam;
66
67 for (i = 0; i < sizeof(cams)/sizeof(cams[0]); i++) {
68 while ((cam = GF_TAILQ_FIRST(&cams[i])) != 0) {
69 cam->remove(&cams[i]);
70 delete cam;
71 }
72 }
73
74 delete boardCam;
75 delete mirrorCam;
76 delete bgCam;
77
78 if (board != NULL) {
79 board->shutdown ();
80 }
81
82 FREEZ(cars);
83
84 if (board != NULL) {
85 delete board;
86 board = NULL;
87 }
88}
89
90int cGrScreen::isInScreen(int x, int y)
91{
92 if (!active) {
93 return 0;
94 }
95
96 if ((x >= scrx) &&
97 (y >= scry) &&
98 (x < (scrx + scrw)) &&
99 (y < (scry + scrh)))
100 {
101 return 1;
102 }
103
104 return 0;
105}
106
108{
109 curCar = newCurCar;
110}
111
112/* Set Screen size & position */
113void cGrScreen::activate(int x, int y, int w, int h)
114{
115 viewRatio = (float)w / (float)h;
116
117 scrx = x;
118 scry = y;
119 scrw = w;
120 scrh = h;
121
122 if (mirrorCam) {
123 mirrorCam->setViewport (scrx, scry, scrw, scrh);
124 mirrorCam->setPos (scrx + scrw / 4, scry + 5 * scrh / 6 - scrh / 10, scrw / 2, scrh / 6);
125 }
126 if (curCam) {
127 curCam->limitFov ();
128 curCam->setZoom (GR_ZOOM_DFLT);
129 }
130 active = 1;
131}
132
134{
135 active = 0;
136}
137
138/* Set camera zoom value */
139void cGrScreen::setZoom(long zoom)
140{
141 curCam->setZoom(zoom);
142}
143
145{
146 selectNextFlag = 1;
147}
148
150{
151 selectPrevFlag = 1;
152}
153
155{
156 board->selectBoard(brd);
157}
158
160{
161 const int BUFSIZE=1024;
162 char path[BUFSIZE];
163 char path2[BUFSIZE];
164 int viewmode;
165
166 board->getTrackMap()->selectTrackMap();
167 viewmode = board->getTrackMap()->getViewMode();
168
169 snprintf(path, BUFSIZE, "%s/%d", GR_SCT_DISPMODE, id);
170 GfParmSetNum(grHandle, path, GR_ATT_MAP, NULL, (tdble)viewmode);
171 /* save also as user's preference if human */
172 if (curCar->_driverType == RM_DRV_HUMAN) {
173 snprintf(path2, BUFSIZE, "%s/%s", GR_SCT_DISPMODE, curCar->_name);
174 GfParmSetNum(grHandle, path2, GR_ATT_MAP, NULL, (tdble)viewmode);
175 }
176 GfParmWriteFile(NULL, grHandle, "Graph");
177}
178
180{
181 const int BUFSIZE=1024;
182 char path[BUFSIZE];
183 char path2[BUFSIZE];
184
186 snprintf(path, BUFSIZE, "%s/%d", GR_SCT_DISPMODE, id);
188 /* save also as user's preference if human */
189 if (curCar->_driverType == RM_DRV_HUMAN) {
190 snprintf(path2, BUFSIZE, "%s/%s", GR_SCT_DISPMODE, curCar->_name);
192 }
193 GfParmWriteFile(NULL, grHandle, "Graph");
194}
195
196
197/* Select the camera by number */
199{
200 const int BUFSIZE=1024;
201 char buf[BUFSIZE];
202 char path[BUFSIZE];
203 char path2[BUFSIZE];
204
205
206 if (cam == curCamHead) {
207 /* Same camera list, choose the next one */
208 curCam = curCam->next();
209 if (curCam == (cGrPerspCamera*)GF_TAILQ_END(&cams[cam])) {
211 }
212 } else {
213 /* Change of camera list, take the first one */
214 curCamHead = cam;
216 }
217
218 if (curCam == NULL) {
219 /* back to default camera */
220 curCamHead = 0;
222 }
223
224 snprintf(path, BUFSIZE, "%s/%d", GR_SCT_DISPMODE, id);
226 GfParmSetNum(grHandle, path, GR_ATT_CAM, (char*)NULL, (tdble)curCam->getId());
228
229 /* save also as user's preference if human */
230 if (curCar->_driverType == RM_DRV_HUMAN) {
231 snprintf(path2, BUFSIZE, "%s/%s", GR_SCT_DISPMODE, curCar->_name);
232 GfParmSetNum(grHandle, path2, GR_ATT_CAM, (char*)NULL, (tdble)curCam->getId());
233 GfParmSetNum(grHandle, path2, GR_ATT_CAM_HEAD, (char*)NULL, (tdble)curCamHead);
234 }
235
236 snprintf(buf, BUFSIZE, "%s-%d-%d", GR_ATT_FOVY, curCamHead, curCam->getId());
237 curCam->loadDefaults(buf);
238 drawCurrent = curCam->getDrawCurrent();
239 curCam->limitFov ();
240 GfParmWriteFile(NULL, grHandle, "Graph");
241}
242
243static class cGrPerspCamera *ThedispCam; /* the display camera */
244
245static int
246comparCars(const void *car1, const void *car2)
247{
248 float d1 = ThedispCam->getDist2(*(tCarElt**)car1);
249 float d2 = ThedispCam->getDist2(*(tCarElt**)car2);
250
251 if (d1 > d2) {
252 return -1;
253 } else {
254 return 1;
255 }
256}
257
259{
260 int i;
261
262 glDisable(GL_COLOR_MATERIAL);
263
264 START_PROFILE("dispCam->update*");
265 grSetRenderOrigin(curCar->_pos_X_D, curCar->_pos_Y_D);
266 dispCam->update(curCar, s);
267 STOP_PROFILE("dispCam->update*");
268
269 if (dispCam->getDrawBackground()) {
270 glDisable(GL_LIGHTING);
271 glDisable(GL_DEPTH_TEST);
273 glClear(GL_DEPTH_BUFFER_BIT);
274 }
275 glEnable(GL_DEPTH_TEST);
276
277 START_PROFILE("dispCam->action*");
278 dispCam->action();
279 STOP_PROFILE("dispCam->action*");
280
281 START_PROFILE("grDrawCar*");
282 glFogf(GL_FOG_START, dispCam->getFogStart());
283 glFogf(GL_FOG_END, dispCam->getFogEnd());
284 glEnable(GL_FOG);
285
286 /*sort the cars by distance for transparent windows */
288 qsort(cars, s->_ncars, sizeof(tCarElt*), comparCars);
289
290 for (i = 0; i < s->_ncars; i++) {
291 grDrawCar(cars[i], curCar, dispCam->getDrawCurrent(), dispCam->getDrawDriver(), s->currentTime, dispCam);
292 }
293 STOP_PROFILE("grDrawCar*");
294
295 START_PROFILE("grDrawScene*");
296 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
297 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
298 grDrawScene();
299 STOP_PROFILE("grDrawScene*");
300}
301
302
303/* Update screen display */
304void cGrScreen::update(tSituation *s, float Fps)
305{
306 int i;
307 //ssgLight *light;
308 int carChanged;
309 const int BUFSIZE=1024;
310 char buf[BUFSIZE];
311
312 if (!active) {
313 return;
314 }
315
316 carChanged = 0;
317 if (selectNextFlag) {
318 for (i = 0; i < (s->_ncars - 1); i++) {
319 if (curCar == s->cars[i]) {
320 curCar = s->cars[i + 1];
321 curCar->priv.collision = 0;
322 carChanged = 1;
323 break;
324 }
325 }
326 selectNextFlag = 0;
327 }
328
329 if (selectPrevFlag) {
330 for (i = 1; i < s->_ncars; i++) {
331 if (curCar == s->cars[i]) {
332 curCar = s->cars[i - 1];
333 curCar->priv.collision = 0;
334 carChanged = 1;
335 break;
336 }
337 }
338 selectPrevFlag = 0;
339 }
340 if (carChanged) {
341 snprintf(buf, BUFSIZE, "%s/%d", GR_SCT_DISPMODE, id);
343 loadParams (s);
344 GfParmWriteFile(NULL, grHandle, "Graph");
345 curCam->onSelect(curCar, s);
346 }
347
348 //light = ssgGetLight (0);
349
350 /* MIRROR */
351 if (mirrorFlag && curCam->isMirrorAllowed ()) {
352 mirrorCam->activateViewport ();
354 glClear (GL_DEPTH_BUFFER_BIT);
355 camDraw (s);
356 mirrorCam->store ();
357 }
358
359 glViewport(scrx, scry, scrw, scrh);
360 dispCam = curCam;
361 camDraw(s);
362
363 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
364 glDisable(GL_CULL_FACE);
365 glDisable(GL_DEPTH_TEST);
366 glDisable(GL_LIGHTING);
367 glDisable(GL_COLOR_MATERIAL);
368 glDisable(GL_ALPHA_TEST);
369 glDisable(GL_FOG);
370 glEnable(GL_TEXTURE_2D);
371
372 /* MIRROR */
373 if (mirrorFlag && curCam->isMirrorAllowed ()) {
374 mirrorCam->display ();
375 glViewport (scrx, scry, scrw, scrh);
376 }
377
378 START_PROFILE("boardCam*");
379 boardCam->action();
380 STOP_PROFILE("boardCam*");
381
382 START_PROFILE("grDisp**");
383 glDisable(GL_TEXTURE_2D);
384
385 TRACE_GL("cGrScreen::update glDisable(GL_DEPTH_TEST)");
386 board->refreshBoard(s, Fps, 0, curCar);
387 TRACE_GL("cGrScreen::update display boards");
388
389 STOP_PROFILE("grDisp**");
390}
391
393{
394 int camNum;
395 int i;
396 class cGrCamera *cam;
397 const char *carName;
398 const int BUFSIZE=1024;
399 char buf[BUFSIZE];
400 char path[BUFSIZE];
401 char path2[BUFSIZE];
402
403 snprintf(path, BUFSIZE, "%s/%d", GR_SCT_DISPMODE, id);
404
405 if (!curCar) {
406 carName = GfParmGetStr(grHandle, path, GR_ATT_CUR_DRV, "");
407 for (i = 0; i < s->_ncars; i++) {
408 if (!strcmp(s->cars[i]->_name, carName)) {
409 break;
410 }
411 }
412 if (i < s->_ncars) {
413 curCar = s->cars[i];
414 } else if (id < s->_ncars) {
415 curCar = s->cars[id];
416 } else {
417 curCar = s->cars[0];
418 }
419 }
420 snprintf(path2, BUFSIZE, "%s/%s", GR_SCT_DISPMODE, curCar->_name);
421
422 curCamHead = (int)GfParmGetNum(grHandle, path, GR_ATT_CAM_HEAD, NULL, 9);
423 camNum = (int)GfParmGetNum(grHandle, path, GR_ATT_CAM, NULL, 0);
426 camNum = (int)GfParmGetNum(grHandle, path2, GR_ATT_CAM, NULL, (tdble)camNum);
428
430 curCam = NULL;
431 while (cam) {
432 if (cam->getId() == camNum) {
433 curCam = (cGrPerspCamera*)cam;
434 break;
435 }
436 cam = cam->next();
437 }
438
439 if (curCam == NULL) {
440 // back to default camera
441 curCamHead = 0;
443 GfParmSetNum(grHandle, path, GR_ATT_CAM, NULL, (tdble)curCam->getId());
445 }
446
447 snprintf(buf, BUFSIZE, "%s-%d-%d", GR_ATT_FOVY, curCamHead, curCam->getId());
448 curCam->loadDefaults(buf);
449 drawCurrent = curCam->getDrawCurrent();
450 board->loadDefaults(curCar);
451}
452
453
454/* Create cameras */
456{
457 tdble fovFactor;
458 int i;
459
460 fovFactor = GfParmGetNum(grHandle, GR_SCT_GRAPHIC, GR_ATT_FOVFACT, (char*)NULL, 1.0);
461 fovFactor *= GfParmGetNum(grTrackHandle, TRK_SECT_GRAPH, TRK_ATT_FOVFACT, (char*)NULL, 1.0);
462
463 if (boardCam == NULL) {
464 boardCam = new cGrOrthoCamera(this,0, grWinw*600/grWinh, 0, 600);
465 }
466
467 if (bgCam == NULL) {
468 bgCam = new cGrBackgroundCam(this);
469 }
470
471 if (mirrorCam == NULL) {
473 this,
474 -1,
475 0, // drawCurr
476 1, // drawBG
477 90.0, // fovy
478 0.0, // fovymin
479 360.0, // fovymax
480 0.3, // near
481 300.0 * fovFactor, // far
482 200.0 * fovFactor, // fog1
483 300.0 * fovFactor // fog
484 );
485 }
486
487 // Scene Cameras
488 unsigned int j;
489 class cGrCamera *cam;
490 for (j = 0; j < sizeof(cams)/sizeof(cams[0]); j++) {
491 while ((cam = GF_TAILQ_FIRST(&cams[j])) != 0) {
492 cam->remove(&cams[j]);
493 delete cam;
494 }
495 }
496 memset(cams, 0, sizeof(cams));
497
498 grCamCreateSceneCameraList(this, cams, fovFactor);
499
500 cars = (tCarElt**)calloc(s->_ncars, sizeof (tCarElt*));
501 for (i = 0; i < s->_ncars; i++) {
502 cars[i] = s->cars[i];
503 }
504
505 loadParams(s);
506}
507
509{
510 if (board == NULL) {
511 board = new cGrBoard (id);
512 }
513 board->initBoard ();
514}
static Vector y[4]
Definition Convex.cpp:56
#define RM_DRV_HUMAN
Definition car.h:122
struct CarElt tCarElt
Car structure (tCarElt).
cGrCamera * next(void)
Definition grcam.h:157
int getId(void)
Definition grcam.h:91
void remove(tGrCamHead *head)
Definition grcam.h:150
int scrx
Definition grscreen.h:47
float viewRatio
Definition grscreen.h:48
tCarElt ** cars
Definition grscreen.h:32
void desactivate(void)
Definition grscreen.cpp:133
class cGrCarCamMirror * mirrorCam
Definition grscreen.h:38
int mirrorFlag
Definition grscreen.h:54
class cGrBackgroundCam * bgCam
Definition grscreen.h:41
int scrh
Definition grscreen.h:47
int selectPrevFlag
Definition grscreen.h:53
void initBoard(void)
Definition grscreen.cpp:508
void selectTrackMap()
Definition grscreen.cpp:159
tCarElt * curCar
Definition grscreen.h:31
int scry
Definition grscreen.h:47
void camDraw(tSituation *s)
Definition grscreen.cpp:258
void initCams(tSituation *s)
Definition grscreen.cpp:455
class cGrBoard * board
Definition grscreen.h:43
void switchMirror(void)
Definition grscreen.cpp:179
class cGrPerspCamera * dispCam
Definition grscreen.h:39
void setZoom(long zoom)
Definition grscreen.cpp:139
class cGrOrthoCamera * boardCam
Definition grscreen.h:40
void loadParams(tSituation *s)
Definition grscreen.cpp:392
int curCamHead
Definition grscreen.h:34
int scrw
Definition grscreen.h:47
cGrScreen(int id)
Definition grscreen.cpp:41
void update(tSituation *s, float Fps)
Definition grscreen.cpp:304
int isInScreen(int x, int y)
Definition grscreen.cpp:90
void activate(int x, int y, int w, int h)
Definition grscreen.cpp:113
int active
Definition grscreen.h:50
void selectNextCar(void)
Definition grscreen.cpp:144
tGrCamHead cams[10]
Definition grscreen.h:35
void selectCamera(long cam)
Definition grscreen.cpp:198
int drawCurrent
Definition grscreen.h:45
void selectPrevCar(void)
Definition grscreen.cpp:149
int selectNextFlag
Definition grscreen.h:52
class cGrPerspCamera * curCam
Definition grscreen.h:37
void setCurrentCar(tCarElt *newCurCar)
Definition grscreen.cpp:107
void selectBoard(long brd)
Definition grscreen.cpp:154
#define GR_ATT_CAM_HEAD
Definition graphic.h:65
#define GR_ATT_CUR_DRV
Definition graphic.h:94
#define GR_ATT_MIRROR
Definition graphic.h:66
#define GR_SCT_GRAPHIC
Definition graphic.h:82
#define GR_SCT_DISPMODE
Definition graphic.h:63
#define GR_ATT_MAP
Definition graphic.h:67
#define GR_ATT_FOVFACT
Definition graphic.h:90
#define GR_ATT_CAM
Definition graphic.h:64
#define GR_ATT_FOVY
Definition graphic.h:68
void grCamCreateSceneCameraList(class cGrScreen *myscreen, tGrCamHead *cams, tdble fovFactor)
Definition grcam.cpp:1452
#define GR_ZOOM_DFLT
Definition grcam.h:286
void grDrawCar(tCarElt *car, tCarElt *curCar, int dispCarFlag, int dispDrvFlag, double curTime, class cGrPerspCamera *curCam)
Definition grcar.cpp:761
void * grHandle
Definition grmain.cpp:58
int grWinw
Definition grmain.cpp:61
int grWinh
Definition grmain.cpp:61
void * grTrackHandle
Definition grmain.cpp:59
float tdble
Floating point type used in TORCS.
Definition tgf.h:47
const char * GfParmGetStr(void *parmHandle, const char *path, const char *key, const char *deflt)
Get a string parameter from the parameter set handle.
Definition params.cpp:2453
int GfParmSetStr(void *handle, const char *path, const char *key, const char *val)
Set a string parameter in the parameter set handle.
Definition params.cpp:2623
tdble GfParmGetNum(void *handle, const char *path, const char *key, const char *unit, tdble deflt)
Get a numerical parameter from the parameter set handle.
Definition params.cpp:2534
int GfParmSetNum(void *handle, const char *path, const char *key, const char *unit, tdble val)
Set a numerical parameter in the parameter set handle.
Definition params.cpp:2737
int GfParmWriteFile(const char *file, void *parmHandle, const char *name)
Write parameter set into file.
Definition params.cpp:1732
#define GF_TAILQ_END(head)
End of a TAILQ.
Definition tgf.h:469
#define GF_TAILQ_FIRST(head)
First element of a TAILQ.
Definition tgf.h:463
void grDrawBackground(class cGrCamera *cam, class cGrBackgroundCam *bgCam)
Definition grscene.cpp:629
void grSetRenderOrigin(double x, double y)
Definition grscene.cpp:257
void grDrawScene(void)
Definition grscene.cpp:249
static int comparCars(const void *car1, const void *car2)
Definition grscreen.cpp:246
static class cGrPerspCamera * ThedispCam
Definition grscreen.cpp:243
#define TRACE_GL(msg)
Definition grutil.h:28
#define _ncars
Definition raceman.h:77
struct Situation tSituation
cars situation used to inform the GUI and the drivers
tCarElt ** cars
list of cars
Definition raceman.h:89
double currentTime
current time in sec since the beginning of the simulation
Definition raceman.h:87
#define STOP_PROFILE(a)
Definition tgf.h:597
#define START_PROFILE(a)
Definition tgf.h:596
#define FREEZ(x)
Definition tgf.h:59
#define TRK_SECT_GRAPH
Definition track.h:59
#define TRK_ATT_FOVFACT
Definition track.h:83