comparison field.cc @ 99:c534f339ee8b

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