283
|
1 #include <iostream>
|
|
2 #include <SDL.h>
|
|
3 #include <SDL_opengl.h>
|
|
4 #include <SDL_image.h>
|
|
5 #include <libxml/parser.h>
|
|
6 #include "SceneGraph.h"
|
|
7 #include "xml.h"
|
|
8 #include "sys.h"
|
|
9 #include "TextureHash.h"
|
|
10 #include "texture.h"
|
|
11 #include "TaskManager.h"
|
|
12
|
|
13 using namespace std;
|
|
14
|
|
15 SceneGraphPtr scene_graph = NULL;
|
|
16 SceneGraphPtr scene_graph_viewer = NULL;
|
|
17
|
|
18 static TextureHash texture_hash;
|
|
19 struct texture_list list[TABLE_SIZE];
|
|
20
|
|
21 // TextureHash.cpp
|
|
22 extern int id_count;
|
|
23
|
|
24 extern int decode(char *cont, FILE *outfile);
|
|
25
|
|
26 static void
|
|
27 no_move(SceneGraphPtr self, int screen_w, int screen_h) {}
|
|
28
|
|
29 static void
|
|
30 no_collision(SceneGraphPtr self, int screen_w, int screen_h,
|
|
31 SceneGraphPtr tree) {}
|
|
32
|
|
33 /**
|
|
34 * 事前に計算したテクスチャの最大縮小率 scale まで、
|
|
35 * テクスチャを 1/2 縮小していく。
|
|
36 * このとき、テクスチャは TEXTURE_SPLIT_PIXELx2 のブロック (Tile) で分割し、
|
|
37 * これらを連続したメモリ領域に格納していく。
|
|
38 * 以下の (1), (2), (3) を Tapestry と呼ぶ
|
|
39 *
|
|
40 * 例 scale = 4 の場合
|
|
41 *
|
|
42 * Tapestry(1) 1/1
|
|
43 * +---+---+---+---+
|
|
44 * | 0 | 1 | 2 | 3 |
|
|
45 * +---+---+---+---+
|
|
46 * | 4 | 5 | 6 | 7 | (2) 1/2
|
|
47 * +---+---+---+---+ +---+---+
|
|
48 * | 8 | 9 | 10| 11| | 16| 17| (3) 1/4
|
|
49 * +---+---+---+---+ +---+---+ +---+
|
|
50 * | 12| 13| 14| 15| | 18| 19| | 20|
|
|
51 * +---+---+---+---+ +---+---+ +---|
|
|
52 *
|
|
53 * (1) (2) (3)
|
|
54 * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
|
|
55 * | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | * | * | * | 14| 15| 16| 17| 18| 19| 20|
|
|
56 * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
|
|
57 *
|
|
58 * @param[in] tex_w Width of orignal texture
|
|
59 * @param[in] tex_h Height of orignal texture
|
|
60 * @param[in] tex_src Original texture
|
|
61 * @param[in] all_pixel_num Tapestry の合計 pixel 数
|
|
62 * @param[in] scale テクスチャの最大縮小率 (= 2^n)
|
|
63 * @return (1) のアドレス
|
|
64 */
|
|
65 static uint32*
|
|
66 makeTapestry(int tex_w, int tex_h, uint32 *tex_src,
|
|
67 int all_pixel_num, int scale_cnt)
|
|
68 {
|
|
69 uint32 *tex_dest;
|
|
70
|
|
71 int t = 0;
|
|
72 int diff = TEXTURE_SPLIT_PIXEL;
|
|
73 int p_diff = 1;
|
|
74
|
|
75 tex_dest = (uint32*)manager->allocate(sizeof(int)*all_pixel_num);
|
|
76
|
|
77 while (scale_cnt) {
|
|
78 for (int y = 0; y < tex_h; y += diff) {
|
|
79 for (int x = 0; x < tex_w; x += diff) {
|
|
80 for (int j = 0; j < diff; j += p_diff) {
|
|
81 for (int i = 0; i < diff; i += p_diff) {
|
|
82 tex_dest[t++]
|
|
83 = tex_src[(x+i) + tex_w*(y+j)];
|
|
84 }
|
|
85 }
|
|
86 }
|
|
87 }
|
|
88
|
|
89 diff <<= 1;
|
|
90 p_diff <<= 1;
|
|
91 scale_cnt >>= 1;
|
|
92 }
|
|
93
|
|
94 return tex_dest;
|
|
95 }
|
|
96
|
|
97
|
|
98 /**
|
|
99 * 何の情報も持ってない SceneGraph の生成
|
|
100 * 今のところ、とりあえず木構造の繋がりに使うぐらい
|
|
101 */
|
|
102 SceneGraph::SceneGraph(void)
|
|
103 {
|
|
104 init();
|
|
105 finalize = &SceneGraph::finalize_copy;
|
|
106
|
|
107 this->name = "NULLPO";
|
|
108 }
|
|
109
|
|
110 /**
|
|
111 * orig のコピーとして SceneGraph を生成する
|
|
112 */
|
|
113 SceneGraph::SceneGraph(SceneGraphPtr orig)
|
|
114 {
|
|
115 init();
|
|
116 memcpy(this, orig, sizeof(SceneGraph));
|
|
117
|
|
118 // コピーしない
|
|
119 //flag_remove = 0;
|
|
120 //flag_drawable = 1;
|
|
121 next = NULL;
|
|
122 prev = NULL;
|
|
123 last = NULL;
|
|
124
|
|
125 parent = NULL;
|
|
126 brother = NULL;
|
|
127 children = NULL;
|
|
128 lastChild = NULL;
|
|
129
|
|
130 finalize = &SceneGraph::finalize_copy;
|
|
131
|
|
132
|
|
133 frame = 0;
|
|
134 }
|
|
135
|
|
136
|
|
137 /* construct polygon from xmlNode. */
|
|
138 SceneGraph::SceneGraph(xmlNodePtr surface)
|
|
139 {
|
|
140 init();
|
|
141
|
|
142 size = atoi((char *)xmlGetProp(surface,(xmlChar *)"size"));
|
|
143 name = (char *)xmlGetProp(surface,(xmlChar *)"name");
|
|
144 parent_name = (char *)xmlGetProp(surface,(xmlChar *)"parent");
|
|
145
|
|
146 //data = new float[size*3*3];
|
|
147 coord_xyz = (float*)manager->allocate(sizeof(float)*size*3);
|
|
148 coord_tex = (float*)manager->allocate(sizeof(float)*size*3);
|
|
149 normal = (float*)manager->allocate(sizeof(float)*size*3);
|
|
150
|
|
151 get_data(surface->children);
|
|
152
|
|
153 finalize = &SceneGraph::finalize_original;
|
|
154 }
|
|
155
|
|
156 void
|
|
157 SceneGraph::init(void)
|
|
158 {
|
|
159 next = NULL;
|
|
160 prev = NULL;
|
|
161 last = NULL;
|
|
162
|
|
163 parent = NULL;
|
|
164 brother = NULL;
|
|
165 children = NULL;
|
|
166 lastChild = NULL;
|
|
167
|
|
168 stack_xyz[0] = 0.0f;
|
|
169 stack_xyz[2] = 0.0f;
|
|
170 stack_xyz[1] = 0.0f;
|
|
171 stack_angle[0] = 0.0f;
|
|
172 stack_angle[1] = 0.0f;
|
|
173 stack_angle[2] = 0.0f;
|
|
174
|
|
175 size = 0;
|
|
176 //data = NULL;
|
|
177 coord_xyz = NULL;
|
|
178 normal = NULL;
|
|
179 coord_tex = NULL;
|
|
180
|
|
181 texture_id = -1;
|
|
182 move = no_move;
|
|
183 collision = no_collision;
|
|
184
|
|
185 flag_remove = 0;
|
|
186 flag_drawable = 1;
|
|
187 sgid = -1;
|
|
188
|
|
189 frame = 0;
|
|
190 }
|
|
191
|
|
192 SceneGraph::~SceneGraph(void)
|
|
193 {
|
|
194 (this->*finalize)();
|
|
195 }
|
|
196
|
|
197 /**
|
|
198 * xml ファイルから生成されたオリジナル SceneGraph なので
|
|
199 * polygon data を削除
|
|
200 */
|
|
201 void
|
|
202 SceneGraph::finalize_original(void)
|
|
203 {
|
|
204 //delete [] data;
|
|
205 free(coord_xyz);
|
|
206 free(coord_tex);
|
|
207 free(normal);
|
|
208 }
|
|
209
|
|
210 /**
|
|
211 * SceneGraph ID から生成された、コピー SceneGraph なので
|
|
212 * polygon data は削除しない。オリジナルの方で削除する。
|
|
213 */
|
|
214 void
|
|
215 SceneGraph::finalize_copy(void)
|
|
216 {
|
|
217 }
|
|
218
|
|
219 /* XMLファイルからポリゴンを作成 */
|
|
220 void
|
|
221 SceneGraph::createFromXMLfile(const char *xmlfile)
|
|
222 {
|
|
223 xmlDocPtr doc;
|
|
224 xmlNodePtr cur;
|
|
225 SceneGraphPtr root = NULL, tmp = NULL, parent;
|
|
226
|
|
227 /* パース DOM生成 */
|
|
228 doc = xmlParseFile(xmlfile);
|
|
229 cur = xmlDocGetRootElement(doc);
|
|
230
|
|
231 /* ?? */
|
|
232 xmlStrcmp(cur->name,(xmlChar*)"OBJECT-3D");
|
|
233
|
|
234 /* XMLのノードを一つずつ解析 */
|
|
235 for (cur=cur->children; cur; cur=cur->next) {
|
|
236 /* 扱うのはsurfaceオンリー */
|
|
237 //if (xmlStrcmp(cur->name,(xmlChar*)"surface") != 0) {
|
|
238 // continue;
|
|
239 //}
|
|
240 if (!xmlStrcmp(cur->name,(xmlChar*)"surface")) {
|
|
241 /* ポリゴン(SceneGraph)生成 */
|
|
242 tmp = new SceneGraph(cur);
|
|
243 if ( tmp->parent_name==NULL || 0==strcmp(tmp->parent_name, "NULL")) {
|
|
244 /* このsurfaceがroot */
|
|
245 root = tmp;
|
|
246 scene_graph = tmp;
|
|
247 } else {
|
|
248 /* 親はこのsurfaceより前に定義されているものとする (していい?) */
|
|
249 // ここで parent_name を用いるのは間違っていて、
|
|
250 // *cur->properties->children から探すべきらしい kono
|
|
251 parent = root->searchSceneGraph(tmp->parent_name);
|
|
252 if (parent==NULL) {
|
|
253 fprintf(stderr, "[%s] No such parent %s\n",
|
|
254 tmp->name, tmp->parent_name);
|
|
255 root->addChild(tmp);
|
|
256 } else {
|
|
257 parent->addChild(tmp);
|
|
258 }
|
|
259
|
|
260 scene_graph->add_next(tmp);
|
|
261 }
|
|
262 }else if (!xmlStrcmp(cur->name,(xmlChar*)"image")) {
|
|
263
|
|
264 char *cont;
|
|
265 char image_name[20] = "/tmp/image_XXXXXX";
|
|
266 char *filename = (char *)xmlGetProp(cur, (xmlChar *)"name");
|
|
267 int fd = mkstemp(image_name);
|
|
268 FILE *outfile = fdopen(fd, "wb");
|
|
269 if(NULL == outfile)
|
|
270 {
|
|
271 cout << "error open file\n";
|
|
272 }
|
|
273 cont = (char *)xmlNodeGetContent(cur);
|
|
274 decode(cont, outfile);
|
|
275 fclose(outfile);
|
|
276
|
|
277 int tex_id = texture_hash.hash_regist(filename);
|
|
278 if (tex_id < 0)
|
|
279 {
|
|
280
|
|
281 texture_image = IMG_Load(image_name);
|
|
282
|
|
283 /**
|
|
284 * image を 32bit(RGBA) に変換する
|
|
285 */
|
|
286
|
|
287 SDL_Surface *tmpImage
|
|
288 = SDL_CreateRGBSurface(SDL_HWSURFACE, texture_image->w,
|
|
289 texture_image->h, 32, redMask,
|
|
290 greenMask, blueMask, alphaMask);
|
|
291 SDL_Surface *converted;
|
|
292 converted = SDL_ConvertSurface(texture_image, tmpImage->format,
|
|
293 SDL_HWSURFACE);
|
|
294 if (converted != NULL) {
|
|
295 SDL_FreeSurface(texture_image);
|
|
296 texture_image = converted;
|
|
297 }
|
|
298
|
|
299 uint32 *tapestry;
|
|
300 int scale = 1;
|
|
301 int tex_w = texture_image->w;
|
|
302 int tex_h = texture_image->h;
|
|
303 int all_pixel_num = 0;
|
|
304
|
|
305 /**
|
|
306 * テクスチャの w or h が 8 pixel で分割できる間、
|
|
307 * 1/2 の縮小画像を作る。
|
|
308 * ここでは、最大の scale (1/scale) を見つける
|
|
309 *
|
|
310 * (ex)
|
|
311 * (128,128) => 64,64 : 32,32: 16,16 : 8,8
|
|
312 * scale = 16
|
|
313 * (128, 64) => 64,32 : 32,16: 16,8
|
|
314 * scale = 8
|
|
315 */
|
|
316
|
|
317 while (tex_w % TEXTURE_SPLIT_PIXEL == 0 &&
|
|
318 tex_h % TEXTURE_SPLIT_PIXEL == 0) {
|
|
319 all_pixel_num += tex_w*tex_h;
|
|
320 tex_w >>= 1; /* tex_w /= 2 */
|
|
321
|
|
322 tex_h >>= 1;
|
|
323 scale <<= 1; /* scale *= 2 */
|
|
324
|
|
325 }
|
|
326
|
|
327 scale >>= 1;
|
|
328
|
|
329 tapestry = makeTapestry(texture_image->w, texture_image->h,
|
|
330 (uint32*)texture_image->pixels,
|
|
331 all_pixel_num,
|
|
332 scale);
|
|
333
|
|
334 list[id_count-1].t_w = texture_image->w;
|
|
335 list[id_count-1].t_h = texture_image->h;
|
|
336 list[id_count-1].pixels_orig = (Uint32*)texture_image->pixels;
|
|
337 list[id_count-1].pixels = tapestry;
|
|
338 list[id_count-1].scale_max = scale;
|
|
339
|
|
340 tmp->texture_id = id_count-1;
|
|
341 tmp->texture_info.t_w = texture_image->w;
|
|
342 tmp->texture_info.t_h = texture_image->h;
|
|
343 tmp->texture_info.pixels_orig = (Uint32*)texture_image->pixels;
|
|
344 tmp->texture_info.pixels = tapestry;
|
|
345 tmp->texture_info.scale_max = scale;
|
|
346
|
|
347 if (unlink(image_name))
|
|
348 {
|
|
349 cout << "unlink error\n";
|
|
350 }
|
|
351
|
|
352 } else {
|
|
353 /**
|
|
354 * 以前に Load されている Texture を共用
|
|
355 */
|
|
356
|
|
357 tmp->texture_id = tex_id;
|
|
358
|
|
359 // こんなことすると list[] のいみあるのかなーと
|
|
360 // 微妙に思う、自分で書き換えた感想 by gongo
|
|
361
|
|
362 tmp->texture_info.t_w = list[tex_id].t_w;
|
|
363 tmp->texture_info.t_h = list[tex_id].t_h;;
|
|
364 tmp->texture_info.pixels_orig = list[tex_id].pixels_orig;
|
|
365 tmp->texture_info.pixels = list[tex_id].pixels;
|
|
366 tmp->texture_info.scale_max = list[tex_id].scale_max;
|
|
367
|
|
368 }
|
|
369 }else {
|
|
370 continue;
|
|
371 }
|
|
372 }
|
|
373
|
|
374 xmlFreeDoc(doc);
|
|
375
|
|
376 //return root;
|
|
377 scene_graph_viewer = root;
|
|
378 }
|
|
379
|
|
380
|
|
381 /**
|
|
382 * add Children
|
|
383 * 親の登録と、brother のリストへ加える
|
|
384 *
|
|
385 * @param child new child
|
|
386 */
|
|
387 SceneGraphPtr
|
|
388 SceneGraph::addChild(SceneGraphPtr child)
|
|
389 {
|
|
390 /* childrenのリストの最後に加える (brother として)*/
|
|
391 if (this->lastChild != NULL) {
|
|
392 SceneGraphPtr last = this->lastChild;
|
|
393 last->brother = child;
|
|
394 }
|
|
395
|
|
396 this->lastChild = child;
|
|
397
|
|
398 if (this->children == NULL) {
|
|
399 this->children = child;
|
|
400 }
|
|
401
|
|
402 child->parent = this;
|
|
403
|
|
404 return child;
|
|
405 }
|
|
406
|
|
407
|
|
408 /**
|
|
409 * add Brother
|
|
410 * addChild() でも brother の操作をしないといけないので、そっちに回す
|
|
411 *
|
|
412 * @param bro new Brother
|
|
413 */
|
|
414 SceneGraphPtr
|
|
415 SceneGraph::addBrother(SceneGraphPtr bro)
|
|
416 {
|
|
417 if (this->parent) {
|
|
418 parent->addChild(bro);
|
|
419 } else {
|
|
420 fprintf(stderr, "error : SceneGraph::%s : %s doesn't have parent\n",
|
|
421 __FUNCTION__, this->name);
|
|
422 }
|
|
423
|
|
424 return bro;
|
|
425 }
|
|
426
|
|
427 /* thisの子や子孫にnameのものが存在すればそいつを返す なければNULL. */
|
|
428 SceneGraphPtr
|
|
429 SceneGraph::searchSceneGraph(const char *name)
|
|
430 {
|
|
431 SceneGraphPtr tmp;
|
|
432 SceneGraphPtr result;
|
|
433
|
|
434 /* 本人か */
|
|
435 if( 0==strcmp(this->name, name) ) return this;
|
|
436
|
|
437 /* 子供から再帰的に探す */
|
|
438 for(tmp = this->children; tmp; tmp = tmp->next) {
|
|
439 if ((result=tmp->searchSceneGraph(name)) != NULL)
|
|
440 return result;
|
|
441 }
|
|
442
|
|
443 /* 無かったら NULL. */
|
|
444 return NULL;
|
|
445 }
|
|
446
|
|
447 void
|
|
448 SceneGraph::tree_check(void)
|
|
449 {
|
|
450 SceneGraphPtr t = this;
|
|
451
|
|
452 while(t)
|
|
453 {
|
|
454 cout << "my_name : " << t->name << endl;
|
|
455 if(t->children != NULL)
|
|
456 {
|
|
457 cout << "--move children : " << t->children->name << endl;
|
|
458 t = t->children;
|
|
459 }
|
|
460 else if(t->brother != NULL)
|
|
461 {
|
|
462 cout << "--move brother : " << t->brother->name << endl;
|
|
463 t = t->brother;
|
|
464 }
|
|
465 else
|
|
466 {
|
|
467 while(t)
|
|
468 {
|
|
469 if(t->brother != NULL)
|
|
470 {
|
|
471 cout << "--move brother : " << t->brother->name << endl;
|
|
472 t = t->brother;
|
|
473 break;
|
|
474 }
|
|
475 else
|
|
476 {
|
|
477 if(t->parent)
|
|
478 {
|
|
479 cout << "--move parent : " << t->parent->name << endl;
|
|
480 }
|
|
481 t = t->parent;
|
|
482 }
|
|
483 }
|
|
484 }
|
|
485 }
|
|
486 }
|
|
487
|
|
488
|
|
489 void
|
|
490 SceneGraph::print_member(void)
|
|
491 {
|
|
492 cout << "size = " << size << endl;
|
|
493 cout << "name = " << name << endl;
|
|
494 cout << "parent_name = " << parent_name << endl;
|
|
495
|
|
496 if (parent != NULL) {
|
|
497 cout << "parent->name = " << parent->name << endl;
|
|
498 }
|
|
499
|
|
500 if (children != NULL) {
|
|
501 cout << "children->name = " << children->name << endl;
|
|
502 }
|
|
503 }
|
|
504
|
|
505
|
|
506 /*
|
|
507 * surface nodeからポリゴンの情報を読み出す 再帰しない
|
|
508 */
|
|
509 void
|
|
510 SceneGraph::get_data(xmlNodePtr cur)
|
|
511 {
|
|
512 char *cont;
|
|
513 //char *image_name;
|
|
514
|
|
515 for(;cur;cur=cur->next)
|
|
516 {
|
|
517 if(!xmlStrcmp(cur->name,(xmlChar*)"coordinate"))
|
|
518 {
|
|
519 cont = (char *)xmlNodeGetContent(cur);
|
|
520 pickup_coordinate(cont);
|
|
521 }
|
|
522 else if(!xmlStrcmp(cur->name,(xmlChar*)"normal"))
|
|
523 {
|
|
524 cont = (char *)xmlNodeGetContent(cur);
|
|
525 pickup_normal(cont);
|
|
526 }
|
|
527 else if(!xmlStrcmp(cur->name,(xmlChar*)"model"))
|
|
528 {
|
|
529 cont = (char *)xmlNodeGetContent(cur);
|
|
530 pickup_model(cont);
|
|
531 }
|
|
532 else if(!xmlStrcmp(cur->name,(xmlChar*)"texture"))
|
|
533 {
|
|
534 cont = (char *)xmlNodeGetContent(cur);
|
|
535 pickup_texture(cont);
|
|
536 }
|
|
537 else if(!xmlStrcmp(cur->name,(xmlChar*)"imageflag"))
|
|
538 {
|
|
539 char *filename = (char *)xmlGetProp(cur, (xmlChar *)"name");
|
|
540 texture_hash.hash_regist(filename);
|
|
541 }
|
|
542 else if(!xmlStrcmp(cur->name,(xmlChar*)"image"))
|
|
543 {
|
|
544 char image_name[20] = "/tmp/image_XXXXXX";
|
|
545 char *filename = (char *)xmlGetProp(cur, (xmlChar *)"name");
|
|
546 int fd = mkstemp(image_name);
|
|
547 FILE *outfile = fdopen(fd, "wb");
|
|
548 if(NULL == outfile)
|
|
549 {
|
|
550 cout << "error open file\n";
|
|
551 }
|
|
552 cont = (char *)xmlNodeGetContent(cur);
|
|
553 //decode(cont, image_name);
|
|
554 decode(cont, outfile);
|
|
555 fclose(outfile);
|
|
556
|
|
557 /**
|
|
558 * image_name を既に Load していれば何もしない
|
|
559 */
|
|
560 int tex_id = texture_hash.hash_regist(filename);
|
|
561 if (tex_id < 0) {
|
|
562
|
|
563 texture_image = IMG_Load(image_name);
|
|
564
|
|
565 /**
|
|
566 * image を 32bit(RGBA) に変換する
|
|
567 */
|
|
568 SDL_Surface *tmpImage
|
|
569 = SDL_CreateRGBSurface(SDL_HWSURFACE, texture_image->w,
|
|
570 texture_image->h, 32, redMask,
|
|
571 greenMask, blueMask, alphaMask);
|
|
572 SDL_Surface *converted;
|
|
573 converted = SDL_ConvertSurface(texture_image, tmpImage->format,
|
|
574 SDL_HWSURFACE);
|
|
575 if (converted != NULL) {
|
|
576 SDL_FreeSurface(texture_image);
|
|
577 texture_image = converted;
|
|
578 }
|
|
579
|
|
580 uint32 *tapestry;
|
|
581 int scale = 1;
|
|
582 int tex_w = texture_image->w;
|
|
583 int tex_h = texture_image->h;
|
|
584 int all_pixel_num = 0;
|
|
585
|
|
586 /**
|
|
587 * テクスチャの w or h が 8 pixel で分割できる間、
|
|
588 * 1/2 の縮小画像を作る。
|
|
589 * ここでは、最大の scale (1/scale) を見つける
|
|
590 *
|
|
591 * (ex)
|
|
592 * (128,128) => 64,64 : 32,32: 16,16 : 8,8
|
|
593 * scale = 16
|
|
594 * (128, 64) => 64,32 : 32,16: 16,8
|
|
595 * scale = 8
|
|
596 */
|
|
597 while (tex_w % TEXTURE_SPLIT_PIXEL == 0 &&
|
|
598 tex_h % TEXTURE_SPLIT_PIXEL == 0) {
|
|
599 all_pixel_num += tex_w*tex_h;
|
|
600 tex_w >>= 1; /* tex_w /= 2 */
|
|
601 tex_h >>= 1;
|
|
602 scale <<= 1; /* scale *= 2 */
|
|
603 }
|
|
604
|
|
605 scale >>= 1;
|
|
606
|
|
607 tapestry = makeTapestry(texture_image->w, texture_image->h,
|
|
608 (uint32*)texture_image->pixels,
|
|
609 all_pixel_num, scale);
|
|
610
|
|
611 list[id_count-1].t_w = texture_image->w;
|
|
612 list[id_count-1].t_h = texture_image->h;
|
|
613 list[id_count-1].pixels_orig = (Uint32*)texture_image->pixels;
|
|
614 list[id_count-1].pixels = tapestry;
|
|
615 list[id_count-1].scale_max = scale;
|
|
616
|
|
617 texture_id = id_count-1;
|
|
618
|
|
619 if (unlink(image_name))
|
|
620 {
|
|
621 cout << "unlink error\n";
|
|
622 }
|
|
623 } else {
|
|
624 /**
|
|
625 * 以前に Load されている Texture を共用
|
|
626 */
|
|
627 texture_id = tex_id;
|
|
628 }
|
|
629
|
|
630 // こんなことすると list[] のいみあるのかなーと
|
|
631 // 微妙に思う、自分で書き換えた感想 by gongo
|
|
632 texture_info.t_w = list[texture_id].t_w;
|
|
633 texture_info.t_h = list[texture_id].t_h;;
|
|
634 texture_info.pixels_orig = list[texture_id].pixels_orig;
|
|
635 texture_info.pixels = list[texture_id].pixels;
|
|
636 texture_info.scale_max = list[texture_id].scale_max;
|
|
637 }
|
|
638 }
|
|
639 }
|
|
640
|
|
641
|
|
642 void
|
|
643 SceneGraph::delete_data(void)
|
|
644 {
|
|
645 SceneGraphPtr n = this->next, m;
|
|
646
|
|
647 //n = this;
|
|
648 //delete [] n->data;
|
|
649
|
|
650 if (next) {
|
|
651 while (n) {
|
|
652 m = n->next;
|
|
653 delete n;
|
|
654 n = m;
|
|
655 }
|
|
656 }
|
|
657 }
|
|
658
|
|
659 void
|
|
660 SceneGraph::move_execute(int w, int h)
|
|
661 {
|
|
662 (*move)(this, w, h);
|
|
663 }
|
|
664
|
|
665 void
|
|
666 SceneGraph::collision_check(int w, int h, SceneGraphPtr tree)
|
|
667 {
|
|
668 (*collision)(this, w, h, tree);
|
|
669 }
|
|
670
|
|
671 void
|
|
672 SceneGraph::all_execute(int screen_w, int screen_h)
|
|
673 {
|
|
674 SceneGraphPtr top = this;
|
|
675 SceneGraphPtr t = top;
|
|
676
|
|
677 while (t) {
|
|
678 t->move_execute(screen_w, screen_h);
|
|
679 t->collision_check(screen_w, screen_h, top);
|
|
680
|
|
681 t->frame++;
|
|
682
|
|
683 if (t->parent != NULL) {
|
|
684 get_matrix(t->matrix, t->angle, t->xyz, t->parent->matrix);
|
|
685 } else {
|
|
686 get_matrix(t->matrix, t->angle, t->xyz, NULL);
|
|
687 }
|
|
688
|
|
689 if (t->children != NULL) {
|
|
690 t = t->children;
|
|
691 } else if (t->brother != NULL) {
|
|
692 t = t->brother;
|
|
693 } else {
|
|
694 while (t) {
|
|
695 if (t->brother != NULL) {
|
|
696 t = t->brother;
|
|
697 break;
|
|
698 } else {
|
|
699 if (t->parent == NULL) {
|
|
700 t = NULL;
|
|
701 break;
|
|
702 } else {
|
|
703 t = t->parent;
|
|
704 }
|
|
705 }
|
|
706 }
|
|
707 }
|
|
708 }
|
|
709 }
|
|
710
|
|
711 void
|
|
712 SceneGraph::set_move_collision(SceneGraphPtr node, move_func new_move,
|
|
713 collision_func new_collision)
|
|
714 {
|
|
715 node->move = new_move;
|
|
716 node->collision = new_collision;
|
|
717 }
|
|
718
|
|
719 void
|
|
720 SceneGraph::set_move_collision(move_func new_move,
|
|
721 collision_func new_collision)
|
|
722 {
|
|
723 this->move = new_move;
|
|
724 this->collision = new_collision;
|
|
725 }
|
|
726
|
|
727 void
|
|
728 SceneGraph::add_next(SceneGraphPtr next)
|
|
729 {
|
|
730 /* next のリストの最後に加える */
|
|
731 if (this->next != NULL) {
|
|
732 SceneGraphPtr tmp = this->last;
|
|
733 tmp->next = next;
|
|
734 } else {
|
|
735 this->next = next;
|
|
736 }
|
|
737
|
|
738 this->last = next;
|
|
739 }
|
|
740
|
|
741 /**
|
|
742 * SceneGraph の clone
|
|
743 * @return clone SceneGraph
|
|
744 */
|
|
745 SceneGraphPtr
|
|
746 SceneGraph::clone(void) {
|
|
747 SceneGraphPtr p = new SceneGraph(this);
|
|
748 return p;
|
|
749 }
|
|
750
|
|
751 /**
|
|
752 * SceneGraph の clone
|
|
753 * 予め allocate されてる領域への placement new を行う
|
|
754 *
|
|
755 * @param buf clone 領域
|
|
756 * @return clone SceneGraph
|
|
757 */
|
|
758 SceneGraphPtr
|
|
759 SceneGraph::clone(void *buf) {
|
|
760 SceneGraphPtr p = new(buf) SceneGraph(this);
|
|
761 return p;
|
|
762 }
|
|
763
|
|
764 void
|
|
765 SceneGraph::remove(void)
|
|
766 {
|
|
767 this->flag_remove = 1;
|
|
768 }
|
|
769
|
|
770 /**
|
|
771 * tree から node を削除する
|
|
772 *
|
|
773 * @param tree SceneGraphTree
|
|
774 * @return node削除後の SceneGraphTree
|
|
775 */
|
|
776 SceneGraphPtr
|
|
777 SceneGraph::realRemoveFromTree(SceneGraphPtr tree)
|
|
778 {
|
|
779 SceneGraphPtr node = this;
|
|
780 SceneGraphPtr parent = node->parent;
|
|
781 SceneGraphPtr ret = tree;
|
|
782
|
|
783 if (parent) {
|
|
784 SceneGraphPtr brother = parent->children;
|
|
785 SceneGraphPtr p, p1 = NULL;
|
|
786
|
|
787 p = brother;
|
|
788 if (p) {
|
|
789 if (p == node) {
|
|
790 parent->children = NULL;
|
|
791 parent->lastChild = NULL;
|
|
792 } else {
|
|
793 p1 = p->brother;
|
|
794
|
|
795 while (p1 && p1 != node) {
|
|
796 p1 = p1->brother;
|
|
797 p = p->brother;
|
|
798 }
|
|
799
|
|
800 if (p1) {
|
|
801 p->brother = p1->brother;
|
|
802
|
|
803 // node が最後尾なら、lastChild を変更
|
|
804 if (parent->lastChild == p1) {
|
|
805 parent->lastChild = p;
|
|
806 }
|
|
807 } else {
|
|
808 // Can't find remove node
|
|
809 }
|
|
810 }
|
|
811 }
|
|
812 } else {
|
|
813 // 親が居ない = tree root なので
|
|
814 // NULL を返す
|
|
815 ret = NULL;
|
|
816 }
|
|
817
|
|
818 return ret;
|
|
819 }
|
|
820
|
|
821 /**
|
|
822 * list から node を削除する
|
|
823 *
|
|
824 * @param list SceneGraphList
|
|
825 * @return node削除後の SceneGraphList
|
|
826 */
|
|
827 SceneGraphPtr
|
|
828 SceneGraph::realRemoveFromList(SceneGraphPtr list)
|
|
829 {
|
|
830 SceneGraphPtr node = this;
|
|
831 SceneGraphPtr prev = node->prev;
|
|
832 SceneGraphPtr next = node->next;
|
|
833 SceneGraphPtr ret = list;
|
|
834
|
|
835 if (prev) {
|
|
836 prev->next = next;
|
|
837 } else {
|
|
838 ret = next;
|
|
839 }
|
|
840
|
|
841 if (next) {
|
|
842 next->prev = prev;
|
|
843 }
|
|
844
|
|
845 return ret;
|
|
846 }
|
|
847
|
|
848 int
|
|
849 SceneGraph::isRemoved(void)
|
|
850 {
|
|
851 return flag_remove;
|
|
852 }
|
|
853
|
|
854 /**
|
|
855 * 平行移動
|
|
856 *
|
|
857 * @param x Ttranslate in the x direction
|
|
858 * @param y Ttranslate in the y direction
|
|
859 * @param z Ttranslate in the z direction
|
|
860 */
|
|
861 void
|
|
862 SceneGraph::translate(float x, float y, float z)
|
|
863 {
|
|
864 this->xyz[0] = x;
|
|
865 this->xyz[1] = y;
|
|
866 this->xyz[2] = z;
|
|
867 }
|
|
868
|
|
869 /**
|
|
870 * x 軸方向への平行移動
|
|
871 *
|
|
872 * @param x Ttranslate in the x direction
|
|
873 */
|
|
874 void
|
|
875 SceneGraph::translateX(float x)
|
|
876 {
|
|
877 this->xyz[0] = x;
|
|
878 }
|
|
879
|
|
880 /**
|
|
881 * y 軸方向への平行移動
|
|
882 *
|
|
883 * @param y Ttranslate in the y direction
|
|
884 */
|
|
885 void
|
|
886 SceneGraph::translateY(float y)
|
|
887 {
|
|
888 this->xyz[1] = y;
|
|
889 }
|
|
890
|
|
891 /**
|
|
892 * z 軸方向への平行移動
|
|
893 *
|
|
894 * @param z Ttranslate in the z direction
|
|
895 */
|
|
896 void
|
|
897 SceneGraph::translateZ(float z)
|
|
898 {
|
|
899 this->xyz[2] = z;
|
|
900 }
|