-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcktrack.cpp
193 lines (168 loc) · 3.02 KB
/
cktrack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "cktrack.h"
cktrack::cktrack(void) {}
cktrack::~cktrack(void) {}
static int TBPP=2;
int cktrack::load_config(void)
{
FILE *fp=NULL;
char data[32];
int i=-1;
int x, y, z;
if((fp=fopen("config/config", "r"))==NULL)
{
fprintf(stderr, "could not open config file config/config\n");
return -1;
}
while(fgets(data, sizeof(data), fp))
{
char *cp=NULL;
char *dp=NULL;
if(!(cp=strchr(data, ':'))) continue;
if((dp=strrchr(data, '\n'))) *dp=0;
if(data[0]=='n')
{
char tmp[64]="";
i++;
snprintf(tmp, sizeof(tmp), "graphics/track/%s", cp+1);
tiles[i].image=CL_PNGProvider::create(tmp, NULL);
}
else if(data[0]=='s')
{
tiles[i].symbol=*(cp+1);
}
}
fclose(fp);
return 0;
}
int cktrack::load_track(const char *path)
{
FILE *fp=NULL;
char data[128]="";
int x=0;
int y=0;
int i=0;
CL_Canvas *tmpCan=new CL_Canvas(TRACK_SIZE_X*64, TRACK_SIZE_Y*64, 1,
CL_Color::get_red_mask(RGB565),
CL_Color::get_green_mask(RGB565),
CL_Color::get_blue_mask(RGB565),
CL_Color::get_alpha_mask(RGB565), true, 0);
if((fp=fopen(path, "r"))==NULL)
{
fprintf(stderr, "could not open config file %s\n", path);
delete tmpCan;
return -1;
}
set_laps(6);
while(fgets(data, sizeof(data), fp))
{
char *cp=data;
if(strncmp(data, "laps=", strlen("laps="))==0)
{
cp+=strlen("laps=");
set_laps(atoi(cp));
continue;
}
else if(strncmp(data, "pos", strlen("pos"))==0)
{
if(i>29) continue;
cp=strchr(data, '=');
spos[i].x=atoi(cp+1);
cp=strchr(data, ' ');
spos[i].y=atoi(cp+1);
spos[i].ang=atoi(strchr(cp+1, ' '));
i++;
continue;
}
while(cp && *cp!='\n')
{
int n=0;
while(n<TILENO)
{
if(*cp==tiles[n].symbol)
{
tiles[n].image->put_target(x, y, 0, tmpCan);
track_grid[x/BLK][y/BLK]=*cp;
break;
}
n++;
if(n==TILENO)
{
fprintf(stderr, "tile symbol '%c' not found\n", *cp);
delete tmpCan;
return -1;
}
}
x+=BLK;
cp++;
}
x=0;
y+=BLK;
}
fclose(fp);
track_data=(unsigned char *)tmpCan->get_data();
TBPP=tmpCan->get_bytes_per_pixel();
image=CL_Surface::create(tmpCan);
return 0;
}
CL_Surface *cktrack::get_surface(void)
{
return image;
}
unsigned char *cktrack::get_data(void)
{
return track_data;
}
char cktrack::get_track_symb(int x, int y)
{
return track_grid[x][y];
}
void cktrack::locate_next_checkpoint(int *x, int *y, char cursymbol)
{
int i;
int j;
int first=0;
for(j=0; j<TRACK_SIZE_Y; j++)
{
for(i=0; i<TRACK_SIZE_X; i++)
{
if(track_grid[i][j]==cursymbol)
{
if(!first) first++;
else
{
*x=i*64+ADJ;
*y=j*64+ADJ;
return;
}
}
}
}
}
void cktrack::set_laps(int val)
{
laps=val;
}
int cktrack::get_laps(void)
{
return laps;
}
int cktrack::get_next_position(void)
{
return next_position;
}
void cktrack::set_next_position(int val)
{
next_position=val;
}
int cktrack::get_sposx(int val)
{
return BLK*spos[val].x+ADJ;
}
int cktrack::get_sposy(int val)
{
return BLK*spos[val].y+ADJ;
}
int cktrack::get_sposang(int val)
{
return spos[val].ang;
}