Debian Codegraph @

apps/examples/Mentor/CXX/12.4.TimerSensor.c++

raw
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 /*--------------------------------------------------------------
38 * This is an example from the Inventor Mentor,
39 * chapter 12, example 4.
40 *
41 * Timer sensors. An object is rotated by a timer sensor.
42 * (called "rotatingSensor"). The interval between calls
43 * controls how often it rotates.
44 * A second timer (called "schedulingSensor") goes off
45 * every 5 seconds and changes the interval of
46 * "rotatingSensor". The interval alternates between
47 * once per second and 10 times per second.
48 * This example could also be done using engines.
49 *------------------------------------------------------------*/
50
51 #include <cstdlib>
52 #include <Inventor/SoDB.h>
53 #include <Inventor/Xt/SoXt.h>
54 #include <Inventor/Xt/viewers/SoXtExaminerViewer.h>
55 #include <Inventor/nodes/SoCone.h>
56 #include <Inventor/nodes/SoRotation.h>
57 #include <Inventor/nodes/SoSeparator.h>
58 #include <Inventor/nodes/SoTransform.h>
59 #include <Inventor/sensors/SoTimerSensor.h>
60
61 ///////////////////////////////////////////////////////////
62 // CODE FOR The Inventor Mentor STARTS HERE
63
64 // This function is called either 10 times/second or once every
65 // second; the scheduling changes every 5 seconds (see below):
66 static void
67 rotatingSensorCallback(void *data, SoSensor *)
68 {
69 // Rotate an object...
70 SoRotation *myRotation = (SoRotation *)data;
71 SbRotation currentRotation = myRotation->rotation.getValue();
72 currentRotation = SbRotation(
73 SbVec3f(0,0,1), M_PI/90.0) * currentRotation;
74 myRotation->rotation.setValue(currentRotation);
75 }
76
77 // This function is called once every 5 seconds, and
78 // reschedules the other sensor.
79 static void
80 schedulingSensorCallback(void *data, SoSensor *)
81 {
82 SoTimerSensor *rotatingSensor = (SoTimerSensor *)data;
83 rotatingSensor->unschedule();
84 if (rotatingSensor->getInterval() == 1.0)
85 rotatingSensor->setInterval(1.0/10.0);
86 else rotatingSensor->setInterval(1.0);
87 rotatingSensor->schedule();
88 }
89
90 // CODE FOR The Inventor Mentor ENDS HERE
91 ///////////////////////////////////////////////////////////
92
93 int
94 main(int argc, char **argv)
95 {
96 if (argc != 2) {
97 fprintf(stderr, "Usage: %s filename.iv\n", argv[0]);
98 exit(1);
99 }
100
101 Widget myWindow = SoXt::init(argv[0]);
102 if (myWindow == NULL) exit(1);
103
104 SoSeparator *root = new SoSeparator;
105 root->ref();
106
107 ///////////////////////////////////////////////////////////
108 // CODE FOR The Inventor Mentor STARTS HERE
109
110 SoRotation *myRotation = new SoRotation;
111 root->addChild(myRotation);
112
113 SoTimerSensor *rotatingSensor =
114 new SoTimerSensor(rotatingSensorCallback, myRotation);
115 rotatingSensor->setInterval(1.0); // scheduled once per second
116 rotatingSensor->schedule();
117
118 SoTimerSensor *schedulingSensor =
119 new SoTimerSensor(schedulingSensorCallback, rotatingSensor);
120 schedulingSensor->setInterval(5.0); // once per 5 seconds
121 schedulingSensor->schedule();
122
123 // CODE FOR The Inventor Mentor ENDS HERE
124 ///////////////////////////////////////////////////////////
125
126 SoInput inputFile;
127 if (inputFile.openFile(argv[1]) == FALSE) {
128 fprintf(stderr, "Could not open file %s\n", argv[1]);
129 exit(1);
130 }
131 root->addChild(SoDB::readAll(&inputFile));
132
133 SoXtExaminerViewer *myViewer =
134 new SoXtExaminerViewer(myWindow);
135 myViewer->setSceneGraph(root);
136 myViewer->setTitle("Two Timers");
137 myViewer->show();
138
139 SoXt::show(myWindow); // Display main window
140 SoXt::mainLoop(); // Main Inventor event loop
141 }