This repository has been archived by the owner on Nov 4, 2023. It is now read-only.
/
glfwgame.cpp
743 lines (595 loc) · 18.3 KB
/
glfwgame.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
//***** glfwgame.h *****
struct BBGlfwVideoMode : public Object{
int Width;
int Height;
int RedBits;
int GreenBits;
int BlueBits;
BBGlfwVideoMode( int w,int h,int r,int g,int b ):Width(w),Height(h),RedBits(r),GreenBits(g),BlueBits(b){}
};
class BBGlfwGame : public BBGame{
public:
BBGlfwGame();
static BBGlfwGame *GlfwGame(){ return _glfwGame; }
virtual void SetUpdateRate( int hertz );
virtual int Millisecs();
virtual bool PollJoystick( int port,Array<Float> joyx,Array<Float> joyy,Array<Float> joyz,Array<bool> buttons );
virtual void OpenUrl( String url );
virtual void SetMouseVisible( bool visible );
virtual int GetDeviceWidth();
virtual int GetDeviceHeight();
virtual void SetDeviceWindow( int width,int height,int flags );
virtual Array<BBDisplayMode*> GetDisplayModes();
virtual BBDisplayMode *GetDesktopMode();
virtual void SetSwapInterval( int interval );
virtual int SaveState( String state );
virtual String LoadState();
virtual String PathToFilePath( String path );
virtual unsigned char *LoadImageData( String path,int *width,int *height,int *depth );
virtual unsigned char *LoadAudioData( String path,int *length,int *channels,int *format,int *hertz );
virtual void SetGlfwWindow( int width,int height,int red,int green,int blue,int alpha,int depth,int stencil,bool fullscreen );
virtual BBGlfwVideoMode *GetGlfwDesktopMode();
virtual Array<BBGlfwVideoMode*> GetGlfwVideoModes();
virtual void Run();
private:
static BBGlfwGame *_glfwGame;
double _updatePeriod;
double _nextUpdate;
String _baseDir;
String _internalDir;
int _swapInterval;
void UpdateEvents();
protected:
static int TransKey( int key );
static int KeyToChar( int key );
static void GLFWCALL OnKey( int key,int action );
static void GLFWCALL OnChar( int chr,int action );
static void GLFWCALL OnMouseButton( int button,int action );
static void GLFWCALL OnMousePos( int x,int y );
static int GLFWCALL OnWindowClose();
};
//***** glfwgame.cpp *****
enum{
VKEY_BACKSPACE=8,VKEY_TAB,
VKEY_ENTER=13,
VKEY_SHIFT=16,
VKEY_CONTROL=17,
VKEY_ESC=27,
VKEY_SPACE=32,
VKEY_PAGEUP=33,VKEY_PAGEDOWN,VKEY_END,VKEY_HOME,
VKEY_LEFT=37,VKEY_UP,VKEY_RIGHT,VKEY_DOWN,
VKEY_INSERT=45,VKEY_DELETE,
VKEY_0=48,VKEY_1,VKEY_2,VKEY_3,VKEY_4,VKEY_5,VKEY_6,VKEY_7,VKEY_8,VKEY_9,
VKEY_A=65,VKEY_B,VKEY_C,VKEY_D,VKEY_E,VKEY_F,VKEY_G,VKEY_H,VKEY_I,VKEY_J,
VKEY_K,VKEY_L,VKEY_M,VKEY_N,VKEY_O,VKEY_P,VKEY_Q,VKEY_R,VKEY_S,VKEY_T,
VKEY_U,VKEY_V,VKEY_W,VKEY_X,VKEY_Y,VKEY_Z,
VKEY_LSYS=91,VKEY_RSYS,
VKEY_NUM0=96,VKEY_NUM1,VKEY_NUM2,VKEY_NUM3,VKEY_NUM4,
VKEY_NUM5,VKEY_NUM6,VKEY_NUM7,VKEY_NUM8,VKEY_NUM9,
VKEY_NUMMULTIPLY=106,VKEY_NUMADD,VKEY_NUMSLASH,
VKEY_NUMSUBTRACT,VKEY_NUMDECIMAL,VKEY_NUMDIVIDE,
VKEY_F1=112,VKEY_F2,VKEY_F3,VKEY_F4,VKEY_F5,VKEY_F6,
VKEY_F7,VKEY_F8,VKEY_F9,VKEY_F10,VKEY_F11,VKEY_F12,
VKEY_LSHIFT=160,VKEY_RSHIFT,
VKEY_LCONTROL=162,VKEY_RCONTROL,
VKEY_LALT=164,VKEY_RALT,
VKEY_TILDE=192,VKEY_MINUS=189,VKEY_EQUALS=187,
VKEY_OPENBRACKET=219,VKEY_BACKSLASH=220,VKEY_CLOSEBRACKET=221,
VKEY_SEMICOLON=186,VKEY_QUOTES=222,
VKEY_COMMA=188,VKEY_PERIOD=190,VKEY_SLASH=191
};
enum{
JOY_A=0x00,
JOY_B=0x01,
JOY_X=0x02,
JOY_Y=0x03,
JOY_LB=0x04,
JOY_RB=0x05,
JOY_BACK=0x06,
JOY_START=0x07,
JOY_LEFT=0x08,
JOY_UP=0x09,
JOY_RIGHT=0x0a,
JOY_DOWN=0x0b,
JOY_LSB=0x0c,
JOY_RSB=0x0d,
JOY_MENU=0x0e
};
BBGlfwGame *BBGlfwGame::_glfwGame;
BBGlfwGame::BBGlfwGame():_updatePeriod(0),_nextUpdate(0),_swapInterval( CFG_GLFW_SWAP_INTERVAL ){
_glfwGame=this;
}
//***** BBGame *****
void Init_GL_Exts();
int glfwGraphicsSeq=0;
void BBGlfwGame::SetUpdateRate( int updateRate ){
BBGame::SetUpdateRate( updateRate );
if( _updateRate ) _updatePeriod=1.0/_updateRate;
_nextUpdate=0;
}
int BBGlfwGame::Millisecs(){
return int( glfwGetTime()*1000.0 );
}
bool BBGlfwGame::PollJoystick( int port,Array<Float> joyx,Array<Float> joyy,Array<Float> joyz,Array<bool> buttons ){
//Just in case...my PC has either started doing weird things with joystick ordering, or I assumed too much in the past!
static int pjoys[4];
if( !port ){
int i=0;
for( int joy=GLFW_JOYSTICK_1;joy<=GLFW_JOYSTICK_16 && i<4;++joy ){
if( glfwGetJoystickParam( joy,GLFW_PRESENT ) ) pjoys[i++]=joy;
}
while( i<4 ) pjoys[i++]=-1;
}
int joy=pjoys[port];
if( joy==-1 ) return false;
//Stopped working on my PC at some point...
// int joy=GLFW_JOYSTICK_1+port;
// if( !glfwGetJoystickParam( joy,GLFW_PRESENT ) ) return false;
//read axes
float axes[6];
memset( axes,0,sizeof(axes) );
int n_axes=glfwGetJoystickPos( joy,axes,6 );
//read buttons
unsigned char buts[32];
memset( buts,0,sizeof(buts) );
int n_buts=glfwGetJoystickButtons( joy,buts,32 );
// static int done;
// if( !done++ ) printf( "n_axes=%i, n_buts=%i\n",n_axes,n_buts );fflush( stdout );
//Ugh...
const int *dev_axes;
const int *dev_buttons;
#if _WIN32
//xbox 360 controller
const int xbox360_axes[]={0,1,2,4,0x43,0x42,999};
const int xbox360_buttons[]={0,1,2,3,4,5,6,7,-4,-3,-2,-1,8,9,999};
//logitech dual action
const int logitech_axes[]={0,1,0x86,2,0x43,0x87,999};
const int logitech_buttons[]={1,2,0,3,4,5,8,9,-4,-3,-2,-1,10,11,999};
if( n_axes==5 && n_buts==14 ){
dev_axes=xbox360_axes;
dev_buttons=xbox360_buttons;
}else{
dev_axes=logitech_axes;
dev_buttons=logitech_buttons;
}
#else
//xbox 360 controller
const int xbox360_axes[]={0,1,0x14,2,3,0x25,999};
const int xbox360_buttons[]={11,12,13,14,8,9,5,4,2,0,3,1,6,7,10,999};
//ps3 controller
const int ps3_axes[]={0,1,0x88,2,3,0x89,999};
const int ps3_buttons[]={14,13,15,12,10,11,0,3,7,4,5,6,1,2,16,999};
//logitech dual action
const int logitech_axes[]={0,1,0x86,2,3,0x87,999};
const int logitech_buttons[]={1,2,0,3,4,5,8,9,15,12,13,14,10,11,999};
if( n_axes==6 && n_buts==15 ){
dev_axes=xbox360_axes;
dev_buttons=xbox360_buttons;
}else if( n_axes==4 && n_buts==19 ){
dev_axes=ps3_axes;
dev_buttons=ps3_buttons;
}else{
dev_axes=logitech_axes;
dev_buttons=logitech_buttons;
}
#endif
const int *p=dev_axes;
float joys[6]={0,0,0,0,0,0};
for( int i=0;i<6 && p[i]!=999;++i ){
int j=p[i]&0xf,k=p[i]&~0xf;
if( k==0x10 ){
joys[i]=(axes[j]+1)/2;
}else if( k==0x20 ){
joys[i]=(1-axes[j])/2;
}else if( k==0x40 ){
joys[i]=-axes[j];
}else if( k==0x80 ){
joys[i]=(buts[j]==GLFW_PRESS);
}else{
joys[i]=axes[j];
}
}
joyx[0]=joys[0];joyy[0]=joys[1];joyz[0]=joys[2];
joyx[1]=joys[3];joyy[1]=joys[4];joyz[1]=joys[5];
p=dev_buttons;
for( int i=0;i<32;++i ) buttons[i]=false;
for( int i=0;i<32 && p[i]!=999;++i ){
int j=p[i];
if( j<0 ) j+=n_buts;
buttons[i]=(buts[j]==GLFW_PRESS);
}
return true;
}
void BBGlfwGame::OpenUrl( String url ){
#if _WIN32
ShellExecute( HWND_DESKTOP,"open",url.ToCString<char>(),0,0,SW_SHOWNORMAL );
#elif __APPLE__
if( CFURLRef cfurl=CFURLCreateWithBytes( 0,url.ToCString<UInt8>(),url.Length(),kCFStringEncodingASCII,0 ) ){
LSOpenCFURLRef( cfurl,0 );
CFRelease( cfurl );
}
#elif __linux
system( ( String( "xdg-open \"" )+url+"\"" ).ToCString<char>() );
#endif
}
void BBGlfwGame::SetMouseVisible( bool visible ){
if( visible ){
glfwEnable( GLFW_MOUSE_CURSOR );
}else{
glfwDisable( GLFW_MOUSE_CURSOR );
}
}
int BBGlfwGame::SaveState( String state ){
#ifdef CFG_GLFW_APP_LABEL
if( FILE *f=OpenFile( "monkey://internal/.monkeystate","wb" ) ){
bool ok=state.Save( f );
fclose( f );
return ok ? 0 : -2;
}
return -1;
#else
return BBGame::SaveState( state );
#endif
}
String BBGlfwGame::LoadState(){
#ifdef CFG_GLFW_APP_LABEL
if( FILE *f=OpenFile( "monkey://internal/.monkeystate","rb" ) ){
String str=String::Load( f );
fclose( f );
return str;
}
return "";
#else
return BBGame::LoadState();
#endif
}
String BBGlfwGame::PathToFilePath( String path ){
if( !_baseDir.Length() ){
String appPath;
#if _WIN32
WCHAR buf[MAX_PATH+1];
GetModuleFileNameW( GetModuleHandleW(0),buf,MAX_PATH );
buf[MAX_PATH]=0;appPath=String( buf ).Replace( "\\","/" );
#elif __APPLE__
char buf[PATH_MAX+1];
uint32_t size=sizeof( buf );
_NSGetExecutablePath( buf,&size );
buf[PATH_MAX]=0;appPath=String( buf ).Replace( "/./","/" );
#elif __linux
char lnk[PATH_MAX+1],buf[PATH_MAX];
sprintf( lnk,"/proc/%i/exe",getpid() );
int n=readlink( lnk,buf,PATH_MAX );
if( n<0 || n>=PATH_MAX ) abort();
appPath=String( buf,n );
#endif
int i=appPath.FindLast( "/" );if( i==-1 ) abort();
_baseDir=appPath.Slice( 0,i );
#if __APPLE__
if( _baseDir.EndsWith( ".app/Contents/MacOS" ) ) _baseDir=_baseDir.Slice( 0,-5 )+"Resources";
#endif
// bbPrint( String( "_baseDir=" )+_baseDir );
}
if( !path.StartsWith( "monkey:" ) ){
return path;
}else if( path.StartsWith( "monkey://data/" ) ){
return _baseDir+"/data/"+path.Slice( 14 );
}else if( path.StartsWith( "monkey://internal/" ) ){
if( !_internalDir.Length() ){
#ifdef CFG_GLFW_APP_LABEL
#if _WIN32
_internalDir=String( getenv( "APPDATA" ) );
#elif __APPLE__
_internalDir=String( getenv( "HOME" ) )+"/Library/Application Support";
#elif __linux
_internalDir=String( getenv( "HOME" ) )+"/.config";
mkdir( _internalDir.ToCString<char>(),0777 );
#endif
#ifdef CFG_GLFW_APP_PUBLISHER
_internalDir=_internalDir+"/"+_STRINGIZE( CFG_GLFW_APP_PUBLISHER );
#if _WIN32
_wmkdir( _internalDir.ToCString<wchar_t>() );
#else
mkdir( _internalDir.ToCString<char>(),0777 );
#endif
#endif
_internalDir=_internalDir+"/"+_STRINGIZE( CFG_GLFW_APP_LABEL );
#if _WIN32
_wmkdir( _internalDir.ToCString<wchar_t>() );
#else
mkdir( _internalDir.ToCString<char>(),0777 );
#endif
#else
_internalDir=_baseDir+"/internal";
#endif
// bbPrint( String( "_internalDir=" )+_internalDir );
}
return _internalDir+"/"+path.Slice( 18 );
}else if( path.StartsWith( "monkey://external/" ) ){
return _baseDir+"/external/"+path.Slice( 18 );
}
return "";
}
unsigned char *BBGlfwGame::LoadImageData( String path,int *width,int *height,int *depth ){
FILE *f=OpenFile( path,"rb" );
if( !f ) return 0;
unsigned char *data=stbi_load_from_file( f,width,height,depth,0 );
fclose( f );
if( data ) gc_ext_malloced( (*width)*(*height)*(*depth) );
return data;
}
unsigned char *BBGlfwGame::LoadAudioData( String path,int *length,int *channels,int *format,int *hertz ){
FILE *f=OpenFile( path,"rb" );
if( !f ) return 0;
unsigned char *data=0;
if( path.ToLower().EndsWith( ".wav" ) ){
data=LoadWAV( f,length,channels,format,hertz );
}else if( path.ToLower().EndsWith( ".ogg" ) ){
data=LoadOGG( f,length,channels,format,hertz );
}
fclose( f );
if( data ) gc_ext_malloced( (*length)*(*channels)*(*format) );
return data;
}
//glfw key to monkey key!
int BBGlfwGame::TransKey( int key ){
if( key>='0' && key<='9' ) return key;
if( key>='A' && key<='Z' ) return key;
switch( key ){
case ' ':return VKEY_SPACE;
case ';':return VKEY_SEMICOLON;
case '=':return VKEY_EQUALS;
case ',':return VKEY_COMMA;
case '-':return VKEY_MINUS;
case '.':return VKEY_PERIOD;
case '/':return VKEY_SLASH;
case '~':return VKEY_TILDE;
case '[':return VKEY_OPENBRACKET;
case ']':return VKEY_CLOSEBRACKET;
case '\"':return VKEY_QUOTES;
case '\\':return VKEY_BACKSLASH;
case '`':return VKEY_TILDE;
case '\'':return VKEY_QUOTES;
case GLFW_KEY_LSHIFT:
case GLFW_KEY_RSHIFT:return VKEY_SHIFT;
case GLFW_KEY_LCTRL:
case GLFW_KEY_RCTRL:return VKEY_CONTROL;
// case GLFW_KEY_LSHIFT:return VKEY_LSHIFT;
// case GLFW_KEY_RSHIFT:return VKEY_RSHIFT;
// case GLFW_KEY_LCTRL:return VKEY_LCONTROL;
// case GLFW_KEY_RCTRL:return VKEY_RCONTROL;
case GLFW_KEY_BACKSPACE:return VKEY_BACKSPACE;
case GLFW_KEY_TAB:return VKEY_TAB;
case GLFW_KEY_ENTER:return VKEY_ENTER;
case GLFW_KEY_ESC:return VKEY_ESC;
case GLFW_KEY_INSERT:return VKEY_INSERT;
case GLFW_KEY_DEL:return VKEY_DELETE;
case GLFW_KEY_PAGEUP:return VKEY_PAGEUP;
case GLFW_KEY_PAGEDOWN:return VKEY_PAGEDOWN;
case GLFW_KEY_HOME:return VKEY_HOME;
case GLFW_KEY_END:return VKEY_END;
case GLFW_KEY_UP:return VKEY_UP;
case GLFW_KEY_DOWN:return VKEY_DOWN;
case GLFW_KEY_LEFT:return VKEY_LEFT;
case GLFW_KEY_RIGHT:return VKEY_RIGHT;
case GLFW_KEY_KP_0:return VKEY_NUM0;
case GLFW_KEY_KP_1:return VKEY_NUM1;
case GLFW_KEY_KP_2:return VKEY_NUM2;
case GLFW_KEY_KP_3:return VKEY_NUM3;
case GLFW_KEY_KP_4:return VKEY_NUM4;
case GLFW_KEY_KP_5:return VKEY_NUM5;
case GLFW_KEY_KP_6:return VKEY_NUM6;
case GLFW_KEY_KP_7:return VKEY_NUM7;
case GLFW_KEY_KP_8:return VKEY_NUM8;
case GLFW_KEY_KP_9:return VKEY_NUM9;
case GLFW_KEY_KP_DIVIDE:return VKEY_NUMDIVIDE;
case GLFW_KEY_KP_MULTIPLY:return VKEY_NUMMULTIPLY;
case GLFW_KEY_KP_SUBTRACT:return VKEY_NUMSUBTRACT;
case GLFW_KEY_KP_ADD:return VKEY_NUMADD;
case GLFW_KEY_KP_DECIMAL:return VKEY_NUMDECIMAL;
case GLFW_KEY_F1:return VKEY_F1;
case GLFW_KEY_F2:return VKEY_F2;
case GLFW_KEY_F3:return VKEY_F3;
case GLFW_KEY_F4:return VKEY_F4;
case GLFW_KEY_F5:return VKEY_F5;
case GLFW_KEY_F6:return VKEY_F6;
case GLFW_KEY_F7:return VKEY_F7;
case GLFW_KEY_F8:return VKEY_F8;
case GLFW_KEY_F9:return VKEY_F9;
case GLFW_KEY_F10:return VKEY_F10;
case GLFW_KEY_F11:return VKEY_F11;
case GLFW_KEY_F12:return VKEY_F12;
}
return 0;
}
//monkey key to special monkey char
int BBGlfwGame::KeyToChar( int key ){
switch( key ){
case VKEY_BACKSPACE:
case VKEY_TAB:
case VKEY_ENTER:
case VKEY_ESC:
return key;
case VKEY_PAGEUP:
case VKEY_PAGEDOWN:
case VKEY_END:
case VKEY_HOME:
case VKEY_LEFT:
case VKEY_UP:
case VKEY_RIGHT:
case VKEY_DOWN:
case VKEY_INSERT:
return key | 0x10000;
case VKEY_DELETE:
return 127;
}
return 0;
}
void BBGlfwGame::OnMouseButton( int button,int action ){
switch( button ){
case GLFW_MOUSE_BUTTON_LEFT:button=0;break;
case GLFW_MOUSE_BUTTON_RIGHT:button=1;break;
case GLFW_MOUSE_BUTTON_MIDDLE:button=2;break;
default:return;
}
int x,y;
glfwGetMousePos( &x,&y );
switch( action ){
case GLFW_PRESS:
_glfwGame->MouseEvent( BBGameEvent::MouseDown,button,x,y );
break;
case GLFW_RELEASE:
_glfwGame->MouseEvent( BBGameEvent::MouseUp,button,x,y );
break;
}
}
void BBGlfwGame::OnMousePos( int x,int y ){
_game->MouseEvent( BBGameEvent::MouseMove,-1,x,y );
}
int BBGlfwGame::OnWindowClose(){
_game->KeyEvent( BBGameEvent::KeyDown,0x1b0 );
_game->KeyEvent( BBGameEvent::KeyUp,0x1b0 );
return GL_FALSE;
}
void BBGlfwGame::OnKey( int key,int action ){
key=TransKey( key );
if( !key ) return;
switch( action ){
case GLFW_PRESS:
_glfwGame->KeyEvent( BBGameEvent::KeyDown,key );
if( int chr=KeyToChar( key ) ) _game->KeyEvent( BBGameEvent::KeyChar,chr );
break;
case GLFW_RELEASE:
_glfwGame->KeyEvent( BBGameEvent::KeyUp,key );
break;
}
}
void BBGlfwGame::OnChar( int chr,int action ){
switch( action ){
case GLFW_PRESS:
_glfwGame->KeyEvent( BBGameEvent::KeyChar,chr );
break;
}
}
void BBGlfwGame::SetGlfwWindow( int width,int height,int red,int green,int blue,int alpha,int depth,int stencil,bool fullscreen ){
for( int i=0;i<=GLFW_KEY_LAST;++i ){
int key=TransKey( i );
if( key && glfwGetKey( i )==GLFW_PRESS ) KeyEvent( BBGameEvent::KeyUp,key );
}
GLFWvidmode desktopMode;
glfwGetDesktopMode( &desktopMode );
glfwCloseWindow();
glfwOpenWindowHint( GLFW_REFRESH_RATE,60 );
glfwOpenWindowHint( GLFW_WINDOW_NO_RESIZE,CFG_GLFW_WINDOW_RESIZABLE ? GL_FALSE : GL_TRUE );
glfwOpenWindow( width,height,red,green,blue,alpha,depth,stencil,fullscreen ? GLFW_FULLSCREEN : GLFW_WINDOW );
++glfwGraphicsSeq;
if( !fullscreen ){
glfwSetWindowPos( (desktopMode.Width-width)/2,(desktopMode.Height-height)/2 );
glfwSetWindowTitle( _STRINGIZE(CFG_GLFW_WINDOW_TITLE) );
}
#if CFG_OPENGL_INIT_EXTENSIONS
Init_GL_Exts();
#endif
if( _swapInterval>=0 ) glfwSwapInterval( _swapInterval );
glfwEnable( GLFW_KEY_REPEAT );
glfwDisable( GLFW_AUTO_POLL_EVENTS );
glfwSetKeyCallback( OnKey );
glfwSetCharCallback( OnChar );
glfwSetMouseButtonCallback( OnMouseButton );
glfwSetMousePosCallback( OnMousePos );
glfwSetWindowCloseCallback( OnWindowClose );
}
Array<BBGlfwVideoMode*> BBGlfwGame::GetGlfwVideoModes(){
GLFWvidmode modes[1024];
int n=glfwGetVideoModes( modes,1024 );
Array<BBGlfwVideoMode*> bbmodes( n );
for( int i=0;i<n;++i ){
bbmodes[i]=new BBGlfwVideoMode( modes[i].Width,modes[i].Height,modes[i].RedBits,modes[i].GreenBits,modes[i].BlueBits );
}
return bbmodes;
}
BBGlfwVideoMode *BBGlfwGame::GetGlfwDesktopMode(){
GLFWvidmode mode;
glfwGetDesktopMode( &mode );
return new BBGlfwVideoMode( mode.Width,mode.Height,mode.RedBits,mode.GreenBits,mode.BlueBits );
}
int BBGlfwGame::GetDeviceWidth(){
int width,height;
glfwGetWindowSize( &width,&height );
return width;
}
int BBGlfwGame::GetDeviceHeight(){
int width,height;
glfwGetWindowSize( &width,&height );
return height;
}
void BBGlfwGame::SetDeviceWindow( int width,int height,int flags ){
SetGlfwWindow( width,height,8,8,8,0,CFG_OPENGL_DEPTH_BUFFER_ENABLED ? 32 : 0,0,(flags&1)!=0 );
}
Array<BBDisplayMode*> BBGlfwGame::GetDisplayModes(){
GLFWvidmode vmodes[1024];
int n=glfwGetVideoModes( vmodes,1024 );
Array<BBDisplayMode*> modes( n );
for( int i=0;i<n;++i ) modes[i]=new BBDisplayMode( vmodes[i].Width,vmodes[i].Height );
return modes;
}
BBDisplayMode *BBGlfwGame::GetDesktopMode(){
GLFWvidmode vmode;
glfwGetDesktopMode( &vmode );
return new BBDisplayMode( vmode.Width,vmode.Height );
}
void BBGlfwGame::SetSwapInterval( int interval ){
_swapInterval=interval;
if( _swapInterval>=0 ) glfwSwapInterval( _swapInterval );
}
void BBGlfwGame::UpdateEvents(){
if( _suspended ){
glfwWaitEvents();
}else{
glfwPollEvents();
}
if( glfwGetWindowParam( GLFW_ACTIVE ) ){
if( _suspended ){
ResumeGame();
_nextUpdate=0;
}
}else if( glfwGetWindowParam( GLFW_ICONIFIED ) || CFG_MOJO_AUTO_SUSPEND_ENABLED ){
if( !_suspended ){
SuspendGame();
_nextUpdate=0;
}
}
}
void BBGlfwGame::Run(){
#if CFG_GLFW_WINDOW_WIDTH && CFG_GLFW_WINDOW_HEIGHT
SetGlfwWindow( CFG_GLFW_WINDOW_WIDTH,CFG_GLFW_WINDOW_HEIGHT,8,8,8,0,CFG_OPENGL_DEPTH_BUFFER_ENABLED ? 32 : 0,0,CFG_GLFW_WINDOW_FULLSCREEN );
#endif
StartGame();
while( glfwGetWindowParam( GLFW_OPENED ) ){
RenderGame();
glfwSwapBuffers();
if( _nextUpdate ){
double delay=_nextUpdate-glfwGetTime();
if( delay>0 ) glfwSleep( delay );
}
//Update user events
UpdateEvents();
//App suspended?
if( _suspended ) continue;
//'Go nuts' mode!
if( !_updateRate ){
UpdateGame();
continue;
}
//Reset update timer?
if( !_nextUpdate ) _nextUpdate=glfwGetTime();
//Catch up updates...
int i=0;
for( ;i<4;++i ){
UpdateGame();
if( !_nextUpdate ) break;
_nextUpdate+=_updatePeriod;
if( _nextUpdate>glfwGetTime() ) break;
}
if( i==4 ) _nextUpdate=0;
}
}