main.cpp 9.1 KB

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