/
ofAppNoWindow.cpp
223 lines (179 loc) · 5.06 KB
/
ofAppNoWindow.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
#include "ofAppNoWindow.h"
#include "ofBaseApp.h"
#include "ofMain.h"
#if defined TARGET_OSX || defined TARGET_LINUX
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <termios.h>
struct termios orig_termios;
struct sigaction act_open;
void reset_terminal_mode()
{
tcsetattr(0, TCSANOW, &orig_termios);
}
void set_conio_terminal_mode()
{
struct termios new_termios;
/* take two copies - one for now, one for later */
tcgetattr(0, &orig_termios);
memcpy(&new_termios, &orig_termios, sizeof(new_termios));
/* register cleanup handler, and set the new terminal mode */
atexit(reset_terminal_mode);
// setup new_termios for raw keyboard input
cfmakeraw(&new_termios);
// handle "\n" properly
new_termios.c_oflag |= OPOST;
//new_termios.c_oflag |= ONLCR;
// set the new_termios
tcsetattr(0, TCSANOW, &new_termios);
}
int kbhit()
{
return 0;
struct timeval tv = { 0L, 0L };
fd_set fds;
FD_SET(0, &fds);
return select(1, &fds, NULL, NULL, &tv);
}
int getch()
{
int r;
unsigned char c;
if ((r = read(0, &c, sizeof(c))) < 0) {
return r;
} else {
return c;
}
}
#endif
//----------------------------------------------------------
ofAppNoWindow::ofAppNoWindow(){
timeNow = 0;
timeThen = 0;
fps = 60; //give a realistic starting value - win32 issues
nFramesForFPS = 0;
nFrameCount = 0;
bFrameRateSet = false;
millisForFrame = 0;
prevMillis = 0;
diffMillis = 0;
frameRate = 0;
lastFrameTime = 0;
}
//------------------------------------------------------------
void ofAppNoWindow::runAppViaInfiniteLoop(ofPtr<ofBaseApp> appPtr){
static ofEventArgs voidEventArgs;
ofAppPtr = appPtr;
if(ofAppPtr){
ofAppPtr->setup();
ofAppPtr->update();
}
#if defined TARGET_OSX || defined TARGET_LINUX
// for keyboard
//set_conio_terminal_mode();
#endif
#ifdef OF_USING_POCO
ofNotifyEvent( ofEvents.setup, voidEventArgs );
ofNotifyEvent( ofEvents.update, voidEventArgs );
#endif
printf( "***\n***\n*** ofAppNoWindow running a headerless openFrameworks app\n"
"***\n*** keyboard input works here\n"
"***\n*** press Esc or Ctrl-C to quit\n"
"***\n");
while (true)
{
if (nFrameCount != 0 && bFrameRateSet == true){
diffMillis = ofGetElapsedTimeMillis() - prevMillis;
if (diffMillis > millisForFrame){
; // we do nothing, we are already slower than target frame
} else {
int waitMillis = millisForFrame - diffMillis;
#ifdef TARGET_WIN32
Sleep(waitMillis); //windows sleep in milliseconds
#else
usleep(waitMillis * 1000); //mac sleep in microseconds - cooler :)
#endif
}
}
prevMillis = ofGetElapsedTimeMillis(); // you have to measure here
/// listen for escape
#ifdef TARGET_WIN32
if (GetAsyncKeyState(VK_ESCAPE))
OF_EXIT_APP(0);
#endif
#if defined TARGET_OSX || defined TARGET_LINUX
while ( kbhit() )
{
int key = getch();
if ( key == 27 )
{
OF_EXIT_APP(0);
}
else if ( key == /* ctrl-c */ 3 )
{
printf("Ctrl-C pressed\n");
OF_EXIT_APP(0);
}
else
{
ofAppPtr->keyPressed( key );
}
}
#endif
/// update
if (ofAppPtr)
ofAppPtr->update();
#ifdef OF_USING_POCO
ofNotifyEvent( ofEvents.update, voidEventArgs);
#endif
// -------------- fps calculation:
timeNow = ofGetElapsedTimef();
double diff = timeNow-timeThen;
if( diff > 0.00001 ){
fps = 1.0 / diff;
frameRate *= 0.9f;
frameRate += 0.1f*fps;
}
lastFrameTime = diff;
timeThen = timeNow;
// --------------
nFrameCount++; // increase the overall frame count
}
}
//------------------------------------------------------------
void ofAppNoWindow::exitApp(){
// -- This already exists in ofExitCallback
// static ofEventArgs voidEventArgs;
//
// if(ofAppPtr)ofAppPtr->exit();
//
// #ifdef OF_USING_POCO
// ofNotifyEvent( ofEvents.exit, voidEventArgs );
// #endif
ofLog(OF_LOG_VERBOSE,"No Window OF app is being terminated!");
reset_terminal_mode();
OF_EXIT_APP(0);
}
//------------------------------------------------------------
float ofAppNoWindow::getFrameRate(){
return frameRate;
}
//------------------------------------------------------------
int ofAppNoWindow::getFrameNum(){
return nFrameCount;
}
//------------------------------------------------------------
void ofAppNoWindow::setFrameRate(float targetRate){
// given this FPS, what is the amount of millis per frame
// that should elapse?
// --- > f / s
if (targetRate == 0){
bFrameRateSet = false;
return;
}
bFrameRateSet = true;
float durationOfFrame = 1.0f / (float)targetRate;
millisForFrame = (int)(1000.0f * durationOfFrame);
frameRate = targetRate;
}