bbruntime.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #include "std.h"
  2. #include "bbsys.h"
  3. #include "bbruntime.h"
  4. void bbEnd(){
  5. RTEX( 0 );
  6. }
  7. void bbStop(){
  8. gx_runtime->debugStop();
  9. if( !gx_runtime->idle() ) RTEX( 0 );
  10. }
  11. void bbAppTitle( BBStr *ti,BBStr *cp ){
  12. gx_runtime->setTitle( *ti,*cp );
  13. delete ti;delete cp;
  14. }
  15. void bbRuntimeError( BBStr *str ){
  16. string t=*str;delete str;
  17. if( t.size()>255 ) t[255]=0;
  18. static char err[256];
  19. strcpy( err,t.c_str() );
  20. RTEX( err );
  21. }
  22. int bbExecFile( BBStr *f ){
  23. string t=*f;delete f;
  24. int n=gx_runtime->execute( t );
  25. if( !gx_runtime->idle() ) RTEX( 0 );
  26. return n;
  27. }
  28. void bbDelay( int ms ){
  29. if( !gx_runtime->delay( ms ) ) RTEX( 0 );
  30. }
  31. int bbMilliSecs(){
  32. return gx_runtime->getMilliSecs();
  33. }
  34. BBStr * bbCommandLine(){
  35. return d_new BBStr( gx_runtime->commandLine() );
  36. }
  37. BBStr * bbSystemProperty( BBStr *p ){
  38. string t=gx_runtime->systemProperty( *p );
  39. delete p;return d_new BBStr( t );
  40. }
  41. BBStr * bbGetEnv( BBStr *env_var ){
  42. char *p=getenv( env_var->c_str() );
  43. BBStr *val=d_new BBStr( p ? p : "" );
  44. delete env_var;
  45. return val;
  46. }
  47. void bbSetEnv( BBStr *env_var,BBStr *val ){
  48. string t=*env_var+"="+*val;
  49. putenv( t.c_str() );
  50. delete env_var;
  51. delete val;
  52. }
  53. gxTimer * bbCreateTimer( int hertz ){
  54. gxTimer *t=gx_runtime->createTimer( hertz );
  55. return t;
  56. }
  57. int bbWaitTimer( gxTimer *t ){
  58. int n=t->wait();
  59. if( !gx_runtime->idle() ) RTEX( 0 );
  60. return n;
  61. }
  62. void bbFreeTimer( gxTimer *t ){
  63. gx_runtime->freeTimer( t );
  64. }
  65. void bbDebugLog( BBStr *t ){
  66. gx_runtime->debugLog( t->c_str() );
  67. delete t;
  68. }
  69. void _bbDebugStmt( int pos,const char *file ){
  70. gx_runtime->debugStmt( pos,file );
  71. if( !gx_runtime->idle() ) RTEX( 0 );
  72. }
  73. void _bbDebugEnter( void *frame,void *env,const char *func ){
  74. gx_runtime->debugEnter( frame,env,func );
  75. }
  76. void _bbDebugLeave(){
  77. gx_runtime->debugLeave();
  78. }
  79. bool basic_create();
  80. bool basic_destroy();
  81. void basic_link( void (*rtSym)( const char *sym,void *pc ) );
  82. bool math_create();
  83. bool math_destroy();
  84. void math_link( void (*rtSym)( const char *sym,void *pc ) );
  85. bool string_create();
  86. bool string_destroy();
  87. void string_link( void (*rtSym)( const char *sym,void *pc ) );
  88. bool stream_create();
  89. bool stream_destroy();
  90. void stream_link( void (*rtSym)( const char *sym,void *pc ) );
  91. bool sockets_create();
  92. bool sockets_destroy();
  93. void sockets_link( void (*rtSym)( const char *sym,void *pc ) );
  94. bool filesystem_create();
  95. bool filesystem_destroy();
  96. void filesystem_link( void (*rtSym)( const char *sym,void *pc ) );
  97. bool bank_create();
  98. bool bank_destroy();
  99. void bank_link( void (*rtSym)( const char *sym,void *pc ) );
  100. bool graphics_create();
  101. bool graphics_destroy();
  102. void graphics_link( void (*rtSym)( const char *sym,void *pc ) );
  103. bool input_create();
  104. bool input_destroy();
  105. void input_link( void (*rtSym)( const char *sym,void *pc ) );
  106. bool audio_create();
  107. bool audio_destroy();
  108. void audio_link( void (*rtSym)( const char *sym,void *pc ) );
  109. bool multiplay_create();
  110. bool multiplay_destroy();
  111. void multiplay_link( void (*rtSym)( const char *sym,void *pc ) );
  112. bool userlibs_create();
  113. void userlibs_destroy();
  114. void userlibs_link( void (*rtSym)( const char *sym,void *pc ) );
  115. #ifdef PRO
  116. bool blitz3d_create();
  117. bool blitz3d_destroy();
  118. void blitz3d_link( void (*rtSym)( const char *sym,void *pc ) );
  119. #else
  120. bool blitz3d_create(){ return true; }
  121. bool blitz3d_destroy(){ return true; }
  122. void blitz3d_link( void (*rtSym)( const char *sym,void *pc ) ){}
  123. #endif
  124. void bbruntime_link( void (*rtSym)( const char *sym,void *pc ) ){
  125. rtSym( "End",bbEnd );
  126. rtSym( "Stop",bbStop );
  127. rtSym( "AppTitle$title$close_prompt=\"\"",bbAppTitle );
  128. rtSym( "RuntimeError$message",bbRuntimeError );
  129. rtSym( "ExecFile$command",bbExecFile );
  130. rtSym( "Delay%millisecs",bbDelay );
  131. rtSym( "%MilliSecs",bbMilliSecs );
  132. rtSym( "$CommandLine",bbCommandLine );
  133. rtSym( "$SystemProperty$property",bbSystemProperty );
  134. rtSym( "$GetEnv$env_var",bbGetEnv );
  135. rtSym( "SetEnv$env_var$value",bbSetEnv );
  136. rtSym( "%CreateTimer%hertz",bbCreateTimer );
  137. rtSym( "%WaitTimer%timer",bbWaitTimer );
  138. rtSym( "FreeTimer%timer",bbFreeTimer );
  139. rtSym( "DebugLog$text",bbDebugLog );
  140. rtSym( "_bbDebugStmt",_bbDebugStmt );
  141. rtSym( "_bbDebugEnter",_bbDebugEnter );
  142. rtSym( "_bbDebugLeave",_bbDebugLeave );
  143. basic_link( rtSym );
  144. math_link( rtSym );
  145. string_link( rtSym );
  146. stream_link( rtSym );
  147. sockets_link( rtSym );
  148. filesystem_link( rtSym );
  149. bank_link( rtSym );
  150. graphics_link( rtSym );
  151. input_link( rtSym );
  152. audio_link( rtSym );
  153. multiplay_link( rtSym );
  154. blitz3d_link( rtSym );
  155. userlibs_link( rtSym );
  156. }
  157. //start up error
  158. static void sue( const char *t ){
  159. string p=string( "Startup Error: " )+t;
  160. gx_runtime->debugInfo( p.c_str() );
  161. }
  162. bool bbruntime_create(){
  163. if( basic_create() ){
  164. if( math_create() ){
  165. if( string_create() ){
  166. if( stream_create() ){
  167. if( sockets_create() ){
  168. if( filesystem_create() ){
  169. if( bank_create() ){
  170. if( graphics_create() ){
  171. if( input_create() ){
  172. if( audio_create() ){
  173. if( multiplay_create() ){
  174. if( blitz3d_create() ){
  175. if( userlibs_create() ){
  176. return true;
  177. }
  178. }else sue( "blitz3d_create failed" );
  179. multiplay_destroy();
  180. }else sue( "multiplay_create failed" );
  181. audio_destroy();
  182. }else sue( "audio_create failed" );
  183. input_destroy();
  184. }else sue( "input_create failed" );
  185. graphics_destroy();
  186. }else sue( "graphics_create failed" );
  187. bank_destroy();
  188. }else sue( "bank_create failed" );
  189. filesystem_destroy();
  190. }else sue( "filesystem_create failed" );
  191. sockets_destroy();
  192. }else sue( "sockets_create failed" );
  193. stream_destroy();
  194. }else sue( "stream_create failed" );
  195. string_destroy();
  196. }else sue( "string_create failed" );
  197. math_destroy();
  198. }else sue( "math_create failed" );
  199. basic_destroy();
  200. }else sue( "basic_create failed" );
  201. return false;
  202. }
  203. bool bbruntime_destroy(){
  204. userlibs_destroy();
  205. blitz3d_destroy();
  206. multiplay_destroy();
  207. audio_destroy();
  208. input_destroy();
  209. graphics_destroy();
  210. bank_destroy();
  211. filesystem_destroy();
  212. sockets_destroy();
  213. stream_destroy();
  214. string_destroy();
  215. math_destroy();
  216. basic_destroy();
  217. return true;
  218. }
  219. const char *bbruntime_run( gxRuntime *rt,void (*pc)(),bool dbg ){
  220. debug=dbg;
  221. gx_runtime=rt;
  222. if( !bbruntime_create() ) return "Unable to start program";
  223. const char *t=0;
  224. try{
  225. if( !gx_runtime->idle() ) RTEX( 0 );
  226. pc();
  227. gx_runtime->debugInfo( "Program has ended" );
  228. }catch( bbEx x ){
  229. t=x.err;
  230. }
  231. bbruntime_destroy();
  232. return t;
  233. }
  234. void bbruntime_panic( const char *err ){
  235. RTEX( err );
  236. }