TORCS  1.3.9
The Open Racing Car Simulator
trackselect.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  trackselect.cpp -- interactive track selection
3  -------------------
4  created : Mon Aug 16 21:43:00 CEST 1999
5  copyright : (C) 1999-2014 by Eric Espie, Bernhard Wymann
6  email : torcs@free.fr
7  version : $Id$
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 
25 #include <stdlib.h>
26 #include <stdio.h>
27 #ifdef WIN32
28 #include <windows.h>
29 #endif
30 #include <tgfclient.h>
31 #include <track.h>
32 #include <osspec.h>
33 #include <raceman.h>
34 #include <racescreens.h>
35 #include <portability.h>
36 
37 
38 /* Tracks Categories */
40 static void *scrHandle;
41 static int TrackLabelId;
42 static int CatLabelId;
43 static int MapId;
44 static int AuthorId;
45 static int LengthId;
46 static int WidthId;
47 static int DescId;
48 static int PitsId;
50 
51 
52 static void rmtsActivate(void * /* dummy */)
53 {
54  /* call display function of graphic */
55  //gfuiReleaseImage(MapId);
56 }
57 
58 
59 static void rmtsFreeLists(void *vl)
60 {
61  GfDirFreeList((tFList*)vl, NULL, true, true);
62 }
63 
64 
65 static char * rmGetMapName(char* buf, const int BUFSIZE)
66 {
67  snprintf(buf, BUFSIZE, "tracks/%s/%s/%s.png", CategoryList->name,
68  ((tFList*)CategoryList->userData)->name, ((tFList*)CategoryList->userData)->name);
69  return buf;
70 }
71 
72 
73 static void rmtsDeactivate(void *screen)
74 {
76 
78  if (screen) {
79  GfuiScreenActivate(screen);
80  }
81 }
82 
83 
84 static void rmUpdateTrackInfo(void)
85 {
86  void *trackHandle;
87  float tmp;
88  tTrack *trk;
89  const int BUFSIZE = 1024;
90  char buf[BUFSIZE];
91 
92  snprintf(buf, BUFSIZE, "tracks/%s/%s/%s.%s", CategoryList->name, ((tFList*)CategoryList->userData)->name,
93  ((tFList*)CategoryList->userData)->name, TRKEXT);
94  trackHandle = GfParmReadFile(buf, GFPARM_RMODE_STD); /* COMMENT VALID? don't release, the name is used later */
95 
96  if (!trackHandle) {
97  GfTrace("File %s has pb\n", buf);
98  return;
99  }
100  trk = ts->trackItf.trkBuild(buf);
101 
104 
105  tmp = GfParmGetNum(trackHandle, TRK_SECT_MAIN, TRK_ATT_WIDTH, NULL, 0);
106  snprintf(buf, BUFSIZE, "%.2f m", tmp);
108  tmp = trk->length;
109  snprintf(buf, BUFSIZE, "%.2f m", tmp);
111 
112  if (trk->pits.nMaxPits != 0) {
113  snprintf(buf, BUFSIZE, "%d", trk->pits.nMaxPits);
115  } else {
117  }
118 
120  GfParmReleaseHandle(trackHandle);
121 }
122 
123 
124 static void rmtsPrevNext(void *vsel)
125 {
126  const int BUFSIZE = 1024;
127  char buf[BUFSIZE];
128 
129  if (vsel == 0) {
130  CategoryList->userData = (void*)(((tFList*)CategoryList->userData)->prev);
131  } else {
132  CategoryList->userData = (void*)(((tFList*)CategoryList->userData)->next);
133  }
134 
138 }
139 
140 
141 static void rmCatPrevNext(void *vsel)
142 {
143  const int BUFSIZE = 1024;
144  char buf[BUFSIZE];
145 
146  if (vsel == 0) {
148  } else {
150  }
151 
156 }
157 
158 
159 void rmtsSelect(void * /* dummy */)
160 {
161  int curTrkIdx;
162  const int BUFSIZE = 1024;
163  char path[BUFSIZE];
164 
165  curTrkIdx = (int)GfParmGetNum(ts->param, RM_SECT_TRACKS, RE_ATTR_CUR_TRACK, NULL, 1);
166  snprintf(path, BUFSIZE, "%s/%d", RM_SECT_TRACKS, curTrkIdx);
169 
171 }
172 
173 
174 static void rmtsAddKeys(void)
175 {
176  GfuiAddKey(scrHandle, 13, "Select Track", NULL, rmtsSelect, NULL);
177  GfuiAddKey(scrHandle, 27, "Cancel Selection", ts->prevScreen, rmtsDeactivate, NULL);
178  GfuiAddSKey(scrHandle, GLUT_KEY_LEFT, "Previous Track", (void*)0, rmtsPrevNext, NULL);
179  GfuiAddSKey(scrHandle, GLUT_KEY_RIGHT, "Next Track", (void*)1, rmtsPrevNext, NULL);
180  GfuiAddSKey(scrHandle, GLUT_KEY_F12, "Screen-Shot", NULL, GfuiScreenShot, NULL);
181  GfuiAddSKey(scrHandle, GLUT_KEY_UP, "Previous Track Category", (void*)0, rmCatPrevNext, NULL);
182  GfuiAddSKey(scrHandle, GLUT_KEY_DOWN, "Next Track Category", (void*)1, rmCatPrevNext, NULL);
183 }
184 
185 
194 char* RmGetTrackName(char *category, char *trackName)
195 {
196  void *trackHandle;
197  char *name;
198  const int BUFSIZE = 1024;
199  char buf[BUFSIZE];
200 
201  snprintf(buf, BUFSIZE, "tracks/%s/%s/%s.%s", category, trackName, trackName, TRKEXT);
202  trackHandle = GfParmReadFile(buf, GFPARM_RMODE_STD); /* don't release, the name is used later */
203 
204  if (trackHandle) {
205  name = strdup(GfParmGetStr(trackHandle, TRK_SECT_HDR, TRK_ATT_NAME, trackName));
206  } else {
207  GfTrace("File %s has pb\n", buf);
208  return strdup("");
209  }
210 
211  GfParmReleaseHandle(trackHandle);
212  return name;
213 }
214 
215 
223 char* RmGetCategoryName(char *category)
224 {
225  void *categoryHandle;
226  char *name;
227  const int BUFSIZE = 1024;
228  char buf[BUFSIZE];
229 
230  snprintf(buf, BUFSIZE, "data/tracks/%s.%s", category, TRKEXT);
231  categoryHandle = GfParmReadFile(buf, GFPARM_RMODE_STD); /* don't release, the name is used later */
232 
233  if (categoryHandle) {
234  name = strdup(GfParmGetStr(categoryHandle, TRK_SECT_HDR, TRK_ATT_NAME, category));
235  } else {
236  GfTrace("File %s has pb\n", buf);
237  return strdup("");
238  }
239 
240  GfParmReleaseHandle(categoryHandle);
241  return name;
242 }
243 
244 
250 void RmTrackSelect(void *vs)
251 {
252  const char *defaultTrack;
253  const char *defaultCategory;
254  tFList *CatCur;
255  tFList *TrList, *TrCur;
256  int Xpos, Ypos, DX, DY;
257  int curTrkIdx;
258  const int BUFSIZE = 1024;
259  char buf[BUFSIZE];
260  char path[BUFSIZE];
261 
262  ts = (tRmTrackSelect*)vs;
263 
264  /* Get the list of categories directories */
265  CategoryList = GfDirGetList("tracks");
266  if (CategoryList == NULL) {
267  GfTrace("RmTrackSelect: No track category available\n");
268  return;
269  }
270 
271  CatCur = CategoryList;
272  do {
273  CatCur->dispName = RmGetCategoryName(CatCur->name);
274  if (strlen(CatCur->dispName) == 0) {
275  GfTrace("RmTrackSelect: No definition for track category %s\n", CatCur->name);
276  return;
277  }
278 
279  /* get the tracks in the category directory */
280  snprintf(buf, BUFSIZE, "tracks/%s", CatCur->name);
281  TrList = GfDirGetList(buf);
282  if (TrList == NULL) {
283  GfTrace("RmTrackSelect: No track for category %s available\n", CatCur->name);
284  return;
285  }
286  TrList = TrList->next; /* get the first one */
287  CatCur->userData = (void*)TrList;
288  TrCur = TrList;
289  do {
290  TrCur->dispName = RmGetTrackName(CatCur->name, TrCur->name);
291  if (strlen(TrCur->dispName) == 0) {
292  GfTrace("RmTrackSelect: No definition for track %s\n", TrCur->name);
293  return;
294  }
295  TrCur = TrCur->next;
296  } while (TrCur != TrList);
297 
298  CatCur = CatCur->next;
299  } while (CatCur != CategoryList);
300 
301  curTrkIdx = (int)GfParmGetNum(ts->param, RM_SECT_TRACKS, RE_ATTR_CUR_TRACK, NULL, 1);
302  snprintf(path, BUFSIZE, "%s/%d", RM_SECT_TRACKS, curTrkIdx);
303  defaultCategory = GfParmGetStr(ts->param, path, RM_ATTR_CATEGORY, CategoryList->name);
304  /* XXX coherency check */
305  defaultTrack = GfParmGetStr(ts->param, path, RM_ATTR_NAME, ((tFList*)CategoryList->userData)->name);
306 
307  CatCur = CategoryList;
308  do {
309  if (strcmp(CatCur->name, defaultCategory) == 0) {
310  CategoryList = CatCur;
311  TrCur = (tFList*)(CatCur->userData);
312  do {
313  if (strcmp(TrCur->name, defaultTrack) == 0) {
314  CatCur->userData = (void*)TrCur;
315  break;
316  }
317  TrCur = TrCur->next;
318  } while (TrCur != TrList);
319  break;
320  }
321  CatCur = CatCur->next;
322  } while (CatCur != CategoryList);
323 
324  scrHandle = GfuiScreenCreateEx((float*)NULL, NULL, rmtsActivate, NULL, (tfuiCallback)NULL, 1);
325  GfuiScreenAddBgImg(scrHandle, "data/img/splash-qrtrk.png");
326 
327  rmtsAddKeys();
328 
329  GfuiTitleCreate(scrHandle, "Select Track", 0);
330 
332  "data/img/arrow-left.png",
333  "data/img/arrow-left.png",
334  "data/img/arrow-left.png",
335  "data/img/arrow-left-pushed.png",
336  80, 400, GFUI_ALIGN_HC_VB, 0,
337  (void*)0, rmCatPrevNext,
338  NULL, (tfuiCallback)NULL, (tfuiCallback)NULL);
339 
340 
344  320, 400, GFUI_ALIGN_HC_VB,
345  30);
346 
348  "data/img/arrow-right.png",
349  "data/img/arrow-right.png",
350  "data/img/arrow-right.png",
351  "data/img/arrow-right-pushed.png",
352  540, 400, GFUI_ALIGN_HC_VB, 0,
353  (void*)1, rmCatPrevNext,
354  NULL, (tfuiCallback)NULL, (tfuiCallback)NULL);
355 
357  "data/img/arrow-left.png",
358  "data/img/arrow-left.png",
359  "data/img/arrow-left.png",
360  "data/img/arrow-left-pushed.png",
361  80, 370, GFUI_ALIGN_HC_VB, 0,
362  (void*)0, rmtsPrevNext,
363  NULL, (tfuiCallback)NULL, (tfuiCallback)NULL);
364 
365 
367  ((tFList*)CategoryList->userData)->dispName,
369  320, 370, GFUI_ALIGN_HC_VB,
370  30);
371 
373  "data/img/arrow-right.png",
374  "data/img/arrow-right.png",
375  "data/img/arrow-right.png",
376  "data/img/arrow-right-pushed.png",
377  540, 370, GFUI_ALIGN_HC_VB, 0,
378  (void*)1, rmtsPrevNext,
379  NULL, (tfuiCallback)NULL, (tfuiCallback)NULL);
380 
381  int scrw, scrh, vw, vh;
382  GfScrGetSize(&scrw, &scrh, &vw, &vh);
384  320, 100, (int) (vh*260.0f/vw), 195,
385  rmGetMapName(buf, BUFSIZE));
386 
388  NULL, rmtsSelect, NULL, (tfuiCallback)NULL, (tfuiCallback)NULL);
389 
391  ts->prevScreen, rmtsDeactivate, NULL, (tfuiCallback)NULL, (tfuiCallback)NULL);
392 
393  Xpos = 20;
394  Ypos = 320;
395  DX = 110;
396  DY = 30;
397 
399  "Description:",
401  Xpos, Ypos,
402  GFUI_ALIGN_HL_VB, 0);
403 
405  "",
407  Xpos + DX, Ypos,
408  GFUI_ALIGN_HL_VB, 50);
409 
410  Ypos -= DY;
411 
413  "Author:",
415  Xpos, Ypos,
416  GFUI_ALIGN_HL_VB, 0);
417 
419  "",
421  Xpos + DX, Ypos,
422  GFUI_ALIGN_HL_VB, 20);
423 
424  Ypos -= DY;
425 
427  "Length:",
429  Xpos, Ypos,
430  GFUI_ALIGN_HL_VB, 0);
431 
433  "",
435  Xpos + DX, Ypos,
436  GFUI_ALIGN_HL_VB, 20);
437 
438  Ypos -= DY;
439 
441  "Width:",
443  Xpos, Ypos,
444  GFUI_ALIGN_HL_VB, 0);
445 
447  "",
449  Xpos + DX, Ypos,
450  GFUI_ALIGN_HL_VB, 20);
451 
452  Ypos -= DY;
453 
455  "Pits:",
457  Xpos, Ypos,
458  GFUI_ALIGN_HL_VB, 0);
459 
461  "",
463  Xpos + DX, Ypos,
464  GFUI_ALIGN_HL_VB, 20);
465 
467 
469 }
tfTrackBuild trkBuild
build track structure for simu
Definition: track.h:604
int nMaxPits
number max of pits
Definition: track.h:462
#define TRK_ATT_NAME
Definition: track.h:47
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:2477
static tRmTrackSelect * ts
Definition: trackselect.cpp:49
void * userData
User data.
Definition: tgf.h:235
#define GFUI_MOUSE_UP
Definition: tgfclient.h:80
void * GfParmReadFile(const char *file, int mode)
Read parameter set from file and return handle to parameter set.
Definition: params.cpp:1157
static int scrh
Definition: mouseconfig.cpp:50
int GfuiButtonCreate(void *scr, const char *text, int font, int x, int y, int width, int align, int mouse, void *userDataOnPush, tfuiCallback onPush, void *userDataOnFocus, tfuiCallback onFocus, tfuiCallback onFocusLost)
Add a button to a screen.
Definition: guibutton.cpp:248
#define GFPARM_RMODE_STD
if handle already openned return it
Definition: tgf.h:265
static void rmtsPrevNext(void *vsel)
#define RM_SECT_TRACKS
Definition: raceman.h:239
void GfParmReleaseHandle(void *parmHandle)
Release given parameter set handle parmHandle.
Definition: params.cpp:1834
void GfuiAddKey(void *scr, unsigned char key, const char *descr, void *userData, tfuiCallback onKeyPressed, tfuiCallback onKeyReleased)
Add a Keyboard callback to a screen.
Definition: gui.cpp:742
Track structure.
Definition: track.h:502
void GfuiScreenRelease(void *scr)
Release the given screen.
Definition: gui.cpp:618
int GfuiLabelCreate(void *scr, const char *text, int font, int x, int y, int align, int maxlen)
Add a label to a screen.
Definition: guilabel.cpp:142
static int DescId
Definition: trackselect.cpp:47
static int CatLabelId
Definition: trackselect.cpp:42
#define TRK_SECT_HDR
Definition: track.h:43
void * param
Pointer to race manager parameter set (to set the track)
Definition: racescreens.h:34
void GfuiScreenAddBgImg(void *scr, const char *filename)
Add an image background to a screen.
Definition: gui.cpp:961
tTrackItf trackItf
Track module interface.
Definition: racescreens.h:37
List of (DLL) files for a Directory.
Definition: tgf.h:229
static void rmtsActivate(void *)
Definition: trackselect.cpp:52
char * RmGetCategoryName(char *category)
Get the track category name from the track category file.
void GfuiStaticImageSet(void *scr, int id, char *name)
Replace an image by another one.
Definition: guiimage.cpp:86
#define RM_ATTR_NAME
Definition: raceman.h:257
void rmtsSelect(void *)
static int WidthId
Definition: trackselect.cpp:46
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:2311
#define GFUI_FONT_LARGE_C
Definition: tgfclient.h:172
The Gaming Framework API (client part).
void RmTrackSelect(void *vs)
Track selection, the race manager parameter set is handed over in vs, tRmTrackSelect.param.
#define GFUI_FONT_LARGE
Definition: tgfclient.h:168
tdble length
main track length
Definition: track.h:512
static void rmCatPrevNext(void *vsel)
void * GfuiScreenCreateEx(float *bgColor, void *userDataOnActivate, tfuiCallback onActivate, void *userDataOnDeactivate, tfuiCallback onDeactivate, int mouseAllowed)
Create a screen.
Definition: gui.cpp:578
#define TRK_ATT_DESCR
Definition: track.h:50
struct FList * next
Next entry.
Definition: tgf.h:231
void GfuiLabelSetText(void *scr, int id, const char *text)
Change the text of a label.
Definition: guilabel.cpp:212
static int scrw
Definition: mouseconfig.cpp:50
char * dispName
Name to display on screen.
Definition: tgf.h:234
static int TrackLabelId
Definition: trackselect.cpp:41
static void rmtsFreeLists(void *vl)
Definition: trackselect.cpp:59
void * nextScreen
Race manager screen to go after select.
Definition: racescreens.h:36
static void rmtsDeactivate(void *screen)
Definition: trackselect.cpp:73
int GfuiStaticImageCreate(void *scr, int x, int y, int w, int h, char *name)
Create a new static image.
Definition: guiimage.cpp:48
static int MapId
Definition: trackselect.cpp:43
void(* tfuiCallback)(void *)
Definition: tgfclient.h:105
This is the race information structures.
static int LengthId
Definition: trackselect.cpp:45
static void * scrHandle
Definition: trackselect.cpp:40
void GfuiScreenActivate(void *screen)
Activate a screen and make it current.
Definition: gui.cpp:467
void GfuiAddSKey(void *scr, int key, const char *descr, void *userData, tfuiCallback onKeyPressed, tfuiCallback onKeyReleased)
Add a Special Keyboard shortcut to the screen.
Definition: gui.cpp:816
void GfDirFreeList(tFList *list, tfDirfreeUserData freeUserData, bool freename, bool freedispname)
Free a directory list.
Definition: directory.cpp:76
void * prevScreen
Race manager screen to go back.
Definition: racescreens.h:35
#define GFUI_ALIGN_HC_VB
Definition: tgfclient.h:72
static int PitsId
Definition: trackselect.cpp:48
Structure for track selection.
Definition: racescreens.h:32
void GfScrGetSize(int *scrw, int *scrh, int *vieww, int *viewh)
Get the screen and viewport sizes.
Definition: screen.cpp:471
#define GFUI_ALIGN_HL_VB
Definition: tgfclient.h:69
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:2392
struct FList * prev
Previous entry.
Definition: tgf.h:232
void GfuiScreenShot(void *)
Save a screen shot in png format.
Definition: gui.cpp:913
int GfuiTitleCreate(void *scr, const char *text, int maxlen)
Add a Title to the screen.
Definition: guilabel.cpp:170
#define RM_ATTR_CATEGORY
Definition: raceman.h:259
int GfuiGrButtonCreate(void *scr, const char *disabled, const char *enabled, const char *focused, const char *pushed, int x, int y, int align, int mouse, void *userDataOnPush, tfuiCallback onPush, void *userDataOnFocus, tfuiCallback onFocus, tfuiCallback onFocusLost)
Add a graphical button to a screen.
Definition: guibutton.cpp:62
#define GFUI_FONT_MEDIUM_C
Definition: tgfclient.h:173
static char * rmGetMapName(char *buf, const int BUFSIZE)
Definition: trackselect.cpp:65
#define TRK_ATT_WIDTH
Definition: track.h:45
#define TRK_ATT_AUTHOR
Definition: track.h:49
char * RmGetTrackName(char *category, char *trackName)
Get the track name defined in the parameters.
#define GFUI_FONT_MEDIUM
Definition: tgfclient.h:169
char * name
File name.
Definition: tgf.h:233
#define GfTrace
Definition: tgf.h:336
tfTrackShutdown trkShutdown
Definition: track.h:612
#define RE_ATTR_CUR_TRACK
Definition: raceman.h:334
static int AuthorId
Definition: trackselect.cpp:44
Track Structure and Track Loader Module Definition.
static tFList * CategoryList
Definition: trackselect.cpp:39
tTrackPitInfo pits
Pits information.
Definition: track.h:514
tFList * GfDirGetList(const char *dir)
Get the list of files of a given directory.
Definition: directory.cpp:43
#define TRK_SECT_MAIN
Definition: track.h:136
static void rmtsAddKeys(void)
static void rmUpdateTrackInfo(void)
Definition: trackselect.cpp:84