comparison Renderer/Test/viewer.cc @ 589:f674ca0fb76d

add viwer.cc
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Fri, 30 Oct 2009 18:39:12 +0900
parents
children d0b8860c17f8
comparison
equal deleted inserted replaced
579:6c1a627303b2 589:f674ca0fb76d
1 #include <math.h>
2 #include <stdlib.h>
3 #include "SceneGraphRoot.h"
4 #include "MainLoop.h"
5 #include "viewer.h"
6
7
8 // prototype
9 static void ball_move(SceneGraphPtr node, int screen_w, int screen_h);
10 static void ball_collision(SceneGraphPtr node, int screen_w, int screen_h, SceneGraphPtr tree);
11 static void ball_collision_idle(SceneGraphPtr, int w, int h, SceneGraphPtr tree);
12
13
14 static float vy = 0.0f; // y 方向速度
15 static float dt = 1.0/1.0f; // frame rate
16
17 static float e = -0.8f; // 反発係数
18 static float g = 9.8f; // 重力加速度
19 //static float v0 = 0.0f; // 初速は 0
20
21 static float h0; // 初期高さ
22 static float ball_radius = 100.0f;
23
24 static float speed = 10.0f;
25
26 static void
27 ball_move_idle2(SceneGraphPtr node, int screen_w, int screen_h)
28 {
29 Pad *pad = sgroot->getController();
30
31 if (pad->circle.isHold()) {
32 if (pad->left.isHold()) {
33 node->xyz[0] -= speed;
34 if(node->xyz[0] < ball_radius)
35 node->xyz[0] = ball_radius;
36 } else if (pad->right.isHold()) {
37 node->xyz[0] += speed;
38 if(node->xyz[0] > screen_w - ball_radius)
39 node->xyz[0] = screen_w - ball_radius;
40 }
41
42 if (pad->up.isHold()) {
43 node->xyz[1] -= speed;
44 } else if (pad->down.isHold()) {
45 node->xyz[1] += speed;
46 if(node->xyz[1] > screen_h - ball_radius)
47 node->xyz[1] = screen_h - ball_radius;
48 }
49 } else {
50 node->set_move_collision(ball_move, ball_collision);
51 }
52 }
53
54 static int time = 0;
55
56 static void
57 ball_move_idle(SceneGraphPtr node, int screen_w, int screen_h)
58 {
59 Pad *pad = sgroot->getController();
60
61 if (pad->circle.isPush()) {
62 node->set_move_collision(ball_move_idle2, ball_collision_idle);
63 time = 0;
64 }
65
66 time++;
67
68 if (time > 90) {
69 float w = (float)random();
70
71 w = fmodf(w, screen_w - ball_radius*2);
72 node->xyz[0] = w + ball_radius;
73 node->xyz[1] = h0;
74 node->set_move_collision(ball_move, ball_collision);
75 time = 0;
76 }
77 }
78
79 static void
80 ball_move(SceneGraphPtr node, int screen_w, int screen_h)
81 {
82 vy += g * dt;
83 node->xyz[1] += vy * dt;
84 // node->xyz[0] += 10.0f;
85 }
86
87 static void
88 ball_collision_idle(SceneGraphPtr, int w, int h, SceneGraphPtr tree)
89 {
90 }
91
92 static void
93 ball_collision(SceneGraphPtr node, int screen_w, int screen_h,
94 SceneGraphPtr tree)
95 {
96 if (node->xyz[1] > screen_h - ball_radius) {
97 node->xyz[1] = screen_h - ball_radius;
98
99 vy *= e;
100 if (vy > -g && vy < 0) {
101 vy = 0.0;
102 node->set_move_collision(ball_move_idle, ball_collision_idle);
103 }
104 }
105 }
106
107 char *xmlfile;
108 #define MAX_ROOT 100
109 char *parts[MAX_ROOT ];
110 int parts_cnt;
111
112 MainLoopPtr
113 viewer::init(Viewer *sgroot, int screen_w, int screen_h)
114 {
115 SceneGraphPtr ball;
116
117 // 固定した値で srandom すると、毎回同じ、random() 列が生成される
118 // random な値が欲しいなら、man random に方法が書いてあります。
119 srandom(100);
120
121 sgroot->createFromXMLfile(xmlfile);
122
123 ball = sgroot->createSceneGraph();
124 ball->set_move_collision(ball_move, ball_collision);
125
126 h0 = screen_h/2;
127 h0 = -1000;
128
129 ball->xyz[0] = screen_w/2;
130 //ball->xyz[0] = 0.0f;
131 ball->xyz[1] = h0;
132 ball->xyz[2] = 30.0f;
133
134 for(int i=0;i<parts_cnt; i++) {
135 ball->addChild(sgroot->createSceneGraph(parts[i]));
136 }
137 sgroot->setSceneData(ball);
138
139 return sgroot;
140 }
141
142 extern Application *
143 application() {
144 return new viewer();
145 }
146
147 const char *usr_help_str = "Usage: ./test_nogl [OPTION]\n";
148
149 extern int init(TaskManager *manager, int argc, char *argv[]);
150 extern void task_initialize();
151 static void TMend(TaskManager *manager);
152
153 int
154 TMmain(TaskManager *manager, int argc, char *argv[])
155 {
156 task_initialize();
157 manager->set_TMend(TMend);
158
159 for(int i=0;i<argc;i++) {
160 if (strcmp(argv[i],"-sg") == 0 && i+1<=argc) {
161 xmlfile = argv[i+1];
162 } else if (strcmp(argv[i],"-name") == 0 && i+1<=argc) {
163 parts[parts_cnt++] = argv[i+1];
164 }
165 }
166 return init(manager, argc, argv);
167
168 }
169
170 void
171 TMend(TaskManager *manager)
172 {
173 printf("test_nogl end\n");
174 }
175
176 /* end */
177