framework2

Форк
0
541 строка · 14.3 Кб
1
#include "ofAppRunner.h"
2

3
#include "ofBaseApp.h"
4
#include "ofAppBaseWindow.h"
5

6
// IDEA BRANCH - remove this comment
7
#include "ofLog.h"
8
#include "ofMath.h"
9
#include "ofGraphicsBaseTypes.h"
10
#include "ofRectangle.h"
11

12
// MASTER - TODO: Remove commented out code
13
// #ifndef TARGET_NO_SOUND
14
// #include "ofSoundPlayer.h"
15
// #endif
16

17
// #include "ofImage.h"
18
// #include "ofTrueTypeFont.h"
19

20

21
#include "ofMainLoop.h"
22

23
using std::shared_ptr;
24

25
#if !defined(TARGET_NODISPLAY)
26
	#if !defined( TARGET_OF_IOS ) & !defined(TARGET_ANDROID) & !defined(TARGET_EMSCRIPTEN) & !defined(TARGET_RASPBERRY_PI_LEGACY)
27
	#include "ofAppGLFWWindow.h"
28
	//special case so we preserve supplied settngs
29
	//TODO: remove me when we remove the ofAppGLFWWindow setters.
30
	//--------------------------------------
31
	void ofSetupOpenGL(const shared_ptr<ofAppGLFWWindow> & windowPtr, int w, int h, ofWindowMode screenMode){
32
		ofInit();
33
		auto settings = windowPtr->getSettings();
34
		settings.setSize(w,h);
35
		settings.windowMode = screenMode;
36
		ofGetMainLoop()->addWindow(windowPtr);
37
		windowPtr->setup(settings);
38
	}
39
	#endif
40
#endif
41

42
#ifdef TARGET_LINUX
43
#include "ofGstUtils.h"
44
#endif
45

46
// adding this for vc2010 compile: error C3861: 'closeQuicktime': identifier not found
47
#if defined(OF_VIDEO_CAPTURE_QUICKTIME) || defined(OF_VIDEO_PLAYER_QUICKTIME)
48
	#include "ofQtUtils.h"
49
#endif
50

51
#if defined (TARGET_WIN32)
52
#include <mmsystem.h>
53
#endif
54

55

56
//--------------------------------------
57
namespace{
58

59
    shared_ptr<ofMainLoop> & mainLoop(){
60
        static shared_ptr<ofMainLoop> * mainLoop(new shared_ptr<ofMainLoop>(new ofMainLoop));
61
        return *mainLoop;
62
    }
63

64
    bool & initialized(){
65
        static bool * initialized = new bool(false);
66
        return *initialized;
67
    }
68

69
	bool & exiting(){
70
		static bool * exiting = new bool(false);
71
		return *exiting;
72
	}
73

74
	ofCoreEvents & noopEvents(){
75
		static auto * noopEvents = new ofCoreEvents();
76
		return *noopEvents;
77
	}
78

79
    #if defined(TARGET_LINUX) || defined(TARGET_OSX)
80
        #include <signal.h>
81
        #include <string.h>
82
        void ofSignalHandler(int signum){
83
            char* pSignalString = strsignal(signum);
84

85
            if(pSignalString){
86
                ofLogVerbose("ofSignalHandler") << pSignalString;
87
            }else{
88
                ofLogVerbose("ofSignalHandler") << "Unknown: " << signum;
89
            }
90

91
            signal(SIGTERM, nullptr);
92
            signal(SIGQUIT, nullptr);
93
            signal(SIGINT,  nullptr);
94
            signal(SIGHUP,  nullptr);
95
            signal(SIGABRT, nullptr);
96

97
            if(mainLoop()){
98
                mainLoop()->shouldClose(signum);
99
            }
100
        }
101
    #endif
102
}
103

104

105
void ofExitCallback();
106
void ofURLFileLoaderShutdown();
107
void ofTrueTypeShutdown();
108
void ofCloseFreeImage();
109

110
#if defined(TARGET_ANDROID) || defined (TARGET_LINUX_ARM)
111
	inline void ofSoundShutdown(){}
112
#else
113
	void ofSoundShutdown();
114
#endif
115

116

117
void ofInit(){
118
	if(initialized()) return;
119
	initialized() = true;
120
	exiting() = false;
121

122
#if defined(TARGET_ANDROID) || defined(TARGET_OF_IOS)
123
    // manage own exit
124
#else
125
	atexit(ofExitCallback);
126
#endif
127

128
#if defined(TARGET_LINUX) || defined(TARGET_OSX)
129
	// see http://www.gnu.org/software/libc/manual/html_node/Termination-Signals.html#Termination-Signals
130
	signal(SIGTERM, &ofSignalHandler);
131
	signal(SIGQUIT, &ofSignalHandler);
132
	signal(SIGINT,  &ofSignalHandler);
133

134
	signal(SIGHUP,  &ofSignalHandler); // not much to be done here
135

136
	// http://www.gnu.org/software/libc/manual/html_node/Program-Error-Signals.html#Program-Error-Signals
137
	signal(SIGABRT, &ofSignalHandler);  // abort signal
138
#endif
139

140
    of::priv::initutils();
141
    of::priv::initfileutils();
142

143
#ifdef WIN32_HIGH_RES_TIMING
144
    timeBeginPeriod(1);		// ! experimental, sets high res time
145
                            // you need to call timeEndPeriod.
146
                            // if you quit the app other than "esc"
147
                            // (ie, close the console, kill the process, etc)
148
                            // at exit wont get called, and the time will
149
                            // remain high res, that could mess things
150
                            // up on your system.
151
                            // info here:http://www.geisswerks.com/ryan/FAQS/timing.html
152
#endif
153

154
#ifdef TARGET_LINUX
155
	if(std::locale().name() == "C"){
156
		try{
157
            std::locale::global(std::locale("C.UTF-8"));
158
		}catch(...){
159
			if(ofToLower(std::locale("").name()).find("utf-8")==std::string::npos){
160
				ofLogWarning("ofInit") << "Couldn't set UTF-8 locale, string manipulation functions\n"
161
						"won't work correctly for non ansi characters unless you specify a UTF-8 locale\n"
162
						"manually using std::locale::global(std::locale(\"locale\"))\n"
163
						"available locales can be queried with 'locale -a' in a terminal.";
164
			}
165
		}
166
	}
167
#endif
168

169
#if defined(TARGET_WIN32) && !_MSC_VER //MSYS2 UTF-8 limited support
170
    setlocale(LC_ALL,"");
171
    ofLogWarning("ofInit") << "MSYS2 has limited support for UTF-8. using "<< std::string( setlocale(LC_ALL,NULL) );
172
#endif
173
}
174

175
//--------------------------------------
176
shared_ptr<ofMainLoop> ofGetMainLoop(){
177
	return mainLoop();
178
}
179

180
//--------------------------------------
181
void ofSetMainLoop(const shared_ptr<ofMainLoop> & newMainLoop) {
182
	mainLoop() = newMainLoop;
183
}
184

185
//--------------------------------------
186
int ofRunApp(ofBaseApp * OFSA){
187
	mainLoop()->run(shared_ptr<ofBaseApp>(OFSA));
188
	auto ret = ofRunMainLoop();
189
#if !defined(TARGET_ANDROID) && !defined(TARGET_OF_IOS)
190
	ofExitCallback();
191
#endif
192
	return ret;
193
}
194

195
//--------------------------------------
196
int ofRunApp(shared_ptr<ofBaseApp> && app){
197
	mainLoop()->run(std::move(app));
198
	auto ret = ofRunMainLoop();
199
#if !defined(TARGET_ANDROID) && !defined(TARGET_OF_IOS)
200
	ofExitCallback();
201
#endif
202
	return ret;
203
}
204

205
//--------------------------------------
206
void ofRunApp(const shared_ptr<ofAppBaseWindow> & window, shared_ptr<ofBaseApp> && app){
207
	mainLoop()->run(window, std::move(app));
208
}
209

210
int ofRunMainLoop(){
211
	auto ret = mainLoop()->loop();
212
	return ret;
213
}
214

215
//--------------------------------------
216
void ofSetupOpenGL(int w, int h, ofWindowMode screenMode){
217
#ifdef TARGET_OPENGLES
218
	ofGLESWindowSettings settings;
219
	settings.glesVersion = 1;
220
#else
221
	ofGLWindowSettings settings;
222
	settings.glVersionMajor = 2;
223
	settings.glVersionMinor = 1;
224
#endif
225

226
	settings.setSize(w, h);
227
	settings.windowMode = screenMode;
228
	ofCreateWindow(settings);
229
}
230

231
shared_ptr<ofAppBaseWindow> ofCreateWindow(const ofWindowSettings & settings){
232
	ofInit();
233
	return mainLoop()->createWindow(settings);
234
}
235

236
//-----------------------	gets called when the app exits
237
//							currently looking at who to turn off
238
//							at the end of the application
239

240
void ofExitCallback(){
241
	if(!initialized()) return;
242

243
	// controlled destruction of the mainLoop before
244
	// any other deinitialization
245
	mainLoop()->exit();
246

247
	// everything should be destroyed here, except for
248
	// static objects
249

250

251
	// finish every library and subsystem
252
	ofURLFileLoaderShutdown();
253

254
	#ifndef TARGET_NO_SOUND
255
		//------------------------
256
		// try to close engine if needed:
257
		ofSoundShutdown();
258
		//------------------------
259
	#endif
260

261
	// try to close quicktime, for non-linux systems:
262
	#if defined(OF_VIDEO_CAPTURE_QUICKTIME) || defined(OF_VIDEO_PLAYER_QUICKTIME)
263
	closeQuicktime();
264
	#endif
265

266

267
	//------------------------
268
	// try to close freeImage:
269
	ofCloseFreeImage();
270

271

272
	#ifdef WIN32_HIGH_RES_TIMING
273
		timeEndPeriod(1);
274
	#endif
275

276
	//------------------------
277
	// try to close gstreamer
278
	#ifdef TARGET_LINUX
279
		ofGstUtils::quitGstMainLoop();
280
	#endif
281

282
	//------------------------
283
	// try to close font libraries
284
	ofTrueTypeShutdown();
285

286
	// static deinitialization happens after this finishes
287
	// every object should have ended by now and won't receive any
288
	// events
289
	of::priv::endutils();
290

291
	initialized() = false;
292
	exiting() = true;
293
}
294

295
//--------------------------------------
296
// core events instance & arguments
297
ofCoreEvents & ofEvents(){
298
	auto window = mainLoop()->getCurrentWindow();
299
	if(window){
300
		return window->events();
301
	}else{
302
		if(!exiting()){
303
			ofLogError("ofEvents") << "Trying to call ofEvents() before a window has been setup";
304
			ofLogError("ofEvents") << "We'll return a void events instance to avoid crashes but somethings might not work";
305
			ofLogError("ofEvents") << "Set a breakpoint in " << __FILE__ << " line " << __LINE__ << " to check where is the wrong call";
306
		}
307
		return noopEvents();
308
	}
309
}
310

311
//--------------------------------------
312
void ofSetEscapeQuitsApp(bool bQuitOnEsc){
313
	mainLoop()->setEscapeQuitsLoop(bQuitOnEsc);
314
}
315

316
//--------------------------------------
317
shared_ptr<ofBaseRenderer> & ofGetCurrentRenderer(){
318
	return mainLoop()->getCurrentWindow()->renderer();
319
}
320

321
//--------------------------------------
322
ofBaseApp * ofGetAppPtr(){
323
	return mainLoop()->getCurrentApp().get();
324
}
325

326
//--------------------------------------
327
std::thread::id ofGetMainThreadId() {
328
	return ofGetMainLoop()->get_thread_id() ;
329
}
330

331
bool ofIsCurrentThreadTheMainThread() {
332
	return ofGetMainThreadId() == std::this_thread::get_id();
333
}
334

335
//--------------------------------------
336
ofAppBaseWindow * ofGetWindowPtr(){
337
	return mainLoop()->getCurrentWindow().get();
338
}
339

340
//--------------------------------------
341
std::shared_ptr<ofAppBaseWindow> ofGetCurrentWindow() {
342
	return mainLoop()->getCurrentWindow();
343
}
344

345
//--------------------------------------
346
void ofExit(int status){
347
	mainLoop()->shouldClose(status);
348
}
349

350
//--------------------------------------
351
void ofHideCursor(){
352
	mainLoop()->getCurrentWindow()->hideCursor();
353
}
354

355
//--------------------------------------
356
void ofShowCursor(){
357
	mainLoop()->getCurrentWindow()->showCursor();
358
}
359

360
//--------------------------------------
361
void ofSetOrientation(ofOrientation orientation, bool vFlip){
362
	mainLoop()->getCurrentWindow()->setOrientation(orientation);
363
	// TODO: every window should set orientation on it's renderer
364
	if(ofGetCurrentRenderer()){
365
		ofGetCurrentRenderer()->setOrientation(orientation,vFlip);
366
	}
367
}
368

369
//--------------------------------------
370
ofOrientation ofGetOrientation(){
371
	return mainLoop()->getCurrentWindow()->getOrientation();
372
}
373

374
//--------------------------------------
375
void ofSetWindowPosition(int x, int y){
376
	mainLoop()->getCurrentWindow()->setWindowPosition(x,y);
377
}
378

379
//--------------------------------------
380
void ofSetWindowShape(int width, int height){
381
	mainLoop()->getCurrentWindow()->setWindowShape(width, height);
382
}
383

384
//--------------------------------------
385
int ofGetWindowPositionX(){
386
	return (int)mainLoop()->getCurrentWindow()->getWindowPosition().x;
387
}
388

389
//--------------------------------------
390
int ofGetWindowPositionY(){
391
	return (int)mainLoop()->getCurrentWindow()->getWindowPosition().y;
392
}
393

394
//--------------------------------------
395
int ofGetScreenWidth(){
396
	return (int)mainLoop()->getCurrentWindow()->getScreenSize().x;
397
}
398

399
//--------------------------------------
400
int ofGetScreenHeight(){
401
	return (int)mainLoop()->getCurrentWindow()->getScreenSize().y;
402
}
403

404
//--------------------------------------------------
405
int ofGetWidth(){
406
	return (int)mainLoop()->getCurrentWindow()->getWidth();
407
}
408
//--------------------------------------------------
409
int ofGetHeight(){
410
	return (int)mainLoop()->getCurrentWindow()->getHeight();
411
}
412

413
//--------------------------------------------------
414
int ofGetWindowWidth(){
415
	return (int)mainLoop()->getCurrentWindow()->getWindowSize().x;
416
}
417
//--------------------------------------------------
418
int ofGetWindowHeight(){
419
	return (int)mainLoop()->getCurrentWindow()->getWindowSize().y;
420
}
421

422
//--------------------------------------------------
423
std::string ofGetClipboardString(){
424
	return mainLoop()->getCurrentWindow()->getClipboardString();
425
}
426

427
//--------------------------------------------------
428
void ofSetClipboardString(const std::string & str){
429
	mainLoop()->getCurrentWindow()->setClipboardString(str);
430
}
431

432
//--------------------------------------------------
433
bool ofDoesHWOrientation(){
434
	return mainLoop()->getCurrentWindow()->doesHWOrientation();
435
}
436

437
//--------------------------------------------------
438
glm::vec2 ofGetWindowSize() {
439
	//this can't return glm::vec2(ofGetWidth(), ofGetHeight()) as width and height change based on orientation.
440
	return mainLoop()->getCurrentWindow()->getWindowSize();
441
}
442

443
//--------------------------------------------------
444
float ofRandomWidth() {
445
	return ofRandom(0.f, ofGetWidth());
446
}
447

448
//--------------------------------------------------
449
float ofRandomHeight() {
450
	return ofRandom(0.f, ofGetHeight());
451
}
452

453
//--------------------------------------------------
454
ofRectangle	ofGetWindowRect() {
455
	return ofRectangle(0, 0, ofGetWindowWidth(), ofGetWindowHeight());
456
}
457

458
//--------------------------------------
459
void ofSetWindowTitle(std::string title){
460
	mainLoop()->getCurrentWindow()->setWindowTitle(title);
461
}
462

463
//----------------------------------------------------------
464
void ofEnableSetupScreen(){
465
	mainLoop()->getCurrentWindow()->enableSetupScreen();
466
}
467

468
//----------------------------------------------------------
469
void ofDisableSetupScreen(){
470
	mainLoop()->getCurrentWindow()->disableSetupScreen();
471
}
472

473
//--------------------------------------
474
void ofToggleFullscreen(){
475
	mainLoop()->getCurrentWindow()->toggleFullscreen();
476
}
477

478
//--------------------------------------
479
void ofSetFullscreen(bool fullscreen){
480
	mainLoop()->getCurrentWindow()->setFullscreen(fullscreen);
481
}
482

483
//--------------------------------------
484
int ofGetWindowMode(){
485
	return mainLoop()->getCurrentWindow()->getWindowMode();
486
}
487

488
//--------------------------------------
489
void ofSetVerticalSync(bool bSync){
490
	mainLoop()->getCurrentWindow()->setVerticalSync(bSync);
491
}
492

493
//-------------------------- native window handles
494
#if defined(TARGET_LINUX) && !defined(TARGET_RASPBERRY_PI_LEGACY)
495
Display* ofGetX11Display(){
496
	return mainLoop()->getCurrentWindow()->getX11Display();
497
}
498

499
Window  ofGetX11Window(){
500
	return mainLoop()->getCurrentWindow()->getX11Window();
501
}
502
#endif
503

504
#if defined(TARGET_LINUX) && !defined(TARGET_OPENGLES)
505
GLXContext ofGetGLXContext(){
506
	return mainLoop()->getCurrentWindow()->getGLXContext();
507
}
508
#endif
509

510
#if defined(TARGET_LINUX) && defined(TARGET_OPENGLES)
511
EGLDisplay ofGetEGLDisplay(){
512
	return mainLoop()->getCurrentWindow()->getEGLDisplay();
513
}
514

515
EGLContext ofGetEGLContext(){
516
	return mainLoop()->getCurrentWindow()->getEGLContext();
517
}
518
EGLSurface ofGetEGLSurface(){
519
	return mainLoop()->getCurrentWindow()->getEGLSurface();
520
}
521
#endif
522

523
#if defined(TARGET_OSX)
524
void * ofGetNSGLContext(){
525
	return mainLoop()->getCurrentWindow()->getNSGLContext();
526
}
527

528
void * ofGetCocoaWindow(){
529
	return mainLoop()->getCurrentWindow()->getCocoaWindow();
530
}
531
#endif
532

533
#if defined(TARGET_WIN32)
534
HGLRC ofGetWGLContext(){
535
	return mainLoop()->getCurrentWindow()->getWGLContext();
536
}
537

538
HWND ofGetWin32Window(){
539
	return mainLoop()->getCurrentWindow()->getWin32Window();
540
}
541
#endif
542

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.