async.cpp 854 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include "async.h"
  2. #include <SDL.h>
  3. bbInt g_mojo_app_AppInstance_AddAsyncCallback( bbFunction<void()> callback );
  4. namespace{
  5. const int INVOKE=0x40000000;
  6. const int REMOVE=0x80000000;
  7. static void postEvent( int code ){
  8. SDL_UserEvent event;
  9. event.type=SDL_USEREVENT;
  10. event.code=code;
  11. event.data1=0;
  12. event.data2=0;
  13. if( SDL_PeepEvents( (SDL_Event*)&event,1,SDL_ADDEVENT,SDL_FIRSTEVENT,SDL_LASTEVENT )!=1 ){
  14. printf(" SDL_PeepEvents error!\n" );fflush( stdout );
  15. }
  16. }
  17. }
  18. int bbAddAsyncCallback( bbFunction<void()> callback ){
  19. return g_mojo_app_AppInstance_AddAsyncCallback( callback );
  20. }
  21. void bbInvokeAsyncCallback( int callback,bool remove ){
  22. int code=callback|INVOKE;
  23. if( remove ) code|=REMOVE;
  24. postEvent( code );
  25. }
  26. void bbRemoveAsyncCallback( int callback ){
  27. int code=callback|REMOVE;
  28. postEvent( code );
  29. }