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