main.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #include <iostream>
  2. #include <irrlicht/irrlicht.h>
  3. #include <getopt.h>
  4. #include <SDL_mixer.h>
  5. #include <SDL.h>
  6. #include "WallpaperEngine/Core/CProject.h"
  7. #include "WallpaperEngine/Irrlicht/CContext.h"
  8. #include "WallpaperEngine/Render/CScene.h"
  9. enum BACKGROUND_RUN_MODE
  10. {
  11. RUN_MODE_UNKNOWN = 0,
  12. RUN_MODE_HELP = 1,
  13. RUN_MODE_DIRECTORY = 2,
  14. RUN_MODE_PACKAGE = 3
  15. };
  16. WallpaperEngine::Irrlicht::CContext* IrrlichtContext = nullptr;
  17. void print_help (const char* route)
  18. {
  19. std::cout
  20. << "Usage:" << route << " [options] " << std::endl
  21. << "options:" << std::endl
  22. << " --silent\t\tMutes all the sound the wallpaper might produce" << std::endl
  23. << " --dir <folder>\tLoads an uncompressed background from the given <folder>" << std::endl
  24. << " --pkg <folder>\tLoads a scene.pkg file from the given <folder>" << std::endl
  25. << " --screen-root <screen name>\tDisplay as screen's background" << std::endl
  26. << " --fps <maximum-fps>\tLimits the FPS to the given number, useful to keep battery consumption low" << std::endl;
  27. }
  28. std::string stringPathFixes(const std::string& s){
  29. std::string str(s);
  30. if(str.empty())
  31. return s;
  32. if(str[0] == '\'' && str[str.size() - 1] == '\''){
  33. str.erase(str.size() - 1, 1);
  34. str.erase(0,1);
  35. }
  36. if(str[str.size() - 1] != '/')
  37. str += '/';
  38. return std::move(str);
  39. }
  40. int main (int argc, char* argv[])
  41. {
  42. std::vector <std::string> screens;
  43. bool isRootWindow = false;
  44. int mode = RUN_MODE_UNKNOWN;
  45. int maximumFPS = 30;
  46. bool shouldEnableAudio = true;
  47. std::string path;
  48. int option_index = 0;
  49. static struct option long_options [] = {
  50. {"screen-root", required_argument, 0, 'r'},
  51. {"pkg", required_argument, 0, 'p'},
  52. {"dir", required_argument, 0, 'd'},
  53. {"silent", no_argument, 0, 's'},
  54. {"help", no_argument, 0, 'h'},
  55. {"fps", required_argument, 0, 'f'},
  56. {nullptr, 0, 0, 0}
  57. };
  58. while (true)
  59. {
  60. int c = getopt_long (argc, argv, "r:p:d:shf:", long_options, &option_index);
  61. if (c == -1)
  62. break;
  63. switch (c)
  64. {
  65. case 'r':
  66. isRootWindow = true;
  67. screens.emplace_back (optarg);
  68. break;
  69. case 'p':
  70. if (mode == RUN_MODE_UNKNOWN)
  71. mode = RUN_MODE_PACKAGE;
  72. path = optarg;
  73. break;
  74. case 'd':
  75. if (mode == RUN_MODE_UNKNOWN)
  76. mode = RUN_MODE_DIRECTORY;
  77. path = optarg;
  78. break;
  79. case 's':
  80. shouldEnableAudio = false;
  81. break;
  82. case 'h':
  83. mode = RUN_MODE_HELP;
  84. break;
  85. case 'f':
  86. maximumFPS = atoi (optarg);
  87. break;
  88. default:
  89. break;
  90. }
  91. }
  92. if (mode == RUN_MODE_UNKNOWN || mode == RUN_MODE_HELP)
  93. {
  94. print_help (argv [0]);
  95. return 0;
  96. }
  97. try
  98. {
  99. IrrlichtContext = new WallpaperEngine::Irrlicht::CContext (screens, isRootWindow);
  100. IrrlichtContext->initializeContext ();
  101. }
  102. catch (std::runtime_error& ex)
  103. {
  104. std::cerr << ex.what () << std::endl;
  105. return 1;
  106. }
  107. path = stringPathFixes (path);
  108. irr::io::path wallpaper_path = IrrlichtContext->getDevice ()->getFileSystem ()->getAbsolutePath (path.c_str ());
  109. irr::io::path project_path = wallpaper_path + "project.json";
  110. if (mode == RUN_MODE_PACKAGE)
  111. {
  112. irr::io::path scene_path = wallpaper_path + "scene.pkg";
  113. // add the package file to the lookup list
  114. IrrlichtContext->getDevice ()->getFileSystem ()->addFileArchive (scene_path, true, false);
  115. }
  116. else if (mode == RUN_MODE_DIRECTORY)
  117. {
  118. project_path = wallpaper_path + "project.json";
  119. // set the working directory to the project folder
  120. IrrlichtContext->getDevice ()->getFileSystem ()->changeWorkingDirectoryTo (wallpaper_path);
  121. }
  122. if (shouldEnableAudio == true)
  123. {
  124. int mixer_flags = MIX_INIT_MP3 | MIX_INIT_FLAC | MIX_INIT_OGG;
  125. if (SDL_Init (SDL_INIT_AUDIO) < 0 || mixer_flags != Mix_Init (mixer_flags))
  126. {
  127. // Mix_GetError is an alias for SDL_GetError, so calling it directly will yield the correct result
  128. // it doesn't matter if SDL_Init or Mix_Init failed, both report the errors through the same functions
  129. IrrlichtContext->getDevice ()->getLogger ()->log ("Cannot initialize SDL audio system", SDL_GetError(),irr::ELL_ERROR);
  130. return 2;
  131. }
  132. // initialize audio engine
  133. Mix_OpenAudio (22050, AUDIO_S16SYS, 2, 640);
  134. }
  135. WallpaperEngine::Core::CProject* project = WallpaperEngine::Core::CProject::fromFile (project_path);
  136. WallpaperEngine::Render::CScene* scene = new WallpaperEngine::Render::CScene (project, IrrlichtContext);
  137. irr::u32 minimumTime = 1000 / maximumFPS;
  138. irr::u32 startTime = 0;
  139. irr::u32 endTime = 0;
  140. IrrlichtContext->getDevice ()->getSceneManager ()->setAmbientLight (scene->getScene ()->getAmbientColor ().toSColor ());
  141. while (IrrlichtContext && IrrlichtContext->getDevice () && IrrlichtContext->getDevice ()->run ())
  142. {
  143. if (IrrlichtContext->getDevice ()->getVideoDriver () == nullptr)
  144. continue;
  145. startTime = IrrlichtContext->getDevice ()->getTimer ()->getTime ();
  146. IrrlichtContext->renderFrame (scene);
  147. endTime = IrrlichtContext->getDevice ()->getTimer ()->getTime ();
  148. IrrlichtContext->getDevice ()->sleep (minimumTime - (endTime - startTime), false);
  149. }
  150. SDL_Quit ();
  151. return 0;
  152. }