framework2

Форк
0
910 строк · 24.6 Кб
1
#include "ofAppGlutWindow.h"
2
#include "ofBaseApp.h"
3
#include "ofPixels.h"
4
#include "ofGLRenderer.h"
5

6
#ifdef TARGET_WIN32
7
	#if (_MSC_VER)
8
		#define GLUT_BUILDING_LIB
9
		#include "glut.h"
10
	#else
11
		#include <GL/glut.h>
12
		#include <GL/freeglut_ext.h>
13
	#endif
14
	#include <Shellapi.h>
15
#endif
16
#ifdef TARGET_OSX
17
    #include <OpenGL/OpenGL.h>
18
	#include "../../../libs/glut/lib/osx/GLUT.framework/Versions/A/Headers/glut.h"
19
    #include <Cocoa/Cocoa.h>
20
#endif
21
#ifdef TARGET_LINUX
22
	#include <GL/glut.h>
23
	#include "ofIcon.h"
24
	#include "ofImage.h"
25
	#include <X11/Xatom.h>
26
	#include <GL/freeglut_ext.h>
27
	#include <GL/glx.h>
28
#endif
29

30
// glut works with static callbacks UGH, so we need static variables here:
31

32
static ofWindowMode windowMode;
33
static bool			bNewScreenMode;
34
static int			buttonInUse;
35
static bool			bEnableSetupScreen;
36
static bool			bDoubleBuffered;
37

38
static int			requestedWidth;
39
static int			requestedHeight;
40
static int 			nonFullScreenX;
41
static int 			nonFullScreenY;
42
static int			windowW;
43
static int			windowH;
44
static int          nFramesSinceWindowResized;
45
static ofOrientation	orientation;
46
static ofAppGlutWindow * instance;
47

48
#ifdef TARGET_WIN32
49

50
//------------------------------------------------
51

52
// this is to fix a bug with glut that doesn't properly close the app
53
// with window closing.  we grab the window procedure, store it, and parse windows messages,
54
// using the close and destroy messages and passing on the others...
55

56
//------------------------------------------------
57

58
static WNDPROC currentWndProc;
59
static HWND handle  = nullptr;
60

61
// This function takes in a wParam from the WM_DROPFILES message and
62
// prints all the files to a message box.
63

64
void HandleFiles(WPARAM wParam)
65
{
66
    // DragQueryFile() takes a LPWSTR for the name so we need a TCHAR string
67
    TCHAR szName[MAX_PATH];
68

69
    // Here we cast the wParam as a HDROP handle to pass into the next functions
70
    HDROP hDrop = (HDROP)wParam;
71

72
	POINT pt;
73
	DragQueryPoint(hDrop, &pt);
74
	//ofLogNotice("ofAppGlutWindow") << "drag point: " << pt.x << pt.y;
75

76
	ofDragInfo info;
77
	info.position.x = pt.x;
78
	info.position.y = pt.y;
79

80

81
    // This functions has a couple functionalities.  If you pass in 0xFFFFFFFF in
82
    // the second parameter then it returns the count of how many filers were drag
83
    // and dropped.  Otherwise, the function fills in the szName string array with
84
    // the current file being queried.
85
    int count = DragQueryFile(hDrop, 0xFFFFFFFF, szName, MAX_PATH);
86

87
    // Here we go through all the files that were drag and dropped then display them
88
    for(int i = 0; i < count; i++)
89
    {
90
        // Grab the name of the file associated with index "i" in the list of files dropped.
91
        // Be sure you know that the name is attached to the FULL path of the file.
92
        DragQueryFile(hDrop, i, szName, MAX_PATH);
93

94
		wchar_t * s =  (wchar_t*)szName;
95
		char dfault = '?';
96
        const std::locale& loc = std::locale();
97
		std::ostringstream stm;
98
		while( *s != L'\0' ) {
99
			stm << std::use_facet< std::ctype<wchar_t> >( loc ).narrow( *s++, dfault );
100
		}
101
		info.files.push_back(std::string(stm.str()));
102

103
			//toUTF8(udispName, dispName);
104

105
        // Bring up a message box that displays the current file being processed
106
        //MessageBox(GetForegroundWindow(), szName, L"Current file received", MB_OK);
107
    }
108

109
    // Finally, we destroy the HDROP handle so the extra memory
110
    // allocated by the application is released.
111
    DragFinish(hDrop);
112

113
	instance->events().notifyDragEvent(info);
114

115
}
116

117

118
static LRESULT CALLBACK winProc(HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam){
119

120
   //we catch close and destroy messages
121
   //and send them to OF
122

123
   switch(Msg){
124

125
      case WM_CLOSE:
126
         OF_EXIT_APP(0);
127
      break;
128
      case WM_DESTROY:
129
         OF_EXIT_APP(0);
130
         break;
131
	  case WM_DROPFILES:
132

133
            // Call our function we created to display all the files.
134
            // We pass the wParam because it's the HDROP handle.
135
            HandleFiles(wParam);
136
            break;
137
      default:
138
         return CallWindowProc(currentWndProc, handle, Msg, wParam, lParam);
139
      break;
140
    }
141

142
    return 0;
143
}
144

145
//--------------------------------------
146
static void fixCloseWindowOnWin32(){
147

148
	//get the HWND
149
	handle = WindowFromDC(wglGetCurrentDC());
150

151
	// enable drag and drop of files.
152
	DragAcceptFiles (handle, TRUE);
153

154
	//store the current message event handler for the window
155
	currentWndProc = (WNDPROC)GetWindowLongPtr(handle, GWLP_WNDPROC);
156

157
	//tell the window to now use our event handler!
158
	SetWindowLongPtr(handle, GWLP_WNDPROC, (LONG_PTR)winProc);
159
}
160

161
#endif
162

163

164

165

166
//----------------------------------------------------------
167
ofAppGlutWindow::ofAppGlutWindow(){
168
	windowMode			= OF_WINDOW;
169
	bNewScreenMode		= true;
170
	nFramesSinceWindowResized = 0;
171
	buttonInUse			= 0;
172
	bEnableSetupScreen	= true;
173
	requestedWidth		= 0;
174
	requestedHeight		= 0;
175
	nonFullScreenX		= -1;
176
	nonFullScreenY		= -1;
177
	displayString		= "";
178
	orientation			= OF_ORIENTATION_DEFAULT;
179
	bDoubleBuffered = true; // LIA
180
	iconSet = false;
181
	instance = this;
182
	windowId = 0;
183
}
184

185
//lets you enable alpha blending using a display string like:
186
// "rgba double samples>=4 depth" ( mac )
187
// "rgb double depth alpha samples>=4" ( some pcs )
188
//------------------------------------------------------------
189
 void ofAppGlutWindow::setGlutDisplayString(std::string displayStr){
190
	displayString = displayStr;
191
 }
192

193
 //------------------------------------------------------------
194
void ofAppGlutWindow::setDoubleBuffering(bool _bDoubleBuffered){
195
	bDoubleBuffered = _bDoubleBuffered;
196
}
197

198
//------------------------------------------------------------
199
void ofAppGlutWindow::setup(const ofGLWindowSettings & settings){
200

201
	int argc = 1;
202
	char *argv = (char*)"openframeworks";
203
	char **vptr = &argv;
204
	glutInit(&argc, vptr);
205

206
	if( displayString != ""){
207
		glutInitDisplayString( displayString.c_str() );
208
	}else{
209
		if(bDoubleBuffered){
210
			glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_ALPHA );
211
		}else{
212
			glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH | GLUT_ALPHA );
213
		}
214
	}
215

216
	windowMode = settings.windowMode;
217
	bNewScreenMode = true;
218

219
	if (windowMode == OF_FULLSCREEN){
220
		glutInitWindowSize(glutGet(GLUT_SCREEN_WIDTH), glutGet(GLUT_SCREEN_HEIGHT));
221
		windowId = glutCreateWindow("");
222

223
		requestedWidth  = settings.getWidth();
224
		requestedHeight = settings.getHeight();
225
	} else if (windowMode != OF_GAME_MODE){
226
		glutInitWindowSize(settings.getWidth(), settings.getHeight());
227
		glutCreateWindow("");
228

229
		/*
230
		ofBackground(200,200,200);		// default bg color
231
		ofSetColor(0xFFFFFF); 			// default draw color
232
		// used to be black, but
233
		// black + texture = black
234
		// so maybe grey bg
235
		// and "white" fg color
236
		// as default works the best...
237
		*/
238

239
		requestedWidth  = glutGet(GLUT_WINDOW_WIDTH);
240
		requestedHeight = glutGet(GLUT_WINDOW_HEIGHT);
241
	} else {
242
		if( displayString != ""){
243
			glutInitDisplayString( displayString.c_str() );
244
		}else{
245
			glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_ALPHA );
246
		}
247

248
    	// w x h, 32bit pixel depth, 60Hz refresh rate
249
		char gameStr[64];
250
		sprintf( gameStr, "%dx%d:%d@%d", settings.getWidth(), settings.getHeight(), 32, 60 );
251

252
    	glutGameModeString(gameStr);
253

254
    	if (!glutGameModeGet(GLUT_GAME_MODE_POSSIBLE)){
255
    		ofLogError("ofAppGlutWindow") << "setupOpenGL(): selected game mode format " << gameStr << " not available";
256
    	}
257
    	// start fullscreen game mode
258
    	glutEnterGameMode();
259
	}
260
	windowW = glutGet(GLUT_WINDOW_WIDTH);
261
	windowH = glutGet(GLUT_WINDOW_HEIGHT);
262

263
	currentRenderer = std::shared_ptr<ofBaseRenderer>(new ofGLRenderer(this));
264

265

266
#ifndef TARGET_OPENGLES
267
	glewExperimental = GL_TRUE;
268
	GLenum err = glewInit();
269
	if (GLEW_OK != err)
270
	{
271
		/* Problem: glewInit failed, something is seriously wrong. */
272
		ofLogError("ofAppRunner") << "couldn't init GLEW: " << glewGetErrorString(err);
273
		return;
274
	}
275
#endif
276
	static_cast<ofGLRenderer*>(currentRenderer.get())->setup();
277
	setVerticalSync(true);
278

279

280
    //----------------------
281
    // setup the callbacks
282

283
    glutMouseFunc(mouse_cb);
284
    glutMotionFunc(motion_cb);
285
    glutPassiveMotionFunc(passive_motion_cb);
286
    glutIdleFunc(idle_cb);
287
    glutDisplayFunc(display);
288

289
    glutKeyboardFunc(keyboard_cb);
290
    glutKeyboardUpFunc(keyboard_up_cb);
291
    glutSpecialFunc(special_key_cb);
292
    glutSpecialUpFunc(special_key_up_cb);
293

294
    glutReshapeFunc(resize_cb);
295
	glutEntryFunc(entry_cb);
296
#ifdef TARGET_LINUX
297
	glutCloseFunc(exit_cb);
298
#endif
299

300
#ifdef TARGET_OSX
301
	glutDragEventFunc(dragEvent);
302
#endif
303

304
    nFramesSinceWindowResized = 0;
305

306
    #ifdef TARGET_WIN32
307
        //----------------------
308
        // this is specific to windows (respond properly to close / destroy)
309
        fixCloseWindowOnWin32();
310
    #endif
311

312
#ifdef TARGET_LINUX
313
    if(!iconSet){
314
		ofPixels iconPixels;
315
		#ifdef DEBUG
316
			iconPixels.allocate(ofIconDebug.width,ofIconDebug.height,ofIconDebug.bytes_per_pixel);
317
			GIMP_IMAGE_RUN_LENGTH_DECODE(iconPixels.getData(),ofIconDebug.rle_pixel_data,iconPixels.getWidth()*iconPixels.getHeight(),ofIconDebug.bytes_per_pixel);
318
		#else
319
			iconPixels.allocate(ofIcon.width,ofIcon.height,ofIcon.bytes_per_pixel);
320
			GIMP_IMAGE_RUN_LENGTH_DECODE(iconPixels.getData(),ofIcon.rle_pixel_data,iconPixels.getWidth()*iconPixels.getHeight(),ofIcon.bytes_per_pixel);
321
		#endif
322
		setWindowIcon(iconPixels);
323
    }
324
#endif
325
	if (settings.isPositionSet()) {
326
		setWindowPosition(settings.getPosition().x,settings.getPosition().y);
327
	}
328

329
#ifdef TARGET_OSX
330
	// The osx implementation of glut changes the cwd, this restores it
331
	// to wherever it was when the app was started
332
	ofRestoreWorkingDirectoryToDefault();
333
#endif
334
}
335

336
#ifdef TARGET_LINUX
337
//------------------------------------------------------------
338
void ofAppGlutWindow::setWindowIcon(const std::string & path){
339
    ofPixels iconPixels;
340
	ofLoadImage(iconPixels,path);
341
	setWindowIcon(iconPixels);
342
}
343

344
//------------------------------------------------------------
345
void ofAppGlutWindow::setWindowIcon(const ofPixels & iconPixels){
346
	iconSet = true;
347
	Display *m_display = glXGetCurrentDisplay();
348
	GLXDrawable m_window = glXGetCurrentDrawable();
349
	iconSet = true;
350
	int length = 2+iconPixels.getWidth()*iconPixels.getHeight();
351
	unsigned long * buffer = new unsigned long[length];
352
	buffer[0]=iconPixels.getWidth();
353
	buffer[1]=iconPixels.getHeight();
354
	for(size_t i=0;i<iconPixels.getWidth()*iconPixels.getHeight();i++){
355
		buffer[i+2] = iconPixels[i*4+3]<<24;
356
		buffer[i+2] += iconPixels[i*4]<<16;
357
		buffer[i+2] += iconPixels[i*4+1]<<8;
358
		buffer[i+2] += iconPixels[i*4];
359
	}
360

361
	XChangeProperty(m_display, m_window, XInternAtom(m_display, "_NET_WM_ICON", False), XA_CARDINAL, 32,
362
						 PropModeReplace,  (const unsigned char*)buffer,  length);
363
	delete[] buffer;
364
	XFlush(m_display);
365
}
366
#endif
367

368
//------------------------------------------------------------
369
void ofAppGlutWindow::update(){
370
	idle_cb();
371
}
372

373
//------------------------------------------------------------
374
void ofAppGlutWindow::draw(){
375
	display();
376
}
377

378
//------------------------------------------------------------
379
void ofAppGlutWindow::close(){
380
	events().notifyExit();
381
	events().disable();
382
#ifdef TARGET_LINUX
383
	glutLeaveMainLoop();
384
#else
385
	std::exit(0);
386
#endif
387
}
388

389
//------------------------------------------------------------
390
void ofAppGlutWindow::loop(){
391
	instance->events().notifySetup();
392
	instance->events().notifyUpdate();
393
	glutMainLoop();
394
}
395

396
//------------------------------------------------------------
397
void ofAppGlutWindow::setWindowTitle(std::string title){
398
	glutSetWindowTitle(title.c_str());
399
}
400

401
//------------------------------------------------------------
402
glm::vec2 ofAppGlutWindow::getWindowSize(){
403
	return {windowW, windowH};
404
}
405

406
//------------------------------------------------------------
407
glm::vec2 ofAppGlutWindow::getWindowPosition(){
408
	int x = glutGet(GLUT_WINDOW_X);
409
	int y = glutGet(GLUT_WINDOW_Y);
410
	if( orientation == OF_ORIENTATION_DEFAULT || orientation == OF_ORIENTATION_180 ){
411
		return {x,y};
412
	}else{
413
		return {y,x};
414
	}
415
}
416

417
//------------------------------------------------------------
418
glm::vec2 ofAppGlutWindow::getScreenSize(){
419
	int width = glutGet(GLUT_SCREEN_WIDTH);
420
	int height = glutGet(GLUT_SCREEN_HEIGHT);
421
	if( orientation == OF_ORIENTATION_DEFAULT || orientation == OF_ORIENTATION_180 ){
422
		return {width, height};
423
	}else{
424
		return {height, width};
425
	}
426
}
427

428
//------------------------------------------------------------
429
int ofAppGlutWindow::getWidth(){
430
	if( orientation == OF_ORIENTATION_DEFAULT || orientation == OF_ORIENTATION_180 ){
431
		return windowW;
432
	}
433
	return windowH;
434
}
435

436
//------------------------------------------------------------
437
int ofAppGlutWindow::getHeight(){
438
	if( orientation == OF_ORIENTATION_DEFAULT || orientation == OF_ORIENTATION_180 ){
439
		return windowH;
440
	}
441
	return windowW;
442
}
443

444
//------------------------------------------------------------
445
void ofAppGlutWindow::setOrientation(ofOrientation orientationIn){
446
	orientation = orientationIn;
447
}
448

449
//------------------------------------------------------------
450
ofOrientation ofAppGlutWindow::getOrientation(){
451
	return orientation;
452
}
453

454
//------------------------------------------------------------
455
void ofAppGlutWindow::setWindowPosition(int x, int y){
456
	glutPositionWindow(x,y);
457
}
458

459
//------------------------------------------------------------
460
void ofAppGlutWindow::setWindowShape(int w, int h){
461
	glutReshapeWindow(w, h);
462
	// this is useful, esp if we are in the first frame (setup):
463
	requestedWidth  = w;
464
	requestedHeight = h;
465
}
466

467
//------------------------------------------------------------
468
void ofAppGlutWindow::hideCursor(){
469
	#if defined(TARGET_OSX) && defined(MAC_OS_X_VERSION_10_7)
470
		 CGDisplayHideCursor(0);
471
	#else
472
		glutSetCursor(GLUT_CURSOR_NONE);
473
	#endif
474
}
475

476
//------------------------------------------------------------
477
void ofAppGlutWindow::showCursor(){
478
	#if defined(TARGET_OSX) && defined(MAC_OS_X_VERSION_10_7)
479
		 CGDisplayShowCursor(0);
480
	#else
481
		glutSetCursor(GLUT_CURSOR_LEFT_ARROW);
482
	#endif
483
}
484

485
//------------------------------------------------------------
486
ofWindowMode ofAppGlutWindow::getWindowMode(){
487
	return windowMode;
488
}
489

490
//------------------------------------------------------------
491
void ofAppGlutWindow::toggleFullscreen(){
492
	if( windowMode == OF_GAME_MODE)return;
493

494
	if( windowMode == OF_WINDOW ){
495
		windowMode = OF_FULLSCREEN;
496
	}else{
497
		windowMode = OF_WINDOW;
498
	}
499

500
	bNewScreenMode = true;
501
}
502

503
//------------------------------------------------------------
504
void ofAppGlutWindow::setFullscreen(bool fullscreen){
505
    if( windowMode == OF_GAME_MODE)return;
506

507
    if(fullscreen && windowMode != OF_FULLSCREEN){
508
        bNewScreenMode  = true;
509
        windowMode      = OF_FULLSCREEN;
510
    }else if(!fullscreen && windowMode != OF_WINDOW) {
511
        bNewScreenMode  = true;
512
        windowMode      = OF_WINDOW;
513
    }
514
}
515

516
//------------------------------------------------------------
517
void ofAppGlutWindow::enableSetupScreen(){
518
	bEnableSetupScreen = true;
519
}
520

521
//------------------------------------------------------------
522
void ofAppGlutWindow::disableSetupScreen(){
523
	bEnableSetupScreen = false;
524
}
525

526
//------------------------------------------------------------
527
void ofAppGlutWindow::setVerticalSync(bool bSync){
528
	//----------------------------
529
	#ifdef TARGET_WIN32
530
	//----------------------------
531
		if (bSync) {
532
			if (WGL_EXT_swap_control) {
533
				wglSwapIntervalEXT (1);
534
			}
535
		} else {
536
			if (WGL_EXT_swap_control) {
537
				wglSwapIntervalEXT (0);
538
			}
539
		}
540
	//----------------------------
541
	#endif
542
	//----------------------------
543

544
	//--------------------------------------
545
	#ifdef TARGET_OSX
546
	//--------------------------------------
547
		GLint sync = bSync == true ? 1 : 0;
548
		CGLSetParameter (CGLGetCurrentContext(), kCGLCPSwapInterval, &sync);
549
	//--------------------------------------
550
	#endif
551
	//--------------------------------------
552

553
	//--------------------------------------
554
	#ifdef TARGET_LINUX
555
	//--------------------------------------
556
		void (*swapIntervalExt)(Display *,GLXDrawable, int)	 = (void (*)(Display *,GLXDrawable, int)) glXGetProcAddress((const GLubyte*) "glXSwapIntervalEXT");
557
		if(swapIntervalExt){
558
			Display *dpy = glXGetCurrentDisplay();
559
			GLXDrawable drawable = glXGetCurrentDrawable();
560
			if (drawable) {
561
				swapIntervalExt(dpy, drawable, bSync ? 1 : 0);
562
				return;
563
			}
564
		}
565
		void (*swapInterval)(int) = (void (*)(int)) glXGetProcAddress((const GLubyte*) "glXSwapIntervalSGI");
566
		if(!swapInterval) {
567
			swapInterval = (void (*)(int)) glXGetProcAddress((const GLubyte*) "glXSwapIntervalMESA");
568
		}
569
		if(swapInterval) {
570
			swapInterval(bSync ? 1 : 0);
571
		}
572
	//--------------------------------------
573
	#endif
574
	//--------------------------------------
575
}
576

577
//------------------------------------------------------------
578
ofCoreEvents & ofAppGlutWindow::events(){
579
	return coreEvents;
580
}
581

582
//------------------------------------------------------------
583
std::shared_ptr<ofBaseRenderer> & ofAppGlutWindow::renderer(){
584
	return currentRenderer;
585
}
586

587
//------------------------------------------------------------
588
void ofAppGlutWindow::display(void){
589

590
	//--------------------------------
591
	// when I had "glutFullScreen()"
592
	// in the initOpenGl, I was gettings a "heap" allocation error
593
	// when debugging via visual studio.  putting it here, changes that.
594
	// maybe it's voodoo, or I am getting rid of the problem
595
	// by removing something unrelated, but everything seems
596
	// to work if I put fullscreen on the first frame of display.
597

598
	if (windowMode != OF_GAME_MODE){
599
		if ( bNewScreenMode ){
600
			if( windowMode == OF_FULLSCREEN){
601

602
				//----------------------------------------------------
603
				// before we go fullscreen, take a snapshot of where we are:
604
				nonFullScreenX = glutGet(GLUT_WINDOW_X);
605
				nonFullScreenY = glutGet(GLUT_WINDOW_Y);
606
				//----------------------------------------------------
607

608
				glutFullScreen();
609

610
				#ifdef TARGET_OSX
611
					[NSApp setPresentationOptions:NSApplicationPresentationHideMenuBar | NSApplicationPresentationHideDock];
612
					#ifdef MAC_OS_X_VERSION_10_7 //needed for Lion as when the machine reboots the app is not at front level
613
						if( instance->events().getFrameNum() <= 10 ){  //is this long enough? too long?
614
							[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
615
						}
616
					#endif
617
				#endif
618

619
			}else if( windowMode == OF_WINDOW ){
620

621
				glutReshapeWindow(requestedWidth, requestedHeight);
622

623
				//----------------------------------------------------
624
				// if we have recorded the screen posion, put it there
625
				// if not, better to let the system do it (and put it where it wants)
626
				if (instance->events().getFrameNum() > 0){
627
					glutPositionWindow(nonFullScreenX,nonFullScreenY);
628
				}
629
				//----------------------------------------------------
630

631
				#ifdef TARGET_OSX
632
					[NSApp setPresentationOptions:NSApplicationPresentationDefault];
633
				#endif
634
			}
635
			bNewScreenMode = false;
636
		}
637
	}
638

639
	instance->currentRenderer->startRender();
640

641
	if( bEnableSetupScreen ) instance->currentRenderer->setupScreen();
642

643
	instance->events().notifyDraw();
644

645
    #ifdef TARGET_WIN32
646
    if (instance->currentRenderer->getBackgroundAuto() == false){
647
        // on a PC resizing a window with this method of accumulation (essentially single buffering)
648
        // is BAD, so we clear on resize events.
649
        if (nFramesSinceWindowResized < 3){
650
            instance->currentRenderer->clear();
651
        } else {
652
            if ( (instance->events().getFrameNum() < 3 || nFramesSinceWindowResized < 3) && bDoubleBuffered)    glutSwapBuffers();
653
            else  glFlush();
654
        }
655
    } else {
656
        if(bDoubleBuffered){
657
			glutSwapBuffers();
658
		} else{
659
			glFlush();
660
		}
661
    }
662
    #else
663
		if (instance->currentRenderer->getBackgroundAuto() == false){
664
			// in accum mode resizing a window is BAD, so we clear on resize events.
665
			if (nFramesSinceWindowResized < 3){
666
				instance->currentRenderer->clear();
667
			}
668
		}
669
		if(bDoubleBuffered){
670
			glutSwapBuffers();
671
		} else{
672
			glFlush();
673
		}
674
    #endif
675

676
	instance->currentRenderer->finishRender();
677

678
    nFramesSinceWindowResized++;
679

680
}
681

682
//------------------------------------------------------------
683
void ofAppGlutWindow::swapBuffers() {
684
	glutSwapBuffers();
685
}
686

687
//--------------------------------------------
688
void ofAppGlutWindow::startRender() {
689
	renderer()->startRender();
690
}
691

692
//--------------------------------------------
693
void ofAppGlutWindow::finishRender() {
694
	renderer()->finishRender();
695
}
696

697
//------------------------------------------------------------
698
static void rotateMouseXY(ofOrientation orientation, int w, int h, int &x, int &y) {
699
	int savedY;
700
	switch(orientation) {
701
		case OF_ORIENTATION_180:
702
			x = w - x;
703
			y = h - y;
704
			break;
705

706
		case OF_ORIENTATION_90_RIGHT:
707
			savedY = y;
708
			y = x;
709
			x = w-savedY;
710
			break;
711

712
		case OF_ORIENTATION_90_LEFT:
713
			savedY = y;
714
			y = h - x;
715
			x = savedY;
716
			break;
717

718
		case OF_ORIENTATION_DEFAULT:
719
		default:
720
			break;
721
	}
722
}
723

724
//------------------------------------------------------------
725
void ofAppGlutWindow::mouse_cb(int button, int state, int x, int y) {
726
	rotateMouseXY(orientation, instance->getWidth(), instance->getHeight(), x, y);
727

728

729
	switch(button){
730
	case GLUT_LEFT_BUTTON:
731
		button = OF_MOUSE_BUTTON_LEFT;
732
		break;
733
	case GLUT_RIGHT_BUTTON:
734
		button = OF_MOUSE_BUTTON_RIGHT;
735
		break;
736
	case GLUT_MIDDLE_BUTTON:
737
		button = OF_MOUSE_BUTTON_MIDDLE;
738
		break;
739
	}
740

741
	if (instance->events().getFrameNum() > 0){
742
		if (state == GLUT_DOWN) {
743
			instance->events().notifyMousePressed(x, y, button);
744
		} else if (state == GLUT_UP) {
745
			instance->events().notifyMouseReleased(x, y, button);
746
		}
747

748
		buttonInUse = button;
749
	}
750
}
751

752
//------------------------------------------------------------
753
void ofAppGlutWindow::motion_cb(int x, int y) {
754
	rotateMouseXY(orientation, instance->getWidth(), instance->getHeight(), x, y);
755

756
	if (instance->events().getFrameNum() > 0){
757
		instance->events().notifyMouseDragged(x, y, buttonInUse);
758
	}
759

760
}
761

762
//------------------------------------------------------------
763
void ofAppGlutWindow::passive_motion_cb(int x, int y) {
764
	rotateMouseXY(orientation, instance->getWidth(), instance->getHeight(), x, y);
765

766
	if (instance->events().getFrameNum() > 0){
767
		instance->events().notifyMouseMoved(x, y);
768
	}
769
}
770

771
//------------------------------------------------------------
772
void ofAppGlutWindow::dragEvent(char ** names, int howManyFiles, int dragX, int dragY){
773

774
	// TODO: we need position info on mac passed through
775
	ofDragInfo info;
776
	info.position.x = dragX;
777
	info.position.y = instance->getHeight()-dragY;
778

779
	for (int i = 0; i < howManyFiles; i++){
780
		std::string temp = std::string(names[i]);
781
		info.files.push_back(temp);
782
	}
783

784
	instance->events().notifyDragEvent(info);
785
}
786

787

788
//------------------------------------------------------------
789
void ofAppGlutWindow::idle_cb(void) {
790
	instance->events().notifyUpdate();
791

792
	glutPostRedisplay();
793
}
794

795

796
//------------------------------------------------------------
797
void ofAppGlutWindow::keyboard_cb(unsigned char key, int x, int y) {
798
	instance->events().notifyKeyPressed(key);
799
}
800

801
//------------------------------------------------------------
802
void ofAppGlutWindow::keyboard_up_cb(unsigned char key, int x, int y){
803
	instance->events().notifyKeyReleased(key);
804
}
805

806
//------------------------------------------------------
807
void ofAppGlutWindow::special_key_cb(int key, int x, int y) {
808
	instance->events().notifyKeyPressed(special_key_to_of(key));
809
}
810

811
//------------------------------------------------------------
812
void ofAppGlutWindow::special_key_up_cb(int key, int x, int y) {
813
	instance->events().notifyKeyReleased(special_key_to_of(key));
814
}
815

816
//------------------------------------------------------------
817
int ofAppGlutWindow::special_key_to_of(int key) {
818
	switch (key) {
819
	case GLUT_KEY_F1:
820
		return OF_KEY_F1;
821

822
	case GLUT_KEY_F2:
823
		return OF_KEY_F2;
824

825
	case GLUT_KEY_F3:
826
		return OF_KEY_F3;
827

828
	case GLUT_KEY_F4:
829
		return OF_KEY_F4;
830

831
	case GLUT_KEY_F5:
832
		return OF_KEY_F5;
833

834
	case GLUT_KEY_F6:
835
		return OF_KEY_F6;
836

837
	case GLUT_KEY_F7:
838
		return OF_KEY_F7;
839

840
	case GLUT_KEY_F8:
841
		return OF_KEY_F8;
842

843
	case GLUT_KEY_F9:
844
		return OF_KEY_F9;
845

846
	case GLUT_KEY_F10:
847
		return OF_KEY_F10;
848

849
	case GLUT_KEY_F11:
850
		return OF_KEY_F11;
851

852
	case GLUT_KEY_F12:
853
		return OF_KEY_F12;
854

855
	case GLUT_KEY_LEFT:
856
		return OF_KEY_LEFT;
857

858
	case GLUT_KEY_UP:
859
		return OF_KEY_UP;
860

861
	case GLUT_KEY_RIGHT:
862
		return OF_KEY_RIGHT;
863

864
	case GLUT_KEY_DOWN:
865
		return OF_KEY_DOWN;
866

867
	case GLUT_KEY_PAGE_UP:
868
		return OF_KEY_PAGE_UP;
869

870
	case GLUT_KEY_PAGE_DOWN:
871
		return OF_KEY_PAGE_DOWN;
872

873
	case GLUT_KEY_HOME:
874
		return OF_KEY_HOME;
875

876
	case GLUT_KEY_END:
877
		return OF_KEY_END;
878

879
	case GLUT_KEY_INSERT:
880
		return OF_KEY_INSERT;
881

882
	default:
883
		return 0;
884
	}
885
}
886

887
//------------------------------------------------------------
888
void ofAppGlutWindow::resize_cb(int w, int h) {
889
	windowW = w;
890
	windowH = h;
891

892
	instance->events().notifyWindowResized(w, h);
893

894
	nFramesSinceWindowResized = 0;
895
}
896

897
//------------------------------------------------------------
898
void ofAppGlutWindow::entry_cb(int state) {
899
	if (state == GLUT_ENTERED){
900
		instance->events().notifyMouseEntered(instance->events().getMouseX(), instance->events().getMouseY());
901
	}else if (state == GLUT_LEFT){
902
		instance->events().notifyMouseExited(instance->events().getMouseX(), instance->events().getMouseY());
903
	}
904
}
905

906
//------------------------------------------------------------
907
void ofAppGlutWindow::exit_cb() {
908
	instance->events().notifyExit();
909
	instance->events().disable();
910
}
911

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

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

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

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