TORCS  1.3.9
The Open Racing Car Simulator
track.cpp
Go to the documentation of this file.
1 /***************************************************************************
2 
3  file : track.cpp
4  created : Sun Jan 30 22:54:56 CET 2000
5  copyright : (C) 2000 by Eric Espie
6  email : torcs@free.fr
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 
20 
21 #include <stdlib.h>
22 #include <math.h>
23 #include <stdio.h>
24 
25 #include <tgf.h>
26 #include <track.h>
27 #include <portability.h>
28 #include "trackinc.h"
29 
30 const tdble DEGPRAD = 180.0 / PI; /* degrees per radian */
31 
32 static tTrack *theTrack = NULL;
34 static void *TrackHandle;
35 
36 static void GetTrackHeader(void *TrackHandle);
37 
38 
39 /*
40  * External function used to (re)build a track
41  * from the track file
42  */
43 tTrack *
44 TrackBuildv1(char *trackfile)
45 {
46  TrackShutdown();
47 
48  theTrack = (tTrack*)calloc(1, sizeof(tTrack));
49  theCamList = (tRoadCam*)NULL;
50 
52 
53  theTrack->filename = strdup(trackfile);
54 
56 
57 
58  switch(theTrack->version) {
59  case 0:
60  case 1:
61  case 2:
62  case 3:
64  break;
65  case 4:
67  break;
68  }
69 
70  return theTrack;
71 }
72 
73 tTrack *
74 TrackBuildEx(char *trackfile)
75 {
76  void *TrackHandle;
77 
78  theTrack = (tTrack*)calloc(1, sizeof(tTrack));
79  theCamList = (tRoadCam*)NULL;
80 
82 
83  theTrack->filename = strdup(trackfile);
84 
86 
87  switch(theTrack->version) {
88  case 0:
89  case 1:
90  case 2:
91  case 3:
93  break;
94  case 4:
96  break;
97  }
98 
99  return theTrack;
100 }
101 
102 
103 /*
104  * Function
105  * GetTrackHeader
106  *
107  * Description
108  * Get the header of the track file
109  * in order to know the number of segments
110  * Parameters
111  *
112  *
113  * Return
114  *
115  *
116  * Remarks
117  *
118  */
119 static void
121 {
122  tTrackGraphicInfo *graphic;
123  const char **env;
124  int i;
125  const int BUFSIZE = 256;
126  char buf[BUFSIZE];
127  char *s;
128 
134 
135  /* Graphic part */
136  graphic = &theTrack->graphic;
137 
139  "background.png");
140  graphic->bgtype = (int)GfParmGetNum(TrackHandle, TRK_SECT_GRAPH, TRK_ATT_BGTYPE, (char*)NULL, 0.0);
141 /* if (graphic->bgtype > 2) { */
142 /* graphic->background2 = GfParmGetStr(TrackHandle, TRK_SECT_GRAPH, TRK_ATT_BKGRND2, */
143 /* "background.png"); */
144 /* } */
145  graphic->bgColor[0] = (float)GfParmGetNum(TrackHandle, TRK_SECT_GRAPH, TRK_ATT_BGCLR_R, (char*)NULL, 0.0f);
146  graphic->bgColor[1] = (float)GfParmGetNum(TrackHandle, TRK_SECT_GRAPH, TRK_ATT_BGCLR_G, (char*)NULL, 0.0f);
147  graphic->bgColor[2] = (float)GfParmGetNum(TrackHandle, TRK_SECT_GRAPH, TRK_ATT_BGCLR_B, (char*)NULL, 0.1f);
148 
149  /* env map images */
150  snprintf(buf, BUFSIZE, "%s/%s", TRK_SECT_GRAPH, TRK_LST_ENV);
151  graphic->envnb = GfParmGetEltNb(TrackHandle, buf);
152  if (graphic->envnb < 1) {
153  graphic->envnb = 1;
154  }
155 
156  graphic->env = (const char**)calloc(graphic->envnb, sizeof(char*));
157  env = graphic->env;
158  for (i = 1; i <= graphic->envnb; i++) {
159  snprintf(buf, BUFSIZE, "%s/%s/%d", TRK_SECT_GRAPH, TRK_LST_ENV, i);
160  *env = GfParmGetStr(TrackHandle, buf, TRK_ATT_ENVNAME, "env.png");
161  env ++;
162  }
163 
164  theTrack->nseg = 0;
165 
166  s = strrchr(theTrack->filename, '/');
167  if (s == NULL) {
168  s = theTrack->filename;
169  } else {
170  s++;
171  }
172 
173  theTrack->internalname = strdup(s);
174  s = strrchr(theTrack->internalname, '.');
175  if (s != NULL) {
176  *s = 0;
177  }
178 
183 }
184 
185 static void
187 {
188  if (seg->barrier[0]) {
189  free(seg->barrier[0]);
190  }
191  if (seg->barrier[1]) {
192  free(seg->barrier[1]);
193  }
194  if (seg->ext) {
195  free(seg->ext->marks);
196  free(seg->ext);
197  }
198  if (seg->lside) {
199  freeSeg(seg->lside);
200  }
201  if (seg->rside) {
202  freeSeg(seg->rside);
203  }
204  free(seg);
205 }
206 
207 void
209 {
210  tTrackSeg *curSeg;
211  tTrackSeg *nextSeg;
212  tTrackSurface *curSurf;
213  tTrackSurface *nextSurf;
214  tRoadCam *curCam;
215  tRoadCam *nextCam;
216 
217  if (!theTrack) {
218  return;
219  }
220 
221  nextSeg = theTrack->seg->next;
222  do {
223  curSeg = nextSeg;
224  nextSeg = nextSeg->next;
225  freeSeg(curSeg);
226  } while (curSeg != theTrack->seg);
227 
228  curSurf = theTrack->surfaces;
229  while (curSurf) {
230  nextSurf = curSurf->next;
231  free(curSurf);
232  curSurf = nextSurf;
233  }
234 
235  curCam = theCamList;
236  if (curCam) {
237  do {
238  nextCam = curCam->next;
239  free(curCam);
240  curCam = nextCam;
241  } while (curCam != theCamList);
242  }
243  theCamList = NULL;
244 
246  free(theTrack->graphic.env);
247  free(theTrack->internalname);
248  free(theTrack->filename);
249  free(theTrack);
250 
252  theTrack = NULL;
253 }
#define TRK_ATT_NAME
Definition: track.h:47
tTrackOwnPit * driversPits
List of pits by driver.
Definition: track.h:475
int * marks
marks array
Definition: track.h:237
void * GfParmReadFile(const char *file, int mode)
Read parameter set from file and return handle to parameter set.
Definition: params.cpp:1157
#define GFPARM_RMODE_STD
if handle already openned return it
Definition: tgf.h:265
#define TRK_SECT_TURNMARKS
Definition: track.h:79
struct trackSurface * next
Next surface in list.
Definition: track.h:243
static tTrack * theTrack
Definition: track.cpp:32
#define TRK_ATT_VSPACE
Definition: track.h:81
void GfParmReleaseHandle(void *parmHandle)
Release given parameter set handle parmHandle.
Definition: params.cpp:1834
#define TRK_ATT_VERSION
Definition: track.h:48
void TrackShutdown(void)
Definition: track.cpp:208
tdble width
main track width
Definition: track.h:513
Track structure.
Definition: track.h:502
const tdble DEGPRAD
Definition: track.cpp:30
tTrack * TrackBuildEx(char *trackfile)
Definition: track.cpp:74
#define TRK_SECT_HDR
Definition: track.h:43
#define TRK_ATT_HSPACE
Definition: track.h:80
const double PI
PI.
Definition: tgf.h:69
#define TRK_ATT_BGCLR_G
Definition: track.h:60
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
char * internalname
Internal name of the track.
Definition: track.h:508
tTrackGraphicInfo graphic
Definition: track.h:520
#define TRK_ATT_BKGRND
Definition: track.h:56
tTurnMarksInfo turnMarksInfo
Definition: track.h:496
tTrackBarrier * barrier[2]
Segment barriers.
Definition: track.h:395
float bgColor[3]
Definition: track.h:493
road camera
Definition: track.h:226
static tRoadCam * theCamList
Definition: track.cpp:33
float tdble
Floating point type used in TORCS.
Definition: tgf.h:48
tdble width
Definition: track.h:483
int nseg
Number of segments.
Definition: track.h:510
static void GetTrackHeader(void *TrackHandle)
Definition: track.cpp:120
static void * TrackHandle
Definition: track.cpp:34
const char * name
Name of the track.
Definition: track.h:504
The Gaming Framework API.
void ReadTrack3(tTrack *theTrack, void *TrackHandle, tRoadCam **camList, int ext)
Definition: track3.cpp:1279
void * params
Parameters handle.
Definition: track.h:507
Surface.
Definition: track.h:242
tdble hSpace
Definition: track.h:484
#define TRK_ATT_CAT
Definition: track.h:51
#define GFPARM_RMODE_PRIVATE
Definition: tgf.h:268
#define TRK_ATT_BGCLR_B
Definition: track.h:61
#define TRK_ATT_BGCLR_R
Definition: track.h:59
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 trackSeg * lside
Definition: track.h:406
int version
Version of the track type.
Definition: track.h:511
Track segment (tTrackSeg) The segments can be straights (type TR_STR): (the track goes from the right...
Definition: track.h:276
const char * author
Author&#39;s name.
Definition: track.h:505
const char * background
Definition: track.h:490
tSegExt * ext
Definition: track.h:392
tTrack * TrackBuildv1(char *trackfile)
Definition: track.cpp:44
#define TRK_ATT_HEIGHT
Definition: track.h:208
int GfParmGetEltNb(void *handle, const char *path)
Count the number of subsections in a section in the parameter set handle.
Definition: params.cpp:2106
struct trackSeg * next
Next segment.
Definition: track.h:397
const char ** env
Definition: track.h:495
#define TRK_ATT_WIDTH
Definition: track.h:45
#define TRK_LST_ENV
Definition: track.h:62
void ReadTrack4(tTrack *theTrack, void *TrackHandle, tRoadCam **camList, int ext)
Definition: track4.cpp:898
#define TRK_ATT_AUTHOR
Definition: track.h:49
#define TRK_ATT_ENVNAME
Definition: track.h:63
#define TRK_SECT_GRAPH
Definition: track.h:53
tTrackSurface * surfaces
Segment surface list.
Definition: track.h:516
struct RoadCam * next
Definition: track.h:230
struct trackSeg * rside
Definition: track.h:406
tdble vSpace
Definition: track.h:485
#define TRK_ATT_BGTYPE
Definition: track.h:58
const char * category
Category of the track.
Definition: track.h:509
Track Structure and Track Loader Module Definition.
tTrackSeg * seg
Main track.
Definition: track.h:515
tTrackPitInfo pits
Pits information.
Definition: track.h:514
#define GFPARM_RMODE_CREAT
Create the file if doesn&#39;t exist.
Definition: tgf.h:267
#define TRK_SECT_MAIN
Definition: track.h:136
char * filename
Filename of the track description.
Definition: track.h:506
tdble height
Definition: track.h:482
static void freeSeg(tTrackSeg *seg)
Definition: track.cpp:186