TORCS  1.3.10
The Open Racing Car Simulator
Loading...
Searching...
No Matches
racegl.cpp
Go to the documentation of this file.
1/***************************************************************************
2
3 file : racegl.cpp
4 created : Sat Nov 16 18:22:00 CET 2002
5 copyright : (C) 2002-2026 by Eric Espie, Bernhard Wymann
6 email : berniw@bluewin.ch
7
8 ***************************************************************************/
9
10/***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 ***************************************************************************/
18
21#include <stdlib.h>
22#include <stdio.h>
23
24#include <tgfclient.h>
25#include <raceman.h>
26#include <robot.h>
27
28#include "racemain.h"
29#include "raceinit.h"
30#include "racestate.h"
31#include "raceengine.h"
32
33#include "racegl.h"
34
35static void *reScreenHandle = 0;
36static void *reHookHandle = 0;
37static int rePauseId;
38static int reMsgId;
39static int reBigMsgId;
40
41static float bgcolor[4] = {0.0, 0.0, 0.0, 0.0};
42static float white[4] = {1.0, 1.0, 1.0, 1.0};
43static float red[4] = {1.0, 0.0, 0.0, 1.0};
44
45static void
47{
49}
50
51static void
52reScreenActivate(void * /* dummy */)
53{
54 glutDisplayFunc(reDisplay);
55
56 if ((ReInfo->s->_raceState & RM_RACE_PAUSED) == 0) {
57 ReStart(); /* resynchro */
58 }
59 glutPostRedisplay();
60}
61
62static void
63ReBoardInfo(void * /* vboard */)
64{
65 if (ReInfo->s->_raceState & RM_RACE_PAUSED) {
66 ReInfo->s->_raceState &= ~RM_RACE_PAUSED;
67 ReStart();
69 } else {
70 ReInfo->s->_raceState |= RM_RACE_PAUSED;
71 ReStop();
73 }
74}
75
76static void
77reSkipPreStart(void * /* dummy */)
78{
79 if (ReInfo->s->currentTime < -1.0) {
80 ReInfo->s->currentTime = -1.0;
81 ReInfo->_reLastTime = -1.0;
82 }
83}
84
85static void
86reMovieCapture(void * /* dummy */)
87{
88 tRmMovieCapture *capture = &(ReInfo->movieCapture);
89
90 if (!capture->enabled || (ReInfo->_displayMode == RM_DISP_MODE_NONE)) {
91 GfOut("Video Capture Mode Not Enabled\n");
92 return;
93 }
94
95 capture->state = 1 - capture->state;
96 if (capture->state) {
97 GfOut("Video Capture Mode On\n");
98 capture->currentFrame = 0;
99 capture->currentCapture++;
100 capture->lastFrame = GfTimeClock() - capture->deltaFrame;
101 ReInfo->_displayMode = RM_DISP_MODE_CAPTURE;
102 } else {
103 GfOut("Video Capture Mode Off\n");
104 ReInfo->_displayMode = RM_DISP_MODE_NORMAL;
105 ReStart();
106 }
107
108}
109
110
111
112static void
114{
116 GfuiAddSKeyAction(reScreenHandle, GLUT_KEY_F12, "Screen Shot", [] { GfuiScreenShot(NULL); }, tGfuiAction());
117
118
119 GfuiAddKeyAction(reScreenHandle, '-', "Slow Time", [] { ReTimeMod(0); }, tGfuiAction());
120 GfuiAddKeyAction(reScreenHandle, '+', "Accelerate Time", [] { ReTimeMod(1); }, tGfuiAction());
121 GfuiAddKeyAction(reScreenHandle, '.', "Real Time", [] { ReTimeMod(2); }, tGfuiAction());
122 GfuiAddKeyAction(reScreenHandle, 'p', "Pause Race", [] { ReBoardInfo(NULL); }, tGfuiAction());
123 GfuiAddKeyAction(reScreenHandle, 27, "Stop Current Race", [] { ReStateApply(RE_STATE_RACE_STOP); }, tGfuiAction());
124 /* GfuiAddKey(reScreenHandle, 'q', "Exit of TORCS", (void*)RE_STATE_EXIT, ReStateApply, NULL); */
125 GfuiAddKeyAction(reScreenHandle, ' ', "Skip Pre Start", [] { reSkipPreStart(NULL); }, tGfuiAction());
126#ifdef DEBUG
127 //GfuiAddKey(reScreenHandle, '0', "One step simulation", (void*)1, reOneStep, NULL);
128#endif
129 GfuiAddKeyAction(reScreenHandle, 'c', "Movie Capture", [] { reMovieCapture(NULL); }, tGfuiAction());
130
131}
132
133
134void
135ReSetRaceMsg(const char *msg)
136{
137 static char *curMsg = 0;
138
139 if (curMsg) free(curMsg);
140
141 if (msg) {
142 curMsg = strdup(msg);
144 } else {
145 curMsg = 0;
147 }
148}
149
150void
151ReSetRaceBigMsg(const char *msg)
152{
153 static char *curMsg = 0;
154
155 if (curMsg) free(curMsg);
156
157 if (msg) {
158 curMsg = strdup(msg);
160 } else {
161 curMsg = 0;
163 }
164}
165
166void *
168{
169
171
173
174 reAddKeys();
175
177 "",
178 red,
180 320,
181 400,
183 42);
184
186 "P A U S E",
187 red,
189 320,
190 420,
192 0);
193
195 "",
196 red,
198 320,
199 360,
201 32);
202
204
205 return reScreenHandle;
206}
207
208
209void
211{
212 if (reScreenHandle) {
214 reScreenHandle = 0;
215 }
216}
217
218
219static void
220reHookActivate(void * /* dummy */)
221{
223}
224
225void *
227{
228 if (reHookHandle) {
229 return reHookHandle;
230 }
231
233
234 return reHookHandle;
235}
236
237
238void
240{
241 if (reHookHandle) {
243 reHookHandle = 0;
244 }
245}
246
247/**************************************************************************/
248
249/*
250 * Result only screen
251 */
252#define LINES 21
253
254static float *reColor[] = {white, red};
255
256static void *reResScreenHdle = 0;
257static int reResTitleId;
258static int reResMsgId[LINES];
259static int reResMsgClr[LINES];
260static char *reResMsg[LINES];
261static int reCurLine;
262
263static void
265{
267 GfuiAddSKeyAction(reResScreenHdle, GLUT_KEY_F12, "Screen Shot", [] { GfuiScreenShot(NULL); }, tGfuiAction());
268
269 GfuiAddKeyAction(reResScreenHdle, 27, "Stop Current Race", [] { ReStateApply(RE_STATE_RACE_STOP); }, tGfuiAction());
270 /* GfuiAddKey(reResScreenHdle, 'q', "Exit of TORCS", (void*)RE_STATE_EXIT, ReStateApply, NULL); */
271}
272
273static void
274reResScreenActivate(void * /* dummy */)
275{
276 glutDisplayFunc(reDisplay);
277 GfuiDisplay();
278 glutPostRedisplay();
279}
280
281
282static void
284{
285 GfuiDisplay();
286 glutPostRedisplay();
287}
288
289
290static void
291reResCont(void * /* dummy */)
292{
294}
295
296static void
297reResScreenShutdown(void * /* dummy */)
298{
299 int i;
300
301 for (i = 1; i < LINES; i++) {
302 FREEZ(reResMsg[i]);
303 }
304}
305
306void *
308{
309 int i;
310 int y, dy;
311 static const char *title[3] = {"Practice", "Qualifications", "Race"};
312
313 if (reResScreenHdle) {
315 }
316
318 [] { reResScreenActivate(NULL); },
319 [] { reResScreenShutdown(NULL); }, 0);
320
321 GfuiTitleCreate(reResScreenHdle, title[ReInfo->s->_raceType], strlen(title[ReInfo->s->_raceType]));
322
323 const char* img = GfParmGetStr(ReInfo->params, RM_SECT_HEADER, RM_ATTR_RUNIMG, 0);
324 if (img) {
326 }
327
328 reAddResKeys();
329
331 "",
332 red,
334 320, 420,
335 GFUI_ALIGN_HC_VB, 50);
336
337 y = 400;
338 dy = 378 / LINES;
339 for (i = 0; i < LINES; i++) {
340 FREEZ(reResMsg[i]);
341 reResMsgClr[i] = 0;
343 "",
344 white,
346 20, y,
347 GFUI_ALIGN_HL_VB, 120);
348 y -= dy;
349 }
350
351 reCurLine = 0;
352 return reResScreenHdle;
353}
354
355void
357{
358 if (reResScreenHdle) {
360 }
361}
362
363void
365{
366 int i;
367
368 if (reCurLine == LINES) {
369 free(reResMsg[0]);
370 for (i = 1; i < LINES; i++) {
371 reResMsg[i - 1] = reResMsg[i];
373 }
374 reCurLine--;
375 }
376 reResMsg[reCurLine] = strdup(text);
378 reCurLine++;
379}
380
381void
382ReResScreenSetText(const char *text, int line, int clr)
383{
384 if (line < LINES) {
385 FREEZ(reResMsg[line]);
386 reResMsg[line] = strdup(text);
387 if ((clr >= 0) && (clr < 2)) {
388 reResMsgClr[line] = clr;
389 } else {
390 reResMsgClr[line] = 0;
391 }
394 }
395}
396
397int
399{
400 return LINES;
401}
402
403void
405{
406 int i;
407
408 for (i = 0; i < LINES; i++) {
409 ReResScreenSetText("", i, 0);
410 }
411}
412
413
414void
416{
417 if (line < LINES) {
418 FREEZ(reResMsg[line]);
420 }
421}
422
423void
425{
426
428 "Continue",
430 320, 15, GFUI_BTNSZ,
432 0, [] { reResCont(NULL); },
434 GfuiAddKeyAction(reResScreenHdle, 13, "Continue", [] { reResCont(NULL); }, tGfuiAction());
435 GfuiAddKeyAction(reResScreenHdle, 27, "Continue", [] { reResCont(NULL); }, tGfuiAction());
436
437 glutDisplayFunc(reContDisplay);
438 glutPostRedisplay();
439}
440
static Vector y[4]
Definition Convex.cpp:56
void * GfuiScreenCreateExAction(float *bgColor, const tGfuiAction &onActivate, const tGfuiAction &onDeactivate, int mouseAllowed)
Create a screen.
Definition gui.cpp:619
void GfuiHelpScreen(void *prevScreen)
Generate a help screen.
Definition guihelp.cpp:47
void GfuiHookRelease(void *hook)
Release the given hook.
Definition gui.cpp:760
void GfuiLabelSetText(void *scr, int id, const char *text)
Change the text of a label.
Definition guilabel.cpp:193
void GfuiScreenRelease(void *scr)
Release the given screen.
Definition gui.cpp:670
int GfuiTitleCreate(void *scr, const char *text, int maxlen)
Add a Title to the screen.
Definition guilabel.cpp:150
void GfuiAddKeyAction(void *scr, unsigned char key, const char *descr, const tGfuiAction &onKeyPressed, const tGfuiAction &onKeyReleased)
Add a Keyboard callback to a screen.
Definition gui.cpp:809
void GfuiScreenAddBgImg(void *scr, const char *filename)
Add an image background to a screen.
Definition gui.cpp:1058
int GfuiVisibilitySet(void *scr, int id, int visible)
Set/unset the visibility attribute of an object.
void GfuiDisplay(void)
Draw the active GUI screen and swap the GLUT buffers.
Definition gui.cpp:175
void GfuiAddSKeyAction(void *scr, int key, const char *descr, const tGfuiAction &onKeyPressed, const tGfuiAction &onKeyReleased)
Add a Special Keyboard shortcut to the screen.
Definition gui.cpp:895
void * GfuiHookCreateAction(const tGfuiAction &onActivate)
Create a callback hook.
Definition gui.cpp:736
int GfuiLabelCreateEx(void *scr, const char *text, float *fgColor, int font, int x, int y, int align, int maxlen)
Create a new label (extended version).
Definition guilabel.cpp:51
void GfuiLabelSetColor(void *scr, int id, float *color)
Change the color of a label.
Definition guilabel.cpp:221
int GfuiButtonCreateAction(void *scr, const char *text, int font, int x, int y, int width, int align, int mouse, const tGfuiAction &onPush, const tGfuiAction &onFocus, const tGfuiAction &onFocusLost)
Add a button to a screen.
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
void GfuiScreenShot(void *)
Save a screen shot in png format.
Definition gui.cpp:998
double GfTimeClock(void)
Get the time in seconds.
Definition os.cpp:50
tRmInfo * ReInfo
void ReStart(void)
void ReTimeMod(int cmd)
void ReStop(void)
static void reResScreenShutdown(void *)
Definition racegl.cpp:297
void * ReScreenInit(void)
Definition racegl.cpp:167
void * ReHookInit(void)
Definition racegl.cpp:226
static void reScreenActivate(void *)
Definition racegl.cpp:52
static void * reScreenHandle
Definition racegl.cpp:35
static float red[4]
Definition racegl.cpp:43
void * ReResScreenInit(void)
Definition racegl.cpp:307
#define LINES
Definition racegl.cpp:252
static char * reResMsg[LINES]
Definition racegl.cpp:260
static int reCurLine
Definition racegl.cpp:261
void ReResScreenSetText(const char *text, int line, int clr)
Definition racegl.cpp:382
void ReResShowCont(void)
Definition racegl.cpp:424
void ReSetRaceBigMsg(const char *msg)
Definition racegl.cpp:151
static int reResMsgId[LINES]
Definition racegl.cpp:258
static void reResCont(void *)
Definition racegl.cpp:291
static void reAddKeys(void)
Definition racegl.cpp:113
void ReResEraseScreen(void)
Definition racegl.cpp:404
static void reAddResKeys(void)
Definition racegl.cpp:264
void ReResScreenRemoveText(int line)
Definition racegl.cpp:415
static int reMsgId
Definition racegl.cpp:38
static float white[4]
Definition racegl.cpp:42
static float bgcolor[4]
Definition racegl.cpp:41
static void reResScreenActivate(void *)
Definition racegl.cpp:274
void ReResScreenAddText(char *text)
Definition racegl.cpp:364
static float * reColor[]
Definition racegl.cpp:254
static int reResMsgClr[LINES]
Definition racegl.cpp:259
static int rePauseId
Definition racegl.cpp:37
void ReSetRaceMsg(const char *msg)
Definition racegl.cpp:135
static void * reResScreenHdle
Definition racegl.cpp:256
void ReHookShutdown(void)
Definition racegl.cpp:239
int ReResGetLines(void)
Definition racegl.cpp:398
static void * reHookHandle
Definition racegl.cpp:36
static void reContDisplay(void)
Definition racegl.cpp:283
static int reBigMsgId
Definition racegl.cpp:39
static void ReBoardInfo(void *)
Definition racegl.cpp:63
static void reSkipPreStart(void *)
Definition racegl.cpp:77
void ReScreenShutdown(void)
Definition racegl.cpp:210
static void reDisplay(void)
Definition racegl.cpp:46
static void reHookActivate(void *)
Definition racegl.cpp:220
void ReResScreenSetTitle(char *title)
Definition racegl.cpp:356
static void reMovieCapture(void *)
Definition racegl.cpp:86
static int reResTitleId
Definition racegl.cpp:257
#define RM_RACE_PAUSED
Definition raceman.h:68
struct RmMovieCapture tRmMovieCapture
#define RE_STATE_RACE_STOP
Definition raceman.h:106
#define RM_ATTR_RUNIMG
Definition raceman.h:263
#define RM_SECT_HEADER
Definition raceman.h:236
#define RM_DISP_MODE_CAPTURE
Definition raceman.h:148
#define RM_DISP_MODE_NONE
Definition raceman.h:149
#define RM_DISP_MODE_NORMAL
Definition raceman.h:147
void ReStateManage(void)
Definition racestate.cpp:52
void ReStateApply(int state)
double deltaFrame
Definition raceman.h:208
int currentCapture
Definition raceman.h:211
double lastFrame
Definition raceman.h:209
#define GfOut
Definition tgf.h:372
#define FREEZ(x)
Definition tgf.h:59
#define GFUI_FONT_BIG_C
Definition tgfclient.h:251
#define GFUI_FONT_LARGE_C
Definition tgfclient.h:252
#define GFUI_FONT_MEDIUM_C
Definition tgfclient.h:253
#define GFUI_ALIGN_HL_VB
Definition tgfclient.h:71
std::function< void()> tGfuiAction
Definition tgfclient.h:107
#define GFUI_BTNSZ
Definition tgfclient.h:292
#define GFUI_ALIGN_HC_VB
Definition tgfclient.h:74