/
main.cpp
283 lines (241 loc) · 7.42 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/* By Piotr Mitros.
Copyright (c) 2002. All Rights Reserved.
May be distributed under GPL.
*/
#include "main.h"
struct Ball {
// Constructor will be necessary when new's/malloc'd
Ball() {
position=0; light=0; x=y=z=dx=dy=dz=0;
up=down=left=right=ix=iy=0;
}
SoTranslation *position;
SoPointLight *light;
SoPerspectiveCamera *camera;
float x, y, z;
float dx, dy, dz;
int ix, iy;
int up, down, left, right;
};
SoSeparator* loadModel(const char* fileName);
SoSeparator* createScene(void) ;
void keyboardCB(void*, SoEventCallback*);
void loadLevel(SoSeparator*map);
Ball player;
int mapX, mapY;
char **map;
int mapAt(int x, int y)
{
if((x<0)||(y<0)||(x>=mapX))
return 0;
if(y>=mapY)
return 1;
return map[y][x];
}
void loadLevel(SoSeparator*root)
{
FILE*f=fopen("level.dat", "r");
if(!f) {
printf("Couldn't open menu\n");
exit(0);
}
fscanf(f, "%i %i\n", &mapX, &mapY);
DEBUG("Loaded map");
map=new (char*)[mapY];
for(int i=0; i<mapY; i++) {
map[i]=new char[mapX];
for(int j=0; j<mapX; j++) {
map[i][j]=fgetc(f)-'0';
if(map[i][j]) {
SoTranslation*position=new SoTranslation;
SoCube*cube=new SoCube;
SoSeparator*separator=new SoSeparator;
SoScale *scale=new SoScale;
scale->scaleFactor.setValue(SbVec3f(1, 0.5, 1));
position->translation.setValue(SbVec3f((j-mapX/2)*2, map[i][j], -i*2));
separator->addChild(scale);
separator->addChild(position);
separator->addChild(cube);
root->addChild(separator);
}
}
fgetc(f);
}
DEBUG("Done reading\n");
fclose(f);
}
// the MAIN function
int main(int argc, char ** argv)
{
Widget mainWindow = SoXt::init(argv[0]);
if (mainWindow == NULL) exit(1);
SoSeparator *root;
root = new SoSeparator;
root->ref();
player.camera = new SoPerspectiveCamera;
root->addChild(player.camera);
root->addChild(createScene()) ;
//player.camera->viewAll(root, myRenderArea->getViewportRegion());
player.camera->position=SbVec3f(0, 5, 20);
player.camera->orientation=SbRotation(-0.1, 0, 0, 1);
player.camera->nearDistance=1;
player.camera->farDistance=150;
player.camera->heightAngle=0.5;
//view the whole scene
// initialize scenemanager instance
SoXtRenderArea *myRenderArea = new SoXtRenderArea(mainWindow);
myRenderArea->setSceneGraph(root);
myRenderArea->setTitle("Bouncing Ball");
myRenderArea->show();
SoXt::show(mainWindow);
SoXt::mainLoop();
return 0;
}
void increment(void *, SoSensor *)
{
player.x+=player.dx;
player.y+=player.dy;
player.z+=player.dz;
player.ix=int(player.x+101)/2+mapX/2-50;
player.iy=int(-player.y+101)/2-50;
player.dz-=0.1;
if(player.z<0) {
// if((player.ix>=0)&&(player.iy>=0)&&(map[player.iy][player.ix])) {
if((player.ix>=0)&&(player.iy>=0)&&(mapAt(player.ix,player.iy))) {
player.dz=0;
player.z=0;
}
else {
player.x=player.dz=player.dy=player.dx=player.y=player.z=0;
}
}
player.position->translation.setValue(SbVec3f(0+player.x,
1.5+player.z,
0+player.y));
player.light->location.setValue(SbVec3f(0+player.x,5+player.z,0+player.y));
player.camera->position=SbVec3f(0+player.x, 5+player.z, 20+player.y);
//printf("%f %f %i %i\n",player.x, player.y,player.ix, player.iy);
// Update acceleration based on whether player is holding arrow key down
// Math used to set maximum speed of 2, and higher acceleration when away
// from max speed.
// Also, if speed is near zero, and player is trying to stop, ball stops,
// rather than taking on pathetically slow velocities.
if(player.left)
if((player.dx<=0)||(player.dx>.2))
player.dx-=.1*(2+player.dx);
else
player.dx=0;
if(player.right)
if((player.dx>=0)||(player.dx<-.2))
player.dx+=.1*(2-player.dx);
else
player.dx=0;
if((!player.left)&&(!player.right)) {
if(fabs(player.dx<0.01))
player.dx=0;
else
player.dx*=0.8;
}
if(player.up)
player.dy-=.1*(2+player.dy);
if(player.down)
player.dy+=.1*(2-player.dy);
}
SoSeparator* createScene(void)
{
SoSeparator *root = new SoSeparator ;
player.light=new SoPointLight;
player.light->location.setValue(SbVec3f(0,5,0));
player.light->intensity.setValue(1);
root->addChild(player.light);
SoDirectionalLight*dl1=new SoDirectionalLight;
// -1 right, 1 left : -1 top, 1 bottom : -1 back, 1 front
dl1->direction.setValue(SbVec3f(0, -1, -0.5));
dl1->intensity.setValue(1);
root->addChild(dl1);
SoMaterial *mat2 = new SoMaterial ;
mat2->diffuseColor.setValue(0.0,0.7,0.7) ;
mat2->ambientColor.setValue(0.0,0.0,0.7) ;
mat2->specularColor.setValue(0.0,1.0,0.0) ;
root->addChild(mat2) ;
SoDrawStyle * drawstyle2 = new SoDrawStyle;
drawstyle2->style.setValue(SoDrawStyleElement::FILLED);
root->addChild(drawstyle2);
SoSeparator *ballSep=new SoSeparator;
SoSphere *ball=new SoSphere;
player.position=new SoTranslation;
player.position->translation.setValue(SbVec3f(0,1.5,0));
ball->radius.setValue(0.5);
ballSep->addChild(player.position);
ballSep->addChild(ball);
root->addChild(ballSep);
//FILLED, LINES, POINTS, INVISIBLE
SoDrawStyle * drawstyle = new SoDrawStyle;
drawstyle->style.setValue(SoDrawStyleElement::FILLED);
//drawstyle->style.setValue(SoDrawStyleElement::LINES);
root->addChild(drawstyle);
SoMaterial *mat = new SoMaterial ;
mat->diffuseColor.setValue(0.7,0.7,0.0) ;
mat->ambientColor.setValue(0.7,0.0,0.0) ;
mat->specularColor.setValue(0.0,1.0,0.0) ;
mat->shininess.setValue(1.0) ;
root->addChild(mat) ;
loadLevel(root);
SoTimerSensor *ts=new SoTimerSensor(increment, (void*)0);
ts->schedule();
SoEventCallback*cb=new SoEventCallback;
cb->addEventCallback(SoKeyboardEvent::getClassTypeId(), keyboardCB, NULL);
root->addChild(cb);
return root ;
}
void keyboardCB(void*, SoEventCallback*cb)
{
//printf("Keyboard\n");
if(SO_KEY_PRESS_EVENT(cb->getEvent(), Q))
exit(0);
if(SO_KEY_PRESS_EVENT(cb->getEvent(), LEFT_ARROW))
player.left=1;
if(SO_KEY_PRESS_EVENT(cb->getEvent(), RIGHT_ARROW))
player.right=1;
if(SO_KEY_PRESS_EVENT(cb->getEvent(), UP_ARROW))
player.up=1;
if(SO_KEY_PRESS_EVENT(cb->getEvent(), DOWN_ARROW))
player.down=1;
if(SO_KEY_RELEASE_EVENT(cb->getEvent(), LEFT_ARROW))
player.left=0;
if(SO_KEY_RELEASE_EVENT(cb->getEvent(), RIGHT_ARROW))
player.right=0;
if(SO_KEY_RELEASE_EVENT(cb->getEvent(), UP_ARROW))
player.up=0;
if(SO_KEY_RELEASE_EVENT(cb->getEvent(), DOWN_ARROW))
player.down=0;
if(SO_KEY_PRESS_EVENT(cb->getEvent(), SPACE))
if((player.z==0)&&(player.dz<.5))
player.dz+=0.8;
cb->setHandled();
}
SoSeparator* loadModel(const char* fileName)
{
SoSeparator *root = new SoSeparator;
SoInput myScene;
//try to open the file
if (!myScene.openFile(fileName)) {
printf("Could not open %s\n",fileName) ;
return NULL;
}
//check if the file is valid
if (!myScene.isValidFile()) {
printf("%s is not a valid Inventor file\n",fileName) ;
return NULL;
}
//try to read the file
root = SoDB::readAll(&myScene) ;
if (root == NULL) {
printf("Problem reading %s\n",fileName) ;
myScene.closeFile() ;
return NULL;
}
//close the file
myScene.closeFile() ;
return root ;
}