annotate TaskManager/Test/test_render/Application/dynamic_create.cc @ 518:003fe4ee39e2 draft

work dynamic_create
author aaa
date Wed, 21 Oct 2009 19:03:07 +0900
parents d2772feb34e3
children 74a8b7c97964 638bd307cde1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
515
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
1 #include <stdlib.h>
518
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
2 #include <stdio.h>
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
3 #include <string.h>
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
4 #include <fcntl.h>
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
5 #include <sys/types.h>
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
6 #include <sys/mman.h>
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
7 #include <sys/stat.h>
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
8 #include <unistd.h>
515
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
9 #include "SceneGraphRoot.h"
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
10
518
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
11 /*
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
12 typedef struct {
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
13 caddr_t file_mmap;
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
14 off_t size;
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
15 } st_mmap_t;
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
16 */
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
17
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
18 int
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
19 fix_byte(int size,int fix_byte_size)
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
20 {
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
21 size = (size/fix_byte_size)*fix_byte_size + ((size%fix_byte_size)!= 0)*fix_byte_size;
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
22
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
23 return size;
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
24 }
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
25
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
26 st_mmap_t
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
27 my_mmap(char *filename)
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
28 {
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
29 int fd = -1;
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
30 int map = MAP_PRIVATE;
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
31 st_mmap_t st_mmap;
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
32 struct stat sb;
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
33
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
34 if ((fd = open(filename, O_RDONLY, 0666)) == 0 ) {
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
35 fprintf(stderr, "Can't open %s\n", filename);
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
36 }
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
37
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
38 if (fstat(fd, &sb)) {
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
39 fprintf(stderr, "Can't fstat %s\n", filename);
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
40 }
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
41
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
42 printf("file size %d\n", (int)sb.st_size);
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
43
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
44 st_mmap.size = fix_byte(sb.st_size, 4096);
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
45
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
46 printf("fix 4096byte file size %d\n", (int)st_mmap.size);
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
47
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
48 st_mmap.file_mmap = (char *)mmap(NULL, st_mmap.size, PROT_READ, map, fd, (off_t)0);
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
49 if (st_mmap.file_mmap == (caddr_t)-1) {
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
50 fprintf(stderr, "Can't mmap file\n");
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
51 perror(NULL);
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
52 exit(0);
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
53 }
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
54
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
55 return st_mmap;
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
56 }
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
57
515
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
58 static void
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
59 earth_collision(SceneGraphPtr node, int screen_w, int screen_h,
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
60 SceneGraphPtr tree)
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
61 {
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
62 }
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
63
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
64 static void
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
65 moon_collision(SceneGraphPtr node, int screen_w, int screen_h,
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
66 SceneGraphPtr tree)
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
67 {
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
68 }
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
69
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
70 static void
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
71 moon_move(SceneGraphPtr node, int screen_w, int screen_h)
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
72 {
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
73 node->angle[0] += 3.0f;
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
74 node->xyz[1] += 1.0f;
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
75 }
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
76
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
77
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
78 static void
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
79 earth_move(SceneGraphPtr node, int screen_w, int screen_h)
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
80 {
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
81 node->angle[1] += 1.0f;
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
82 if (node->angle[1] > 360.0f) {
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
83 node->angle[1] = 0.0f;
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
84 }
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
85
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
86 node->xyz[0] += node->stack_xyz[0];
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
87 if ((int)node->xyz[0] > screen_w || (int)node->xyz[0] < 0) {
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
88 node->stack_xyz[0] = -node->stack_xyz[0];
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
89 }
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
90
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
91 node->xyz[1] += node->stack_xyz[1];
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
92 if ((int)node->xyz[1] > screen_h || (int)node->xyz[1] < 0) {
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
93 node->stack_xyz[1] = -node->stack_xyz[1];
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
94 }
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
95
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
96 Pad *pad = sgroot->getController();
518
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
97
515
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
98 if (pad->right.isPush()) {
518
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
99 SceneGraphPtr earth;
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
100
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
101 st_mmap_t m = my_mmap("xml_file/universe.xml");
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
102
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
103 //sgroot->createFromXMLmemory(sgroot->tmanager, "xml_file/universe.xml");
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
104 sgroot->createFromXMLmemory(sgroot->tmanager, m);
515
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
105 earth = sgroot->createSceneGraph("Earth");
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
106 earth->set_move_collision(moon_move, moon_collision);
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
107 node->addChild(earth);
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
108 }
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
109 }
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
110
518
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
111 /*
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
112 void
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
113 linda_init()
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
114 {
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
115 init_linda();
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
116 tspace = open_linda_java("localhost", PORT);
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
117 printf("open");
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
118 }
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
119 */
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
120
515
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
121 void
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
122 dynamic_init(TaskManager *manager)
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
123 {
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
124 SceneGraphPtr earth;
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
125 sgroot->tmanager = manager;
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
126
518
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
127
003fe4ee39e2 work dynamic_create
aaa
parents: 515
diff changeset
128
515
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
129 #if 0
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
130 // テスト用に mmap したデータを第2引数に渡す
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
131 sgroot->createFromXMLmemory(manager, "xml_file/universe.xml");
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
132
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
133 // sglist に登録されている name から sgid を引き、sg_src[sgid] からコピーして返す
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
134 earth = sgroot->createSceneGraph("Earth");
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
135 #else
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
136 SceneGraphPtr parent;
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
137 parent = sgroot->createSceneGraph();
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
138 parent->set_move_collision(earth_move, earth_collision);
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
139 #endif
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
140
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
141 // SceneGraphRoot に、使用する SceneGraph を設定する
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
142 // このとき、ユーザーが記述した SceneGraph の root を渡す。
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
143 sgroot->setSceneData(parent);
d2772feb34e3 add Application/dynamic_create.cc
aaa
parents:
diff changeset
144 }