main.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  1. /*************************************************************************/
  2. /* main.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "main.h"
  30. #include "os/os.h"
  31. #include "globals.h"
  32. #include "splash.h"
  33. #include "core/register_core_types.h"
  34. #include "scene/register_scene_types.h"
  35. #include "drivers/register_driver_types.h"
  36. #include "servers/register_server_types.h"
  37. #include "modules/register_module_types.h"
  38. #include "script_debugger_local.h"
  39. #include "script_debugger_remote.h"
  40. #include "message_queue.h"
  41. #include "path_remap.h"
  42. #include "input_map.h"
  43. #include "io/resource_loader.h"
  44. #include "scene/main/scene_main_loop.h"
  45. #include "script_language.h"
  46. #include "io/resource_loader.h"
  47. #include "bin/tests/test_main.h"
  48. #include "os/dir_access.h"
  49. #include "core/io/ip.h"
  50. #include "scene/resources/packed_scene.h"
  51. #include "scene/main/viewport.h"
  52. #ifdef TOOLS_ENABLED
  53. #include "tools/editor/editor_node.h"
  54. #include "tools/editor/project_manager.h"
  55. #include "tools/editor/console.h"
  56. #include "tools/pck/pck_packer.h"
  57. #endif
  58. #include "io/file_access_network.h"
  59. #include "tools/doc/doc_data.h"
  60. #include "servers/spatial_sound_server.h"
  61. #include "servers/spatial_sound_2d_server.h"
  62. #include "servers/physics_2d_server.h"
  63. #include "core/io/stream_peer_tcp.h"
  64. #include "core/os/thread.h"
  65. #include "core/io/file_access_pack.h"
  66. #include "core/io/file_access_zip.h"
  67. #include "translation.h"
  68. #include "version.h"
  69. #include "performance.h"
  70. static Globals *globals=NULL;
  71. static InputMap *input_map=NULL;
  72. static bool _start_success=false;
  73. static ScriptDebugger *script_debugger=NULL;
  74. static MessageQueue *message_queue=NULL;
  75. static Performance *performance = NULL;
  76. static PathRemap *path_remap;
  77. static PackedData *packed_data=NULL;
  78. static FileAccessNetworkClient *file_access_network_client=NULL;
  79. static TranslationServer *translation_server = NULL;
  80. static OS::VideoMode video_mode;
  81. static int video_driver_idx=-1;
  82. static int audio_driver_idx=-1;
  83. static String locale;
  84. static String unescape_cmdline(const String& p_str) {
  85. return p_str.replace("%20"," ");
  86. }
  87. //#define DEBUG_INIT
  88. #ifdef DEBUG_INIT
  89. #define MAIN_PRINT(m_txt) print_line(m_txt)
  90. #else
  91. #define MAIN_PRINT(m_txt)
  92. #endif
  93. void Main::print_help(const char* p_binary) {
  94. OS::get_singleton()->print(VERSION_FULL_NAME" (c) 2008-2015 Juan Linietsky, Ariel Manzur.\n");
  95. OS::get_singleton()->print("Usage: %s [options] [scene]\n",p_binary);
  96. OS::get_singleton()->print("Options:\n");
  97. OS::get_singleton()->print("\t-path [dir] : Path to a game, containing engine.cfg\n");
  98. #ifdef TOOLS_ENABLED
  99. OS::get_singleton()->print("\t-e,-editor : Bring up the editor instead of running the scene.\n");
  100. #endif
  101. OS::get_singleton()->print("\t-test [test] : Run a test.\n");
  102. OS::get_singleton()->print("\t\t(");
  103. const char **test_names=tests_get_names();
  104. const char* coma = "";
  105. while(*test_names) {
  106. OS::get_singleton()->print("%s%s", coma, *test_names);
  107. test_names++;
  108. coma = ", ";
  109. }
  110. OS::get_singleton()->print(")\n");
  111. OS::get_singleton()->print("\t-r WIDTHxHEIGHT\t : Request Screen Resolution\n");
  112. OS::get_singleton()->print("\t-f\t\t : Request Fullscreen\n");
  113. OS::get_singleton()->print("\t-vd DRIVER\t : Video Driver (");
  114. for (int i=0;i<OS::get_singleton()->get_video_driver_count();i++) {
  115. if (i!=0)
  116. OS::get_singleton()->print(", ");
  117. OS::get_singleton()->print("%s",OS::get_singleton()->get_video_driver_name(i));
  118. }
  119. OS::get_singleton()->print(")\n");
  120. OS::get_singleton()->print("\t-ad DRIVER\t : Audio Driver (");
  121. for (int i=0;i<OS::get_singleton()->get_audio_driver_count();i++) {
  122. if (i!=0)
  123. OS::get_singleton()->print(", ");
  124. OS::get_singleton()->print("%s",OS::get_singleton()->get_audio_driver_name(i));
  125. }
  126. OS::get_singleton()->print("\t-rthread <mode>\t : Render Thread Mode ('unsafe', 'safe', 'separate).");
  127. OS::get_singleton()->print(")\n");
  128. OS::get_singleton()->print("\t-s,-script [script] : Run a script.\n");
  129. OS::get_singleton()->print("\t-d,-debug : Debug (local stdout debugger).\n");
  130. OS::get_singleton()->print("\t-rdebug ADDRESS : Remote debug (<ip>:<port> host address).\n");
  131. OS::get_singleton()->print("\t-fdelay [msec]: Simulate high CPU load (delay each frame by [msec]).\n");
  132. OS::get_singleton()->print("\t-timescale [msec]: Simulate high CPU load (delay each frame by [msec]).\n");
  133. OS::get_singleton()->print("\t-bp : breakpoint list as source::line comma separated pairs, no spaces (%%20,%%2C,etc instead).\n");
  134. OS::get_singleton()->print("\t-v : Verbose stdout mode\n");
  135. OS::get_singleton()->print("\t-lang [locale]: Use a specific locale\n");
  136. OS::get_singleton()->print("\t-rfs <host/ip>[:<port>] : Remote FileSystem.\n");
  137. OS::get_singleton()->print("\t-rfs_pass <password> : Password for Remote FileSystem.\n");
  138. #ifdef TOOLS_ENABLED
  139. OS::get_singleton()->print("\t-doctool FILE: Dump the whole engine api to FILE in XML format. If FILE exists, it will be merged.\n");
  140. OS::get_singleton()->print("\t-nodocbase: Disallow dump the base types (used with -doctool).\n");
  141. OS::get_singleton()->print("\t-optimize FILE Save an optimized copy of scene to FILE.\n");
  142. OS::get_singleton()->print("\t-optimize_preset [preset] Use a given preset for optimization.\n");
  143. OS::get_singleton()->print("\t-export [target] Export the project using given export target.\n");
  144. #endif
  145. }
  146. Error Main::setup(const char *execpath,int argc, char *argv[],bool p_second_phase) {
  147. RID_OwnerBase::init_rid();
  148. OS::get_singleton()->initialize_core();
  149. ObjectTypeDB::init();
  150. MAIN_PRINT("Main: Initialize CORE");
  151. register_core_types();
  152. register_core_driver_types();
  153. MAIN_PRINT("Main: Initialize Globals");
  154. Thread::_main_thread_id = Thread::get_caller_ID();
  155. globals = memnew( Globals );
  156. input_map = memnew( InputMap );
  157. path_remap = memnew( PathRemap );
  158. translation_server = memnew( TranslationServer );
  159. performance = memnew( Performance );
  160. globals->add_singleton(Globals::Singleton("Performance",performance));
  161. MAIN_PRINT("Main: Parse CMDLine");
  162. /* argument parsing and main creation */
  163. List<String> args;
  164. List<String> main_args;
  165. for(int i=0;i<argc;i++) {
  166. args.push_back(String::utf8(argv[i]));
  167. }
  168. List<String>::Element *I=args.front();
  169. I=args.front();
  170. while (I) {
  171. I->get()=unescape_cmdline(I->get().strip_escapes());
  172. // print_line("CMD: "+I->get());
  173. I=I->next();
  174. }
  175. I=args.front();
  176. video_mode = OS::get_singleton()->get_default_video_mode();
  177. String video_driver="";
  178. String audio_driver="";
  179. String game_path=".";
  180. String debug_mode;
  181. String debug_host;
  182. String main_pack;
  183. bool quiet_stdout=false;
  184. int rtm=-1;
  185. String remotefs;
  186. String remotefs_pass;
  187. String screen = "";
  188. List<String> pack_list;
  189. Vector<String> breakpoints;
  190. bool use_custom_res=true;
  191. bool force_res=false;
  192. I=args.front();
  193. packed_data = PackedData::get_singleton();
  194. if (!packed_data)
  195. packed_data = memnew(PackedData);
  196. #ifdef MINIZIP_ENABLED
  197. packed_data->add_pack_source(ZipArchive::get_singleton());
  198. #endif
  199. bool editor=false;
  200. while(I) {
  201. List<String>::Element *N=I->next();
  202. if (I->get() == "-noop") {
  203. // no op
  204. } else if (I->get()=="-h" || I->get()=="--help" || I->get()=="/?") { // resolution
  205. goto error;
  206. } else if (I->get()=="-r") { // resolution
  207. if (I->next()) {
  208. String vm=I->next()->get();
  209. if (vm.find("x")==-1) { // invalid parameter format
  210. goto error;
  211. }
  212. int w=vm.get_slice("x",0).to_int();
  213. int h=vm.get_slice("x",1).to_int();
  214. if (w==0 || h==0) {
  215. goto error;
  216. }
  217. video_mode.width=w;
  218. video_mode.height=h;
  219. force_res=true;
  220. N=I->next()->next();
  221. } else {
  222. goto error;
  223. }
  224. } else if (I->get()=="-vd") { // video driver
  225. if (I->next()) {
  226. video_driver=I->next()->get();
  227. N=I->next()->next();
  228. } else {
  229. goto error;
  230. }
  231. } else if (I->get()=="-lang") { // language
  232. if (I->next()) {
  233. locale=I->next()->get();
  234. N=I->next()->next();
  235. } else {
  236. goto error;
  237. }
  238. } else if (I->get()=="-rfs") { // language
  239. if (I->next()) {
  240. remotefs=I->next()->get();
  241. N=I->next()->next();
  242. } else {
  243. goto error;
  244. }
  245. } else if (I->get()=="-rfs_pass") { // language
  246. if (I->next()) {
  247. remotefs_pass=I->next()->get();
  248. N=I->next()->next();
  249. } else {
  250. goto error;
  251. }
  252. } else if (I->get()=="-rthread") { // language
  253. if (I->next()) {
  254. if (I->next()->get()=="safe")
  255. rtm=OS::RENDER_THREAD_SAFE;
  256. else if (I->next()->get()=="unsafe")
  257. rtm=OS::RENDER_THREAD_UNSAFE;
  258. else if (I->next()->get()=="separate")
  259. rtm=OS::RENDER_SEPARATE_THREAD;
  260. N=I->next()->next();
  261. } else {
  262. goto error;
  263. }
  264. } else if (I->get()=="-ad") { // video driver
  265. if (I->next()) {
  266. audio_driver=I->next()->get();
  267. N=I->next()->next();
  268. } else {
  269. goto error;
  270. }
  271. } else if (I->get()=="-f") { // fullscreen
  272. video_mode.fullscreen=true;
  273. } else if (I->get()=="-e" || I->get()=="-editor") { // fonud editor
  274. editor=true;
  275. } else if (I->get()=="-nowindow") { // fullscreen
  276. OS::get_singleton()->set_no_window_mode(true);
  277. } else if (I->get()=="-quiet") { // fullscreen
  278. quiet_stdout=true;
  279. } else if (I->get()=="-v") { // fullscreen
  280. OS::get_singleton()->_verbose_stdout=true;
  281. } else if (I->get()=="-path") { // resolution
  282. if (I->next()) {
  283. String p = I->next()->get();
  284. if (OS::get_singleton()->set_cwd(p)==OK) {
  285. //nothing
  286. } else {
  287. game_path=I->next()->get(); //use game_path instead
  288. }
  289. N=I->next()->next();
  290. } else {
  291. goto error;
  292. }
  293. } else if (I->get()=="-bp") { // /breakpoints
  294. if (I->next()) {
  295. String bplist = I->next()->get();
  296. breakpoints= bplist.split(",");
  297. N=I->next()->next();
  298. } else {
  299. goto error;
  300. }
  301. } else if (I->get()=="-fdelay") { // resolution
  302. if (I->next()) {
  303. OS::get_singleton()->set_frame_delay(I->next()->get().to_int());
  304. N=I->next()->next();
  305. } else {
  306. goto error;
  307. }
  308. } else if (I->get()=="-timescale") { // resolution
  309. if (I->next()) {
  310. OS::get_singleton()->set_time_scale(I->next()->get().to_double());
  311. N=I->next()->next();
  312. } else {
  313. goto error;
  314. }
  315. } else if (I->get() == "-pack") {
  316. if (I->next()) {
  317. pack_list.push_back(I->next()->get());
  318. N = I->next()->next();
  319. } else {
  320. goto error;
  321. };
  322. } else if (I->get() == "-main_pack") {
  323. if (I->next()) {
  324. main_pack=I->next()->get();
  325. N = I->next()->next();
  326. } else {
  327. goto error;
  328. };
  329. } else if (I->get()=="-debug" || I->get()=="-d") {
  330. debug_mode="local";
  331. } else if (I->get()=="-editor_scene") {
  332. if (I->next()) {
  333. Globals::get_singleton()->set("editor_scene",game_path=I->next()->get());
  334. } else {
  335. goto error;
  336. }
  337. } else if (I->get()=="-rdebug") {
  338. if (I->next()) {
  339. debug_mode="remote";
  340. debug_host=I->next()->get();
  341. if (debug_host.find(":")==-1) //wrong host
  342. goto error;
  343. N=I->next()->next();
  344. } else {
  345. goto error;
  346. }
  347. } else {
  348. //test for game path
  349. bool gpfound=false;
  350. if (!I->get().begins_with("-") && game_path=="") {
  351. DirAccess* da = DirAccess::open(I->get());
  352. if (da!=NULL) {
  353. game_path=I->get();
  354. gpfound=true;
  355. memdelete(da);
  356. }
  357. }
  358. if (!gpfound) {
  359. main_args.push_back(I->get());
  360. }
  361. }
  362. I=N;
  363. }
  364. if (debug_mode == "remote") {
  365. ScriptDebuggerRemote *sdr = memnew( ScriptDebuggerRemote );
  366. uint16_t debug_port = GLOBAL_DEF("debug/remote_port",6007);
  367. if (debug_host.find(":")!=-1) {
  368. debug_port=debug_host.get_slice(":",1).to_int();
  369. debug_host=debug_host.get_slice(":",0);
  370. }
  371. Error derr = sdr->connect_to_host(debug_host,debug_port);
  372. if (derr!=OK) {
  373. memdelete(sdr);
  374. } else {
  375. script_debugger=sdr;
  376. }
  377. } else if (debug_mode=="local") {
  378. script_debugger = memnew( ScriptDebuggerLocal );
  379. }
  380. if (remotefs!="") {
  381. file_access_network_client=memnew(FileAccessNetworkClient);
  382. int port;
  383. if (remotefs.find(":")!=-1) {
  384. port=remotefs.get_slice(":",1).to_int();
  385. remotefs=remotefs.get_slice(":",0);
  386. } else {
  387. port=6010;
  388. }
  389. Error err = file_access_network_client->connect(remotefs,port,remotefs_pass);
  390. if (err) {
  391. OS::get_singleton()->printerr("Could not connect to remotefs: %s:%i\n",remotefs.utf8().get_data(),port);
  392. goto error;
  393. }
  394. FileAccess::make_default<FileAccessNetwork>(FileAccess::ACCESS_RESOURCES);
  395. }
  396. if (script_debugger) {
  397. //there is a debugger, parse breakpoints
  398. for(int i=0;i<breakpoints.size();i++) {
  399. String bp = breakpoints[i];
  400. int sp=bp.find_last(":");
  401. if (sp==-1) {
  402. ERR_EXPLAIN("Invalid breakpoint: '"+bp+"', expected file:line format.");
  403. ERR_CONTINUE(sp==-1);
  404. }
  405. script_debugger->insert_breakpoint(bp.substr(sp+1,bp.length()).to_int(),bp.substr(0,sp));
  406. }
  407. }
  408. #ifdef TOOLS_ENABLED
  409. if (editor) {
  410. packed_data->set_disabled(true);
  411. globals->set_disable_platform_override(true);
  412. }
  413. #endif
  414. if (globals->setup(game_path,main_pack)!=OK) {
  415. #ifdef TOOLS_ENABLED
  416. editor=false;
  417. #else
  418. OS::get_singleton()->print("error: Couldn't load game path '%s'\n",game_path.ascii().get_data());
  419. goto error;
  420. #endif
  421. }
  422. if (editor) {
  423. main_args.push_back("-editor");
  424. use_custom_res=false;
  425. }
  426. if (bool(Globals::get_singleton()->get("application/disable_stdout"))) {
  427. quiet_stdout=true;
  428. }
  429. if (quiet_stdout)
  430. _print_line_enabled=false;
  431. OS::get_singleton()->set_cmdline(execpath, main_args);
  432. #ifdef TOOLS_ENABLED
  433. if (main_args.size()==0 && (!Globals::get_singleton()->has("application/main_loop_type")) && (!Globals::get_singleton()->has("application/main_scene") || String(Globals::get_singleton()->get("application/main_scene"))==""))
  434. use_custom_res=false; //project manager (run without arguments)
  435. #endif
  436. input_map->load_from_globals();
  437. if (video_driver=="") // specified in engine.cfg
  438. video_driver=_GLOBAL_DEF("display/driver",Variant((const char*)OS::get_singleton()->get_video_driver_name(0)));
  439. if (!force_res && use_custom_res && globals->has("display/width"))
  440. video_mode.width=globals->get("display/width");
  441. if (!force_res &&use_custom_res && globals->has("display/height"))
  442. video_mode.height=globals->get("display/height");
  443. if (use_custom_res && globals->has("display/fullscreen"))
  444. video_mode.fullscreen=globals->get("display/fullscreen");
  445. if (use_custom_res && globals->has("display/resizable"))
  446. video_mode.resizable=globals->get("display/resizable");
  447. if (!force_res && use_custom_res && globals->has("display/test_width") && globals->has("display/test_height")) {
  448. int tw = globals->get("display/test_width");
  449. int th = globals->get("display/test_height");
  450. if (tw>0 && th>0) {
  451. video_mode.width=tw;
  452. video_mode.height=th;
  453. }
  454. }
  455. GLOBAL_DEF("display/width",video_mode.width);
  456. GLOBAL_DEF("display/height",video_mode.height);
  457. GLOBAL_DEF("display/fullscreen",video_mode.fullscreen);
  458. GLOBAL_DEF("display/resizable",video_mode.resizable);
  459. GLOBAL_DEF("display/test_width",0);
  460. GLOBAL_DEF("display/test_height",0);
  461. if (rtm==-1) {
  462. rtm=GLOBAL_DEF("render/thread_model",OS::RENDER_THREAD_SAFE);
  463. }
  464. if (rtm>=0 && rtm<3)
  465. OS::get_singleton()->_render_thread_mode=OS::RenderThreadMode(rtm);
  466. /* Determine Video Driver */
  467. if (audio_driver=="") // specified in engine.cfg
  468. audio_driver=GLOBAL_DEF("audio/driver",OS::get_singleton()->get_audio_driver_name(0));
  469. for (int i=0;i<OS::get_singleton()->get_video_driver_count();i++) {
  470. if (video_driver==OS::get_singleton()->get_video_driver_name(i)) {
  471. video_driver_idx=i;
  472. break;
  473. }
  474. }
  475. if (video_driver_idx<0) {
  476. OS::get_singleton()->alert( "Invalid Video Driver: "+video_driver );
  477. video_driver_idx = 0;
  478. //goto error;
  479. }
  480. for (int i=0;i<OS::get_singleton()->get_audio_driver_count();i++) {
  481. if (audio_driver==OS::get_singleton()->get_audio_driver_name(i)) {
  482. audio_driver_idx=i;
  483. break;
  484. }
  485. }
  486. if (audio_driver_idx<0) {
  487. OS::get_singleton()->alert( "Invalid Audio Driver: "+audio_driver );
  488. goto error;
  489. }
  490. {
  491. String orientation = GLOBAL_DEF("display/orientation","landscape");
  492. if (orientation=="portrait")
  493. OS::get_singleton()->set_screen_orientation(OS::SCREEN_PORTRAIT);
  494. else if (orientation=="reverse_landscape")
  495. OS::get_singleton()->set_screen_orientation(OS::SCREEN_REVERSE_LANDSCAPE);
  496. else if (orientation=="reverse_portrait")
  497. OS::get_singleton()->set_screen_orientation(OS::SCREEN_REVERSE_PORTRAIT);
  498. else if (orientation=="sensor_landscape")
  499. OS::get_singleton()->set_screen_orientation(OS::SCREEN_SENSOR_LANDSCAPE);
  500. else if (orientation=="sensor_portrait")
  501. OS::get_singleton()->set_screen_orientation(OS::SCREEN_SENSOR_PORTRAIT);
  502. else if (orientation=="sensor")
  503. OS::get_singleton()->set_screen_orientation(OS::SCREEN_SENSOR);
  504. else
  505. OS::get_singleton()->set_screen_orientation(OS::SCREEN_LANDSCAPE);
  506. }
  507. OS::get_singleton()->set_iterations_per_second(GLOBAL_DEF("physics/fixed_fps",60));
  508. OS::get_singleton()->set_target_fps(GLOBAL_DEF("application/target_fps",0));
  509. if (!OS::get_singleton()->_verbose_stdout) //overrided
  510. OS::get_singleton()->_verbose_stdout=GLOBAL_DEF("debug/verbose_stdout",false);
  511. message_queue = memnew( MessageQueue );
  512. Globals::get_singleton()->register_global_defaults();
  513. if (p_second_phase)
  514. return setup2();
  515. return OK;
  516. error:
  517. video_driver="";
  518. audio_driver="";
  519. game_path="";
  520. args.clear();
  521. main_args.clear();
  522. print_help(execpath);
  523. if (performance)
  524. memdelete(performance);
  525. if (input_map)
  526. memdelete(input_map);
  527. if (translation_server)
  528. memdelete( translation_server );
  529. if (globals)
  530. memdelete(globals);
  531. if (script_debugger)
  532. memdelete(script_debugger);
  533. if (packed_data)
  534. memdelete(packed_data);
  535. if (file_access_network_client)
  536. memdelete(file_access_network_client);
  537. unregister_core_types();
  538. OS::get_singleton()->_cmdline.clear();
  539. if (message_queue)
  540. memdelete( message_queue);
  541. OS::get_singleton()->finalize_core();
  542. locale=String();
  543. return ERR_INVALID_PARAMETER;
  544. }
  545. Error Main::setup2() {
  546. OS::get_singleton()->initialize(video_mode,video_driver_idx,audio_driver_idx);
  547. register_core_singletons();
  548. MAIN_PRINT("Main: Setup Logo");
  549. bool show_logo=true;
  550. #ifdef JAVASCRIPT_ENABLED
  551. show_logo=false;
  552. #endif
  553. if (show_logo) { //boot logo!
  554. Image boot_logo=GLOBAL_DEF("application/boot_logo",Image());
  555. if (!boot_logo.empty()) {
  556. Color clear = GLOBAL_DEF("render/default_clear_color",Color(0.3,0.3,0.3));
  557. VisualServer::get_singleton()->set_default_clear_color(clear);
  558. Color boot_bg = GLOBAL_DEF("application/boot_bg_color", clear);
  559. VisualServer::get_singleton()->set_boot_image(boot_logo, boot_bg);
  560. #ifndef TOOLS_ENABLED
  561. //no tools, so free the boot logo (no longer needed)
  562. Globals::get_singleton()->set("application/boot_logo",Image());
  563. #endif
  564. } else {
  565. #ifndef NO_DEFAULT_BOOT_LOGO
  566. MAIN_PRINT("Main: Create botsplash");
  567. Image splash(boot_splash_png);
  568. MAIN_PRINT("Main: ClearColor");
  569. VisualServer::get_singleton()->set_default_clear_color(boot_splash_bg_color);
  570. MAIN_PRINT("Main: Image");
  571. VisualServer::get_singleton()->set_boot_image(splash, boot_splash_bg_color);
  572. #endif
  573. MAIN_PRINT("Main: DCC");
  574. VisualServer::get_singleton()->set_default_clear_color(GLOBAL_DEF("render/default_clear_color",Color(0.3,0.3,0.3)));
  575. MAIN_PRINT("Main: END");
  576. }
  577. Image icon(app_icon_png);
  578. OS::get_singleton()->set_icon(icon);
  579. }
  580. GLOBAL_DEF("application/icon",String());
  581. Globals::get_singleton()->set_custom_property_info("application/icon",PropertyInfo(Variant::STRING,"application/icon",PROPERTY_HINT_FILE,"*.png,*.webp"));
  582. MAIN_PRINT("Main: Load Remaps");
  583. path_remap->load_remaps();
  584. MAIN_PRINT("Main: Load Scene Types");
  585. register_scene_types();
  586. register_server_types();
  587. #ifdef TOOLS_ENABLED
  588. EditorNode::register_editor_types();
  589. ObjectTypeDB::register_type<PCKPacker>(); // todo: move somewhere else
  590. #endif
  591. MAIN_PRINT("Main: Load Scripts, Modules, Drivers");
  592. register_module_types();
  593. register_driver_types();
  594. ScriptServer::init_languages();
  595. MAIN_PRINT("Main: Load Translations");
  596. translation_server->setup(); //register translations, load them, etc.
  597. if (locale!="") {
  598. translation_server->set_locale(locale);
  599. }
  600. translation_server->load_translations();
  601. _start_success=true;
  602. locale=String();
  603. MAIN_PRINT("Main: Done");
  604. return OK;
  605. }
  606. bool Main::start() {
  607. ERR_FAIL_COND_V(!_start_success,false);
  608. bool editor=false;
  609. String doc_tool;
  610. bool doc_base=true;
  611. String game_path;
  612. String script;
  613. String test;
  614. String screen;
  615. String optimize;
  616. String optimize_preset;
  617. String _export_platform;
  618. String _import;
  619. String _import_script;
  620. String dumpstrings;
  621. bool noquit=false;
  622. bool convert_old=false;
  623. bool export_debug=false;
  624. List<String> args = OS::get_singleton()->get_cmdline_args();
  625. for (int i=0;i<args.size();i++) {
  626. if (args[i]=="-doctool" && i <(args.size()-1)) {
  627. doc_tool=args[i+1];
  628. i++;
  629. }else if (args[i]=="-nodocbase") {
  630. doc_base=false;
  631. } else if ((args[i]=="-script" || args[i]=="-s") && i <(args.size()-1)) {
  632. script=args[i+1];
  633. i++;
  634. } else if ((args[i]=="-level" || args[i]=="-l") && i <(args.size()-1)) {
  635. OS::get_singleton()->_custom_level=args[i+1];
  636. i++;
  637. } else if (args[i]=="-test" && i <(args.size()-1)) {
  638. test=args[i+1];
  639. i++;
  640. } else if (args[i]=="-optimize" && i <(args.size()-1)) {
  641. optimize=args[i+1];
  642. i++;
  643. } else if (args[i]=="-optimize_preset" && i <(args.size()-1)) {
  644. optimize_preset=args[i+1];
  645. i++;
  646. } else if (args[i]=="-export" && i <(args.size()-1)) {
  647. editor=true; //needs editor
  648. _export_platform=args[i+1];
  649. i++;
  650. } else if (args[i]=="-export_debug" && i <(args.size()-1)) {
  651. editor=true; //needs editor
  652. _export_platform=args[i+1];
  653. export_debug=true;
  654. i++;
  655. } else if (args[i]=="-import" && i <(args.size()-1)) {
  656. editor=true; //needs editor
  657. _import=args[i+1];
  658. i++;
  659. } else if (args[i]=="-import_script" && i <(args.size()-1)) {
  660. editor=true; //needs editor
  661. _import_script=args[i+1];
  662. i++;
  663. } else if (args[i]=="-noquit" ) {
  664. noquit=true;
  665. } else if (args[i]=="-dumpstrings" && i <(args.size()-1)) {
  666. editor=true; //needs editor
  667. dumpstrings=args[i+1];
  668. i++;
  669. } else if (args[i]=="-editor" || args[i]=="-e") {
  670. editor=true;
  671. } else if (args[i]=="-convert_old") {
  672. convert_old=true;
  673. } else if (args[i].length() && args[i][0] != '-' && game_path == "") {
  674. game_path=args[i];
  675. }
  676. }
  677. if (editor)
  678. Globals::get_singleton()->set("editor_active",true);
  679. String main_loop_type;
  680. #ifdef TOOLS_ENABLED
  681. if(doc_tool!="") {
  682. DocData doc;
  683. doc.generate(doc_base);
  684. DocData docsrc;
  685. if (docsrc.load(doc_tool)==OK) {
  686. print_line("Doc exists. Merging..");
  687. doc.merge_from(docsrc);
  688. } else {
  689. print_line("No Doc exists. Generating empty.");
  690. }
  691. doc.save(doc_tool);
  692. return false;
  693. }
  694. if (optimize!="")
  695. editor=true; //need editor
  696. #endif
  697. if(script=="" && game_path=="" && !editor && String(GLOBAL_DEF("application/main_scene",""))!="") {
  698. game_path=GLOBAL_DEF("application/main_scene","");
  699. }
  700. MainLoop *main_loop=NULL;
  701. if (editor) {
  702. main_loop = memnew(SceneTree);
  703. };
  704. if (test!="") {
  705. #ifdef DEBUG_ENABLED
  706. main_loop = test_main(test,args);
  707. if (!main_loop)
  708. return false;
  709. #endif
  710. } else if (script!="") {
  711. Ref<Script> script_res = ResourceLoader::load(script);
  712. ERR_EXPLAIN("Can't load script: "+script);
  713. ERR_FAIL_COND_V(script_res.is_null(),false);
  714. if( script_res->can_instance() /*&& script_res->inherits_from("SceneTreeScripted")*/) {
  715. StringName instance_type=script_res->get_instance_base_type();
  716. Object *obj = ObjectTypeDB::instance(instance_type);
  717. MainLoop *script_loop = obj?obj->cast_to<MainLoop>():NULL;
  718. if (!script_loop) {
  719. if (obj)
  720. memdelete(obj);
  721. ERR_EXPLAIN("Can't load script '"+script+"', it does not inherit from a MainLoop type");
  722. ERR_FAIL_COND_V(!script_loop,false);
  723. }
  724. script_loop->set_init_script(script_res);
  725. main_loop=script_loop;
  726. } else {
  727. return false;
  728. }
  729. } else {
  730. main_loop_type=GLOBAL_DEF("application/main_loop_type","");
  731. }
  732. if (!main_loop && main_loop_type=="")
  733. main_loop_type="SceneTree";
  734. if (!main_loop) {
  735. if (!ObjectTypeDB::type_exists(main_loop_type)) {
  736. OS::get_singleton()->alert("godot: error: MainLoop type doesn't exist: "+main_loop_type);
  737. return false;
  738. } else {
  739. Object *ml = ObjectTypeDB::instance(main_loop_type);
  740. if (!ml) {
  741. ERR_EXPLAIN("Can't instance MainLoop type");
  742. ERR_FAIL_V(false);
  743. }
  744. main_loop=ml->cast_to<MainLoop>();
  745. if (!main_loop) {
  746. memdelete(ml);
  747. ERR_EXPLAIN("Invalid MainLoop type");
  748. ERR_FAIL_V(false);
  749. }
  750. }
  751. }
  752. if (main_loop->is_type("SceneTree")) {
  753. SceneTree *sml = main_loop->cast_to<SceneTree>();
  754. #ifdef TOOLS_ENABLED
  755. EditorNode *editor_node=NULL;
  756. if (editor) {
  757. editor_node = memnew( EditorNode );
  758. sml->get_root()->add_child(editor_node);
  759. //root_node->set_editor(editor);
  760. //startup editor
  761. if (_export_platform!="") {
  762. editor_node->export_platform(_export_platform,game_path,export_debug,"",true);
  763. game_path=""; //no load anything
  764. }
  765. }
  766. #endif
  767. if (!editor) {
  768. //standard helpers that can be changed from main config
  769. String stretch_mode = GLOBAL_DEF("display/stretch_mode","disabled");
  770. String stretch_aspect = GLOBAL_DEF("display/stretch_aspect","ignore");
  771. Size2i stretch_size = Size2(GLOBAL_DEF("display/width",0),GLOBAL_DEF("display/height",0));
  772. SceneTree::StretchMode sml_sm=SceneTree::STRETCH_MODE_DISABLED;
  773. if (stretch_mode=="2d")
  774. sml_sm=SceneTree::STRETCH_MODE_2D;
  775. else if (stretch_mode=="viewport")
  776. sml_sm=SceneTree::STRETCH_MODE_VIEWPORT;
  777. SceneTree::StretchAspect sml_aspect=SceneTree::STRETCH_ASPECT_IGNORE;
  778. if (stretch_aspect=="keep")
  779. sml_aspect=SceneTree::STRETCH_ASPECT_KEEP;
  780. else if (stretch_aspect=="keep_width")
  781. sml_aspect=SceneTree::STRETCH_ASPECT_KEEP_WIDTH;
  782. else if (stretch_aspect=="keep_height")
  783. sml_aspect=SceneTree::STRETCH_ASPECT_KEEP_HEIGHT;
  784. sml->set_screen_stretch(sml_sm,sml_aspect,stretch_size);
  785. sml->set_auto_accept_quit(GLOBAL_DEF("application/auto_accept_quit",true));
  786. String appname = Globals::get_singleton()->get("application/name");
  787. appname = TranslationServer::get_singleton()->translate(appname);
  788. OS::get_singleton()->set_window_title(appname);
  789. } else {
  790. GLOBAL_DEF("display/stretch_mode","disabled");
  791. Globals::get_singleton()->set_custom_property_info("display/stretch_mode",PropertyInfo(Variant::STRING,"display/stretch_mode",PROPERTY_HINT_ENUM,"disabled,2d,viewport"));
  792. GLOBAL_DEF("display/stretch_aspect","ignore");
  793. Globals::get_singleton()->set_custom_property_info("display/stretch_aspect",PropertyInfo(Variant::STRING,"display/stretch_aspect",PROPERTY_HINT_ENUM,"ignore,keep,keep_width,keep_height"));
  794. sml->set_auto_accept_quit(GLOBAL_DEF("application/auto_accept_quit",true));
  795. }
  796. if (game_path!="") {
  797. String local_game_path=game_path.replace("\\","/");
  798. if (!local_game_path.begins_with("res://")) {
  799. bool absolute=(local_game_path.size()>1) && (local_game_path[0]=='/' || local_game_path[1]==':');
  800. if (!absolute) {
  801. if (Globals::get_singleton()->is_using_datapack()) {
  802. local_game_path="res://"+local_game_path;
  803. } else {
  804. int sep=local_game_path.find_last("/");
  805. if (sep==-1) {
  806. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  807. local_game_path=da->get_current_dir()+"/"+local_game_path;
  808. memdelete(da) ;
  809. } else {
  810. DirAccess *da = DirAccess::open(local_game_path.substr(0,sep));
  811. if (da) {
  812. local_game_path=da->get_current_dir()+"/"+local_game_path.substr(sep+1,local_game_path.length());;
  813. memdelete(da);
  814. }
  815. }
  816. }
  817. }
  818. }
  819. local_game_path=Globals::get_singleton()->localize_path(local_game_path);
  820. #ifdef TOOLS_ENABLED
  821. if (editor) {
  822. if (_import!="") {
  823. //editor_node->import_scene(_import,local_game_path,_import_script);
  824. if (!noquit)
  825. sml->quit();
  826. game_path=""; //no load anything
  827. } else {
  828. Error serr = editor_node->load_scene(local_game_path);
  829. if (serr==OK) {
  830. if (optimize!="") {
  831. editor_node->save_optimized_copy(optimize,optimize_preset);
  832. if (!noquit)
  833. sml->quit();
  834. }
  835. if (dumpstrings!="") {
  836. editor_node->save_translatable_strings(dumpstrings);
  837. if (!noquit)
  838. sml->quit();
  839. }
  840. }
  841. }
  842. //editor_node->set_edited_scene(game);
  843. } else {
  844. #endif
  845. {
  846. //autoload
  847. List<PropertyInfo> props;
  848. Globals::get_singleton()->get_property_list(&props);
  849. for(List<PropertyInfo>::Element *E=props.front();E;E=E->next()) {
  850. String s = E->get().name;
  851. if (!s.begins_with("autoload/"))
  852. continue;
  853. String name = s.get_slice("/",1);
  854. String path = Globals::get_singleton()->get(s);
  855. RES res = ResourceLoader::load(path);
  856. ERR_EXPLAIN("Can't autoload: "+path);
  857. ERR_CONTINUE(res.is_null());
  858. Node *n=NULL;
  859. if (res->is_type("PackedScene")) {
  860. Ref<PackedScene> ps = res;
  861. n=ps->instance();
  862. } else if (res->is_type("Script")) {
  863. Ref<Script> s = res;
  864. StringName ibt = s->get_instance_base_type();
  865. ERR_EXPLAIN("Script does not inherit a Node: "+path);
  866. ERR_CONTINUE( !ObjectTypeDB::is_type(ibt,"Node") );
  867. Object *obj = ObjectTypeDB::instance(ibt);
  868. ERR_EXPLAIN("Cannot instance node for autoload type: "+String(ibt));
  869. ERR_CONTINUE( obj==NULL );
  870. n = obj->cast_to<Node>();
  871. n->set_script(s.get_ref_ptr());
  872. }
  873. ERR_EXPLAIN("Path in autoload not a node or script: "+path);
  874. ERR_CONTINUE(!n);
  875. n->set_name(name);
  876. sml->get_root()->add_child(n);
  877. }
  878. }
  879. Node *scene=NULL;
  880. Ref<PackedScene> scenedata = ResourceLoader::load(local_game_path);
  881. if (scenedata.is_valid())
  882. scene=scenedata->instance();
  883. ERR_EXPLAIN("Failed loading scene: "+local_game_path);
  884. ERR_FAIL_COND_V(!scene,false)
  885. sml->get_root()->add_child(scene);
  886. String iconpath = GLOBAL_DEF("application/icon","Variant()""");
  887. if (iconpath!="") {
  888. Image icon;
  889. if (icon.load(iconpath)==OK)
  890. OS::get_singleton()->set_icon(icon);
  891. }
  892. //singletons
  893. #ifdef TOOLS_ENABLED
  894. }
  895. #endif
  896. }
  897. #ifdef TOOLS_ENABLED
  898. /*if (_export_platform!="") {
  899. sml->quit();
  900. }*/
  901. /*
  902. if (sml->get_root_node()) {
  903. Console *console = memnew( Console );
  904. sml->get_root_node()->cast_to<RootNode>()->set_console(console);
  905. if (GLOBAL_DEF("console/visible_default",false).operator bool()) {
  906. console->show();
  907. } else {P
  908. console->hide();
  909. };
  910. }
  911. */
  912. if (script=="" && test=="" && game_path=="" && !editor) {
  913. ProjectManager *pmanager = memnew( ProjectManager );
  914. sml->get_root()->add_child(pmanager);
  915. }
  916. #endif
  917. }
  918. OS::get_singleton()->set_main_loop( main_loop );
  919. return true;
  920. }
  921. uint64_t Main::last_ticks=0;
  922. uint64_t Main::target_ticks=0;
  923. float Main::time_accum=0;
  924. uint32_t Main::frames=0;
  925. uint32_t Main::frame=0;
  926. bool Main::force_redraw_requested = false;
  927. static uint64_t fixed_process_max=0;
  928. static uint64_t idle_process_max=0;
  929. bool Main::iteration() {
  930. uint64_t ticks=OS::get_singleton()->get_ticks_usec();
  931. uint64_t ticks_elapsed=ticks-last_ticks;
  932. frame+=ticks_elapsed;
  933. last_ticks=ticks;
  934. double step=(double)ticks_elapsed / 1000000.0;
  935. float frame_slice=1.0/OS::get_singleton()->get_iterations_per_second();
  936. if (step>frame_slice*8)
  937. step=frame_slice*8;
  938. time_accum+=step;
  939. float time_scale = OS::get_singleton()->get_time_scale();
  940. bool exit=false;
  941. int iters = 0;
  942. while(time_accum>frame_slice) {
  943. uint64_t fixed_begin = OS::get_singleton()->get_ticks_usec();
  944. PhysicsServer::get_singleton()->sync();
  945. PhysicsServer::get_singleton()->flush_queries();
  946. Physics2DServer::get_singleton()->sync();
  947. Physics2DServer::get_singleton()->flush_queries();
  948. if (OS::get_singleton()->get_main_loop()->iteration( frame_slice*time_scale )) {
  949. exit=true;
  950. break;
  951. }
  952. message_queue->flush();
  953. PhysicsServer::get_singleton()->step(frame_slice*time_scale);
  954. Physics2DServer::get_singleton()->step(frame_slice*time_scale);
  955. time_accum-=frame_slice;
  956. message_queue->flush();
  957. //if (AudioServer::get_singleton())
  958. // AudioServer::get_singleton()->update();
  959. fixed_process_max=MAX(OS::get_singleton()->get_ticks_usec()-fixed_begin,fixed_process_max);
  960. iters++;
  961. }
  962. uint64_t idle_begin = OS::get_singleton()->get_ticks_usec();
  963. OS::get_singleton()->get_main_loop()->idle( step*time_scale );
  964. message_queue->flush();
  965. if (SpatialSoundServer::get_singleton())
  966. SpatialSoundServer::get_singleton()->update( step*time_scale );
  967. if (SpatialSound2DServer::get_singleton())
  968. SpatialSound2DServer::get_singleton()->update( step*time_scale );
  969. if (OS::get_singleton()->can_draw()) {
  970. if ((!force_redraw_requested) && OS::get_singleton()->is_in_low_processor_usage_mode()) {
  971. if (VisualServer::get_singleton()->has_changed()) {
  972. VisualServer::get_singleton()->draw(); // flush visual commands
  973. OS::get_singleton()->frames_drawn++;
  974. }
  975. } else {
  976. VisualServer::get_singleton()->draw(); // flush visual commands
  977. OS::get_singleton()->frames_drawn++;
  978. force_redraw_requested = false;
  979. }
  980. } else {
  981. VisualServer::get_singleton()->flush(); // flush visual commands
  982. }
  983. if (AudioServer::get_singleton())
  984. AudioServer::get_singleton()->update();
  985. for(int i=0;i<ScriptServer::get_language_count();i++) {
  986. ScriptServer::get_language(i)->frame();
  987. }
  988. idle_process_max=MAX(OS::get_singleton()->get_ticks_usec()-idle_begin,idle_process_max);
  989. if (script_debugger)
  990. script_debugger->idle_poll();
  991. // x11_delay_usec(10000);
  992. frames++;
  993. if (frame>1000000) {
  994. if (GLOBAL_DEF("debug/print_fps", OS::get_singleton()->is_stdout_verbose())) {
  995. print_line("FPS: "+itos(frames));
  996. };
  997. OS::get_singleton()->_fps=frames;
  998. performance->set_process_time(idle_process_max/1000000.0);
  999. performance->set_fixed_process_time(fixed_process_max/1000000.0);
  1000. idle_process_max=0;
  1001. fixed_process_max=0;
  1002. if (GLOBAL_DEF("debug/print_metrics", false)) {
  1003. //PerformanceMetrics::print();
  1004. };
  1005. frame%=1000000;
  1006. frames=0;
  1007. }
  1008. if (OS::get_singleton()->is_in_low_processor_usage_mode() || !OS::get_singleton()->can_draw())
  1009. OS::get_singleton()->delay_usec(25000); //apply some delay to force idle time
  1010. else {
  1011. uint32_t frame_delay = OS::get_singleton()->get_frame_delay();
  1012. if (frame_delay)
  1013. OS::get_singleton()->delay_usec( OS::get_singleton()->get_frame_delay()*1000 );
  1014. }
  1015. int taret_fps = OS::get_singleton()->get_target_fps();
  1016. if (taret_fps>0) {
  1017. uint64_t time_step = 1000000L/taret_fps;
  1018. target_ticks += time_step;
  1019. uint64_t current_ticks = OS::get_singleton()->get_ticks_usec();
  1020. if (current_ticks<target_ticks) OS::get_singleton()->delay_usec(target_ticks-current_ticks);
  1021. current_ticks = OS::get_singleton()->get_ticks_usec();
  1022. target_ticks = MIN(MAX(target_ticks,current_ticks-time_step),current_ticks+time_step);
  1023. }
  1024. return exit;
  1025. }
  1026. void Main::force_redraw() {
  1027. force_redraw_requested = true;
  1028. };
  1029. void Main::cleanup() {
  1030. ERR_FAIL_COND(!_start_success);
  1031. if (script_debugger)
  1032. memdelete(script_debugger);
  1033. OS::get_singleton()->delete_main_loop();
  1034. OS::get_singleton()->_cmdline.clear();
  1035. OS::get_singleton()->_execpath="";
  1036. OS::get_singleton()->_local_clipboard="";
  1037. #ifdef TOOLS_ENABLED
  1038. EditorNode::unregister_editor_types();
  1039. #endif
  1040. unregister_driver_types();
  1041. unregister_module_types();
  1042. unregister_scene_types();
  1043. unregister_server_types();
  1044. OS::get_singleton()->finalize();
  1045. if (packed_data)
  1046. memdelete(packed_data);
  1047. if (file_access_network_client)
  1048. memdelete(file_access_network_client);
  1049. if (performance)
  1050. memdelete(performance);
  1051. if (input_map)
  1052. memdelete(input_map);
  1053. if (translation_server)
  1054. memdelete( translation_server );
  1055. if (path_remap)
  1056. memdelete(path_remap);
  1057. if (globals)
  1058. memdelete(globals);
  1059. memdelete( message_queue );
  1060. unregister_core_driver_types();
  1061. unregister_core_types();
  1062. //PerformanceMetrics::finish();
  1063. OS::get_singleton()->clear_last_error();
  1064. OS::get_singleton()->finalize_core();
  1065. }