main.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. std::cout << "Detected scene.pkg file at " << scene_path << ". Adding to list of searchable paths" << std::endl;
  125. }
  126. catch(std::filesystem::filesystem_error ex)
  127. {
  128. // ignore this error, the package file was not found
  129. std::cout << "No scene.pkg file found at " << path << ". Defaulting to normal folder storage" << std::endl;
  130. }
  131. catch (std::runtime_error ex)
  132. {
  133. // the package was found but there was an error loading it (wrong header or something)
  134. fprintf (stderr, "Failed to load scene.pkg file: %s\n", ex.what());
  135. return 4;
  136. }
  137. // add containers to the list
  138. containers->add (new WallpaperEngine::Assets::CDirectory ("./assets/"));
  139. // parse the project.json file
  140. auto project = WallpaperEngine::Core::CProject::fromFile ("project.json", containers);
  141. WallpaperEngine::Render::CWallpaper* wallpaper;
  142. // initialize custom context class
  143. WallpaperEngine::Render::CContext* context = new WallpaperEngine::Render::CContext (screens);
  144. // auto projection = project->getWallpaper ()->as <WallpaperEngine::Core::CScene> ()->getOrthogonalProjection ();
  145. // create the window!
  146. // TODO: DO WE NEED TO PASS MONITOR HERE OR ANYTHING?
  147. // TODO: FIGURE OUT HOW TO PUT THIS WINDOW IN THE BACKGROUND
  148. GLFWwindow* window = glfwCreateWindow (1920, 1080, "WallpaperEngine", NULL, NULL);
  149. if (window == nullptr)
  150. {
  151. fprintf (stderr, "Failed to open a GLFW window");
  152. glfwTerminate ();
  153. return 2;
  154. }
  155. // initialize inputs
  156. CMouseInput* mouseInput = new CMouseInput (window);
  157. context->setMouse (mouseInput);
  158. glfwMakeContextCurrent (window);
  159. // TODO: FIGURE THESE OUT BASED ON THE SCREEN
  160. int windowWidth = 1920;
  161. int windowHeight = 1080;
  162. // get the real framebuffer size
  163. glfwGetFramebufferSize (window, &windowWidth, &windowHeight);
  164. // set the default viewport
  165. context->setDefaultViewport ({0, 0, windowWidth, windowHeight});
  166. if (glewInit () != GLEW_OK)
  167. {
  168. fprintf (stderr, "Failed to initialize GLEW");
  169. glfwTerminate ();
  170. return 3;
  171. }
  172. if (project->getType () == "scene")
  173. {
  174. WallpaperEngine::Core::CScene* scene = project->getWallpaper ()->as <WallpaperEngine::Core::CScene> ();
  175. wallpaper = new WallpaperEngine::Render::CScene (scene, containers, context);
  176. }
  177. else if (project->getType () == "video")
  178. {
  179. // special steps, running a video needs a root directory change, files are not loaded from the container classes
  180. // as they're streamed from disk
  181. chdir (path.c_str ());
  182. WallpaperEngine::Core::CVideo* video = project->getWallpaper ()->as <WallpaperEngine::Core::CVideo> ();
  183. wallpaper = new WallpaperEngine::Render::CVideo (video, containers, context);
  184. }
  185. else
  186. {
  187. throw std::runtime_error ("Unsupported wallpaper type");
  188. }
  189. // ensure the context knows what wallpaper to render
  190. context->setWallpaper (wallpaper);
  191. if (shouldEnableAudio == true)
  192. {
  193. int mixer_flags = MIX_INIT_MP3 | MIX_INIT_FLAC | MIX_INIT_OGG;
  194. if (SDL_Init (SDL_INIT_AUDIO) < 0 || mixer_flags != Mix_Init (mixer_flags))
  195. {
  196. // Mix_GetError is an alias for SDL_GetError, so calling it directly will yield the correct result
  197. // it doesn't matter if SDL_Init or Mix_Init failed, both report the errors through the same functions
  198. fprintf (stderr, "Cannot initialize SDL audio system, SDL_GetError: %s", SDL_GetError ());
  199. return 2;
  200. }
  201. // initialize audio engine
  202. Mix_OpenAudio (22050, AUDIO_S16SYS, 2, 640);
  203. }
  204. // TODO: FIGURE OUT THE REQUIRED INPUT MODE, AS SOME WALLPAPERS USE THINGS LIKE MOUSE POSITION
  205. // glfwSetInputMode (window, GLFW_STICKY_KEYS, GL_TRUE);
  206. // enable depth text
  207. glEnable (GL_DEPTH_TEST);
  208. glDepthFunc (GL_LESS);
  209. // cull anything that doesn't look at the camera (might be useful to disable in the future)
  210. glDisable (GL_CULL_FACE);
  211. double minimumTime = 1.0 / maximumFPS;
  212. double startTime = 0.0;
  213. double endTime = 0.0;
  214. while (glfwWindowShouldClose (window) == 0)
  215. {
  216. // get the real framebuffer size
  217. glfwGetFramebufferSize (window, &windowWidth, &windowHeight);
  218. // set the default viewport
  219. context->setDefaultViewport ({0, 0, windowWidth, windowHeight});
  220. // calculate the current time value
  221. g_Time = (float) glfwGetTime ();
  222. // get the start time of the frame
  223. startTime = glfwGetTime ();
  224. // update our inputs first
  225. mouseInput->update ();
  226. // render the scene
  227. context->render ();
  228. // do buffer swapping
  229. glfwSwapBuffers (window);
  230. // poll for events (like closing the window)
  231. glfwPollEvents ();
  232. // get the end time of the frame
  233. endTime = glfwGetTime ();
  234. // ensure the frame time is correct to not overrun FPS
  235. if ((endTime - startTime) < minimumTime)
  236. usleep ((minimumTime - (endTime - startTime)) * CLOCKS_PER_SEC);
  237. }
  238. // terminate gl
  239. glfwTerminate ();
  240. // terminate SDL
  241. SDL_Quit ();
  242. // terminate free image
  243. FreeImage_DeInitialise ();
  244. return 0;
  245. }