main.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #include <iostream>
  2. #include <getopt.h>
  3. #include <unistd.h>
  4. #include <SDL_mixer.h>
  5. #include <SDL.h>
  6. #include <FreeImage.h>
  7. #include <sys/stat.h>
  8. #include <GL/glew.h>
  9. #include <GL/glx.h>
  10. #include <filesystem>
  11. #include "GLFW/glfw3.h"
  12. #include "WallpaperEngine/Core/CProject.h"
  13. #include "WallpaperEngine/Render/CWallpaper.h"
  14. #include "WallpaperEngine/Render/CContext.h"
  15. #include "WallpaperEngine/Render/CScene.h"
  16. #include "WallpaperEngine/Render/CVideo.h"
  17. #include "WallpaperEngine/Assets/CPackage.h"
  18. #include "WallpaperEngine/Assets/CDirectory.h"
  19. #include "WallpaperEngine/Assets/CCombinedContainer.h"
  20. float g_Time;
  21. using namespace WallpaperEngine::Core::Types;
  22. void print_help (const char* route)
  23. {
  24. std::cout
  25. << "Usage:" << route << " [options] background_path" << std::endl
  26. << "options:" << std::endl
  27. << " --silent\t\tMutes all the sound the wallpaper might produce" << 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. {
  33. if (s.empty () == true)
  34. return s;
  35. std::string str (s);
  36. // remove single-quotes from the arguments
  37. if (str [0] == '\'' && str [str.size() - 1] == '\'')
  38. str
  39. .erase (str.size() - 1, 1)
  40. .erase (0, 1);
  41. // ensure there's a slash at the end of the path
  42. if (str [str.size() - 1] != '/')
  43. str += '/';
  44. return std::move (str);
  45. }
  46. int main (int argc, char* argv[])
  47. {
  48. std::vector <std::string> screens;
  49. int maximumFPS = 30;
  50. bool shouldEnableAudio = true;
  51. std::string path;
  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, nullptr);
  64. if (c == -1)
  65. break;
  66. switch (c)
  67. {
  68. case 'r':
  69. screens.emplace_back (optarg);
  70. break;
  71. case 'p':
  72. case 'd':
  73. std::cerr << "--dir/--pkg is deprecated and not used anymore" << std::endl;
  74. path = stringPathFixes (optarg);
  75. break;
  76. case 's':
  77. shouldEnableAudio = false;
  78. break;
  79. case 'h':
  80. print_help (argv [0]);
  81. break;
  82. case 'f':
  83. maximumFPS = atoi (optarg);
  84. break;
  85. default:
  86. break;
  87. }
  88. }
  89. // increment the option index (useful for when no options were found)
  90. // option_index ++;
  91. if (path.empty () == true)
  92. {
  93. if (optind < argc && strlen (argv [optind]) > 0)
  94. {
  95. path = argv [optind];
  96. }
  97. else
  98. {
  99. print_help (argv [0]);
  100. return 0;
  101. }
  102. }
  103. char* finalPath = realpath(path.c_str (), nullptr);
  104. if (finalPath == nullptr)
  105. {
  106. if (errno == ENAMETOOLONG)
  107. throw std::runtime_error ("Cannot get wallpaper's folder, path is too long");
  108. else
  109. throw std::runtime_error ("Cannot find the specified folder");
  110. }
  111. // ensure the path points to a folder
  112. struct stat pathinfo;
  113. if (stat (finalPath, &pathinfo) != 0)
  114. throw std::runtime_error ("Cannot find the specified wallpaper folder");
  115. if (!S_ISDIR (pathinfo.st_mode))
  116. throw std::runtime_error ("The specified path is not a folder");
  117. // ensure the path has a trailing slash
  118. // first of all, initialize the window
  119. if (glfwInit () == GLFW_FALSE)
  120. {
  121. fprintf (stderr, "Failed to initialize GLFW\n");
  122. return 1;
  123. }
  124. // initialize freeimage
  125. FreeImage_Initialise (TRUE);
  126. // set some window hints (opengl version to be used)
  127. glfwWindowHint (GLFW_SAMPLES, 4);
  128. glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 2);
  129. glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 1);
  130. auto containers = new WallpaperEngine::Assets::CCombinedContainer ();
  131. // update the used path with the full one
  132. path = finalPath;
  133. path += "/";
  134. // the background's path is required to load project.json regardless of the type of background we're using
  135. containers->add (new WallpaperEngine::Assets::CDirectory (path));
  136. // check if scene.pkg exists and add it to the list
  137. try
  138. {
  139. std::string scene_path = path + "scene.pkg";
  140. // add the package to the list
  141. containers->add (new WallpaperEngine::Assets::CPackage (scene_path));
  142. std::cout << "Detected scene.pkg file at " << scene_path << ". Adding to list of searchable paths" << std::endl;
  143. }
  144. catch(std::filesystem::filesystem_error ex)
  145. {
  146. // ignore this error, the package file was not found
  147. std::cout << "No scene.pkg file found at " << path << ". Defaulting to normal folder storage" << std::endl;
  148. }
  149. catch (std::runtime_error ex)
  150. {
  151. // the package was found but there was an error loading it (wrong header or something)
  152. fprintf (stderr, "Failed to load scene.pkg file: %s\n", ex.what());
  153. return 4;
  154. }
  155. // add containers to the list
  156. containers->add (new WallpaperEngine::Assets::CDirectory ("./assets/"));
  157. // parse the project.json file
  158. auto project = WallpaperEngine::Core::CProject::fromFile ("project.json", containers);
  159. WallpaperEngine::Render::CWallpaper* wallpaper;
  160. // initialize custom context class
  161. WallpaperEngine::Render::CContext* context = new WallpaperEngine::Render::CContext (screens);
  162. // auto projection = project->getWallpaper ()->as <WallpaperEngine::Core::CScene> ()->getOrthogonalProjection ();
  163. // create the window!
  164. // TODO: DO WE NEED TO PASS MONITOR HERE OR ANYTHING?
  165. // TODO: FIGURE OUT HOW TO PUT THIS WINDOW IN THE BACKGROUND
  166. GLFWwindow* window = glfwCreateWindow (1920, 1080, "WallpaperEngine", NULL, NULL);
  167. if (window == nullptr)
  168. {
  169. fprintf (stderr, "Failed to open a GLFW window");
  170. glfwTerminate ();
  171. return 2;
  172. }
  173. // initialize inputs
  174. CMouseInput* mouseInput = new CMouseInput (window);
  175. context->setMouse (mouseInput);
  176. glfwMakeContextCurrent (window);
  177. // TODO: FIGURE THESE OUT BASED ON THE SCREEN
  178. int windowWidth = 1920;
  179. int windowHeight = 1080;
  180. // get the real framebuffer size
  181. glfwGetFramebufferSize (window, &windowWidth, &windowHeight);
  182. // set the default viewport
  183. context->setDefaultViewport ({0, 0, windowWidth, windowHeight});
  184. if (glewInit () != GLEW_OK)
  185. {
  186. fprintf (stderr, "Failed to initialize GLEW");
  187. glfwTerminate ();
  188. return 3;
  189. }
  190. if (project->getType () == "scene")
  191. {
  192. WallpaperEngine::Core::CScene* scene = project->getWallpaper ()->as <WallpaperEngine::Core::CScene> ();
  193. wallpaper = new WallpaperEngine::Render::CScene (scene, containers, context);
  194. }
  195. else if (project->getType () == "video")
  196. {
  197. // special steps, running a video needs a root directory change, files are not loaded from the container classes
  198. // as they're streamed from disk
  199. chdir (path.c_str ());
  200. WallpaperEngine::Core::CVideo* video = project->getWallpaper ()->as <WallpaperEngine::Core::CVideo> ();
  201. wallpaper = new WallpaperEngine::Render::CVideo (video, containers, context);
  202. }
  203. else
  204. {
  205. throw std::runtime_error ("Unsupported wallpaper type");
  206. }
  207. // ensure the context knows what wallpaper to render
  208. context->setWallpaper (wallpaper);
  209. if (shouldEnableAudio == true)
  210. {
  211. int mixer_flags = MIX_INIT_MP3 | MIX_INIT_FLAC | MIX_INIT_OGG;
  212. if (SDL_Init (SDL_INIT_AUDIO) < 0 || mixer_flags != Mix_Init (mixer_flags))
  213. {
  214. // Mix_GetError is an alias for SDL_GetError, so calling it directly will yield the correct result
  215. // it doesn't matter if SDL_Init or Mix_Init failed, both report the errors through the same functions
  216. fprintf (stderr, "Cannot initialize SDL audio system, SDL_GetError: %s", SDL_GetError ());
  217. return 2;
  218. }
  219. // initialize audio engine
  220. Mix_OpenAudio (22050, AUDIO_S16SYS, 2, 640);
  221. }
  222. // TODO: FIGURE OUT THE REQUIRED INPUT MODE, AS SOME WALLPAPERS USE THINGS LIKE MOUSE POSITION
  223. // glfwSetInputMode (window, GLFW_STICKY_KEYS, GL_TRUE);
  224. // enable depth text
  225. glEnable (GL_DEPTH_TEST);
  226. glDepthFunc (GL_LESS);
  227. // cull anything that doesn't look at the camera (might be useful to disable in the future)
  228. glDisable (GL_CULL_FACE);
  229. double minimumTime = 1.0 / maximumFPS;
  230. double startTime = 0.0;
  231. double endTime = 0.0;
  232. while (glfwWindowShouldClose (window) == 0)
  233. {
  234. // get the real framebuffer size
  235. glfwGetFramebufferSize (window, &windowWidth, &windowHeight);
  236. // set the default viewport
  237. context->setDefaultViewport ({0, 0, windowWidth, windowHeight});
  238. // calculate the current time value
  239. g_Time = (float) glfwGetTime ();
  240. // get the start time of the frame
  241. startTime = glfwGetTime ();
  242. // update our inputs first
  243. mouseInput->update ();
  244. // render the scene
  245. context->render ();
  246. // do buffer swapping
  247. glfwSwapBuffers (window);
  248. // poll for events (like closing the window)
  249. glfwPollEvents ();
  250. // get the end time of the frame
  251. endTime = glfwGetTime ();
  252. // ensure the frame time is correct to not overrun FPS
  253. if ((endTime - startTime) < minimumTime)
  254. usleep ((minimumTime - (endTime - startTime)) * CLOCKS_PER_SEC);
  255. }
  256. // terminate gl
  257. glfwTerminate ();
  258. // terminate SDL
  259. SDL_Quit ();
  260. // terminate free image
  261. FreeImage_DeInitialise ();
  262. // free paths
  263. free(finalPath);
  264. return 0;
  265. }