main.cpp 9.1 KB

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