main.cpp 39 KB

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