0
|
1 /*
|
18
|
2 * $Id$
|
0
|
3 */
|
|
4 #include <stdio.h>
|
|
5 #include <stdlib.h>
|
|
6 #include "libps2.h"
|
|
7 #include "ps2util.h"
|
|
8 #include "mytype.h"
|
|
9 #include "field.h"
|
|
10 #include "car.h"
|
|
11 #include "quotanion.h"
|
|
12
|
|
13 #define DEFAULT_SET (EFFECT_TEXTURE_USE | EFFECT_ALPHABLEND_UNUSE | EFFECT_SHADING_FLAT)
|
|
14 #define BUFSIZE 256
|
|
15
|
|
16 /* --- main.c --- */
|
|
17 extern FILE *main_fp;
|
|
18
|
|
19 static int field_num_max = 0;
|
|
20 static int field_num = 1;
|
|
21
|
|
22 static FieldPtr field_list;
|
|
23 static FieldPtr field_actual;
|
|
24
|
|
25 static FieldPtr
|
|
26 field_create(char *fieldImg, char *colImg, TEXTURE* tex)
|
|
27 {
|
|
28 FieldPtr self;
|
|
29 OBJECT *obj;
|
|
30 PolygonInfo *xml;
|
|
31 void *free_addr;
|
|
32
|
|
33 FVECTOR location = {0, 0, 0, 1};
|
|
34 FVECTOR direction = {0, 0, 0, 1};
|
|
35
|
|
36 obj = ps2util_obj_Create_fromXML(fieldImg);
|
|
37 memcpy(obj->xyz, location, sizeof(FVECTOR));
|
|
38 memcpy(obj->angle, direction, sizeof(FVECTOR));
|
|
39 ps2util_obj_Renew_transMatrix(obj);
|
|
40 ps2util_obj_Set_effect(obj, DEFAULT_SET);
|
|
41 ps2util_obj_Set_texture(obj, tex);
|
|
42
|
88
|
43 xml = xml_read_file(colImg); //ここでコースの走れる場所(当たり判定)
|
0
|
44
|
|
45 if (malloc_align16(&free_addr, &self, sizeof(Field)) == -1) {
|
|
46 fprintf(stderr, "field.c: malloc_align16 error\n");
|
|
47 fflush(stderr);
|
|
48 exit(EXIT_FAILURE);
|
|
49 }
|
|
50 self->object = obj;
|
|
51 self->next = NULL;
|
|
52 self->prev = NULL;
|
|
53 self->free_addr = free_addr;
|
|
54 #ifdef DEBUG
|
33
|
55 fprintf(main_fp, "malloc fieldNode addr = %x(free), %x(obj)\n", (int)free_addr, (int)self);
|
0
|
56 #endif
|
|
57 col_init(&self->colface, xml);
|
|
58
|
34
|
59 #ifdef DEBUG
|
35
|
60 fprintf(main_fp, " facenum = %d\n", self->colface.facenum);
|
37
|
61 fprintf(main_fp, " facenum = %x(free), ", (int)self->colface.free_addr);
|
36
|
62 fprintf(main_fp, "facenum = %x(obj)\n", (int)self->colface.colface);
|
34
|
63 #endif
|
|
64
|
0
|
65 xml_free(xml);
|
|
66
|
|
67 return self;
|
|
68 }
|
|
69
|
|
70 static void
|
32
|
71 field_new_readCSV(FILE *fp, int id)
|
0
|
72 {
|
|
73 FieldPtr fieldHead = NULL;
|
|
74 FieldPtr newField = NULL;
|
|
75 FieldPtr prevField = NULL;
|
|
76 char buff[BUFSIZE], *bufp;
|
|
77 char fieldImg[BUFSIZE], colImg[BUFSIZE], texname[BUFSIZE];
|
|
78 TEXTURE *tex = NULL;
|
|
79 int course_id;
|
|
80 Bool flag = FALSE;
|
|
81
|
|
82 while ((bufp = fgets(buff, BUFSIZE, fp)) != NULL) {
|
|
83 bufp++;
|
|
84
|
|
85 switch (buff[0]) {
|
88
|
86 case 'n': // コースIDの読み取り
|
0
|
87 if (!flag) {
|
|
88 sscanf(bufp, " %d\n", &course_id);
|
32
|
89 if (id == course_id) {
|
0
|
90 flag = TRUE;
|
|
91 }
|
|
92 } else {
|
|
93 goto FINISH_LOAD;
|
|
94 }
|
|
95 break;
|
88
|
96 case 't': // テクスチャのロード
|
0
|
97 if (flag) {
|
|
98 sscanf(bufp, " %s\n", texname);
|
|
99 tex = read_png_file(texname);
|
|
100 ps2util_tex_Set(tex);
|
|
101 }
|
|
102 break;
|
88
|
103 case 'c': // コースのロード
|
0
|
104 if (flag) {
|
|
105 sscanf(bufp, " %s %s\n", fieldImg, colImg);
|
|
106 newField = field_create(fieldImg, colImg, tex);
|
|
107 if (!fieldHead) {
|
|
108 fieldHead = prevField = newField;
|
|
109 } else {
|
|
110 prevField->next = newField;
|
|
111 newField->prev = prevField;
|
|
112 prevField = newField;
|
|
113 }
|
|
114 newField->init = fieldHead;
|
|
115 field_num_max++;
|
|
116 }
|
|
117 break;
|
|
118 default:
|
|
119 break;
|
|
120 }
|
|
121 }
|
|
122
|
|
123 FINISH_LOAD:
|
88
|
124 // 先頭と最後尾のリンクを張る
|
0
|
125 newField->next = newField->init;
|
|
126 newField->init->prev = newField;
|
|
127
|
|
128 field_list = field_actual = fieldHead;
|
|
129 }
|
|
130
|
|
131
|
|
132 /*---------------------/
|
|
133 FieldPtr
|
|
134 field_init:
|
88
|
135 指定したファイルからコースを生成する。
|
|
136 戻り値
|
|
137 コース先頭のポインタ
|
0
|
138 /---------------------*/
|
|
139 void
|
32
|
140 field_init(int id)
|
0
|
141 {
|
|
142 FILE *fp;
|
|
143 char *filename = "course/course.dat";
|
|
144
|
|
145 if (!(fp = fopen(filename, "r"))) {
|
|
146 fprintf(main_fp, "error read file %s\n", filename);
|
|
147 exit(EXIT_FAILURE);
|
|
148 }
|
32
|
149 field_new_readCSV(fp, id);
|
0
|
150 fclose(fp);
|
|
151 }
|
|
152
|
|
153
|
|
154 static void
|
|
155 field_draw(FieldPtr p)
|
|
156 {
|
|
157 ps2_vu0_unit_matrix(p->object->transfer);
|
|
158
|
|
159 p->object->transfer[3][0] = p->location[0];
|
|
160 p->object->transfer[3][1] = p->location[1];
|
|
161 p->object->transfer[3][2] = p->location[2];
|
|
162 p->object->transfer[3][3] = p->location[3];
|
|
163 ps2util_obj_Draw(p->object);
|
|
164 }
|
|
165
|
|
166
|
|
167 void
|
|
168 field_update(CarPtr car)
|
|
169 {
|
|
170 FieldPtr p;
|
|
171
|
|
172 p = field_actual;
|
|
173 // current
|
|
174 p->location[0] = -car->location[0];
|
|
175 p->location[1] = -car->location[1];
|
|
176 p->location[2] = -car->location[2];
|
|
177 p->location[3] = 1;
|
|
178 field_draw(p);
|
|
179
|
|
180 // next
|
|
181 p = field_actual->next;
|
|
182 if (p) {
|
|
183 p->location[0] = -car->location[0];
|
|
184 p->location[1] = -car->location[1];
|
|
185 p->location[2] = -car->location[2];
|
|
186 p->location[3] = 1;
|
|
187 field_draw(p);
|
|
188 }
|
|
189
|
|
190 // prev
|
|
191 p = field_actual->prev;
|
|
192 if (p) {
|
|
193 p->location[0] = -car->location[0];
|
|
194 p->location[1] = -car->location[1];
|
|
195 p->location[2] = -car->location[2];
|
|
196 p->location[3] = 1;
|
|
197 field_draw(p);
|
|
198 }
|
|
199 }
|
|
200
|
|
201 void
|
|
202 field_destroy()
|
|
203 {
|
|
204 FieldPtr p = field_list;
|
|
205 FieldPtr d;
|
|
206 TEXTURE *t;
|
|
207
|
|
208 p->prev->next = NULL;
|
|
209
|
|
210 t = p->object->surfaces->texture;
|
|
211 ps2util_tex_Exclude(t);
|
|
212 free_texture(t);
|
|
213
|
|
214 while (p) {
|
|
215 d = p;
|
|
216 p = p->next;
|
|
217 ps2util_obj_Free(d->object);
|
38
|
218 free(d->colface.free_addr);
|
0
|
219 free(d->free_addr);
|
|
220 #ifdef DEBUG
|
|
221 fprintf(main_fp, "free fieldNode addr = %x\n", (int)d->free_addr);
|
|
222 #endif
|
|
223 }
|
|
224
|
|
225 field_num_max = 0;
|
|
226 field_num = 1;
|
|
227 }
|
|
228
|
|
229 /*
|
88
|
230 * 周回記録。1周する毎に1を返す。
|
0
|
231 */
|
|
232 int
|
|
233 field_rap_increment(int n)
|
|
234 {
|
|
235 field_num += n;
|
|
236 if (field_num > field_num_max) {
|
|
237 field_num = 1;
|
|
238 return 1;
|
|
239 } else {
|
|
240 return 0;
|
|
241 }
|
|
242 }
|
|
243
|
|
244 void
|
|
245 field_set_actual(FieldPtr p)
|
|
246 {
|
|
247 field_actual = p;
|
|
248 }
|
|
249
|
|
250 FieldPtr
|
|
251 field_get_actual()
|
|
252 {
|
|
253 return field_actual;
|
|
254 }
|