/** * @brief Initializes the CLI environment. */ int cli_init (void) { /* Already loaded. */ if (cli_state != NULL) return 0; /* Calculate size. */ cli_width = SCREEN_W - 100; cli_height = SCREEN_H - 100; /* Create the state. */ cli_state = nlua_newState(); nlua_loadBasic( cli_state ); nlua_loadStandard( cli_state, 0 ); nlua_loadCLI( cli_state ); luaL_register( cli_state, "_G", cli_methods ); lua_settop( cli_state, 0 ); /* Set the font. */ cli_font = malloc( sizeof(glFont) ); gl_fontInit( cli_font, "dat/mono.ttf", CONSOLE_FONT_SIZE ); /* Clear the buffer. */ memset( cli_buffer, 0, sizeof(cli_buffer) ); /* Put a friendly message at first. */ cli_addMessage( "Welcome to the Lua console!" ); cli_addMessage( "" ); return 0; }
/** * @brief Initializes the CLI environment. */ int cli_init (void) { /* Already loaded. */ if (cli_state != NULL) return 0; /* Set the height. */ cli_height = CLI_HEIGHT; /* Create the state. */ cli_state = nlua_newState(); nlua_loadStandard( cli_state, 0 ); nlua_loadCol( cli_state, 0 ); nlua_loadTex( cli_state, 0 ); nlua_loadBackground( cli_state, 0 ); nlua_loadCamera( cli_state, 0 ); nlua_loadTk( cli_state ); nlua_loadCLI( cli_state ); nlua_loadMusic( cli_state, 0 ); luaL_register( cli_state, "_G", cli_methods ); lua_settop( cli_state, 0 ); /* Mark as console. */ lua_pushboolean( cli_state, 1 ); lua_setglobal( cli_state, "__cli" ); /* Set the font. */ cli_font = malloc( sizeof(glFont) ); gl_fontInit( cli_font, "dat/mono.ttf", conf.font_size_console ); /* Clear the buffer. */ memset( cli_buffer, 0, sizeof(cli_buffer) ); return 0; }
/** * @brief Gets a font by size. */ static int omsg_getFontID( int size ) { int i; omsg_font_t *font; /* Create array if not done so yet. */ if (omsg_font_array == NULL) omsg_font_array = array_create( omsg_font_t ); /* Try to match. */ for (i=0; i<array_size( omsg_font_array ); i++) if (size == omsg_font_array[i].size) return i; /* Create font. */ font = &array_grow( &omsg_font_array ); gl_fontInit( &font->font, OMSG_FONT_DEFAULT_PATH, size ); font->size = size; return array_size(omsg_font_array) - 1; }
/** * @brief Loads the intro stuff. */ static int intro_load( const char *text ) { uint32_t intro_size; char *intro_buf; int i, p, n; int mem; /* Load text. */ intro_buf = ndata_read( text, &intro_size ); intro_length = intro_size; /* Length aproximation. */ /* Create intro font. */ gl_fontInit( &intro_font, NULL, INTRO_FONT_SIZE ); /* Load lines. */ p = 0; n = 0; mem = 0; while ((uint32_t)p < intro_size) { /* Get the length. */ i = gl_printWidthForText( &intro_font, &intro_buf[p], SCREEN_W - 200. ); /* Copy the line. */ if (n+1 > mem) { mem += 128; intro_lines = realloc( intro_lines, sizeof(char*) * mem ); } intro_lines[n] = malloc( i + 1 ); strncpy( intro_lines[n], &intro_buf[p], i ); intro_lines[n][i] = '\0'; p += i + 1; /* Move pointer. */ n++; /* New line. */ } /* Clean up. */ free(intro_buf); intro_nlines = n; return 0; }
/** * @brief Loads the intro stuff. */ static int intro_load( const char *text ) { uint32_t intro_size; char *intro_buf; char img_src[128]; /* path to image to be displayed alongside text. */ int length; int i, p, n; int mem; has_side_gfx = 0; /* Load text. */ intro_buf = ndata_read( text, &intro_size ); intro_length = intro_size; /* Length aproximation. */ /* Create intro font. */ gl_fontInit( &intro_font, "dat/mono.ttf", INTRO_FONT_SIZE ); /* Load lines. */ p = 0; n = 0; mem = 0; while ((uint32_t)p < intro_size) { /* Copy the line. */ if (n+1 > mem) { mem += 128; intro_lines = realloc( intro_lines, sizeof(char*) * mem ); } if ( intro_buf[p] == '[' /* Don't do sscanf for every line! */ && sscanf( &intro_buf[p], "[fadein %s", img_src ) == 1 ) { /* an image to appear next to text. */ /* Get the length. */ for (i = 0; intro_buf[p + i] != '\n' && intro_buf[p + i] != '\0'; ++i); length = strlen( img_src ); intro_lines[n] = malloc( length + 2 ); intro_lines[n][0] = 'i'; strncpy( &intro_lines[n][1], img_src, length ); intro_lines[n][length] = '\0'; /* Mark that there are graphics. */ has_side_gfx = 1; } else if ( intro_buf[p] == '[' /* Don't do strncmp for every line! */ && strncmp( &intro_buf[p], "[fadeout]", 9 ) == 0 ) { /* fade out the image next to the text. */ for (i = 0; intro_buf[p + i] != '\n' && intro_buf[p + i] != '\0'; ++i); intro_lines[n] = malloc( 2 ); intro_lines[n][0] = 'o'; intro_lines[n][1] = '\0'; /* not strictly necessary, but safe. */ } else { /* plain old text. */ /* Get the length. */ i = gl_printWidthForText( &intro_font, &intro_buf[p], SCREEN_W - 500. ); intro_lines[n] = malloc( i + 2 ); intro_lines[n][0] = 't'; strncpy( &intro_lines[n][1], &intro_buf[p], i ); intro_lines[n][i+1] = '\0'; } p += i + 1; /* Move pointer. */ n++; /* New line. */ } /* Clean up. */ free(intro_buf); intro_nlines = n; return 0; }
/** * @brief The entry point of Naev. * * @param[in] argc Number of arguments. * @param[in] argv Array of argc arguments. * @return EXIT_SUCCESS on success. */ int main( int argc, char** argv ) { char buf[PATH_MAX]; /* Save the binary path. */ binary_path = strdup(argv[0]); /* Print the version */ LOG( " "APPNAME" v%s", naev_version(0) ); #ifdef GIT_COMMIT DEBUG( " git HEAD at " GIT_COMMIT ); #endif /* GIT_COMMIT */ /* Initializes SDL for possible warnings. */ SDL_Init(0); /* Initialize the threadpool */ threadpool_init(); /* Set up debug signal handlers. */ debug_sigInit(); /* Must be initialized before input_init is called. */ if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) { WARN("Unable to initialize SDL Video: %s", SDL_GetError()); return -1; } /* Get desktop dimensions. */ #if SDL_VERSION_ATLEAST(1,2,10) const SDL_VideoInfo *vidinfo = SDL_GetVideoInfo(); gl_screen.desktop_w = vidinfo->current_w; gl_screen.desktop_h = vidinfo->current_h; #else /* #elif SDL_VERSION_ATLEAST(1,2,10) */ gl_screen.desktop_w = 0; gl_screen.desktop_h = 0; #endif /* #elif SDL_VERSION_ATLEAST(1,2,10) */ /* We'll be parsing XML. */ LIBXML_TEST_VERSION xmlInitParser(); /* Input must be initialized for config to work. */ input_init(); conf_setDefaults(); /* set the default config values */ /* * Attempts to load the data path from datapath.lua * At this early point in the load process, the binary path * is the only place likely to be checked. */ conf_loadConfigPath(); /* Parse the user data path override first. */ conf_parseCLIPath( argc, argv ); /* Create the home directory if needed. */ if (nfile_dirMakeExist("%s", nfile_configPath())) WARN("Unable to create config directory '%s'", nfile_configPath()); /* Set the configuration. */ nsnprintf(buf, PATH_MAX, "%s"CONF_FILE, nfile_configPath()); #if HAS_UNIX /* TODO get rid of this cruft ASAP. */ int oldconfig = 0; if (!nfile_fileExists( buf )) { char *home, buf2[PATH_MAX]; home = SDL_getenv( "HOME" ); if (home != NULL) { nsnprintf( buf2, PATH_MAX, "%s/.naev/"CONF_FILE, home ); if (nfile_fileExists( buf2 )) oldconfig = 1; } } #endif /* HAS_UNIX */ conf_loadConfig(buf); /* Lua to parse the configuration file */ conf_parseCLI( argc, argv ); /* parse CLI arguments */ /* Enable FPU exceptions. */ #if defined(HAVE_FEENABLEEXCEPT) && defined(DEBUGGING) if (conf.fpu_except) feenableexcept( FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW ); #endif /* defined(HAVE_FEENABLEEXCEPT) && defined(DEBUGGING) */ /* Open data. */ if (ndata_open() != 0) ERR("Failed to open ndata."); /* Load the start info. */ if (start_load()) ERR("Failed to load module start data."); /* Load the data basics. */ LOG(" %s", ndata_name()); DEBUG(); /* Display the SDL Version. */ print_SDLversion(); DEBUG(); /* random numbers */ rng_init(); /* * OpenGL */ if (gl_init()) { /* initializes video output */ ERR("Initializing video output failed, exiting..."); SDL_Quit(); exit(EXIT_FAILURE); } window_caption(); gl_fontInit( NULL, NULL, conf.font_size_def ); /* initializes default font to size */ gl_fontInit( &gl_smallFont, NULL, conf.font_size_small ); /* small font */ /* Display the load screen. */ loadscreen_load(); loadscreen_render( 0., "Initializing subsystems..." ); time_ms = SDL_GetTicks(); /* * Input */ if ((conf.joystick_ind >= 0) || (conf.joystick_nam != NULL)) { if (joystick_init()) WARN("Error initializing joystick input"); if (conf.joystick_nam != NULL) { /* use the joystick name to find a joystick */ if (joystick_use(joystick_get(conf.joystick_nam))) { WARN("Failure to open any joystick, falling back to default keybinds"); input_setDefault(); } free(conf.joystick_nam); } else if (conf.joystick_ind >= 0) /* use a joystick id instead */ if (joystick_use(conf.joystick_ind)) { WARN("Failure to open any joystick, falling back to default keybinds"); input_setDefault(); } } /* * OpenAL - Sound */ if (conf.nosound) { LOG("Sound is disabled!"); sound_disabled = 1; music_disabled = 1; } if (sound_init()) WARN("Problem setting up sound!"); music_choose("load"); /* FPS stuff. */ fps_setPos( 15., (double)(gl_screen.h-15-gl_defFont.h) ); /* Misc graphics init */ if (nebu_init() != 0) { /* Initializes the nebula */ /* An error has happened */ ERR("Unable to initialize the Nebula subsystem!"); /* Weirdness will occur... */ } gui_init(); /* initializes the GUI graphics */ toolkit_init(); /* initializes the toolkit */ map_init(); /* initializes the map. */ cond_init(); /* Initialize conditional subsystem. */ cli_init(); /* Initialize console. */ /* Data loading */ load_all(); /* Generate the CSV. */ if (conf.devcsv) dev_csv(); /* Unload load screen. */ loadscreen_unload(); /* Start menu. */ menu_main(); /* Force a minimum delay with loading screen */ if ((SDL_GetTicks() - time_ms) < NAEV_INIT_DELAY) SDL_Delay( NAEV_INIT_DELAY - (SDL_GetTicks() - time_ms) ); fps_init(); /* initializes the time_ms */ #if HAS_UNIX /* Tell the player to migrate their configuration files out of ~/.naev */ /* TODO get rid of this cruft ASAP. */ if ((oldconfig) && (!conf.datapath)) { char path[PATH_MAX], *script, *home; uint32_t scriptsize; int ret; nsnprintf( path, PATH_MAX, "%s/naev-confupdate.sh", ndata_getDirname() ); home = SDL_getenv("HOME"); ret = dialogue_YesNo( "Warning", "Your configuration files are in a deprecated location and must be migrated:\n" " \er%s/.naev/\e0\n\n" "The update script can likely be found in your Naev data directory:\n" " \er%s\e0\n\n" "Would you like to run it automatically?", home, path ); /* Try to run the script. */ if (ret) { ret = -1; /* Running from ndata. */ if (ndata_getPath() != NULL) { script = ndata_read( "naev-confupdate.sh", &scriptsize ); if (script != NULL) ret = system(script); } /* Running from laid-out files or ndata_read failed. */ if ((nfile_fileExists(path)) && (ret == -1)) { script = nfile_readFile( (int*)&scriptsize, path ); if (script != NULL) ret = system(script); } /* We couldn't find the script. */ if (ret == -1) { dialogue_alert( "The update script was not found at:\n\er%s\e0\n\n" "Please locate and run it manually.", path ); } /* Restart, as the script succeeded. */ else if (!ret) { dialogue_msg( "Update Completed", "Configuration files were successfully migrated. Naev will now restart." ); execv(argv[0], argv); } else { /* I sincerely hope this else is never hit. */ dialogue_alert( "The update script encountered an error. Please exit Naev and move your config and save files manually:\n\n" "\er%s/%s\e0 =>\n \eD%s\e0\n\n" "\er%s/%s\e0 =>\n \eD%s\e0\n\n" "\er%s/%s\e0 =>\n \eD%snebula/\e0\n\n", home, ".naev/conf.lua", nfile_configPath(), home, ".naev/{saves,screenshots}/", nfile_dataPath(), home, ".naev/gen/*.png", nfile_cachePath() ); } } else {
/** * @brief The entry point of NAEV. * * @param[in] argc Number of arguments. * @param[in] argv Array of argc arguments. * @return EXIT_SUCCESS on success. */ int main( int argc, char** argv ) { char buf[PATH_MAX]; /* Save the binary path. */ binary_path = argv[0]; /* Print the version */ LOG( " "APPNAME" v%s", naev_version(0) ); #ifdef GIT_COMMIT DEBUG( " git HEAD at " GIT_COMMIT ); #endif /* GIT_COMMIT */ /* Initializes SDL for possible warnings. */ SDL_Init(0); /* Set up debug signal handlers. */ debug_sigInit(); /* Create the home directory if needed. */ if (nfile_dirMakeExist("%s", nfile_basePath())) WARN("Unable to create naev directory '%s'", nfile_basePath()); /* Must be initialized before input_init is called. */ if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) { WARN("Unable to initialize SDL Video: %s", SDL_GetError()); return -1; } /* Get desktop dimensions. */ #if SDL_VERSION_ATLEAST(1,2,10) const SDL_VideoInfo *vidinfo = SDL_GetVideoInfo(); gl_screen.desktop_w = vidinfo->current_w; gl_screen.desktop_h = vidinfo->current_h; #else /* #elif SDL_VERSION_ATLEAST(1,2,10) */ gl_screen.desktop_w = 0; gl_screen.desktop_h = 0; #endif /* #elif SDL_VERSION_ATLEAST(1,2,10) */ /* We'll be parsing XML. */ LIBXML_TEST_VERSION xmlInitParser(); /* Input must be initialized for config to work. */ input_init(); /* Set the configuration. */ snprintf(buf, PATH_MAX, "%s"CONF_FILE, nfile_basePath()); conf_setDefaults(); /* set the default config values */ conf_loadConfig(buf); /* Lua to parse the configuration file */ conf_parseCLI( argc, argv ); /* parse CLI arguments */ /* Enable FPU exceptions. */ #if !(HAS_WIN32) && defined(DEBUGGING) if (conf.fpu_except) feenableexcept( FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW ); #endif /* DEBUGGING */ /* Open data. */ if (ndata_open() != 0) ERR("Failed to open ndata."); /* Load the data basics. */ LOG(" %s", ndata_name()); DEBUG(); /* Display the SDL Version. */ print_SDLversion(); DEBUG(); /* random numbers */ rng_init(); /* * OpenGL */ if (gl_init()) { /* initializes video output */ ERR("Initializing video output failed, exiting..."); SDL_Quit(); exit(EXIT_FAILURE); } window_caption(); gl_fontInit( NULL, NULL, FONT_SIZE ); /* initializes default font to size */ gl_fontInit( &gl_smallFont, NULL, FONT_SIZE_SMALL ); /* small font */ /* Display the load screen. */ loadscreen_load(); loadscreen_render( 0., "Initializing subsystems..." ); time = SDL_GetTicks(); /* * Input */ if ((conf.joystick_ind >= 0) || (conf.joystick_nam != NULL)) { if (joystick_init()) WARN("Error initializing joystick input"); if (conf.joystick_nam != NULL) { /* use the joystick name to find a joystick */ if (joystick_use(joystick_get(conf.joystick_nam))) { WARN("Failure to open any joystick, falling back to default keybinds"); input_setDefault(); } free(conf.joystick_nam); } else if (conf.joystick_ind >= 0) /* use a joystick id instead */ if (joystick_use(conf.joystick_ind)) { WARN("Failure to open any joystick, falling back to default keybinds"); input_setDefault(); } } /* * OpenAL - Sound */ if (conf.nosound) { LOG("Sound is disabled!"); sound_disabled = 1; music_disabled = 1; } if (sound_init()) WARN("Problem setting up sound!"); music_choose("load"); /* Misc graphics init */ if (nebu_init() != 0) { /* Initializes the nebula */ /* An error has happened */ ERR("Unable to initialize the Nebula subsystem!"); /* Weirdness will occur... */ } gui_init(); /* initializes the GUI graphics */ toolkit_init(); /* initializes the toolkit */ map_init(); /* initializes the map. */ cond_init(); /* Initialize conditional subsystem. */ /* Data loading */ load_all(); /* Unload load screen. */ loadscreen_unload(); /* Start menu. */ menu_main(); /* Force a minimum delay with loading screen */ if ((SDL_GetTicks() - time) < NAEV_INIT_DELAY) SDL_Delay( NAEV_INIT_DELAY - (SDL_GetTicks() - time) ); time = SDL_GetTicks(); /* initializes the time */ /* * main loop */ SDL_Event event; /* flushes the event loop since I noticed that when the joystick is loaded it * creates button events that results in the player starting out acceling */ while (SDL_PollEvent(&event)); /* primary loop */ while (!quit) { while (SDL_PollEvent(&event)) { /* event loop */ if (event.type == SDL_QUIT) quit = 1; /* quit is handled here */ input_handle(&event); /* handles all the events and player keybinds */ } main_loop(); } /* Save configuration. */ conf_saveConfig(buf); /* cleanup some stuff */ player_cleanup(); /* cleans up the player stuff */ gui_free(); /* cleans up the player's GUI */ weapon_exit(); /* destroys all active weapons */ pilots_free(); /* frees the pilots, they were locked up :( */ cond_exit(); /* destroy conditional subsystem. */ land_exit(); /* Destroys landing vbo and friends. */ /* data unloading */ unload_all(); /* cleanup opengl fonts */ gl_freeFont(NULL); gl_freeFont(&gl_smallFont); /* Close data. */ ndata_close(); /* Destroy conf. */ conf_cleanup(); /* Frees some memory the configuration allocated. */ /* exit subsystems */ map_exit(); /* destroys the map. */ toolkit_exit(); /* kills the toolkit */ ai_exit(); /* stops the Lua AI magic */ joystick_exit(); /* releases joystick */ input_exit(); /* cleans up keybindings */ nebu_exit(); /* destroys the nebula */ gl_exit(); /* kills video output */ sound_exit(); /* kills the sound */ news_exit(); /* destroys the news. */ /* Free the icon. */ if (naev_icon) free(naev_icon); SDL_Quit(); /* quits SDL */ /* all is well */ exit(EXIT_SUCCESS); }