main.cpp 9.1 KB

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