main.cpp 5.6 KB

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