framework2
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
23using 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//--------------------------------------
31void ofSetupOpenGL(const shared_ptr<ofAppGLFWWindow> & windowPtr, int w, int h, ofWindowMode screenMode){
32ofInit();
33auto settings = windowPtr->getSettings();
34settings.setSize(w,h);
35settings.windowMode = screenMode;
36ofGetMainLoop()->addWindow(windowPtr);
37windowPtr->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//--------------------------------------
57namespace{
58
59shared_ptr<ofMainLoop> & mainLoop(){
60static shared_ptr<ofMainLoop> * mainLoop(new shared_ptr<ofMainLoop>(new ofMainLoop));
61return *mainLoop;
62}
63
64bool & initialized(){
65static bool * initialized = new bool(false);
66return *initialized;
67}
68
69bool & exiting(){
70static bool * exiting = new bool(false);
71return *exiting;
72}
73
74ofCoreEvents & noopEvents(){
75static auto * noopEvents = new ofCoreEvents();
76return *noopEvents;
77}
78
79#if defined(TARGET_LINUX) || defined(TARGET_OSX)
80#include <signal.h>
81#include <string.h>
82void ofSignalHandler(int signum){
83char* pSignalString = strsignal(signum);
84
85if(pSignalString){
86ofLogVerbose("ofSignalHandler") << pSignalString;
87}else{
88ofLogVerbose("ofSignalHandler") << "Unknown: " << signum;
89}
90
91signal(SIGTERM, nullptr);
92signal(SIGQUIT, nullptr);
93signal(SIGINT, nullptr);
94signal(SIGHUP, nullptr);
95signal(SIGABRT, nullptr);
96
97if(mainLoop()){
98mainLoop()->shouldClose(signum);
99}
100}
101#endif
102}
103
104
105void ofExitCallback();
106void ofURLFileLoaderShutdown();
107void ofTrueTypeShutdown();
108void ofCloseFreeImage();
109
110#if defined(TARGET_ANDROID) || defined (TARGET_LINUX_ARM)
111inline void ofSoundShutdown(){}
112#else
113void ofSoundShutdown();
114#endif
115
116
117void ofInit(){
118if(initialized()) return;
119initialized() = true;
120exiting() = false;
121
122#if defined(TARGET_ANDROID) || defined(TARGET_OF_IOS)
123// manage own exit
124#else
125atexit(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
130signal(SIGTERM, &ofSignalHandler);
131signal(SIGQUIT, &ofSignalHandler);
132signal(SIGINT, &ofSignalHandler);
133
134signal(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
137signal(SIGABRT, &ofSignalHandler); // abort signal
138#endif
139
140of::priv::initutils();
141of::priv::initfileutils();
142
143#ifdef WIN32_HIGH_RES_TIMING
144timeBeginPeriod(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
155if(std::locale().name() == "C"){
156try{
157std::locale::global(std::locale("C.UTF-8"));
158}catch(...){
159if(ofToLower(std::locale("").name()).find("utf-8")==std::string::npos){
160ofLogWarning("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
170setlocale(LC_ALL,"");
171ofLogWarning("ofInit") << "MSYS2 has limited support for UTF-8. using "<< std::string( setlocale(LC_ALL,NULL) );
172#endif
173}
174
175//--------------------------------------
176shared_ptr<ofMainLoop> ofGetMainLoop(){
177return mainLoop();
178}
179
180//--------------------------------------
181void ofSetMainLoop(const shared_ptr<ofMainLoop> & newMainLoop) {
182mainLoop() = newMainLoop;
183}
184
185//--------------------------------------
186int ofRunApp(ofBaseApp * OFSA){
187mainLoop()->run(shared_ptr<ofBaseApp>(OFSA));
188auto ret = ofRunMainLoop();
189#if !defined(TARGET_ANDROID) && !defined(TARGET_OF_IOS)
190ofExitCallback();
191#endif
192return ret;
193}
194
195//--------------------------------------
196int ofRunApp(shared_ptr<ofBaseApp> && app){
197mainLoop()->run(std::move(app));
198auto ret = ofRunMainLoop();
199#if !defined(TARGET_ANDROID) && !defined(TARGET_OF_IOS)
200ofExitCallback();
201#endif
202return ret;
203}
204
205//--------------------------------------
206void ofRunApp(const shared_ptr<ofAppBaseWindow> & window, shared_ptr<ofBaseApp> && app){
207mainLoop()->run(window, std::move(app));
208}
209
210int ofRunMainLoop(){
211auto ret = mainLoop()->loop();
212return ret;
213}
214
215//--------------------------------------
216void ofSetupOpenGL(int w, int h, ofWindowMode screenMode){
217#ifdef TARGET_OPENGLES
218ofGLESWindowSettings settings;
219settings.glesVersion = 1;
220#else
221ofGLWindowSettings settings;
222settings.glVersionMajor = 2;
223settings.glVersionMinor = 1;
224#endif
225
226settings.setSize(w, h);
227settings.windowMode = screenMode;
228ofCreateWindow(settings);
229}
230
231shared_ptr<ofAppBaseWindow> ofCreateWindow(const ofWindowSettings & settings){
232ofInit();
233return 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
240void ofExitCallback(){
241if(!initialized()) return;
242
243// controlled destruction of the mainLoop before
244// any other deinitialization
245mainLoop()->exit();
246
247// everything should be destroyed here, except for
248// static objects
249
250
251// finish every library and subsystem
252ofURLFileLoaderShutdown();
253
254#ifndef TARGET_NO_SOUND
255//------------------------
256// try to close engine if needed:
257ofSoundShutdown();
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)
263closeQuicktime();
264#endif
265
266
267//------------------------
268// try to close freeImage:
269ofCloseFreeImage();
270
271
272#ifdef WIN32_HIGH_RES_TIMING
273timeEndPeriod(1);
274#endif
275
276//------------------------
277// try to close gstreamer
278#ifdef TARGET_LINUX
279ofGstUtils::quitGstMainLoop();
280#endif
281
282//------------------------
283// try to close font libraries
284ofTrueTypeShutdown();
285
286// static deinitialization happens after this finishes
287// every object should have ended by now and won't receive any
288// events
289of::priv::endutils();
290
291initialized() = false;
292exiting() = true;
293}
294
295//--------------------------------------
296// core events instance & arguments
297ofCoreEvents & ofEvents(){
298auto window = mainLoop()->getCurrentWindow();
299if(window){
300return window->events();
301}else{
302if(!exiting()){
303ofLogError("ofEvents") << "Trying to call ofEvents() before a window has been setup";
304ofLogError("ofEvents") << "We'll return a void events instance to avoid crashes but somethings might not work";
305ofLogError("ofEvents") << "Set a breakpoint in " << __FILE__ << " line " << __LINE__ << " to check where is the wrong call";
306}
307return noopEvents();
308}
309}
310
311//--------------------------------------
312void ofSetEscapeQuitsApp(bool bQuitOnEsc){
313mainLoop()->setEscapeQuitsLoop(bQuitOnEsc);
314}
315
316//--------------------------------------
317shared_ptr<ofBaseRenderer> & ofGetCurrentRenderer(){
318return mainLoop()->getCurrentWindow()->renderer();
319}
320
321//--------------------------------------
322ofBaseApp * ofGetAppPtr(){
323return mainLoop()->getCurrentApp().get();
324}
325
326//--------------------------------------
327std::thread::id ofGetMainThreadId() {
328return ofGetMainLoop()->get_thread_id() ;
329}
330
331bool ofIsCurrentThreadTheMainThread() {
332return ofGetMainThreadId() == std::this_thread::get_id();
333}
334
335//--------------------------------------
336ofAppBaseWindow * ofGetWindowPtr(){
337return mainLoop()->getCurrentWindow().get();
338}
339
340//--------------------------------------
341std::shared_ptr<ofAppBaseWindow> ofGetCurrentWindow() {
342return mainLoop()->getCurrentWindow();
343}
344
345//--------------------------------------
346void ofExit(int status){
347mainLoop()->shouldClose(status);
348}
349
350//--------------------------------------
351void ofHideCursor(){
352mainLoop()->getCurrentWindow()->hideCursor();
353}
354
355//--------------------------------------
356void ofShowCursor(){
357mainLoop()->getCurrentWindow()->showCursor();
358}
359
360//--------------------------------------
361void ofSetOrientation(ofOrientation orientation, bool vFlip){
362mainLoop()->getCurrentWindow()->setOrientation(orientation);
363// TODO: every window should set orientation on it's renderer
364if(ofGetCurrentRenderer()){
365ofGetCurrentRenderer()->setOrientation(orientation,vFlip);
366}
367}
368
369//--------------------------------------
370ofOrientation ofGetOrientation(){
371return mainLoop()->getCurrentWindow()->getOrientation();
372}
373
374//--------------------------------------
375void ofSetWindowPosition(int x, int y){
376mainLoop()->getCurrentWindow()->setWindowPosition(x,y);
377}
378
379//--------------------------------------
380void ofSetWindowShape(int width, int height){
381mainLoop()->getCurrentWindow()->setWindowShape(width, height);
382}
383
384//--------------------------------------
385int ofGetWindowPositionX(){
386return (int)mainLoop()->getCurrentWindow()->getWindowPosition().x;
387}
388
389//--------------------------------------
390int ofGetWindowPositionY(){
391return (int)mainLoop()->getCurrentWindow()->getWindowPosition().y;
392}
393
394//--------------------------------------
395int ofGetScreenWidth(){
396return (int)mainLoop()->getCurrentWindow()->getScreenSize().x;
397}
398
399//--------------------------------------
400int ofGetScreenHeight(){
401return (int)mainLoop()->getCurrentWindow()->getScreenSize().y;
402}
403
404//--------------------------------------------------
405int ofGetWidth(){
406return (int)mainLoop()->getCurrentWindow()->getWidth();
407}
408//--------------------------------------------------
409int ofGetHeight(){
410return (int)mainLoop()->getCurrentWindow()->getHeight();
411}
412
413//--------------------------------------------------
414int ofGetWindowWidth(){
415return (int)mainLoop()->getCurrentWindow()->getWindowSize().x;
416}
417//--------------------------------------------------
418int ofGetWindowHeight(){
419return (int)mainLoop()->getCurrentWindow()->getWindowSize().y;
420}
421
422//--------------------------------------------------
423std::string ofGetClipboardString(){
424return mainLoop()->getCurrentWindow()->getClipboardString();
425}
426
427//--------------------------------------------------
428void ofSetClipboardString(const std::string & str){
429mainLoop()->getCurrentWindow()->setClipboardString(str);
430}
431
432//--------------------------------------------------
433bool ofDoesHWOrientation(){
434return mainLoop()->getCurrentWindow()->doesHWOrientation();
435}
436
437//--------------------------------------------------
438glm::vec2 ofGetWindowSize() {
439//this can't return glm::vec2(ofGetWidth(), ofGetHeight()) as width and height change based on orientation.
440return mainLoop()->getCurrentWindow()->getWindowSize();
441}
442
443//--------------------------------------------------
444float ofRandomWidth() {
445return ofRandom(0.f, ofGetWidth());
446}
447
448//--------------------------------------------------
449float ofRandomHeight() {
450return ofRandom(0.f, ofGetHeight());
451}
452
453//--------------------------------------------------
454ofRectangle ofGetWindowRect() {
455return ofRectangle(0, 0, ofGetWindowWidth(), ofGetWindowHeight());
456}
457
458//--------------------------------------
459void ofSetWindowTitle(std::string title){
460mainLoop()->getCurrentWindow()->setWindowTitle(title);
461}
462
463//----------------------------------------------------------
464void ofEnableSetupScreen(){
465mainLoop()->getCurrentWindow()->enableSetupScreen();
466}
467
468//----------------------------------------------------------
469void ofDisableSetupScreen(){
470mainLoop()->getCurrentWindow()->disableSetupScreen();
471}
472
473//--------------------------------------
474void ofToggleFullscreen(){
475mainLoop()->getCurrentWindow()->toggleFullscreen();
476}
477
478//--------------------------------------
479void ofSetFullscreen(bool fullscreen){
480mainLoop()->getCurrentWindow()->setFullscreen(fullscreen);
481}
482
483//--------------------------------------
484int ofGetWindowMode(){
485return mainLoop()->getCurrentWindow()->getWindowMode();
486}
487
488//--------------------------------------
489void ofSetVerticalSync(bool bSync){
490mainLoop()->getCurrentWindow()->setVerticalSync(bSync);
491}
492
493//-------------------------- native window handles
494#if defined(TARGET_LINUX) && !defined(TARGET_RASPBERRY_PI_LEGACY)
495Display* ofGetX11Display(){
496return mainLoop()->getCurrentWindow()->getX11Display();
497}
498
499Window ofGetX11Window(){
500return mainLoop()->getCurrentWindow()->getX11Window();
501}
502#endif
503
504#if defined(TARGET_LINUX) && !defined(TARGET_OPENGLES)
505GLXContext ofGetGLXContext(){
506return mainLoop()->getCurrentWindow()->getGLXContext();
507}
508#endif
509
510#if defined(TARGET_LINUX) && defined(TARGET_OPENGLES)
511EGLDisplay ofGetEGLDisplay(){
512return mainLoop()->getCurrentWindow()->getEGLDisplay();
513}
514
515EGLContext ofGetEGLContext(){
516return mainLoop()->getCurrentWindow()->getEGLContext();
517}
518EGLSurface ofGetEGLSurface(){
519return mainLoop()->getCurrentWindow()->getEGLSurface();
520}
521#endif
522
523#if defined(TARGET_OSX)
524void * ofGetNSGLContext(){
525return mainLoop()->getCurrentWindow()->getNSGLContext();
526}
527
528void * ofGetCocoaWindow(){
529return mainLoop()->getCurrentWindow()->getCocoaWindow();
530}
531#endif
532
533#if defined(TARGET_WIN32)
534HGLRC ofGetWGLContext(){
535return mainLoop()->getCurrentWindow()->getWGLContext();
536}
537
538HWND ofGetWin32Window(){
539return mainLoop()->getCurrentWindow()->getWin32Window();
540}
541#endif
542