TORCS  1.3.10
The Open Racing Car Simulator
Loading...
Searching...
No Matches
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-2026 by Eric EspiƩ, 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
19
20#include <stdlib.h>
21#include <math.h>
22#include <stdio.h>
23
24#include <tgf.h>
25#include <track.h>
26#include <portability.h>
27#include "trackinc.h"
28
29const tdble DEGPRAD = 180.0 / PI; /* degrees per radian */
30
31static tTrack *theTrack = NULL;
33static void *TrackHandle;
34
35static void GetTrackHeader(void *TrackHandle);
36
37
38/*
39 * External function used to (re)build a track
40 * from the track file
41 */
42tTrack *
43TrackBuildv1(char *trackfile)
44{
46
47 theTrack = (tTrack*)calloc(1, sizeof(tTrack));
48 theCamList = (tRoadCam*)NULL;
49
51
52 theTrack->filename = strdup(trackfile);
53
55
56
57 switch(theTrack->version) {
58 case 0:
59 case 1:
60 case 2:
61 case 3:
63 break;
64 case 4:
66 break;
67 }
68
69 return theTrack;
70}
71
72tTrack *
73TrackBuildEx(char *trackfile)
74{
76
77 theTrack = (tTrack*)calloc(1, sizeof(tTrack));
78 theCamList = (tRoadCam*)NULL;
79
81
82 theTrack->filename = strdup(trackfile);
83
85
86 switch(theTrack->version) {
87 case 0:
88 case 1:
89 case 2:
90 case 3:
92 break;
93 case 4:
95 break;
96 }
97
98 return theTrack;
99}
100
101
102/*
103 * Function
104 * GetTrackHeader
105 *
106 * Description
107 * Get the header of the track file
108 * in order to know the number of segments
109 * Parameters
110 *
111 *
112 * Return
113 *
114 *
115 * Remarks
116 *
117 */
118static void
120{
121 tTrackGraphicInfo *graphic;
122 const char **env;
123 int i;
124 const int BUFSIZE = 256;
125 char buf[BUFSIZE];
126 char *s;
127
129 theTrack->version = (int)GfParmGetNum(TrackHandle, TRK_SECT_HDR, TRK_ATT_VERSION, (char*)NULL, 0);
130 theTrack->width = GfParmGetNum(TrackHandle, TRK_SECT_MAIN, TRK_ATT_WIDTH, (char*)NULL, 15.0);
133
134 /* Graphic part */
135 graphic = &theTrack->graphic;
136
138 "background.png");
139 graphic->bgtype = (int)GfParmGetNum(TrackHandle, TRK_SECT_GRAPH, TRK_ATT_BGTYPE, (char*)NULL, 0.0);
140/* if (graphic->bgtype > 2) { */
141/* graphic->background2 = GfParmGetStr(TrackHandle, TRK_SECT_GRAPH, TRK_ATT_BKGRND2, */
142/* "background.png"); */
143/* } */
144 graphic->bgColor[0] = (float)GfParmGetNum(TrackHandle, TRK_SECT_GRAPH, TRK_ATT_BGCLR_R, (char*)NULL, 0.0f);
145 graphic->bgColor[1] = (float)GfParmGetNum(TrackHandle, TRK_SECT_GRAPH, TRK_ATT_BGCLR_G, (char*)NULL, 0.0f);
146 graphic->bgColor[2] = (float)GfParmGetNum(TrackHandle, TRK_SECT_GRAPH, TRK_ATT_BGCLR_B, (char*)NULL, 0.1f);
147
148 /* env map images */
149 snprintf(buf, BUFSIZE, "%s/%s", TRK_SECT_GRAPH, TRK_LST_ENV);
150 graphic->envnb = GfParmGetEltNb(TrackHandle, buf);
151 if (graphic->envnb < 1) {
152 graphic->envnb = 1;
153 }
154
155 graphic->env = (const char**)calloc(graphic->envnb, sizeof(char*));
156 env = graphic->env;
157 for (i = 1; i <= graphic->envnb; i++) {
158 snprintf(buf, BUFSIZE, "%s/%s/%d", TRK_SECT_GRAPH, TRK_LST_ENV, i);
159 *env = GfParmGetStr(TrackHandle, buf, TRK_ATT_ENVNAME, "env.png");
160 env ++;
161 }
162
163 theTrack->nseg = 0;
164
165 s = strrchr(theTrack->filename, '/');
166 if (s == NULL) {
167 s = theTrack->filename;
168 } else {
169 s++;
170 }
171
172 theTrack->internalname = strdup(s);
173 s = strrchr(theTrack->internalname, '.');
174 if (s != NULL) {
175 *s = 0;
176 }
177
182}
183
184static void
186{
187 if (seg->barrier[0]) {
188 free(seg->barrier[0]);
189 }
190 if (seg->barrier[1]) {
191 free(seg->barrier[1]);
192 }
193 if (seg->ext) {
194 free(seg->ext->marks);
195 free(seg->ext);
196 }
197 if (seg->lside) {
198 freeSeg(seg->lside);
199 }
200 if (seg->rside) {
201 freeSeg(seg->rside);
202 }
203 free(seg);
204}
205
206void
208{
209 tTrackSeg *curSeg;
210 tTrackSeg *nextSeg;
211 tTrackSurface *curSurf;
212 tTrackSurface *nextSurf;
213 tRoadCam *curCam;
214 tRoadCam *nextCam;
215
216 if (!theTrack) {
217 return;
218 }
219
220 if (theTrack->seg) {
221 nextSeg = theTrack->seg->next;
222 do {
223 curSeg = nextSeg;
224 nextSeg = nextSeg->next;
225 freeSeg(curSeg);
226 } while (curSeg != theTrack->seg);
227 }
228
229 curSurf = theTrack->surfaces;
230 while (curSurf) {
231 nextSurf = curSurf->next;
232 free(curSurf);
233 curSurf = nextSurf;
234 }
235
236 curCam = theCamList;
237 if (curCam) {
238 do {
239 nextCam = curCam->next;
240 free(curCam);
241 curCam = nextCam;
242 } while (curCam != theCamList);
243 }
244 theCamList = NULL;
245
246 if (theTrack->pits.driversPits) free(theTrack->pits.driversPits);
247 free(theTrack->graphic.env);
248 free(theTrack->internalname);
249 free(theTrack->filename);
250 free(theTrack);
251
253 TrackHandle = NULL;
254 theTrack = NULL;
255}
static const double PI
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
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
void GfParmReleaseHandle(void *parmHandle)
Release given parameter set handle parmHandle.
Definition params.cpp:1963
void * GfParmReadFile(const char *file, int mode)
Read parameter set from file and return handle to parameter set.
Definition params.cpp:1173
int GfParmGetEltNb(void *handle, const char *path)
Count the number of subsections in a section in the parameter set handle.
Definition params.cpp:2243
struct trackSeg tTrackSeg
Track segment (tTrackSeg) The segments can be straights (type TR_STR): (the track goes from the right...
struct RoadCam * next
Definition track.h:247
int * marks
marks array
Definition track.h:254
tTurnMarksInfo turnMarksInfo
Definition track.h:511
float bgColor[3]
Definition track.h:508
const char ** env
Definition track.h:510
const char * background
Definition track.h:505
Track structure.
Definition track.h:533
tdble height
Definition track.h:497
tdble vSpace
Definition track.h:500
tdble width
Definition track.h:498
tdble hSpace
Definition track.h:499
tSegExt * ext
Definition track.h:407
struct trackSeg * rside
Definition track.h:421
tTrackBarrier * barrier[2]
Segment barriers.
Definition track.h:410
struct trackSeg * next
Next segment.
Definition track.h:412
struct trackSeg * lside
Definition track.h:421
struct trackSurface * next
Next surface in list.
Definition track.h:260
#define GFPARM_RMODE_CREAT
Allow an empty in-memory set on file-open failure; does not write a file.
Definition tgf.h:266
#define GFPARM_RMODE_STD
Return a new handle sharing cached data for this filename, if available.
Definition tgf.h:264
#define GFPARM_RMODE_PRIVATE
Private means the handle is not shared/cached.
Definition tgf.h:267
void ReadTrack3(tTrack *theTrack, void *TrackHandle, tRoadCam **camList, int ext)
Definition track3.cpp:1282
void ReadTrack4(tTrack *theTrack, void *TrackHandle, tRoadCam **camList, int ext)
Definition track4.cpp:1018
static tRoadCam * theCamList
Definition track.cpp:32
static void * TrackHandle
Definition track.cpp:33
const tdble DEGPRAD
Definition track.cpp:29
tTrack * TrackBuildEx(char *trackfile)
Definition track.cpp:73
static void GetTrackHeader(void *TrackHandle)
Definition track.cpp:119
tTrack * TrackBuildv1(char *trackfile)
Definition track.cpp:43
void TrackShutdown(void)
Definition track.cpp:207
static tTrack * theTrack
Definition track.cpp:31
static void freeSeg(tTrackSeg *seg)
Definition track.cpp:185
#define TRK_ATT_CAT
Definition track.h:51
#define TRK_ATT_AUTHOR
Definition track.h:49
#define TRK_ATT_BKGRND
Definition track.h:62
#define TRK_ATT_WIDTH
Definition track.h:45
#define TRK_SECT_GRAPH
Definition track.h:59
#define TRK_LST_ENV
Definition track.h:68
struct trackSurface tTrackSurface
Surface.
#define TRK_SECT_MAIN
Definition track.h:150
#define TRK_ATT_BGCLR_B
Definition track.h:67
#define TRK_ATT_VERSION
Definition track.h:48
#define TRK_ATT_BGCLR_G
Definition track.h:66
struct RoadCam tRoadCam
road camera
#define TRK_ATT_BGCLR_R
Definition track.h:65
#define TRK_ATT_HEIGHT
Definition track.h:207
#define TRK_ATT_NAME
Definition track.h:47
#define TRK_ATT_VSPACE
Definition track.h:87
#define TRK_ATT_ENVNAME
Definition track.h:69
#define TRK_ATT_HSPACE
Definition track.h:86
#define TRK_SECT_HDR
Definition track.h:43
#define TRK_ATT_BGTYPE
Definition track.h:64
#define TRK_SECT_TURNMARKS
Definition track.h:85