-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMOAISim.cpp
1161 lines (806 loc) · 33.9 KB
/
MOAISim.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
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
THIS ONE IS BROKEN PLEASE VIEW MOAISIM_2.cpp
#include <android/log.h>
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG , "SDL", __VA_ARGS__)
#include <SDL.h>
#include "pch.h"
#include <moaicore/MOAIActionMgr.h>
#include <moaicore/MOAIDebugLines.h>
#include <moaicore/MOAIGfxDevice.h>
#include <moaicore/MOAIInputMgr.h>
#include <moaicore/MOAILogMessages.h>
#include <moaicore/MOAINodeMgr.h>
#include <moaicore/MOAIProp.h>
#include <moaicore/MOAISim.h>
#include <moaicore/MOAITextureBase.h>
#include <moaicore/MOAIRenderMgr.h >
#include <moaiext-android/MOAIKeyboardAndroid.h >
#if USE_CURL
#include <moaicore/MOAIUrlMgrCurl.h>
#endif
#if MOAI_OS_NACL
#include <moaicore/MOAIUrlMgrNaCl.h>
#endif
#include <aku/AKU.h>
#if defined(_WIN32)
#include <Psapi.h>
#elif defined(__APPLE__) //&& defined(TARGET_IPHONE_SIMULATOR)
// Not sure if using mach API is disallowed in the app store. :/
#include <mach/mach.h>
#endif
//================================================================//
// local
//================================================================//
//----------------------------------------------------------------//
/** @name clearLoopFlags
@text Uses the mask provided to clear the loop flags.
@opt number mask Default value is 0xffffffff.
@out nil
*/
int MOAISim::_clearLoopFlags ( lua_State* L ) {
MOAILuaState state ( L );
MOAISim::Get ().mLoopFlags &= ~state.GetValue < u32 >( 1, 0xffffffff );
return 0;
}
//----------------------------------------------------------------//
/** @name crash
@text Crashes moai with a null pointer dereference.
@out nil
*/
int MOAISim::_crash ( lua_State* L ) {
UNUSED(L);
int *p = NULL;
(*p) = 0;
return 0;
}
//----------------------------------------------------------------//
/** @name enterFullscreenMode
@text Enters fullscreen mode on the device if possible.
@out nil
*/
int MOAISim::_enterFullscreenMode ( lua_State* L ) {
return 0;
}
//----------------------------------------------------------------//
/** @name exitFullscreenMode
@text Exits fullscreen mode on the device if possible.
@out nil
*/
int MOAISim::_exitFullscreenMode ( lua_State* L ) {
MOAILuaState state ( L );
AKUEnterFullscreenModeFunc exitFullscreenMode = AKUGetFunc_ExitFullscreenMode ();
if ( exitFullscreenMode ) {
exitFullscreenMode ();
}
return 0;
}
//----------------------------------------------------------------//
/** @name forceGarbageCollection
@text Runs the garbage collector repeatedly until no more MOAIObjects
can be collected.
@out nil
*/
int MOAISim::_forceGarbageCollection ( lua_State* L ) {
UNUSED ( L );
MOAINodeMgr::Get ().Update ();
MOAILuaRuntime::Get ().ForceGarbageCollection ();
return 0;
}
//----------------------------------------------------------------//
/** @name framesToTime
@text Converts the number of frames to time passed in seconds.
@in number frames The number of frames.
@out number time The equivilant number of seconds for the specified number of frames.
*/
int MOAISim::_framesToTime ( lua_State* L ) {
MOAILuaState state ( L );
if ( !state.CheckParams ( 1, "N" )) return 0;
float frames = state.GetValue < float >( 1, 0.0f );
MOAISim& device = MOAISim::Get ();
lua_pushnumber ( state, frames * device.mStep );
return 1;
}
//----------------------------------------------------------------//
/** @name getDeviceTime
@text Gets the raw device clock. This is a replacement for Lua's os.time ().
@out number time The device clock time in seconds.
*/
int MOAISim::_getDeviceTime ( lua_State* L ) {
lua_pushnumber ( L, USDeviceTime::GetTimeInSeconds ());
return 1;
}
//----------------------------------------------------------------//
/** @name getElapsedFrames
@text Gets the number of frames elapsed since the application was started.
@out number frames The number of elapsed frames.
*/
int MOAISim::_getElapsedFrames ( lua_State* L ) {
MOAISim& device = MOAISim::Get ();
lua_pushnumber ( L, device.mSimTime / device.mStep );
return 1;
}
//----------------------------------------------------------------//
/** @name getElapsedTime
@text Gets the number of seconds elapsed since the application was started.
@out number time The number of elapsed seconds.
*/
int MOAISim::_getElapsedTime ( lua_State* L ) {
lua_pushnumber ( L, MOAISim::Get ().mSimTime );
return 1;
}
//----------------------------------------------------------------//
/** @name getHistogram
@text Generates a histogram of active MOAIObjects and returns it
in a table containing object tallies indexed by object
class names.
@out table histogram
*/
int MOAISim::_getHistogram ( lua_State* L ) {
MOAILuaState state ( L );
MOAILuaRuntime::Get ().PushHistogram ( state );
return 1;
}
//----------------------------------------------------------------//
/** @name getLoopFlags
@text Returns the current loop flags.
@out number mask
*/
int MOAISim::_getLoopFlags ( lua_State* L ) {
lua_pushnumber ( L, MOAISim::Get ().mLoopFlags );
return 1;
}
//----------------------------------------------------------------//
/** @name getLuaObjectCount
@text Gets the total number of objects in memory that inherit MOAILuaObject. Count includes
objects that are not bound to the Lua runtime.
@out number count
*/
int MOAISim::_getLuaObjectCount ( lua_State* L ) {
lua_pushnumber ( L, MOAILuaRuntime::Get ().GetObjectCount ());
return 1;
}
//----------------------------------------------------------------//
/** @name getMemoryUsage
@text Get the current amount of memory used by MOAI and its subsystems. This will
attempt to return reasonable estimates where exact values cannot be obtained.
Some fields represent informational fields (i.e. are not double counted in the
total, but present to assist debugging) and may be only available on certain
platforms (e.g. Windows, etc). These fields begin with a '_' character.
@out table usage The breakdown of each subsystem's memory usage, in bytes. There is also a "total" field that contains the summed value.
*/
int MOAISim::_getMemoryUsage ( lua_State* L ) {
float divisor = 1.0f;
if( lua_type(L, 1) == LUA_TSTRING )
{
cc8* str = lua_tostring(L, 1);
if( str[0] == 'k' || str[0] == 'K' )
divisor = 1024.0f;
else if( str[0] == 'm' || str[0] == 'M' )
divisor = 1024.0f * 1024.0f;
else if( str[0] == 'b' || str[0] == 'B' )
divisor = 1.0f;
}
size_t total = 0;
lua_newtable(L);
size_t count;
count = MOAILuaRuntime::Get().GetMemoryUsage ();
lua_pushnumber(L, count / divisor);
lua_setfield(L, -2, "lua");
total += count;
// This is informational only (i.e. don't double count with the previous field).
// It doesn't actually seem to represent the real usage of lua, but maybe
// someone is interested.
lua_pushnumber ( L, lua_gc ( L, LUA_GCCOUNTB, 0 ) / divisor );
lua_setfield ( L, -2, "_luagc_count" );
count = MOAIGfxDevice::Get ().GetTextureMemoryUsage ();
lua_pushnumber ( L, count / divisor );
lua_setfield ( L, -2, "texture" );
total += count;
#if defined(_WIN32)
PROCESS_MEMORY_COUNTERS pmc;
// Print the process identifier.
if ( GetProcessMemoryInfo( GetCurrentProcess(), &pmc, sizeof(pmc)) )
{
lua_pushnumber(L, pmc.PagefileUsage / divisor);
lua_setfield(L, -2, "_sys_vs");
lua_pushnumber(L, pmc.WorkingSetSize / divisor);
lua_setfield(L, -2, "_sys_rss");
}
#elif defined(__APPLE__) //&& defined(TARGET_IPHONE_SIMULATOR)
// Tricky undocumented mach polling of memory
struct task_basic_info t_info;
mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
kern_return_t kr = task_info(mach_task_self(),
TASK_BASIC_INFO,
reinterpret_cast<task_info_t>(&t_info),
&t_info_count);
// Most likely cause for failure: |task| is a zombie.
if( kr == KERN_SUCCESS )
{
lua_pushnumber(L, t_info.virtual_size / divisor);
lua_setfield(L, -2, "_sys_vs");
lua_pushnumber(L, t_info.resident_size / divisor);
lua_setfield(L, -2, "_sys_rss");
}
#endif
lua_pushnumber(L, total / divisor);
lua_setfield(L, -2, "total");
return 1;
}
//----------------------------------------------------------------//
/** @name getPerformance
@text Returns an estimated frames per second based on measurements
taken at every render.
@out number fps Estimated frames per second.
*/
int MOAISim::_getPerformance ( lua_State* L ) {
MOAISim& device = MOAISim::Get ();
lua_pushnumber ( L, device.mFrameRate );
return 1;
}
//----------------------------------------------------------------//
/** @name getStep
@text Gets the amount of time (in seconds) that it takes for one frame to pass.
@out number size The size of the frame; the time it takes for one frame to pass.
*/
int MOAISim::_getStep ( lua_State* L ) {
lua_pushnumber ( L, MOAISim::Get ().GetStep ());
return 1;
}
//----------------------------------------------------------------//
// TODO: doxygen
int MOAISim::_getTaskSubscriber ( lua_State* L ) {
MOAISim& device = MOAISim::Get ();
MOAILuaState state ( L );
device.mTaskSubscriber->PushLuaUserdata ( state );
return 1;
}
//----------------------------------------------------------------//
/** @name openWindow
@text Opens a new window for the application to render on. This must be called before any rendering can be done, and it must only be called once.
@in string title The title of the window.
@in number width The width of the window in pixels.
@in number height The height of the window in pixels.
@out nil
*/
int MOAISim::_openWindow ( lua_State* L ) {
MOAILuaState state ( L );
if ( !state.CheckParams ( 1, "SNN" )) return 0;
cc8* title = lua_tostring ( state, 1 );
u32 width = state.GetValue < u32 >( 2, 320 );
u32 height = state.GetValue < u32 >( 3, 480 );
AKUOpenWindowFunc openWindow = AKUGetFunc_OpenWindow ();
if ( openWindow ) {
MOAIGfxDevice::Get ().SetBufferSize ( width, height );
openWindow ( title, width, height );
}
return 0;
}
//----------------------------------------------------------------//
/** @name reportHistogram
@text Generates a histogram of active MOAIObjects.
@out nil
*/
int MOAISim::_reportHistogram ( lua_State* L ) {
MOAILuaState state ( L );
MOAILuaRuntime::Get ().ReportHistogram ( MOAILogMgr::Get ().GetFile ());
return 0;
}
//----------------------------------------------------------------//
/** @name reportLeaks
@text Analyze the currently allocated MOAI objects and create a textual
report of where they were declared, and what Lua references (if any)
can be found. NOTE: This is incredibly slow, so only use to debug
leaking memory issues.
This will also trigger a full garbage collection before performing
the required report. (Equivalent of collectgarbage("collect").)
@in bool clearAfter If true, it will reset the allocation tables (without
freeing the underlying objects). This allows this
method to be called after a known operation and
get only those allocations created since the last call
to this function.
@out nil
*/
int MOAISim::_reportLeaks ( lua_State* L ) {
MOAILuaState state ( L );
bool clearAfter = state.GetValue < bool >( 1, false );
MOAILuaRuntime& luaRuntime = MOAILuaRuntime::Get ();
luaRuntime.ReportLeaksFormatted ( MOAILogMgr::Get ().GetFile ());
if ( clearAfter ) {
luaRuntime.ResetLeakTracking ();
}
return 0;
}
//----------------------------------------------------------------//
/** @name setBoostThreshold
@text Sets the boost threshold, a scalar applied to step. If the gap
between simulation time and device time is greater than the step
size multiplied by the boost threshold and MOAISim.SIM_LOOP_ALLOW_BOOST
is set in the loop flags, then the simulation is updated once with a
large, variable step to make up the entire gap.
@opt number boostThreshold Default value is DEFAULT_BOOST_THRESHOLD.
@out nil
*/
int MOAISim::_setBoostThreshold ( lua_State* L ) {
MOAILuaState state ( L );
MOAISim::Get ().mBoostThreshold = state.GetValue < double >( 1, DEFAULT_BOOST_THRESHOLD );
return 0;
}
//----------------------------------------------------------------//
/** @name setCpuBudget
@text Sets the amount of time (given in simulation steps) to allow
for updating the simulation.
@in number budget Default value is DEFAULT_CPU_BUDGET.
@out nil
*/
int MOAISim::_setCpuBudget ( lua_State* L ) {
MOAILuaState state ( L );
MOAISim::Get ().mCpuBudget = state.GetValue < u32 >( 1, DEFAULT_CPU_BUDGET );
return 0;
}
//----------------------------------------------------------------//
/** @name setHistogramEnabled
@text Enable tracking of every MOAILuaObject so that an object count
histogram may be generated.
@opt bool enable Default value is false.
@out nil
*/
int MOAISim::_setHistogramEnabled ( lua_State* L ) {
MOAILuaState state ( L );
MOAILuaRuntime::Get ().EnableHistogram ( state.GetValue < bool >( 1, false ));
return 0;
}
//----------------------------------------------------------------//
/** @name setLeakTrackingEnabled
@text Enable extra memory book-keeping measures that allow all MOAI objects to be
tracked back to their point of allocation (in Lua). Use together with
MOAISim.reportLeaks() to determine exactly where your memory usage is
being created. NOTE: This is very expensive in terms of both CPU and
the extra memory associated with the stack info book-keeping. Use only
when tracking down leaks.
@opt bool enable Default value is false.
@out nil
*/
int MOAISim::_setLeakTrackingEnabled ( lua_State* L ) {
MOAILuaState state ( L );
MOAILuaRuntime::Get ().EnableLeakTracking ( state.GetValue < bool >( 1, false ));
return 0;
}
//----------------------------------------------------------------//
/** @name setLongDelayThreshold
@text Sets the long delay threshold. If the sim step falls behind
the given threshold, the deficit will be dropped: sim will
neither spin nor boost to catch up.
@opt number longDelayThreshold Default value is DEFAULT_LONG_DELAY_THRESHOLD.
@out nil
*/
int MOAISim::_setLongDelayThreshold ( lua_State* L ) {
MOAILuaState state ( L );
MOAISim::Get ().mLongDelayThreshold = state.GetValue < double >( 1, DEFAULT_LONG_DELAY_THRESHOLD );
return 0;
}
//----------------------------------------------------------------//
/** @name setLoopFlags
@text Fine tune behavior of the simulation loop. MOAISim.SIM_LOOP_ALLOW_SPIN
will allow the simulation step to run multiple times per update to try
and catch up with device time, but will abort if processing the simulation
exceeds the configfured step time. MOAISim.SIM_LOOP_ALLOW_BOOST will permit
a *variable* update step if simulation time falls too far behind
device time (based on the boost threshold). Be warned: this can wreak
havok with physics and stepwise animation or game AI.
Three presets are provided: MOAISim.LOOP_FLAGS_DEFAULT, MOAISim.LOOP_FLAGS_FIXED,
and MOAISim.LOOP_FLAGS_MULTISTEP.
@opt number flags Mask or a combination of MOAISim.SIM_LOOP_FORCE_STEP, MOAISim.SIM_LOOP_ALLOW_BOOST,
MOAISim.SIM_LOOP_ALLOW_SPIN, MOAISim.SIM_LOOP_NO_DEFICIT, MOAISim.SIM_LOOP_NO_SURPLUS,
MOAISim.SIM_LOOP_RESET_CLOCK. Default value is 0.
@out nil
*/
int MOAISim::_setLoopFlags ( lua_State* L ) {
MOAILuaState state ( L );
MOAISim::Get ().mLoopFlags |= state.GetValue < u32 >( 1, 0 );
return 0;
}
//----------------------------------------------------------------//
/** @name setLuaAllocLogEnabled
@text Toggles log messages from Lua allocator.
@opt boolean enable Default value is 'false.'
@out nil
*/
int MOAISim::_setLuaAllocLogEnabled ( lua_State* L ) {
MOAILuaState state ( L );
MOAILuaRuntime::Get ().SetAllocLogEnabled ( state.GetValue < bool >( 1, false ));
return 0;
}
//----------------------------------------------------------------//
/** @name setStep
@text Sets the size of each simulation step (in seconds).
@in number step The step size. Default value is 1 / DEFAULT_STEPS_PER_SECOND.
@out nil
*/
int MOAISim::_setStep ( lua_State* L ) {
MOAILuaState state ( L );
MOAISim::Get ().SetStep ( state.GetValue < double >( 1, 1.0 / ( double )DEFAULT_STEPS_PER_SECOND ));
return 0;
}
//----------------------------------------------------------------//
/** @name setStepMultiplier
@text Runs the simulation multiple times per step (but with a fixed
step size). This is used to speed up the simulation without
providing a larger step size (which could destabilize physics
simulation).
@in number count Default value is DEFAULT_STEP_MULTIPLIER.
@out nil
*/
int MOAISim::_setStepMultiplier ( lua_State* L ) {
MOAILuaState state ( L );
MOAISim::Get ().mStepMultiplier = state.GetValue < u32 >( 1, DEFAULT_STEP_MULTIPLIER );
return 0;
}
//----------------------------------------------------------------//
/** @name setTimerError
@text Sets the tolerance for timer error. This is a multiplier of step.
Timer error tolerance is step * timerError.
@in number timerError Default value is 0.0.
@out nil
*/
int MOAISim::_setTimerError ( lua_State* L ) {
MOAILuaState state ( L );
MOAISim::Get ().mTimerError = state.GetValue < double >( 1, 0.0 );
return 0;
}
//----------------------------------------------------------------//
/** @name setTraceback
@text Sets the function to call when a traceback occurs in lua
@in function callback Function to execute when the traceback occurs
@out nil
*/
int MOAISim::_setTraceback ( lua_State* L ) {
UNUSED ( L );
MOAILuaRuntime::Get ().GetCustomTraceback().SetStrongRef ( MOAILuaRuntime::Get ().GetMainState(), 1 );
return 0;
}
//----------------------------------------------------------------//
/** @name timeToFrames
@text Converts the number of time passed in seconds to frames.
@in number time The number of seconds.
@out number frames The equivilant number of frames for the specified number of seconds.
*/
int MOAISim::_timeToFrames ( lua_State* L ) {
MOAILuaState state ( L );
if ( !state.CheckParams ( 1, "N" )) return 0;
float time = state.GetValue < float >( 1, 0.0f );
MOAISim& device = MOAISim::Get ();
lua_pushnumber ( state, time / device.mStep );
return 1;
}
//================================================================//
// DOXYGEN
//================================================================//
#ifdef DOXYGEN
//----------------------------------------------------------------//
/** @name clearRenderStack
@text Alias for MOAIRenderMgr.clearRenderStack (). THIS METHOD
IS DEPRECATED AND WILL BE REMOVED IN A FUTURE RELEASE.
@out nil
*/
int MOAISim::_clearRenderStack ( lua_State* L ) {
}
//----------------------------------------------------------------//
/** @name popRenderPass
@text Alias for MOAIRenderMgr.popRenderPass (). THIS METHOD
IS DEPRECATED AND WILL BE REMOVED IN A FUTURE RELEASE.
@out nil
*/
int MOAISim::_popRenderPass ( lua_State* L ) {
}
//----------------------------------------------------------------//
/** @name pushRenderPass
@text Alias for MOAIRenderMgr.pushRenderPass (). THIS METHOD
IS DEPRECATED AND WILL BE REMOVED IN A FUTURE RELEASE.
@in MOAIRenderable renderable
@out nil
*/
int MOAISim::_pushRenderPass ( lua_State* L ) {
}
//----------------------------------------------------------------//
/** @name removeRenderPass
@text Alias for MOAIRenderMgr.removeRenderPass (). THIS METHOD
IS DEPRECATED AND WILL BE REMOVED IN A FUTURE RELEASE.
@in MOAIRenderable renderable
@out nil
*/
int MOAISim::_removeRenderPass ( lua_State* L ) {
}
#endif
//================================================================//
// MOAISim
//================================================================//
//----------------------------------------------------------------//
MOAISim::MOAISim () :
mLoopState ( STOPPED ),
mStep ( 1.0 / ( double )DEFAULT_STEPS_PER_SECOND ),
mSimTime ( 0.0 ),
mRealTime ( 0.0 ),
mFrameTime ( 0.0 ),
mFrameRate ( 0.0f ),
mFrameRateIdx ( 0 ),
mLoopFlags ( LOOP_FLAGS_DEFAULT ),
mBoostThreshold ( DEFAULT_BOOST_THRESHOLD ),
mLongDelayThreshold ( DEFAULT_LONG_DELAY_THRESHOLD ),
mCpuBudget ( DEFAULT_CPU_BUDGET ),
mStepMultiplier ( DEFAULT_STEP_MULTIPLIER ),
mTimerError ( 0.0 ) {
RTTI_SINGLE ( MOAIGlobalEventSource )
for ( u32 i = 0; i < FPS_BUFFER_SIZE; ++i ) {
this->mFrameRateBuffer [ i ] = 0.0f;
}
this->mFrameTime = USDeviceTime::GetTimeInSeconds ();
this->mTaskSubscriber.Set ( *this, new MOAITaskSubscriber ());
}
//----------------------------------------------------------------//
MOAISim::~MOAISim () {
this->mTaskSubscriber.Set ( *this, 0 );
}
//----------------------------------------------------------------//
double MOAISim::MeasureFrameRate () {
double frameTime = USDeviceTime::GetTimeInSeconds ();
double delay = frameTime - this->mFrameTime;
this->mFrameTime = frameTime;
if ( delay > 0.0 ) {
float sample = ( float )( 1.0 / delay );
this->mFrameRateBuffer [ this->mFrameRateIdx++ ] = sample;
this->mFrameRateIdx %= FPS_BUFFER_SIZE;
sample = 0.0f;
for ( u32 i = 0; i < FPS_BUFFER_SIZE; ++i ) {
sample += this->mFrameRateBuffer [ i ];
}
this->mFrameRate = sample / ( float )FPS_BUFFER_SIZE;
}
return delay;
}
//----------------------------------------------------------------//
void MOAISim::OnGlobalsFinalize () {
this->SendFinalizeEvent ();
}
//----------------------------------------------------------------//
void MOAISim::OnGlobalsRestore () {
}
//----------------------------------------------------------------//
void MOAISim::OnGlobalsRetire () {
}
//----------------------------------------------------------------//
/** @name pauseTimer
@text Pauses or unpauses the device timer, preventing any visual updates (rendering) while paused.
@in boolean pause Whether the device timer should be paused.
@out nil
*/
int MOAISim::_pauseTimer ( lua_State* L ) {
MOAILuaState state ( L );
int pause = state.GetValue < int >( 1, -1 );
if ( pause == 0) {
__android_log_print(ANDROID_LOG_INFO, "C++", "PAUSEING-A %s", "sim");
MOAISim::Get ().PauseMOAI ();
} else if ( pause == 1) {
__android_log_print(ANDROID_LOG_INFO, "C++", "RUSUMING-A %s", "sim");
MOAISim::Get ().ResumeMOAI ();
}
return 0;
}
int MOAISim::_timerPause ( lua_State* L ) {
MOAILuaState state ( L );
__android_log_print(ANDROID_LOG_INFO, "C++", "PAUSEING-A %s", "sim");
MOAISim::Get ().PauseMOAI ();
return 0;
}
////////////////////////////////
int MOAISim::_timerStart ( lua_State* L ) {
MOAILuaState state ( L );
__android_log_print(ANDROID_LOG_INFO, "C++", "RUSUMING-A %s", "sim");
MOAISim::Get ().ResumeMOAI ();
return 0;
}
////////////////////////////////
int MOAISim::_timerSleep ( lua_State* L ) {
MOAILuaState state ( L );
usleep ( 200 );
return 0;
}
//----------------------------------------------------------------//
void MOAISim::RegisterLuaClass ( MOAILuaState& state ) {
MOAIGlobalEventSource::RegisterLuaClass ( state );
state.SetField ( -1, "EVENT_FINALIZE", ( u32 )EVENT_FINALIZE );
state.SetField ( -1, "EVENT_PAUSE", ( u32 )EVENT_PAUSE );
state.SetField ( -1, "EVENT_RESUME", ( u32 )EVENT_RESUME );
state.SetField ( -1, "SIM_LOOP_FORCE_STEP", ( u32 )SIM_LOOP_FORCE_STEP );
state.SetField ( -1, "SIM_LOOP_ALLOW_BOOST", ( u32 )SIM_LOOP_ALLOW_BOOST );
state.SetField ( -1, "SIM_LOOP_ALLOW_SPIN", ( u32 )SIM_LOOP_ALLOW_SPIN );
state.SetField ( -1, "SIM_LOOP_NO_DEFICIT", ( u32 )SIM_LOOP_NO_DEFICIT );
state.SetField ( -1, "SIM_LOOP_NO_SURPLUS", ( u32 )SIM_LOOP_NO_SURPLUS );
state.SetField ( -1, "SIM_LOOP_RESET_CLOCK", ( u32 )SIM_LOOP_RESET_CLOCK );
state.SetField ( -1, "SIM_LOOP_ALLOW_SOAK", ( u32 )SIM_LOOP_ALLOW_SOAK );
state.SetField ( -1, "LOOP_FLAGS_DEFAULT", ( u32 )LOOP_FLAGS_DEFAULT );
state.SetField ( -1, "LOOP_FLAGS_FIXED", ( u32 )LOOP_FLAGS_FIXED );
state.SetField ( -1, "LOOP_FLAGS_MULTISTEP", ( u32 )LOOP_FLAGS_MULTISTEP );
state.SetField ( -1, "LOOP_FLAGS_SOAK", ( u32 )LOOP_FLAGS_SOAK );
state.SetField ( -1, "DEFAULT_STEPS_PER_SECOND", ( u32 )DEFAULT_STEPS_PER_SECOND );
state.SetField ( -1, "DEFAULT_BOOST_THRESHOLD", ( u32 )DEFAULT_BOOST_THRESHOLD );
state.SetField ( -1, "DEFAULT_LONG_DELAY_THRESHOLD", ( u32 )DEFAULT_LONG_DELAY_THRESHOLD );
state.SetField ( -1, "DEFAULT_CPU_BUDGET", ( u32 )DEFAULT_CPU_BUDGET );
state.SetField ( -1, "DEFAULT_STEP_MULTIPLIER", ( u32 )DEFAULT_STEP_MULTIPLIER );
luaL_Reg regTable [] = {
{ "clearLoopFlags", _clearLoopFlags },
{ "crash", _crash },
{ "enterFullscreenMode", _enterFullscreenMode },
{ "exitFullscreenMode", _exitFullscreenMode },
{ "forceGarbageCollection", _forceGarbageCollection },
{ "framesToTime", _framesToTime },
{ "getDeviceTime", _getDeviceTime },
{ "getElapsedFrames", _getElapsedFrames },
{ "getElapsedTime", _getElapsedTime },
{ "getHistogram", _getHistogram },
{ "getLoopFlags", _getLoopFlags },
{ "getLuaObjectCount", _getLuaObjectCount },
{ "getMemoryUsage", _getMemoryUsage },
{ "getPerformance", _getPerformance },
{ "getStep", _getStep },
{ "getTaskSubscriber", _getTaskSubscriber },
{ "openWindow", _openWindow },
{ "pauseTimer", _pauseTimer },
{"timerPause", _timerPause},
{"timerStart", _timerStart},
{"timerSleep", _timerSleep},
{ "reportHistogram", _reportHistogram },
{ "reportLeaks", _reportLeaks },
{ "setBoostThreshold", _setBoostThreshold },
{ "setCpuBudget", _setCpuBudget},
{ "setHistogramEnabled", _setHistogramEnabled },
{ "setLeakTrackingEnabled", _setLeakTrackingEnabled },
{ "setListener", &MOAIGlobalEventSource::_setListener < MOAISim > },
{ "setLongDelayThreshold", _setLongDelayThreshold },
{ "setLoopFlags", _setLoopFlags },
{ "setLuaAllocLogEnabled", _setLuaAllocLogEnabled },
{ "setStep", _setStep },
{ "setStepMultiplier", _setStepMultiplier },
{ "setTimerError", _setTimerError },
{ "setTraceback", _setTraceback },
{ "timeToFrames", _timeToFrames },
{ NULL, NULL }
};
luaL_register( state, 0, regTable );
}
//----------------------------------------------------------------//
void MOAISim::RegisterLuaFuncs ( MOAILuaState& state ) {
UNUSED ( state );
}
//----------------------------------------------------------------//
void MOAISim::ResumeMOAI() {
//if ( this->mLoopState == PAUSED ) {
// this->SendResumeEvent();
// this->mLoopState = START;
//}
__android_log_print(ANDROID_LOG_INFO, "C++", "RUSUMING-B %s", "sim");
this->SendResumeEvent();
this->mLoopState = RUNNING; //RESUMING
//if ( this->mLoopState == PAUSED ) {
//}
}
//----------------------------------------------------------------//
void MOAISim::PauseMOAI () {
__android_log_print(ANDROID_LOG_INFO, "C++", "PAUSEING-B %s", "sim");
this->SendPauseEvent();
this->mLoopState = PAUSED;
}
//----------------------------------------------------------------//
void MOAISim::SendFinalizeEvent () {
MOAILuaStateHandle state = MOAILuaRuntime::Get ().State ();
if ( this->PushListener ( EVENT_FINALIZE, state )) {
state.DebugCall ( 0, 0 );
}
}
//----------------------------------------------------------------//
void MOAISim::SendPauseEvent () {
MOAILuaStateHandle state = MOAILuaRuntime::Get ().State ();
if ( this->PushListener ( EVENT_PAUSE, state )) {
state.DebugCall ( 0, 0 );
}
}
//----------------------------------------------------------------//
void MOAISim::SendResumeEvent () {
MOAILuaStateHandle state = MOAILuaRuntime::Get ().State ();
if ( this->PushListener ( EVENT_RESUME, state )) {
state.DebugCall ( 0, 0 );
}
}
//----------------------------------------------------------------//
void MOAISim::SetStep ( double step ) {
if ( this->mStep != step ) {
this->mStep = step;
AKUSetSimStepFunc setSimStep = AKUGetFunc_SetSimStep ();
if ( setSimStep ) {
setSimStep ( step );
}
}
}
//----------------------------------------------------------------//
double MOAISim::StepSim ( double step, u32 multiplier ) {
double time = USDeviceTime::GetTimeInSeconds ();
for ( u32 s = 0; s < multiplier; ++s ) {
MOAIInputMgr::Get ().Update ();
MOAIActionMgr::Get ().Update (( float )step );
MOAINodeMgr::Get ().Update ();
this->mSimTime += step;
}
return USDeviceTime::GetTimeInSeconds () - time;
}