main.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. float g_Time;
  20. using namespace WallpaperEngine::Core::Types;
  21. void print_help (const char* route)
  22. {
  23. std::cout
  24. << "Usage:" << route << " [options] background_path" << std::endl
  25. << "options:" << std::endl
  26. << " --silent\t\tMutes all the sound the wallpaper might produce" << std::endl
  27. << " --screen-root <screen name>\tDisplay as screen's background" << std::endl
  28. << " --fps <maximum-fps>\tLimits the FPS to the given number, useful to keep battery consumption low" << std::endl;
  29. }
  30. std::string stringPathFixes(const std::string& s)
  31. {
  32. if (s.empty () == true)
  33. return s;
  34. std::string str (s);
  35. // remove single-quotes from the arguments
  36. if (str [0] == '\'' && str [str.size() - 1] == '\'')
  37. str
  38. .erase (str.size() - 1, 1)
  39. .erase (0, 1);
  40. // ensure there's a slash at the end of the path
  41. if (str [str.size() - 1] != '/')
  42. str += '/';
  43. return std::move (str);
  44. }
  45. int main (int argc, char* argv[])
  46. {
  47. std::vector <std::string> screens;
  48. int maximumFPS = 30;
  49. bool shouldEnableAudio = true;
  50. std::string path;
  51. int option_index = 0;
  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, &option_index);
  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 (option_index < argc && strlen (argv [option_index]) > 0)
  94. {
  95. path = argv [option_index];
  96. }
  97. else
  98. {
  99. print_help (argv [0]);
  100. return 0;
  101. }
  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. // initialize inputs
  155. CMouseInput* mouseInput = new CMouseInput (window);
  156. context->setMouse (mouseInput);
  157. glfwMakeContextCurrent (window);
  158. // TODO: FIGURE THESE OUT BASED ON THE SCREEN
  159. int windowWidth = 1920;
  160. int windowHeight = 1080;
  161. // get the real framebuffer size
  162. glfwGetFramebufferSize (window, &windowWidth, &windowHeight);
  163. // set the default viewport
  164. context->setDefaultViewport ({0, 0, windowWidth, windowHeight});
  165. if (glewInit () != GLEW_OK)
  166. {
  167. fprintf (stderr, "Failed to initialize GLEW");
  168. glfwTerminate ();
  169. return 3;
  170. }
  171. if (project->getType () == "scene")
  172. {
  173. WallpaperEngine::Core::CScene* scene = project->getWallpaper ()->as <WallpaperEngine::Core::CScene> ();
  174. wallpaper = new WallpaperEngine::Render::CScene (scene, containers, context);
  175. }
  176. else if (project->getType () == "video")
  177. {
  178. // special steps, running a video needs a root directory change, files are not loaded from the container classes
  179. // as they're streamed from disk
  180. chdir (path.c_str ());
  181. WallpaperEngine::Core::CVideo* video = project->getWallpaper ()->as <WallpaperEngine::Core::CVideo> ();
  182. wallpaper = new WallpaperEngine::Render::CVideo (video, containers, context);
  183. }
  184. else
  185. {
  186. throw std::runtime_error ("Unsupported wallpaper type");
  187. }
  188. // ensure the context knows what wallpaper to render
  189. context->setWallpaper (wallpaper);
  190. if (shouldEnableAudio == true)
  191. {
  192. int mixer_flags = MIX_INIT_MP3 | MIX_INIT_FLAC | MIX_INIT_OGG;
  193. if (SDL_Init (SDL_INIT_AUDIO) < 0 || mixer_flags != Mix_Init (mixer_flags))
  194. {
  195. // Mix_GetError is an alias for SDL_GetError, so calling it directly will yield the correct result
  196. // it doesn't matter if SDL_Init or Mix_Init failed, both report the errors through the same functions
  197. fprintf (stderr, "Cannot initialize SDL audio system, SDL_GetError: %s", SDL_GetError ());
  198. return 2;
  199. }
  200. // initialize audio engine
  201. Mix_OpenAudio (22050, AUDIO_S16SYS, 2, 640);
  202. }
  203. // TODO: FIGURE OUT THE REQUIRED INPUT MODE, AS SOME WALLPAPERS USE THINGS LIKE MOUSE POSITION
  204. // glfwSetInputMode (window, GLFW_STICKY_KEYS, GL_TRUE);
  205. // enable depth text
  206. glEnable (GL_DEPTH_TEST);
  207. glDepthFunc (GL_LESS);
  208. // cull anything that doesn't look at the camera (might be useful to disable in the future)
  209. glDisable (GL_CULL_FACE);
  210. double minimumTime = 1.0 / maximumFPS;
  211. double startTime = 0.0;
  212. double endTime = 0.0;
  213. while (glfwWindowShouldClose (window) == 0)
  214. {
  215. // get the real framebuffer size
  216. glfwGetFramebufferSize (window, &windowWidth, &windowHeight);
  217. // set the default viewport
  218. context->setDefaultViewport ({0, 0, windowWidth, windowHeight});
  219. // calculate the current time value
  220. g_Time = (float) glfwGetTime ();
  221. // get the start time of the frame
  222. startTime = glfwGetTime ();
  223. // update our inputs first
  224. mouseInput->update ();
  225. // render the scene
  226. context->render ();
  227. // do buffer swapping
  228. glfwSwapBuffers (window);
  229. // poll for events (like closing the window)
  230. glfwPollEvents ();
  231. // get the end time of the frame
  232. endTime = glfwGetTime ();
  233. // ensure the frame time is correct to not overrun FPS
  234. if ((endTime - startTime) < minimumTime)
  235. usleep ((minimumTime - (endTime - startTime)) * CLOCKS_PER_SEC);
  236. }
  237. // terminate gl
  238. glfwTerminate ();
  239. // terminate SDL
  240. SDL_Quit ();
  241. // terminate free image
  242. FreeImage_DeInitialise ();
  243. return 0;
  244. }