main.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. #include <iostream>
  2. #include <irrlicht/irrlicht.h>
  3. #include <getopt.h>
  4. #include <unistd.h>
  5. #include <SDL_mixer.h>
  6. #include <SDL.h>
  7. #include <GL/glew.h>
  8. #include <GLFW/glfw3.h>
  9. #include <glm/glm.hpp>
  10. #include <glm/gtc/matrix_transform.hpp>
  11. #include "WallpaperEngine/Core/CProject.h"
  12. #include "WallpaperEngine/Irrlicht/CContext.h"
  13. #include "WallpaperEngine/Render/CWallpaper.h"
  14. #include "WallpaperEngine/Render/CScene.h"
  15. #include "WallpaperEngine/Render/CVideo.h"
  16. #include "WallpaperEngine/Assets/CPackage.h"
  17. #include "WallpaperEngine/Assets/CDirectory.h"
  18. #include "WallpaperEngine/Assets/CCombinedContainer.h"
  19. #include "WallpaperEngine/Core/Types/FloatColor.h"
  20. enum BACKGROUND_RUN_MODE
  21. {
  22. RUN_MODE_UNKNOWN = 0,
  23. RUN_MODE_HELP = 1,
  24. RUN_MODE_DIRECTORY = 2,
  25. RUN_MODE_PACKAGE = 3
  26. };
  27. WallpaperEngine::Irrlicht::CContext* IrrlichtContext = nullptr;
  28. double g_Time;
  29. using namespace WallpaperEngine::Core::Types;
  30. void print_help (const char* route)
  31. {
  32. std::cout
  33. << "Usage:" << route << " [options] " << std::endl
  34. << "options:" << std::endl
  35. << " --silent\t\tMutes all the sound the wallpaper might produce" << std::endl
  36. << " --dir <folder>\tLoads an uncompressed background from the given <folder>" << std::endl
  37. << " --pkg <folder>\tLoads a scene.pkg file from the given <folder>" << std::endl
  38. << " --screen-root <screen name>\tDisplay as screen's background" << std::endl
  39. << " --fps <maximum-fps>\tLimits the FPS to the given number, useful to keep battery consumption low" << std::endl;
  40. }
  41. std::string stringPathFixes(const std::string& s){
  42. std::string str(s);
  43. if(str.empty())
  44. return s;
  45. if(str[0] == '\'' && str[str.size() - 1] == '\''){
  46. str.erase(str.size() - 1, 1);
  47. str.erase(0,1);
  48. }
  49. if(str[str.size() - 1] != '/')
  50. str += '/';
  51. return std::move(str);
  52. }
  53. int main (int argc, char* argv[])
  54. {
  55. std::vector <std::string> screens;
  56. bool isRootWindow = false;
  57. int mode = RUN_MODE_UNKNOWN;
  58. int maximumFPS = 30;
  59. bool shouldEnableAudio = true;
  60. std::string path;
  61. int option_index = 0;
  62. static struct option long_options [] = {
  63. {"screen-root", required_argument, 0, 'r'},
  64. {"pkg", required_argument, 0, 'p'},
  65. {"dir", required_argument, 0, 'd'},
  66. {"silent", no_argument, 0, 's'},
  67. {"help", no_argument, 0, 'h'},
  68. {"fps", required_argument, 0, 'f'},
  69. {nullptr, 0, 0, 0}
  70. };
  71. while (true)
  72. {
  73. int c = getopt_long (argc, argv, "r:p:d:shf:", long_options, &option_index);
  74. if (c == -1)
  75. break;
  76. switch (c)
  77. {
  78. case 'r':
  79. isRootWindow = true;
  80. screens.emplace_back (optarg);
  81. break;
  82. case 'p':
  83. if (mode == RUN_MODE_UNKNOWN)
  84. mode = RUN_MODE_PACKAGE;
  85. path = optarg;
  86. break;
  87. case 'd':
  88. if (mode == RUN_MODE_UNKNOWN)
  89. mode = RUN_MODE_DIRECTORY;
  90. path = optarg;
  91. break;
  92. case 's':
  93. shouldEnableAudio = false;
  94. break;
  95. case 'h':
  96. mode = RUN_MODE_HELP;
  97. break;
  98. case 'f':
  99. maximumFPS = atoi (optarg);
  100. break;
  101. default:
  102. break;
  103. }
  104. }
  105. if (mode == RUN_MODE_UNKNOWN || mode == RUN_MODE_HELP)
  106. {
  107. print_help (argv [0]);
  108. return 0;
  109. }
  110. // first of all, initialize the window
  111. if (glfwInit () == GLFW_FALSE)
  112. {
  113. fprintf (stderr, "Failed to initialize GLFW\n");
  114. return 1;
  115. }
  116. // set some window hints (opengl version to be used)
  117. glfwWindowHint (GLFW_SAMPLES, 4);
  118. glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 2);
  119. glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 1);
  120. // create the window!
  121. // TODO: DO WE NEED TO PASS MONITOR HERE OR ANYTHING?
  122. // TODO: FIGURE OUT HOW TO PUT THIS WINDOW IN THE BACKGROUND
  123. GLFWwindow* window = glfwCreateWindow (1920, 1080, "WallpaperEngine", NULL, NULL);
  124. if (window == nullptr)
  125. {
  126. fprintf (stderr, "Failed to open a GLFW window");
  127. glfwTerminate ();
  128. return 2;
  129. }
  130. glfwMakeContextCurrent (window);
  131. int windowWidth = 1920;
  132. int windowHeight = 1080;
  133. // get the real framebuffer size
  134. glfwGetFramebufferSize (window, &windowWidth, &windowHeight);
  135. if (glewInit () != GLEW_OK)
  136. {
  137. fprintf (stderr, "Failed to initialize GLEW");
  138. glfwTerminate ();
  139. return 3;
  140. }
  141. std::string project_path = path + "project.json";
  142. auto containers = new WallpaperEngine::Assets::CCombinedContainer ();
  143. // add containers to the list
  144. containers->add (new WallpaperEngine::Assets::CDirectory ("./assets/"));
  145. // the background's path is required to load project.json regardless of the type of background we're using
  146. containers->add (new WallpaperEngine::Assets::CDirectory (path));
  147. if (mode == RUN_MODE_PACKAGE)
  148. {
  149. std::string scene_path = path + "scene.pkg";
  150. // add the package to the list
  151. containers->add (new WallpaperEngine::Assets::CPackage (scene_path));
  152. }
  153. else if (mode == RUN_MODE_DIRECTORY)
  154. {
  155. // nothing to do here anymore
  156. }
  157. // parse the project.json file
  158. auto project = WallpaperEngine::Core::CProject::fromFile ("project.json", containers);
  159. WallpaperEngine::Render::CWallpaper* wallpaper;
  160. if (project->getType () == "scene")
  161. {
  162. WallpaperEngine::Core::CScene* scene = project->getWallpaper ()->as <WallpaperEngine::Core::CScene> ();
  163. wallpaper = new WallpaperEngine::Render::CScene (scene, containers);
  164. // TODO: BUILD THE SCENE
  165. }
  166. else if (project->getType () == "video")
  167. {
  168. // TODO: BUILD THE VIDEO OBJECT
  169. }
  170. else
  171. {
  172. throw std::runtime_error ("Unsupported wallpaper type");
  173. }
  174. if (shouldEnableAudio == true)
  175. {
  176. int mixer_flags = MIX_INIT_MP3 | MIX_INIT_FLAC | MIX_INIT_OGG;
  177. if (SDL_Init (SDL_INIT_AUDIO) < 0 || mixer_flags != Mix_Init (mixer_flags))
  178. {
  179. // Mix_GetError is an alias for SDL_GetError, so calling it directly will yield the correct result
  180. // it doesn't matter if SDL_Init or Mix_Init failed, both report the errors through the same functions
  181. IrrlichtContext->getDevice ()->getLogger ()->log ("Cannot initialize SDL audio system", SDL_GetError(),irr::ELL_ERROR);
  182. return 2;
  183. }
  184. // initialize audio engine
  185. Mix_OpenAudio (22050, AUDIO_S16SYS, 2, 640);
  186. }
  187. // TODO: FIGURE OUT THE REQUIRED INPUT MODE, AS SOME WALLPAPERS USE THINGS LIKE MOUSE POSITION
  188. // glfwSetInputMode (window, GLFW_STICKY_KEYS, GL_TRUE);
  189. // set the scene clear color
  190. auto sceneInformation = project->getWallpaper ()->as <WallpaperEngine::Core::CScene> ();
  191. FloatColor clearColor = sceneInformation->getClearColor ();
  192. glClearColor (clearColor.r, clearColor.g, clearColor.b, clearColor.a);
  193. // enable depth text
  194. glEnable (GL_DEPTH_TEST);
  195. glDepthFunc (GL_LESS);
  196. // cull anything that doesn't look at the camera (might be useful to disable in the future)
  197. //glEnable (GL_CULL_FACE);
  198. clock_t minimumTime = 1000 / maximumFPS;
  199. clock_t startTime = 0;
  200. clock_t endTime = 0;
  201. while (glfwWindowShouldClose (window) == 0)
  202. {
  203. // calculate the current time value
  204. g_Time += static_cast <double> (endTime - startTime) / CLOCKS_PER_SEC;
  205. // get the start time of the frame
  206. startTime = clock ();
  207. // do not use any framebuffer for now
  208. glBindFramebuffer (GL_FRAMEBUFFER, 0);
  209. // ensure we render over the whole screen
  210. glViewport (0, 0, 1920, 1080);
  211. // clear window
  212. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  213. // render the scene
  214. wallpaper->render ();
  215. // do buffer swapping
  216. glfwSwapBuffers (window);
  217. // poll for events (like closing the window)
  218. glfwPollEvents ();
  219. // get the end time of the frame
  220. endTime = clock ();
  221. // ensure the frame time is correct to not overrun FPS
  222. if ((endTime - startTime) < minimumTime)
  223. usleep (static_cast <unsigned int> ((static_cast <double> (endTime - startTime) / CLOCKS_PER_SEC) * 1000));
  224. }
  225. // terminate gl
  226. glfwTerminate ();
  227. // terminate SDL
  228. SDL_Quit ();
  229. return 0;
  230. /*
  231. try
  232. {
  233. IrrlichtContext = new WallpaperEngine::Irrlicht::CContext (screens, isRootWindow);
  234. IrrlichtContext->initializeContext ();
  235. }
  236. catch (std::runtime_error& ex)
  237. {
  238. std::cerr << ex.what () << std::endl;
  239. return 1;
  240. }
  241. path = stringPathFixes (path);
  242. irr::io::path wallpaper_path = IrrlichtContext->getDevice ()->getFileSystem ()->getAbsolutePath (path.c_str ());
  243. irr::io::path project_path = wallpaper_path + "project.json";
  244. if (mode == RUN_MODE_PACKAGE)
  245. {
  246. irr::io::path scene_path = wallpaper_path + "scene.pkg";
  247. // add the package file to the lookup list
  248. IrrlichtContext->getDevice ()->getFileSystem ()->addFileArchive (scene_path, true, false);
  249. }
  250. else if (mode == RUN_MODE_DIRECTORY)
  251. {
  252. project_path = wallpaper_path + "project.json";
  253. // set the working directory to the project folder
  254. IrrlichtContext->getDevice ()->getFileSystem ()->changeWorkingDirectoryTo (wallpaper_path);
  255. }
  256. WallpaperEngine::Core::CProject* project = WallpaperEngine::Core::CProject::fromFile (project_path);
  257. WallpaperEngine::Render::CWallpaper* wallpaper;
  258. if (project->getType () == "scene")
  259. {
  260. WallpaperEngine::Core::CScene* scene = project->getWallpaper ()->as <WallpaperEngine::Core::CScene> ();
  261. wallpaper = new WallpaperEngine::Render::CScene (scene, IrrlichtContext);
  262. IrrlichtContext->getDevice ()->getSceneManager ()->setAmbientLight (
  263. scene->getAmbientColor ().toSColor ()
  264. );
  265. }
  266. else if (project->getType () == "video")
  267. {
  268. wallpaper = new WallpaperEngine::Render::CVideo (
  269. project->getWallpaper ()->as <WallpaperEngine::Core::CVideo> (),
  270. IrrlichtContext
  271. );
  272. }
  273. else
  274. {
  275. throw std::runtime_error ("Unsupported wallpaper type");
  276. }
  277. irr::u32 minimumTime = 1000 / maximumFPS;
  278. irr::u32 startTime = 0;
  279. irr::u32 endTime = 0;
  280. while (IrrlichtContext && IrrlichtContext->getDevice () && IrrlichtContext->getDevice ()->run ())
  281. {
  282. if (IrrlichtContext->getDevice ()->getVideoDriver () == nullptr)
  283. continue;
  284. startTime = IrrlichtContext->getDevice ()->getTimer ()->getTime ();
  285. g_Time = startTime / 1000.0f;
  286. IrrlichtContext->renderFrame (wallpaper);
  287. endTime = IrrlichtContext->getDevice ()->getTimer ()->getTime ();
  288. IrrlichtContext->getDevice ()->sleep (minimumTime - (endTime - startTime), false);
  289. }
  290. SDL_Quit ();
  291. return 0;*/
  292. }