main.cpp 5.7 KB

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