gxruntime.cpp 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  1. #include "std.h"
  2. #include "gxruntime.h"
  3. #include "zmouse.h"
  4. #define SPI_SETMOUSESPEED 113
  5. struct gxRuntime::GfxMode{
  6. DDSURFACEDESC2 desc;
  7. };
  8. struct gxRuntime::GfxDriver{
  9. GUID *guid;
  10. std::string name;
  11. std::vector<GfxMode*> modes;
  12. #ifdef PRO
  13. D3DDEVICEDESC7 d3d_desc;
  14. #endif
  15. };
  16. static const int static_ws=WS_VISIBLE|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX;
  17. static const int scaled_ws=WS_VISIBLE|WS_CAPTION|WS_SYSMENU|WS_SIZEBOX|WS_MINIMIZEBOX|WS_MAXIMIZEBOX;
  18. static string app_title;
  19. static string app_close;
  20. static gxRuntime *runtime;
  21. static bool busy,suspended;
  22. static volatile bool run_flag;
  23. static DDSURFACEDESC2 desktop_desc;
  24. typedef int (_stdcall *LibFunc)( const void *in,int in_sz,void *out,int out_sz );
  25. struct gxDll{
  26. HINSTANCE hinst;
  27. map<string,LibFunc> funcs;
  28. };
  29. static map<string,gxDll*> libs;
  30. static LRESULT CALLBACK windowProc( HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam );
  31. //current gfx mode
  32. //
  33. //0=NONE
  34. //1=SCALED WINDOW
  35. //2=FIXED SIZE WINDOW
  36. //3=EXCLUSIVE
  37. //
  38. static int gfx_mode;
  39. static bool gfx_lost;
  40. static bool auto_suspend;
  41. //for modes 1 and 2
  42. static int mod_cnt;
  43. static MMRESULT timerID;
  44. static IDirectDrawClipper *clipper;
  45. static IDirectDrawSurface7 *primSurf;
  46. static Debugger *debugger;
  47. static set<gxTimer*> timers;
  48. enum{
  49. WM_STOP=WM_USER+1,WM_RUN,WM_END
  50. };
  51. ////////////////////
  52. // STATIC STARTUP //
  53. ////////////////////
  54. gxRuntime *gxRuntime::openRuntime( HINSTANCE hinst,const string &cmd_line,Debugger *d ){
  55. if( runtime ) return 0;
  56. //create debugger
  57. debugger=d;
  58. //create WNDCLASS
  59. WNDCLASS wndclass;
  60. memset(&wndclass,0,sizeof(wndclass));
  61. wndclass.style=CS_HREDRAW|CS_VREDRAW|CS_OWNDC;
  62. wndclass.lpfnWndProc=::windowProc;
  63. wndclass.hInstance=hinst;
  64. wndclass.lpszClassName="Blitz Runtime Class";
  65. wndclass.hCursor=(HCURSOR)LoadCursor( 0,IDC_ARROW );
  66. wndclass.hbrBackground=(HBRUSH)GetStockObject( BLACK_BRUSH );
  67. RegisterClass( &wndclass );
  68. gfx_mode=0;
  69. clipper=0;primSurf=0;
  70. busy=suspended=false;
  71. run_flag=true;
  72. const char *app_t=" ";
  73. int ws=WS_CAPTION,ws_ex=0;
  74. HWND hwnd=CreateWindowEx( ws_ex,"Blitz Runtime Class",app_t,ws,0,0,0,0,0,0,0,0 );
  75. UpdateWindow( hwnd );
  76. runtime=d_new gxRuntime( hinst,cmd_line,hwnd );
  77. return runtime;
  78. }
  79. void gxRuntime::closeRuntime( gxRuntime *r ){
  80. if( !runtime || runtime!=r ) return;
  81. map<string,gxDll*>::const_iterator it;
  82. for( it=libs.begin();it!=libs.end();++it ){
  83. FreeLibrary( it->second->hinst );
  84. }
  85. libs.clear();
  86. delete runtime;
  87. runtime=0;
  88. }
  89. //////////////////////////
  90. // RUNTIME CONSTRUCTION //
  91. //////////////////////////
  92. typedef int (_stdcall *SetAppCompatDataFunc)( int x,int y );
  93. gxRuntime::gxRuntime( HINSTANCE hi,const string &cl,HWND hw ):
  94. hinst(hi),cmd_line(cl),hwnd(hw),curr_driver(0),enum_all(false),
  95. pointer_visible(true),audio(0),input(0),graphics(0),fileSystem(0),use_di(false){
  96. CoInitialize( 0 );
  97. enumGfx();
  98. TIMECAPS tc;
  99. timeGetDevCaps( &tc,sizeof(tc) );
  100. timeBeginPeriod( tc.wPeriodMin );
  101. memset( &osinfo,0,sizeof(osinfo) );
  102. osinfo.dwOSVersionInfoSize=sizeof(osinfo);
  103. GetVersionEx( &osinfo );
  104. HMODULE ddraw=LoadLibraryA( "ddraw.dll" );
  105. if( ddraw ){
  106. SetAppCompatDataFunc SetAppCompatData=(SetAppCompatDataFunc)GetProcAddress( ddraw,"SetAppCompatData" );
  107. if( SetAppCompatData ) SetAppCompatData( 12,0 );
  108. FreeLibrary( ddraw );
  109. }
  110. }
  111. gxRuntime::~gxRuntime(){
  112. while( timers.size() ) freeTimer( *timers.begin() );
  113. if( audio ) closeAudio( audio );
  114. if( graphics ) closeGraphics( graphics );
  115. if( input ) closeInput( input );
  116. TIMECAPS tc;
  117. timeGetDevCaps( &tc,sizeof(tc) );
  118. timeEndPeriod( tc.wPeriodMin );
  119. denumGfx();
  120. DestroyWindow( hwnd );
  121. UnregisterClass( "Blitz Runtime Class",hinst );
  122. CoUninitialize();
  123. }
  124. void gxRuntime::pauseAudio(){
  125. if( audio ) audio->pause();
  126. }
  127. void gxRuntime::resumeAudio(){
  128. if( audio ) audio->resume();
  129. }
  130. void gxRuntime::backupGraphics(){
  131. if( auto_suspend ){
  132. graphics->backup();
  133. }
  134. }
  135. void gxRuntime::restoreGraphics(){
  136. if( auto_suspend ){
  137. if( !graphics->restore() ) gfx_lost=true;
  138. }
  139. }
  140. void gxRuntime::resetInput(){
  141. if( input ) input->reset();
  142. }
  143. void gxRuntime::acquireInput(){
  144. if( !input ) return;
  145. if( gfx_mode==3 ){
  146. if( use_di ){
  147. use_di=input->acquire();
  148. }else{
  149. }
  150. }
  151. input->reset();
  152. }
  153. void gxRuntime::unacquireInput(){
  154. if( !input ) return;
  155. if( gfx_mode==3 && use_di ) input->unacquire();
  156. input->reset();
  157. }
  158. /////////////
  159. // SUSPEND //
  160. /////////////
  161. void gxRuntime::suspend(){
  162. busy=true;
  163. pauseAudio();
  164. backupGraphics();
  165. unacquireInput();
  166. suspended=true;
  167. busy=false;
  168. if( gfx_mode==3 ) ShowCursor(1);
  169. if( debugger ) debugger->debugStop();
  170. }
  171. ////////////
  172. // RESUME //
  173. ////////////
  174. void gxRuntime::resume(){
  175. if( gfx_mode==3 ) ShowCursor(0);
  176. busy=true;
  177. acquireInput();
  178. restoreGraphics();
  179. resumeAudio();
  180. suspended=false;
  181. busy=false;
  182. if( debugger ) debugger->debugRun();
  183. }
  184. ///////////////////
  185. // FORCE SUSPEND //
  186. ///////////////////
  187. void gxRuntime::forceSuspend(){
  188. if( gfx_mode==3 ){
  189. SetForegroundWindow( GetDesktopWindow() );
  190. ShowWindow( GetDesktopWindow(),SW_SHOW );
  191. }else{
  192. suspend();
  193. }
  194. }
  195. //////////////////
  196. // FORCE RESUME //
  197. //////////////////
  198. void gxRuntime::forceResume(){
  199. if( gfx_mode==3 ){
  200. SetForegroundWindow( hwnd );
  201. ShowWindow( hwnd,SW_SHOWMAXIMIZED );
  202. }else{
  203. resume();
  204. }
  205. }
  206. ///////////
  207. // PAINT //
  208. ///////////
  209. void gxRuntime::paint(){
  210. switch( gfx_mode ){
  211. case 0:
  212. {
  213. }
  214. break;
  215. case 1:case 2: //scaled windowed mode.
  216. {
  217. RECT src,dest;
  218. src.left=src.top=0;
  219. GetClientRect( hwnd,&dest );
  220. src.right=gfx_mode==1 ? graphics->getWidth() : dest.right;
  221. src.bottom=gfx_mode==1 ? graphics->getHeight() : dest.bottom;
  222. POINT p;p.x=p.y=0;ClientToScreen( hwnd,&p );
  223. dest.left+=p.x;dest.right+=p.x;
  224. dest.top+=p.y;dest.bottom+=p.y;
  225. gxCanvas *f=graphics->getFrontCanvas();
  226. primSurf->Blt( &dest,f->getSurface(),&src,0,0 );
  227. }
  228. break;
  229. case 3:
  230. {
  231. //exclusive mode
  232. }
  233. break;
  234. }
  235. }
  236. //////////
  237. // FLIP //
  238. //////////
  239. void gxRuntime::flip( bool vwait ){
  240. gxCanvas *b=graphics->getBackCanvas();
  241. gxCanvas *f=graphics->getFrontCanvas();
  242. int n;
  243. switch( gfx_mode ){
  244. case 1:case 2:
  245. if( vwait ) graphics->vwait();
  246. f->setModify( b->getModify() );
  247. if( f->getModify()!=mod_cnt ){
  248. mod_cnt=f->getModify();
  249. paint();
  250. }
  251. break;
  252. case 3:
  253. if( vwait ){
  254. BOOL vb;
  255. while( graphics->dirDraw->GetVerticalBlankStatus( &vb )>=0 && vb ) {}
  256. n=f->getSurface()->Flip( 0,DDFLIP_WAIT );
  257. }else{
  258. n=f->getSurface()->Flip( 0,DDFLIP_NOVSYNC|DDFLIP_WAIT );
  259. }
  260. if( n>=0 ) return;
  261. string t="Flip Failed! Return code:"+itoa(n&0x7fff);
  262. debugLog( t.c_str() );
  263. break;
  264. }
  265. }
  266. ////////////////
  267. // MOVE MOUSE //
  268. ////////////////
  269. void gxRuntime::moveMouse( int x,int y ){
  270. POINT p;
  271. RECT rect;
  272. switch( gfx_mode ){
  273. case 1:
  274. GetClientRect( hwnd,&rect );
  275. x=x*(rect.right-rect.left)/graphics->getWidth();
  276. y=y*(rect.bottom-rect.top)/graphics->getHeight();
  277. case 2:
  278. p.x=x;p.y=y;ClientToScreen( hwnd,&p );x=p.x;y=p.y;
  279. break;
  280. case 3:
  281. if( use_di ) return;
  282. break;
  283. default:
  284. return;
  285. }
  286. SetCursorPos( x,y );
  287. }
  288. /////////////////
  289. // WINDOW PROC //
  290. /////////////////
  291. LRESULT gxRuntime::windowProc( HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam ){
  292. if( busy ){
  293. return DefWindowProc( hwnd,msg,wparam,lparam );
  294. }
  295. PAINTSTRUCT ps;
  296. //handle 'special' messages!
  297. switch( msg ){
  298. case WM_PAINT:
  299. if( gfx_mode && !auto_suspend ){
  300. if( !graphics->restore() ) gfx_lost=true;
  301. }
  302. BeginPaint( hwnd,&ps );
  303. paint();
  304. EndPaint( hwnd,&ps );
  305. return DefWindowProc( hwnd,msg,wparam,lparam );
  306. case WM_ERASEBKGND:
  307. return gfx_mode ? 1 : DefWindowProc( hwnd,msg,wparam,lparam );
  308. case WM_CLOSE:
  309. if( app_close.size() ){
  310. int n=MessageBox( hwnd,app_close.c_str(),app_title.c_str(),MB_OKCANCEL|MB_ICONWARNING|MB_SETFOREGROUND|MB_TOPMOST );
  311. if( n!=IDOK ) return 0;
  312. }
  313. asyncEnd();
  314. return 0;
  315. case WM_SETCURSOR:
  316. if( !suspended ){
  317. if( gfx_mode==3 ){
  318. SetCursor( 0 );
  319. return 1;
  320. }else if( !pointer_visible ){
  321. POINT p;
  322. GetCursorPos( &p );
  323. ScreenToClient( hwnd,&p );
  324. RECT r;GetClientRect( hwnd,&r );
  325. if( p.x>=0 && p.y>=0 && p.x<r.right && p.y<r.bottom ){
  326. SetCursor( 0 );
  327. return 1;
  328. }
  329. }
  330. }
  331. break;
  332. case WM_ACTIVATEAPP:
  333. if( auto_suspend ){
  334. if( wparam ){
  335. if( suspended ) resume();
  336. }else{
  337. if( !suspended ) suspend();
  338. }
  339. }
  340. break;
  341. }
  342. if( !input || suspended ) return DefWindowProc( hwnd,msg,wparam,lparam );
  343. if( gfx_mode==3 && use_di ){
  344. use_di=input->acquire();
  345. return DefWindowProc( hwnd,msg,wparam,lparam );
  346. }
  347. static const int MK_ALLBUTTONS=MK_LBUTTON|MK_RBUTTON|MK_MBUTTON;
  348. //handle input messages
  349. switch( msg ){
  350. case WM_LBUTTONDOWN:
  351. input->wm_mousedown(1);
  352. SetCapture(hwnd);
  353. break;
  354. case WM_LBUTTONUP:
  355. input->wm_mouseup(1);
  356. if( !(wparam&MK_ALLBUTTONS) ) ReleaseCapture();
  357. break;
  358. case WM_RBUTTONDOWN:
  359. input->wm_mousedown(2);
  360. SetCapture( hwnd );
  361. break;
  362. case WM_RBUTTONUP:
  363. input->wm_mouseup(2);
  364. if( !(wparam&MK_ALLBUTTONS) ) ReleaseCapture();
  365. break;
  366. case WM_MBUTTONDOWN:
  367. input->wm_mousedown(3);
  368. SetCapture( hwnd );
  369. break;
  370. case WM_MBUTTONUP:
  371. input->wm_mouseup(3);
  372. if( !(wparam&MK_ALLBUTTONS) ) ReleaseCapture();
  373. break;
  374. case WM_MOUSEMOVE:
  375. if( !graphics ) break;
  376. if( gfx_mode==3 && !use_di ){
  377. POINT p;GetCursorPos( &p );
  378. input->wm_mousemove( p.x,p.y );
  379. }else{
  380. int x=(short)(lparam&0xffff),y=lparam>>16;
  381. if( gfx_mode==1 ){
  382. RECT rect;GetClientRect( hwnd,&rect );
  383. x=x*graphics->getWidth()/(rect.right-rect.left);
  384. y=y*graphics->getHeight()/(rect.bottom-rect.top);
  385. }
  386. if( x<0 ) x=0;
  387. else if( x>=graphics->getWidth() ) x=graphics->getWidth()-1;
  388. if( y<0 ) y=0;
  389. else if( y>=graphics->getHeight() ) y=graphics->getHeight()-1;
  390. input->wm_mousemove( x,y );
  391. }
  392. break;
  393. case WM_MOUSEWHEEL:
  394. input->wm_mousewheel( (short)HIWORD( wparam ) );
  395. break;
  396. case WM_KEYDOWN:case WM_SYSKEYDOWN:
  397. if( lparam & 0x40000000 ) break;
  398. if( int n=((lparam>>17)&0x80)|((lparam>>16)&0x7f) ) input->wm_keydown( n );
  399. break;
  400. case WM_KEYUP:case WM_SYSKEYUP:
  401. if( int n=((lparam>>17)&0x80)|((lparam>>16)&0x7f) ) input->wm_keyup( n );
  402. break;
  403. default:
  404. return DefWindowProc( hwnd,msg,wparam,lparam );
  405. }
  406. return 0;
  407. }
  408. static LRESULT CALLBACK windowProc( HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam ){
  409. if( runtime ) return runtime->windowProc( hwnd,msg,wparam,lparam );
  410. return DefWindowProc( hwnd,msg,wparam,lparam );
  411. }
  412. //////////////////////////////
  413. //STOP FROM EXTERNAL SOURCE //
  414. //////////////////////////////
  415. void gxRuntime::asyncStop(){
  416. PostMessage( hwnd,WM_STOP,0,0 );
  417. }
  418. //////////////////////////////
  419. //RUN FROM EXTERNAL SOURCE //
  420. //////////////////////////////
  421. void gxRuntime::asyncRun(){
  422. PostMessage( hwnd,WM_RUN,0,0 );
  423. }
  424. //////////////////////////////
  425. // END FROM EXTERNAL SOURCE //
  426. //////////////////////////////
  427. void gxRuntime::asyncEnd(){
  428. PostMessage( hwnd,WM_END,0,0 );
  429. }
  430. //////////
  431. // IDLE //
  432. //////////
  433. bool gxRuntime::idle(){
  434. for(;;){
  435. MSG msg;
  436. if( suspended && run_flag ){
  437. GetMessage( &msg,0,0,0 );
  438. }else{
  439. if( !PeekMessage( &msg,0,0,0,PM_REMOVE ) ) return run_flag;
  440. }
  441. switch( msg.message ){
  442. case WM_STOP:
  443. if( !suspended ) forceSuspend();
  444. break;
  445. case WM_RUN:
  446. if( suspended ) forceResume();
  447. break;
  448. case WM_END:
  449. debugger=0;
  450. run_flag=false;
  451. break;
  452. default:
  453. DispatchMessage( &msg );
  454. }
  455. }
  456. return run_flag;
  457. }
  458. ///////////
  459. // DELAY //
  460. ///////////
  461. bool gxRuntime::delay( int ms ){
  462. int t=timeGetTime()+ms;
  463. for(;;){
  464. if( !idle() ) return false;
  465. int d=t-timeGetTime(); //how long left to wait
  466. if( d<=0 ) return true;
  467. if( d>100 ) d=100;
  468. Sleep( d );
  469. }
  470. }
  471. ///////////////
  472. // DEBUGSTMT //
  473. ///////////////
  474. void gxRuntime::debugStmt( int pos,const char *file ){
  475. if( debugger ) debugger->debugStmt( pos,file );
  476. }
  477. ///////////////
  478. // DEBUGSTOP //
  479. ///////////////
  480. void gxRuntime::debugStop(){
  481. if( !suspended ) forceSuspend();
  482. }
  483. ////////////////
  484. // DEBUGENTER //
  485. ////////////////
  486. void gxRuntime::debugEnter( void *frame,void *env,const char *func ){
  487. if( debugger ) debugger->debugEnter( frame,env,func );
  488. }
  489. ////////////////
  490. // DEBUGLEAVE //
  491. ////////////////
  492. void gxRuntime::debugLeave(){
  493. if( debugger ) debugger->debugLeave();
  494. }
  495. ////////////////
  496. // DEBUGERROR //
  497. ////////////////
  498. void gxRuntime::debugError( const char *t ){
  499. if( !debugger ) return;
  500. Debugger *d=debugger;
  501. asyncEnd();
  502. if( !suspended ){
  503. forceSuspend();
  504. }
  505. d->debugMsg( t,true );
  506. }
  507. ///////////////
  508. // DEBUGINFO //
  509. ///////////////
  510. void gxRuntime::debugInfo( const char *t ){
  511. if( !debugger ) return;
  512. Debugger *d=debugger;
  513. asyncEnd();
  514. if( !suspended ){
  515. forceSuspend();
  516. }
  517. d->debugMsg( t,false );
  518. }
  519. //////////////
  520. // DEBUGLOG //
  521. //////////////
  522. void gxRuntime::debugLog( const char *t ){
  523. if( debugger ) debugger->debugLog( t );
  524. }
  525. /////////////////////////
  526. // RETURN COMMAND LINE //
  527. /////////////////////////
  528. string gxRuntime::commandLine(){
  529. return cmd_line;
  530. }
  531. /////////////
  532. // EXECUTE //
  533. /////////////
  534. bool gxRuntime::execute( const string &cmd_line ){
  535. if( !cmd_line.size() ) return false;
  536. //convert cmd_line to cmd and params
  537. string cmd=cmd_line,params;
  538. while( cmd.size() && cmd[0]==' ' ) cmd=cmd.substr( 1 );
  539. if( cmd.find( '\"' )==0 ){
  540. int n=cmd.find( '\"',1 );
  541. if( n!=string::npos ){
  542. params=cmd.substr( n+1 );
  543. cmd=cmd.substr( 1,n-1 );
  544. }
  545. }else{
  546. int n=cmd.find( ' ' );
  547. if( n!=string::npos ){
  548. params=cmd.substr( n+1 );
  549. cmd=cmd.substr( 0,n );
  550. }
  551. }
  552. while( params.size() && params[0]==' ' ) params=params.substr( 1 );
  553. while( params.size() && params[params.size()-1]==' ' ) params=params.substr( 0,params.size()-1 );
  554. SetForegroundWindow( GetDesktopWindow() );
  555. return (int)ShellExecute( GetDesktopWindow(),0,cmd.c_str(),params.size() ? params.c_str() : 0,0,SW_SHOW )>32;
  556. }
  557. ///////////////
  558. // APP TITLE //
  559. ///////////////
  560. void gxRuntime::setTitle( const string &t,const string &e ){
  561. app_title=t;
  562. app_close=e;
  563. SetWindowText( hwnd,app_title.c_str() );
  564. }
  565. //////////////////
  566. // GETMILLISECS //
  567. //////////////////
  568. int gxRuntime::getMilliSecs(){
  569. return timeGetTime();
  570. }
  571. /////////////////////
  572. // POINTER VISIBLE //
  573. /////////////////////
  574. void gxRuntime::setPointerVisible( bool vis ){
  575. if( pointer_visible==vis ) return;
  576. pointer_visible=vis;
  577. if( gfx_mode==3 ) return;
  578. //force a WM_SETCURSOR
  579. POINT pt;
  580. GetCursorPos( &pt );
  581. SetCursorPos( pt.x,pt.y );
  582. }
  583. /////////////////
  584. // AUDIO SETUP //
  585. /////////////////
  586. gxAudio *gxRuntime::openAudio( int flags ){
  587. if( audio ) return 0;
  588. int f_flags=
  589. FSOUND_INIT_GLOBALFOCUS|
  590. FSOUND_INIT_USEDEFAULTMIDISYNTH;
  591. FSOUND_SetHWND( hwnd );
  592. if( !FSOUND_Init( 44100,1024,f_flags ) ){
  593. return 0;
  594. }
  595. audio=d_new gxAudio( this );
  596. return audio;
  597. }
  598. void gxRuntime::closeAudio( gxAudio *a ){
  599. if( !audio || audio!=a ) return;
  600. delete audio;
  601. audio=0;
  602. }
  603. /////////////////
  604. // INPUT SETUP //
  605. /////////////////
  606. gxInput *gxRuntime::openInput( int flags ){
  607. if( input ) return 0;
  608. IDirectInput7 *di;
  609. if( DirectInputCreateEx( hinst,DIRECTINPUT_VERSION,IID_IDirectInput7,(void**)&di,0 )>=0 ){
  610. input=d_new gxInput( this,di );
  611. acquireInput();
  612. }else{
  613. debugInfo( "Create DirectInput failed" );
  614. }
  615. return input;
  616. }
  617. void gxRuntime::closeInput( gxInput *i ){
  618. if( !input || input!=i ) return;
  619. unacquireInput();
  620. delete input;
  621. input=0;
  622. }
  623. /////////////////////////////////////////////////////
  624. // TIMER CALLBACK FOR AUTOREFRESH OF WINDOWED MODE //
  625. /////////////////////////////////////////////////////
  626. static void CALLBACK timerCallback( UINT id,UINT msg,DWORD user,DWORD dw1,DWORD dw2 ){
  627. if( gfx_mode ){
  628. gxCanvas *f=runtime->graphics->getFrontCanvas();
  629. if( f->getModify()!=mod_cnt ){
  630. mod_cnt=f->getModify();
  631. InvalidateRect( runtime->hwnd,0,false );
  632. }
  633. }
  634. }
  635. ////////////////////
  636. // GRAPHICS SETUP //
  637. ////////////////////
  638. void gxRuntime::backupWindowState(){
  639. GetWindowRect( hwnd,&t_rect );
  640. t_style=GetWindowLong( hwnd,GWL_STYLE );
  641. }
  642. void gxRuntime::restoreWindowState(){
  643. SetWindowLong( hwnd,GWL_STYLE,t_style );
  644. SetWindowPos(
  645. hwnd,0,t_rect.left,t_rect.top,
  646. t_rect.right-t_rect.left,t_rect.bottom-t_rect.top,
  647. SWP_NOZORDER|SWP_FRAMECHANGED );
  648. }
  649. bool gxRuntime::setDisplayMode( int w,int h,int d,bool d3d,IDirectDraw7 *dirDraw ){
  650. if( d ) return dirDraw->SetDisplayMode( w,h,d,0,0 )>=0;
  651. int best_d=0;
  652. if( d3d ){
  653. #ifdef PRO
  654. int bd=curr_driver->d3d_desc.dwDeviceRenderBitDepth;
  655. if( bd & DDBD_32 ) best_d=32;
  656. else if( bd & DDBD_24 ) best_d=24;
  657. else if( bd & DDBD_16 ) best_d=16;
  658. #endif
  659. }else{
  660. int best_n=0;
  661. for( d=16;d<=32;d+=8 ){
  662. if( dirDraw->SetDisplayMode( w,h,d,0,0 )<0 ) continue;
  663. DDCAPS caps={ sizeof(caps) };
  664. dirDraw->GetCaps( &caps,0 );
  665. int n=0;
  666. if( caps.dwCaps & DDCAPS_BLT ) ++n;
  667. if( caps.dwCaps & DDCAPS_BLTCOLORFILL ) ++n;
  668. if( caps.dwCKeyCaps & DDCKEYCAPS_SRCBLT ) ++n;
  669. if( caps.dwCaps2 & DDCAPS2_WIDESURFACES ) ++n;
  670. if( n==4 ) return true;
  671. if( n>best_n ){
  672. best_d=d;
  673. best_n=n;
  674. }
  675. dirDraw->RestoreDisplayMode();
  676. }
  677. }
  678. return best_d ? dirDraw->SetDisplayMode( w,h,best_d,0,0 )>=0 : false;
  679. }
  680. gxGraphics *gxRuntime::openWindowedGraphics( int w,int h,int d,bool d3d ){
  681. IDirectDraw7 *dd;
  682. if( DirectDrawCreateEx( curr_driver->guid,(void**)&dd,IID_IDirectDraw7,0 )<0 ) return 0;
  683. //set coop level
  684. if( dd->SetCooperativeLevel( hwnd,DDSCL_NORMAL )>=0 ){
  685. //create primary surface
  686. IDirectDrawSurface7 *ps;
  687. DDSURFACEDESC2 desc={sizeof(desc)};
  688. desc.dwFlags=DDSD_CAPS;
  689. desc.ddsCaps.dwCaps=DDSCAPS_PRIMARYSURFACE;
  690. if( dd->CreateSurface( &desc,&ps,0 )>=0 ){
  691. //create clipper
  692. IDirectDrawClipper *cp;
  693. if( dd->CreateClipper( 0,&cp,0 )>=0 ){
  694. //attach clipper
  695. if( ps->SetClipper( cp )>=0 ){
  696. //set clipper HWND
  697. if( cp->SetHWnd( 0,hwnd )>=0 ){
  698. //create front buffer
  699. IDirectDrawSurface7 *fs;
  700. DDSURFACEDESC2 desc={sizeof(desc)};
  701. desc.dwFlags=DDSD_WIDTH|DDSD_HEIGHT|DDSD_CAPS;
  702. desc.dwWidth=w;desc.dwHeight=h;
  703. desc.ddsCaps.dwCaps=DDSCAPS_OFFSCREENPLAIN;
  704. if( d3d ) desc.ddsCaps.dwCaps|=DDSCAPS_3DDEVICE;
  705. if( dd->CreateSurface( &desc,&fs,0 )>=0 ){
  706. if( timerID=timeSetEvent( 100,10,timerCallback,0,TIME_PERIODIC ) ){
  707. //Success!
  708. clipper=cp;
  709. primSurf=ps;
  710. mod_cnt=0;
  711. fs->AddRef();
  712. return d_new gxGraphics( this,dd,fs,fs,d3d );
  713. }
  714. fs->Release();
  715. }
  716. }
  717. }
  718. cp->Release();
  719. }
  720. ps->Release();
  721. }
  722. }
  723. dd->Release();
  724. return 0;
  725. }
  726. gxGraphics *gxRuntime::openExclusiveGraphics( int w,int h,int d,bool d3d ){
  727. IDirectDraw7 *dd;
  728. if( DirectDrawCreateEx( curr_driver->guid,(void**)&dd,IID_IDirectDraw7,0 )<0 ) return 0;
  729. //Set coop level
  730. if( dd->SetCooperativeLevel( hwnd,DDSCL_EXCLUSIVE|DDSCL_FULLSCREEN|DDSCL_ALLOWREBOOT )>=0 ){
  731. //Set display mode
  732. if( setDisplayMode( w,h,d,d3d,dd ) ){
  733. //create primary surface
  734. IDirectDrawSurface7 *ps;
  735. DDSURFACEDESC2 desc={sizeof(desc)};
  736. desc.dwFlags=DDSD_CAPS|DDSD_BACKBUFFERCOUNT;
  737. desc.ddsCaps.dwCaps=DDSCAPS_PRIMARYSURFACE|DDSCAPS_COMPLEX|DDSCAPS_FLIP;
  738. desc.dwBackBufferCount=1;
  739. if( d3d ) desc.ddsCaps.dwCaps|=DDSCAPS_3DDEVICE;
  740. if( dd->CreateSurface( &desc,&ps,0 )>=0 ){
  741. //find back surface
  742. IDirectDrawSurface7 *bs;
  743. DDSCAPS2 caps={sizeof caps};
  744. caps.dwCaps=DDSCAPS_BACKBUFFER;
  745. if( ps->GetAttachedSurface( &caps,&bs )>=0 ){
  746. return d_new gxGraphics( this,dd,ps,bs,d3d );
  747. }
  748. ps->Release();
  749. }
  750. dd->RestoreDisplayMode();
  751. }
  752. }
  753. dd->Release();
  754. return 0;
  755. }
  756. gxGraphics *gxRuntime::openGraphics( int w,int h,int d,int driver,int flags ){
  757. if( graphics ) return 0;
  758. busy=true;
  759. bool d3d=flags & gxGraphics::GRAPHICS_3D ? true : false;
  760. bool windowed=flags & gxGraphics::GRAPHICS_WINDOWED ? true : false;
  761. if( windowed ) driver=0;
  762. curr_driver=drivers[driver];
  763. if( windowed ){
  764. if( graphics=openWindowedGraphics( w,h,d,d3d ) ){
  765. gfx_mode=(flags & gxGraphics::GRAPHICS_SCALED) ? 1 : 2;
  766. auto_suspend=(flags & gxGraphics::GRAPHICS_AUTOSUSPEND) ? true : false;
  767. int ws,ww,hh;
  768. if( gfx_mode==1 ){
  769. ws=scaled_ws;
  770. RECT c_r;
  771. GetClientRect( hwnd,&c_r );
  772. ww=c_r.right-c_r.left;
  773. hh=c_r.bottom-c_r.top;
  774. }else{
  775. ws=static_ws;
  776. ww=w;
  777. hh=h;
  778. }
  779. SetWindowLong( hwnd,GWL_STYLE,ws );
  780. SetWindowPos( hwnd,0,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED );
  781. RECT w_r,c_r;
  782. GetWindowRect( hwnd,&w_r );
  783. GetClientRect( hwnd,&c_r );
  784. int tw=(w_r.right-w_r.left)-(c_r.right-c_r.left);
  785. int th=(w_r.bottom-w_r.top)-(c_r.bottom-c_r.top );
  786. int cx=( GetSystemMetrics( SM_CXSCREEN )-ww )/2;
  787. int cy=( GetSystemMetrics( SM_CYSCREEN )-hh )/2;
  788. POINT zz={0,0};
  789. ClientToScreen( hwnd,&zz );
  790. int bw=zz.x-w_r.left,bh=zz.y-w_r.top;
  791. int wx=cx-bw,wy=cy-bh;if( wy<0 ) wy=0; //not above top!
  792. MoveWindow( hwnd,wx,wy,ww+tw,hh+th,true );
  793. }
  794. }else{
  795. backupWindowState();
  796. SetWindowLong( hwnd,GWL_STYLE,WS_VISIBLE|WS_POPUP );
  797. SetWindowPos( hwnd,0,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED );
  798. ShowCursor( 0 );
  799. if( graphics=openExclusiveGraphics( w,h,d,d3d ) ){
  800. gfx_mode=3;
  801. auto_suspend=true;
  802. SetCursorPos(0,0);
  803. acquireInput();
  804. }else{
  805. ShowCursor( 1 );
  806. restoreWindowState();
  807. }
  808. }
  809. if( !graphics ) curr_driver=0;
  810. gfx_lost=false;
  811. busy=false;
  812. return graphics;
  813. }
  814. void gxRuntime::closeGraphics( gxGraphics *g ){
  815. if( !graphics || graphics!=g ) return;
  816. auto_suspend=false;
  817. busy=true;
  818. unacquireInput();
  819. if( timerID ){ timeKillEvent( timerID );timerID=0; }
  820. if( clipper ){ clipper->Release();clipper=0; }
  821. if( primSurf ){ primSurf->Release();primSurf=0; }
  822. delete graphics;graphics=0;
  823. if( gfx_mode==3 ){
  824. ShowCursor( 1 );
  825. restoreWindowState();
  826. }
  827. gfx_mode=0;
  828. gfx_lost=false;
  829. busy=false;
  830. }
  831. bool gxRuntime::graphicsLost(){
  832. return gfx_lost;
  833. }
  834. gxFileSystem *gxRuntime::openFileSystem( int flags ){
  835. if( fileSystem ) return 0;
  836. fileSystem=d_new gxFileSystem();
  837. return fileSystem;
  838. }
  839. void gxRuntime::closeFileSystem( gxFileSystem *f ){
  840. if( !fileSystem || fileSystem!=f ) return;
  841. delete fileSystem;
  842. fileSystem=0;
  843. }
  844. ////////////////////
  845. // GFX ENUM STUFF //
  846. ////////////////////
  847. static HRESULT WINAPI enumMode( DDSURFACEDESC2 *desc,void *context ){
  848. int dp=desc->ddpfPixelFormat.dwRGBBitCount;
  849. if( dp==16 || dp==24 || dp==32 ){
  850. gxRuntime::GfxMode *m=d_new gxRuntime::GfxMode;
  851. m->desc=*desc;
  852. gxRuntime::GfxDriver *d=(gxRuntime::GfxDriver*)context;
  853. d->modes.push_back( m );
  854. }
  855. return DDENUMRET_OK;
  856. }
  857. #ifdef PRO
  858. static int maxDevType;
  859. static HRESULT CALLBACK enumDevice( char *desc,char *name,D3DDEVICEDESC7 *devDesc,void *context ){
  860. int t=0;
  861. GUID guid=devDesc->deviceGUID;
  862. if( guid==IID_IDirect3DRGBDevice ) t=1;
  863. else if( guid==IID_IDirect3DHALDevice ) t=2;
  864. else if( guid==IID_IDirect3DTnLHalDevice ) t=3;
  865. if( t>1 && t>maxDevType ){
  866. maxDevType=t;
  867. gxRuntime::GfxDriver *d=(gxRuntime::GfxDriver*)context;
  868. d->d3d_desc=*devDesc;
  869. }
  870. return D3DENUMRET_OK;
  871. }
  872. #endif
  873. static BOOL WINAPI enumDriver( GUID FAR *guid,LPSTR desc,LPSTR name,LPVOID context,HMONITOR hm ){
  874. IDirectDraw7 *dd;
  875. if( DirectDrawCreateEx( guid,(void**)&dd,IID_IDirectDraw7,0 )<0 ) return 0;
  876. if( !guid && !desktop_desc.ddpfPixelFormat.dwRGBBitCount ){
  877. desktop_desc.dwSize=sizeof(desktop_desc);
  878. dd->GetDisplayMode( &desktop_desc );
  879. }
  880. gxRuntime::GfxDriver *d=d_new gxRuntime::GfxDriver;
  881. d->guid=guid ? d_new GUID( *guid ) : 0;
  882. d->name=desc;//string( name )+" "+string( desc );
  883. #ifdef PRO
  884. memset( &d->d3d_desc,0,sizeof(d->d3d_desc) );
  885. IDirect3D7 *dir3d;
  886. if( dd->QueryInterface( IID_IDirect3D7,(void**)&dir3d )>=0 ){
  887. maxDevType=0;
  888. dir3d->EnumDevices( enumDevice,d );
  889. dir3d->Release();
  890. }
  891. #endif
  892. vector<gxRuntime::GfxDriver*> *drivers=(vector<gxRuntime::GfxDriver*>*)context;
  893. drivers->push_back( d );
  894. dd->EnumDisplayModes( 0,0,d,enumMode );
  895. dd->Release();
  896. return 1;
  897. }
  898. void gxRuntime::enumGfx(){
  899. denumGfx();
  900. if( enum_all ){
  901. DirectDrawEnumerateEx( enumDriver,&drivers,DDENUM_ATTACHEDSECONDARYDEVICES|DDENUM_NONDISPLAYDEVICES );
  902. }else{
  903. DirectDrawEnumerateEx( enumDriver,&drivers,0 );
  904. }
  905. }
  906. void gxRuntime::denumGfx(){
  907. for( int k=0;k<drivers.size();++k ){
  908. gxRuntime::GfxDriver *d=drivers[k];
  909. for( int j=0;j<d->modes.size();++j ) delete d->modes[j];
  910. delete d->guid;
  911. delete d;
  912. }
  913. drivers.clear();
  914. }
  915. int gxRuntime::numGraphicsDrivers(){
  916. if( !enum_all ){
  917. enum_all=true;
  918. enumGfx();
  919. }
  920. return drivers.size();
  921. }
  922. void gxRuntime::graphicsDriverInfo( int driver,string *name,int *c ){
  923. GfxDriver *g=drivers[driver];
  924. int caps=0;
  925. #ifdef PRO
  926. if( g->d3d_desc.dwDeviceRenderBitDepth ) caps|=GFXMODECAPS_3D;
  927. #endif
  928. *name=g->name;
  929. *c=caps;
  930. }
  931. int gxRuntime::numGraphicsModes( int driver ){
  932. return drivers[driver]->modes.size();
  933. }
  934. void gxRuntime::graphicsModeInfo( int driver,int mode,int *w,int *h,int *d,int *c ){
  935. GfxDriver *g=drivers[driver];
  936. GfxMode *m=g->modes[mode];
  937. int caps=0;
  938. #ifdef PRO
  939. int bd=0;
  940. switch( m->desc.ddpfPixelFormat.dwRGBBitCount ){
  941. case 16:bd=DDBD_16;break;
  942. case 24:bd=DDBD_24;break;
  943. case 32:bd=DDBD_32;break;
  944. }
  945. if( g->d3d_desc.dwDeviceRenderBitDepth & bd ) caps|=GFXMODECAPS_3D;
  946. #endif
  947. *w=m->desc.dwWidth;
  948. *h=m->desc.dwHeight;
  949. *d=m->desc.ddpfPixelFormat.dwRGBBitCount;
  950. *c=caps;
  951. }
  952. void gxRuntime::windowedModeInfo( int *c ){
  953. int caps=0;
  954. #ifdef PRO
  955. int bd=0;
  956. switch( desktop_desc.ddpfPixelFormat.dwRGBBitCount ){
  957. case 16:bd=DDBD_16;break;
  958. case 24:bd=DDBD_24;break;
  959. case 32:bd=DDBD_32;break;
  960. }
  961. if( drivers[0]->d3d_desc.dwDeviceRenderBitDepth & bd ) caps|=GFXMODECAPS_3D;
  962. #endif
  963. *c=caps;
  964. }
  965. gxTimer *gxRuntime::createTimer( int hertz ){
  966. gxTimer *t=d_new gxTimer( this,hertz );
  967. timers.insert( t );
  968. return t;
  969. }
  970. void gxRuntime::freeTimer( gxTimer *t ){
  971. if( !timers.count( t ) ) return;
  972. timers.erase( t );
  973. delete t;
  974. }
  975. static string toDir( string t ){
  976. if( t.size() && t[t.size()-1]!='\\' ) t+='\\';
  977. return t;
  978. }
  979. string gxRuntime::systemProperty( const std::string &p ){
  980. char buff[MAX_PATH+1];
  981. string t=tolower(p);
  982. if( t=="cpu" ){
  983. return "Intel";
  984. }else if( t=="os" ){
  985. switch( osinfo.dwMajorVersion ){
  986. case 3:
  987. switch( osinfo.dwMinorVersion ){
  988. case 51:return "Windows NT 3.1";
  989. }
  990. break;
  991. case 4:
  992. switch( osinfo.dwMinorVersion ){
  993. case 0:return "Windows 95";
  994. case 10:return "Windows 98";
  995. case 90:return "Windows ME";
  996. }
  997. break;
  998. case 5:
  999. switch( osinfo.dwMinorVersion ){
  1000. case 0:return "Windows 2000";
  1001. case 1:return "Windows XP";
  1002. case 2:return "Windows Server 2003";
  1003. }
  1004. break;
  1005. case 6:
  1006. switch( osinfo.dwMinorVersion ){
  1007. case 0:return "Windows Vista";
  1008. case 1:return "Windows 7";
  1009. }
  1010. break;
  1011. }
  1012. }else if( t=="appdir" ){
  1013. if( GetModuleFileName( 0,buff,MAX_PATH ) ){
  1014. string t=buff;
  1015. int n=t.find_last_of( '\\' );
  1016. if( n!=string::npos ) t=t.substr( 0,n );
  1017. return toDir( t );
  1018. }
  1019. }else if( t=="apphwnd" ){
  1020. return itoa( (int)hwnd );
  1021. }else if( t=="apphinstance" ){
  1022. return itoa( (int)hinst );
  1023. }else if( t=="windowsdir" ){
  1024. if( GetWindowsDirectory( buff,MAX_PATH ) ) return toDir( buff );
  1025. }else if( t=="systemdir" ){
  1026. if( GetSystemDirectory( buff,MAX_PATH ) ) return toDir( buff );
  1027. }else if( t=="tempdir" ){
  1028. if( GetTempPath( MAX_PATH,buff ) ) return toDir( buff );
  1029. }else if( t=="direct3d7" ){
  1030. if( graphics ) return itoa( (int)graphics->dir3d );
  1031. }else if( t=="direct3ddevice7" ){
  1032. if( graphics ) return itoa( (int)graphics->dir3dDev );
  1033. }else if( t=="directdraw7" ){
  1034. if( graphics ) return itoa( (int)graphics->dirDraw );
  1035. }else if( t=="directinput7" ){
  1036. if( input ) return itoa( (int)input->dirInput );
  1037. }
  1038. return "";
  1039. }
  1040. void gxRuntime::enableDirectInput( bool enable ){
  1041. if( use_di=enable ){
  1042. acquireInput();
  1043. }else{
  1044. unacquireInput();
  1045. }
  1046. }
  1047. int gxRuntime::callDll( const std::string &dll,const std::string &func,const void *in,int in_sz,void *out,int out_sz ){
  1048. map<string,gxDll*>::const_iterator lib_it=libs.find( dll );
  1049. if( lib_it==libs.end() ){
  1050. HINSTANCE h=LoadLibrary( dll.c_str() );
  1051. if( !h ) return 0;
  1052. gxDll *t=d_new gxDll;
  1053. t->hinst=h;
  1054. lib_it=libs.insert( make_pair( dll,t ) ).first;
  1055. }
  1056. gxDll *t=lib_it->second;
  1057. map<string,LibFunc>::const_iterator fun_it=t->funcs.find( func );
  1058. if( fun_it==t->funcs.end() ){
  1059. LibFunc f=(LibFunc)GetProcAddress( t->hinst,func.c_str() );
  1060. if( !f ) return 0;
  1061. fun_it=t->funcs.insert( make_pair( func,f ) ).first;
  1062. }
  1063. static void *save_esp;
  1064. _asm{
  1065. mov [save_esp],esp
  1066. };
  1067. int n=fun_it->second( in,in_sz,out,out_sz );
  1068. _asm{
  1069. mov esp,[save_esp]
  1070. };
  1071. return n;
  1072. }