main.cpp 38 KB

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