main.cpp 42 KB

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