framework2
315 строк · 9.3 Кб
1#include "ofAppNoWindow.h"
2#include "ofGraphics.h"
3#include "ofPath.h"
4#include "of3dGraphics.h"
5#include <memory>
6
7
8#if defined TARGET_OSX || defined TARGET_LINUX
9#include <stdlib.h>
10#include <string.h>
11#include <sys/select.h>
12#include <termios.h>
13#include <signal.h>
14
15struct termios orig_termios;
16struct sigaction act_open;
17
18void reset_terminal_mode()
19{
20tcsetattr(0, TCSANOW, &orig_termios);
21}
22
23
24void set_conio_terminal_mode()
25{
26struct termios new_termios;
27
28/* take two copies - one for now, one for later */
29tcgetattr(0, &orig_termios);
30memcpy(&new_termios, &orig_termios, sizeof(new_termios));
31
32/* register cleanup handler, and set the new terminal mode */
33atexit(reset_terminal_mode);
34// setup new_termios for raw keyboard input
35cfmakeraw(&new_termios);
36// handle "\n" properly
37new_termios.c_oflag |= OPOST;
38//new_termios.c_oflag |= ONLCR;
39// set the new_termios
40tcsetattr(0, TCSANOW, &new_termios);
41}
42
43int kbhit()
44{
45return 0;
46// struct timeval tv = { 0L, 0L };
47// fd_set fds;
48// FD_SET(0, &fds);
49// return select(1, &fds, nullptr, nullptr, &tv);
50}
51
52int getch()
53{
54int r;
55unsigned char c;
56if ((r = read(0, &c, sizeof(c))) < 0) {
57return r;
58} else {
59return c;
60}
61}
62
63#endif
64
65class ofNoopRenderer: public ofBaseRenderer{
66public:
67ofNoopRenderer():graphics3d(this){}
68private:
69static const std::string TYPE;
70const std::string & getType(){return TYPE;}
71
72void startRender(){}
73void finishRender(){}
74void update(){}
75
76void draw(const ofPolyline & poly) const{}
77void draw(const ofPath & shape) const{}
78void draw(const of3dPrimitive&, ofPolyRenderMode) const{}
79void draw(const ofNode&) const{}
80void draw(const ofMesh & vertexData, ofPolyRenderMode renderType, bool useColors, bool useTextures, bool useNormals) const{}
81void draw(const ofImage & image, float x, float y, float z, float w, float h, float sx, float sy, float sw, float sh) const{}
82void draw(const ofFloatImage & image, float x, float y, float z, float w, float h, float sx, float sy, float sw, float sh) const{}
83void draw(const ofShortImage & image, float x, float y, float z, float w, float h, float sx, float sy, float sw, float sh) const{}
84void draw(const ofBaseVideoDraws & video, float x, float y, float w, float h) const{}
85
86//--------------------------------------------
87// transformations
88void pushView(){};
89void popView(){};
90
91// setup matrices and viewport (upto you to push and pop view before and after)
92// if width or height are 0, assume windows dimensions (ofGetWidth(), ofGetHeight())
93// if nearDist or farDist are 0 assume defaults (calculated based on width / height)
94void viewport(ofRectangle viewport){};
95void viewport(float x = 0, float y = 0, float width = 0, float height = 0, bool invertY = true){};
96void setupScreenPerspective(float width = 0, float height = 0, float fov = 60, float nearDist = 0, float farDist = 0){}
97void setupScreenOrtho(float width = 0, float height = 0, float nearDist = -1, float farDist = 1){};
98ofRectangle getCurrentViewport() const{return ofRectangle();};
99ofRectangle getNativeViewport() const{return ofRectangle();};
100int getViewportWidth() const{return 0;};
101int getViewportHeight() const{return 0;};
102
103void setCoordHandedness(ofHandednessType handedness){};
104ofHandednessType getCoordHandedness() const{return OF_LEFT_HANDED;};
105
106//our openGL wrappers
107void pushMatrix(){};
108void popMatrix(){};
109void translate(float x, float y, float z = 0){};
110void translate(const glm::vec3 & p){};
111void scale(float xAmnt, float yAmnt, float zAmnt = 1){};
112void rotateRad(float radians, float vecX, float vecY, float vecZ){};
113void rotateXRad(float radians){};
114void rotateYRad(float radians){};
115void rotateZRad(float radians){};
116void rotateRad(float radians){};
117
118// screen coordinate things / default gl values
119void setupGraphicDefaults(){};
120void setupScreen(){};
121
122void setOrientation(ofOrientation, bool){}
123bool isVFlipped() const{return true;}
124glm::mat4 getCurrentMatrix(ofMatrixMode) const{return glm::mat4(1.0);};
125glm::mat4 getCurrentOrientationMatrix() const{return glm::mat4(1.0);}
126void matrixMode(ofMatrixMode){}
127void loadIdentityMatrix(){}
128void loadMatrix(const glm::mat4&){}
129void loadMatrix(const float*){}
130void multMatrix(const glm::mat4&){}
131void multMatrix(const float*){}
132void loadViewMatrix(const glm::mat4&){}
133void multViewMatrix(const glm::mat4&){}
134glm::mat4 getCurrentViewMatrix() const{return glm::mat4(1.0);}
135glm::mat4 getCurrentNormalMatrix() const{return glm::mat4(1.0);}
136void enableAntiAliasing(){}
137void disableAntiAliasing(){}
138
139
140// drawing modes
141void setRectMode(ofRectMode mode){}
142ofRectMode getRectMode(){return OF_RECTMODE_CORNER;}
143void setFillMode(ofFillFlag fill){}
144ofFillFlag getFillMode(){return OF_FILLED;}
145void setLineWidth(float lineWidth){}
146void setBlendMode(ofBlendMode blendMode){}
147void setLineSmoothing(bool smooth){}
148void setCircleResolution(int res){};
149void enablePointSprites(){};
150void disablePointSprites(){};
151void setDepthTest(bool){};
152
153// color options
154void setColor(int r, int g, int b){}; // 0-255
155void setColor(int r, int g, int b, int a){}; // 0-255
156void setColor(const ofColor & color){};
157void setColor(const ofColor & color, int _a){};
158void setColor(int gray){}; // new set a color as grayscale with one argument
159void setHexColor( int hexColor ){}; // hex, like web 0xFF0033;
160
161// bg color
162ofColor getBackgroundColor(){return ofColor(200);}
163void setBackgroundColor(const ofColor & color){}
164bool getBackgroundAuto(){
165return true;
166}
167void background(const ofColor & c){};
168void background(float brightness){};
169void background(int hexColor, float _a=255.0f){};
170void background(int r, int g, int b, int a=255){};
171
172void setBackgroundAuto(bool bManual){}; // default is true
173
174void clear(){};
175void clear(float r, float g, float b, float a=0){};
176void clear(float brightness, float a=0){};
177void clearAlpha(){};
178
179// drawing
180void drawLine(float x1, float y1, float z1, float x2, float y2, float z2) const{}
181void drawRectangle(float x, float y, float z, float w, float h) const{}
182void drawTriangle(float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3) const{}
183void drawCircle(float x, float y, float z, float radius) const{}
184void drawSphere(float x, float y, float z, float radius) const{}
185void drawEllipse(float x, float y, float z, float width, float height) const{}
186void drawString(std::string text, float x, float y, float z) const{}
187void drawString(const ofTrueTypeFont & font, std::string text, float x, float y) const{}
188
189void setBitmapTextMode(ofDrawBitmapMode mode){}
190ofStyle getStyle() const{ return ofStyle(); }
191void pushStyle(){}
192void popStyle(){}
193void setStyle(const ofStyle & style){}
194void setCurveResolution(int res){}
195void setPolyMode(ofPolyWindingMode){}
196
197void bind(const ofCamera & camera, const ofRectangle & viewport){};
198void unbind(const ofCamera & camera){};
199ofPath & getPath(){ return path; }
200const of3dGraphics & get3dGraphics() const{
201return graphics3d;
202}
203
204of3dGraphics & get3dGraphics(){
205return graphics3d;
206}
207of3dGraphics graphics3d;
208ofPath path;
209};
210
211
212const std::string ofNoopRenderer::TYPE="NOOP";
213
214//----------------------------------------------------------
215ofAppNoWindow::ofAppNoWindow()
216:coreEvents(new ofCoreEvents)
217,currentRenderer(new ofNoopRenderer){
218ofAppPtr = nullptr;
219width = 0;
220height = 0;
221}
222
223
224//----------------------------------------------------------
225void ofAppNoWindow::setup(const ofWindowSettings & settings){
226width = settings.getWidth();
227height = settings.getHeight();
228}
229
230//----------------------------------------------------------
231void ofAppNoWindow::update(){
232
233/// listen for escape
234#ifdef TARGET_WIN32
235if (GetAsyncKeyState(VK_ESCAPE))
236events().notifyKeyPressed(OF_KEY_ESC);
237#endif
238
239#if defined TARGET_OSX || defined TARGET_LINUX
240while ( kbhit() )
241{
242int key = getch();
243if ( key == 27 )
244{
245events().notifyKeyPressed(OF_KEY_ESC);
246}
247else if ( key == /* ctrl-c */ 3 )
248{
249ofLogNotice("ofAppNoWindow") << "Ctrl-C pressed" << std::endl;
250break;
251}
252else
253{
254events().notifyKeyPressed(key);
255}
256}
257#endif
258
259
260events().notifyUpdate();
261}
262
263//----------------------------------------------------------
264void ofAppNoWindow::draw(){
265events().notifyDraw();
266}
267
268//------------------------------------------------------------
269void ofAppNoWindow::exitApp(){
270ofLogVerbose("ofAppNoWindow") << "terminating headless (no window) app!";
271
272
273#if defined TARGET_OSX || defined TARGET_LINUX
274// this doesn't exist on windows and gives linking errors, so commented out.
275reset_terminal_mode();
276#endif
277
278OF_EXIT_APP(0);
279}
280
281//----------------------------------------------------------
282glm::vec2 ofAppNoWindow::getWindowPosition(){
283return {0.f, 0.f};
284}
285
286//----------------------------------------------------------
287glm::vec2 ofAppNoWindow::getWindowSize(){
288return {width, height};
289}
290
291//----------------------------------------------------------
292glm::vec2 ofAppNoWindow::getScreenSize(){
293return {width, height};
294}
295
296
297//----------------------------------------------------------
298int ofAppNoWindow::getWidth(){
299return width;
300}
301
302//----------------------------------------------------------
303int ofAppNoWindow::getHeight(){
304return height;
305}
306
307
308//----------------------------------------------------------
309ofCoreEvents & ofAppNoWindow::events(){
310return *coreEvents;
311}
312
313std::shared_ptr<ofBaseRenderer> & ofAppNoWindow::renderer(){
314return currentRenderer;
315}
316
317