/
ofAppNoWindow.cpp
316 lines (261 loc) · 9.32 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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include "ofAppNoWindow.h"
#include "ofGraphics.h"
#include "ofPath.h"
#include "of3dGraphics.h"
#include <memory>
#if defined TARGET_OSX || defined TARGET_LINUX
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <termios.h>
#include <signal.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, nullptr, nullptr, &tv);
}
int getch()
{
int r;
unsigned char c;
if ((r = read(0, &c, sizeof(c))) < 0) {
return r;
} else {
return c;
}
}
#endif
class ofNoopRenderer: public ofBaseRenderer{
public:
ofNoopRenderer():graphics3d(this){}
private:
static const std::string TYPE;
const std::string & getType(){return TYPE;}
void startRender(){}
void finishRender(){}
void update(){}
void draw(const ofPolyline & poly) const{}
void draw(const ofPath & shape) const{}
void draw(const of3dPrimitive&, ofPolyRenderMode) const{}
void draw(const ofNode&) const{}
void draw(const ofMesh & vertexData, ofPolyRenderMode renderType, bool useColors, bool useTextures, bool useNormals) const{}
void draw(const ofImage & image, float x, float y, float z, float w, float h, float sx, float sy, float sw, float sh) const{}
void draw(const ofFloatImage & image, float x, float y, float z, float w, float h, float sx, float sy, float sw, float sh) const{}
void draw(const ofShortImage & image, float x, float y, float z, float w, float h, float sx, float sy, float sw, float sh) const{}
void draw(const ofBaseVideoDraws & video, float x, float y, float w, float h) const{}
//--------------------------------------------
// transformations
void pushView(){};
void popView(){};
// setup matrices and viewport (upto you to push and pop view before and after)
// if width or height are 0, assume windows dimensions (ofGetWidth(), ofGetHeight())
// if nearDist or farDist are 0 assume defaults (calculated based on width / height)
void viewport(ofRectangle viewport){};
void viewport(float x = 0, float y = 0, float width = 0, float height = 0, bool invertY = true){};
void setupScreenPerspective(float width = 0, float height = 0, float fov = 60, float nearDist = 0, float farDist = 0){}
void setupScreenOrtho(float width = 0, float height = 0, float nearDist = -1, float farDist = 1){};
ofRectangle getCurrentViewport() const{return ofRectangle();};
ofRectangle getNativeViewport() const{return ofRectangle();};
int getViewportWidth() const{return 0;};
int getViewportHeight() const{return 0;};
void setCoordHandedness(ofHandednessType handedness){};
ofHandednessType getCoordHandedness() const{return OF_LEFT_HANDED;};
//our openGL wrappers
void pushMatrix(){};
void popMatrix(){};
void translate(float x, float y, float z = 0){};
void translate(const glm::vec3 & p){};
void scale(float xAmnt, float yAmnt, float zAmnt = 1){};
void rotateRad(float radians, float vecX, float vecY, float vecZ){};
void rotateXRad(float radians){};
void rotateYRad(float radians){};
void rotateZRad(float radians){};
void rotateRad(float radians){};
// screen coordinate things / default gl values
void setupGraphicDefaults(){};
void setupScreen(){};
void setOrientation(ofOrientation, bool){}
bool isVFlipped() const{return true;}
glm::mat4 getCurrentMatrix(ofMatrixMode) const{return glm::mat4(1.0);};
glm::mat4 getCurrentOrientationMatrix() const{return glm::mat4(1.0);}
void matrixMode(ofMatrixMode){}
void loadIdentityMatrix(){}
void loadMatrix(const glm::mat4&){}
void loadMatrix(const float*){}
void multMatrix(const glm::mat4&){}
void multMatrix(const float*){}
void loadViewMatrix(const glm::mat4&){}
void multViewMatrix(const glm::mat4&){}
glm::mat4 getCurrentViewMatrix() const{return glm::mat4(1.0);}
glm::mat4 getCurrentNormalMatrix() const{return glm::mat4(1.0);}
void enableAntiAliasing(){}
void disableAntiAliasing(){}
// drawing modes
void setRectMode(ofRectMode mode){}
ofRectMode getRectMode(){return OF_RECTMODE_CORNER;}
void setFillMode(ofFillFlag fill){}
ofFillFlag getFillMode(){return OF_FILLED;}
void setLineWidth(float lineWidth){}
void setBlendMode(ofBlendMode blendMode){}
void setLineSmoothing(bool smooth){}
void setCircleResolution(int res){};
void enablePointSprites(){};
void disablePointSprites(){};
void setDepthTest(bool){};
// color options
void setColor(float r, float g, float b){}; // 0-1
void setColor(float r, float g, float b, float a){}; // 0-1
void setColor(const ofFloatColor & color){};
void setColor(const ofFloatColor & color, float _a){};
void setColor(float gray){}; // new set a color as grayscale with one argument
void setHexColor( int hexColor ){}; // hex, like web 0xFF0033;
// bg color
ofFloatColor getBackgroundColor(){return ofFloatColor(200.f/255.f);}
void setBackgroundColor(const ofFloatColor & color){}
bool getBackgroundAuto(){
return true;
}
void background(const ofFloatColor & c){};
void background(float brightness){};
void background(int hexColor, int _a=255){};
void background(float r, float g, float b, float a=1.f){};
void setBackgroundAuto(bool bManual){}; // default is true
void clear(){};
void clear(float r, float g, float b, float a=0){};
void clear(float brightness, float a=0){};
void clearAlpha(){};
// drawing
void drawLine(float x1, float y1, float z1, float x2, float y2, float z2) const{}
void drawRectangle(float x, float y, float z, float w, float h) const{}
void drawTriangle(float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3) const{}
void drawCircle(float x, float y, float z, float radius) const{}
void drawSphere(float x, float y, float z, float radius) const{}
void drawEllipse(float x, float y, float z, float width, float height) const{}
void drawString(std::string text, float x, float y, float z) const{}
void drawString(const ofTrueTypeFont & font, std::string text, float x, float y) const{}
void setBitmapTextMode(ofDrawBitmapMode mode){}
ofStyle getStyle() const{ return ofStyle(); }
void pushStyle(){}
void popStyle(){}
void setStyle(const ofStyle & style){}
void setCurveResolution(int res){}
void setPolyMode(ofPolyWindingMode){}
void bind(const ofCamera & camera, const ofRectangle & viewport){};
void unbind(const ofCamera & camera){};
ofPath & getPath(){ return path; }
const of3dGraphics & get3dGraphics() const{
return graphics3d;
}
of3dGraphics & get3dGraphics(){
return graphics3d;
}
of3dGraphics graphics3d;
ofPath path;
};
const std::string ofNoopRenderer::TYPE="NOOP";
//----------------------------------------------------------
ofAppNoWindow::ofAppNoWindow()
:coreEvents(new ofCoreEvents)
,currentRenderer(new ofNoopRenderer){
ofAppPtr = nullptr;
width = 0;
height = 0;
}
//----------------------------------------------------------
void ofAppNoWindow::setup(const ofWindowSettings & settings){
width = settings.getWidth();
height = settings.getHeight();
}
//----------------------------------------------------------
void ofAppNoWindow::update(){
/// listen for escape
#ifdef TARGET_WIN32
if (GetAsyncKeyState(VK_ESCAPE))
events().notifyKeyPressed(OF_KEY_ESC);
#endif
#if defined TARGET_OSX || defined TARGET_LINUX
while ( kbhit() )
{
int key = getch();
if ( key == 27 )
{
events().notifyKeyPressed(OF_KEY_ESC);
}
else if ( key == /* ctrl-c */ 3 )
{
ofLogNotice("ofAppNoWindow") << "Ctrl-C pressed" << std::endl;
break;
}
else
{
events().notifyKeyPressed(key);
}
}
#endif
events().notifyUpdate();
}
//----------------------------------------------------------
void ofAppNoWindow::draw(){
events().notifyDraw();
}
//------------------------------------------------------------
void ofAppNoWindow::exitApp(){
ofLogVerbose("ofAppNoWindow") << "terminating headless (no window) app!";
#if defined TARGET_OSX || defined TARGET_LINUX
// this doesn't exist on windows and gives linking errors, so commented out.
reset_terminal_mode();
#endif
OF_EXIT_APP(0);
}
//----------------------------------------------------------
glm::vec2 ofAppNoWindow::getWindowPosition(){
return {0.f, 0.f};
}
//----------------------------------------------------------
glm::vec2 ofAppNoWindow::getWindowSize(){
return {width, height};
}
//----------------------------------------------------------
glm::vec2 ofAppNoWindow::getScreenSize(){
return {width, height};
}
//----------------------------------------------------------
int ofAppNoWindow::getWidth(){
return width;
}
//----------------------------------------------------------
int ofAppNoWindow::getHeight(){
return height;
}
//----------------------------------------------------------
ofCoreEvents & ofAppNoWindow::events(){
return *coreEvents;
}
std::shared_ptr<ofBaseRenderer> & ofAppNoWindow::renderer(){
return currentRenderer;
}