main.cpp 11 KB

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