main.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. #define GLFW_EXPOSE_NATIVE_X11
  8. #include <GL/glew.h>
  9. #include <GLFW/glfw3.h>
  10. // #include <GLFW/glfw3native.h>
  11. #include <glm/glm.hpp>
  12. #include <glm/gtc/matrix_transform.hpp>
  13. #include "WallpaperEngine/Core/CProject.h"
  14. #include "WallpaperEngine/Render/CWallpaper.h"
  15. #include "WallpaperEngine/Render/CScene.h"
  16. #include "WallpaperEngine/Render/CVideo.h"
  17. #include "WallpaperEngine/Assets/CPackage.h"
  18. #include "WallpaperEngine/Assets/CDirectory.h"
  19. #include "WallpaperEngine/Assets/CCombinedContainer.h"
  20. #include "WallpaperEngine/Core/Types/FloatColor.h"
  21. enum BACKGROUND_RUN_MODE
  22. {
  23. RUN_MODE_UNKNOWN = 0,
  24. RUN_MODE_HELP = 1,
  25. RUN_MODE_DIRECTORY = 2,
  26. RUN_MODE_PACKAGE = 3
  27. };
  28. float g_Time;
  29. using namespace WallpaperEngine::Core::Types;
  30. void print_help (const char* route)
  31. {
  32. std::cout
  33. << "Usage:" << route << " [options] " << std::endl
  34. << "options:" << std::endl
  35. << " --silent\t\tMutes all the sound the wallpaper might produce" << std::endl
  36. << " --dir <folder>\tLoads an uncompressed background from the given <folder>" << std::endl
  37. << " --pkg <folder>\tLoads a scene.pkg file from the given <folder>" << std::endl
  38. << " --screen-root <screen name>\tDisplay as screen's background" << std::endl
  39. << " --fps <maximum-fps>\tLimits the FPS to the given number, useful to keep battery consumption low" << std::endl;
  40. }
  41. std::string stringPathFixes(const std::string& s)
  42. {
  43. if (s.empty () == true)
  44. return s;
  45. std::string str (s);
  46. // remove single-quotes from the arguments
  47. if (str [0] == '\'' && str [str.size() - 1] == '\'')
  48. str
  49. .erase (str.size() - 1, 1)
  50. .erase (0, 1);
  51. // ensure there's a slash at the end of the path
  52. if (str [str.size() - 1] != '/')
  53. str += '/';
  54. return std::move (str);
  55. }
  56. int main (int argc, char* argv[])
  57. {
  58. std::vector <std::string> screens;
  59. bool isRootWindow = false;
  60. int mode = RUN_MODE_UNKNOWN;
  61. int maximumFPS = 30;
  62. bool shouldEnableAudio = true;
  63. std::string path;
  64. int option_index = 0;
  65. static struct option long_options [] = {
  66. {"screen-root", required_argument, 0, 'r'},
  67. {"pkg", required_argument, 0, 'p'},
  68. {"dir", required_argument, 0, 'd'},
  69. {"silent", no_argument, 0, 's'},
  70. {"help", no_argument, 0, 'h'},
  71. {"fps", required_argument, 0, 'f'},
  72. {nullptr, 0, 0, 0}
  73. };
  74. while (true)
  75. {
  76. int c = getopt_long (argc, argv, "r:p:d:shf:", long_options, &option_index);
  77. if (c == -1)
  78. break;
  79. switch (c)
  80. {
  81. case 'r':
  82. isRootWindow = true;
  83. screens.emplace_back (optarg);
  84. break;
  85. case 'p':
  86. if (mode == RUN_MODE_UNKNOWN)
  87. mode = RUN_MODE_PACKAGE;
  88. path = stringPathFixes (optarg);
  89. break;
  90. case 'd':
  91. if (mode == RUN_MODE_UNKNOWN)
  92. mode = RUN_MODE_DIRECTORY;
  93. path = stringPathFixes (optarg);
  94. break;
  95. case 's':
  96. shouldEnableAudio = false;
  97. break;
  98. case 'h':
  99. mode = RUN_MODE_HELP;
  100. break;
  101. case 'f':
  102. maximumFPS = atoi (optarg);
  103. break;
  104. default:
  105. break;
  106. }
  107. }
  108. if (mode == RUN_MODE_UNKNOWN || mode == RUN_MODE_HELP)
  109. {
  110. print_help (argv [0]);
  111. return 0;
  112. }
  113. // first of all, initialize the window
  114. if (glfwInit () == GLFW_FALSE)
  115. {
  116. fprintf (stderr, "Failed to initialize GLFW\n");
  117. return 1;
  118. }
  119. // initialize freeimage
  120. FreeImage_Initialise (TRUE);
  121. // set some window hints (opengl version to be used)
  122. glfwWindowHint (GLFW_SAMPLES, 4);
  123. glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 2);
  124. glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 1);
  125. std::string project_path = path + "project.json";
  126. auto containers = new WallpaperEngine::Assets::CCombinedContainer ();
  127. // add containers to the list
  128. containers->add (new WallpaperEngine::Assets::CDirectory ("./assets/"));
  129. // the background's path is required to load project.json regardless of the type of background we're using
  130. containers->add (new WallpaperEngine::Assets::CDirectory (path));
  131. if (mode == RUN_MODE_PACKAGE)
  132. {
  133. std::string scene_path = path + "scene.pkg";
  134. // add the package to the list
  135. containers->add (new WallpaperEngine::Assets::CPackage (scene_path));
  136. }
  137. else if (mode == RUN_MODE_DIRECTORY)
  138. {
  139. // nothing to do here anymore
  140. }
  141. // parse the project.json file
  142. auto project = WallpaperEngine::Core::CProject::fromFile ("project.json", containers);
  143. WallpaperEngine::Render::CWallpaper* wallpaper;
  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. glfwMakeContextCurrent (window);
  156. // TODO: FIGURE THESE OUT BASED ON THE SCREEN
  157. int windowWidth = 1920;
  158. int windowHeight = 1080;
  159. // get the real framebuffer size
  160. glfwGetFramebufferSize (window, &windowWidth, &windowHeight);
  161. if (glewInit () != GLEW_OK)
  162. {
  163. fprintf (stderr, "Failed to initialize GLEW");
  164. glfwTerminate ();
  165. return 3;
  166. }
  167. if (project->getType () == "scene")
  168. {
  169. WallpaperEngine::Core::CScene* scene = project->getWallpaper ()->as <WallpaperEngine::Core::CScene> ();
  170. wallpaper = new WallpaperEngine::Render::CScene (scene, containers);
  171. }
  172. else if (project->getType () == "video")
  173. {
  174. // special steps, running a video needs a root directory change, files are not loaded from the container classes
  175. // as they're streamed from disk
  176. chdir (path.c_str ());
  177. WallpaperEngine::Core::CVideo* video = project->getWallpaper ()->as <WallpaperEngine::Core::CVideo> ();
  178. wallpaper = new WallpaperEngine::Render::CVideo (video, containers);
  179. }
  180. else
  181. {
  182. throw std::runtime_error ("Unsupported wallpaper type");
  183. }
  184. if (shouldEnableAudio == true)
  185. {
  186. int mixer_flags = MIX_INIT_MP3 | MIX_INIT_FLAC | MIX_INIT_OGG;
  187. if (SDL_Init (SDL_INIT_AUDIO) < 0 || mixer_flags != Mix_Init (mixer_flags))
  188. {
  189. // Mix_GetError is an alias for SDL_GetError, so calling it directly will yield the correct result
  190. // it doesn't matter if SDL_Init or Mix_Init failed, both report the errors through the same functions
  191. fprintf (stderr, "Cannot initialize SDL audio system, SDL_GetError: %s", SDL_GetError ());
  192. return 2;
  193. }
  194. // initialize audio engine
  195. Mix_OpenAudio (22050, AUDIO_S16SYS, 2, 640);
  196. }
  197. // TODO: FIGURE OUT THE REQUIRED INPUT MODE, AS SOME WALLPAPERS USE THINGS LIKE MOUSE POSITION
  198. // glfwSetInputMode (window, GLFW_STICKY_KEYS, GL_TRUE);
  199. // enable depth text
  200. glEnable (GL_DEPTH_TEST);
  201. glDepthFunc (GL_LESS);
  202. // cull anything that doesn't look at the camera (might be useful to disable in the future)
  203. //glEnable (GL_CULL_FACE);
  204. clock_t minimumTime = 1000 / maximumFPS;
  205. clock_t startTime = 0;
  206. clock_t endTime = 0;
  207. while (glfwWindowShouldClose (window) == 0)
  208. {
  209. // calculate the current time value
  210. g_Time = (float) glfwGetTime ();
  211. // get the start time of the frame
  212. startTime = clock ();
  213. // render the scene
  214. wallpaper->render ();
  215. // do buffer swapping
  216. glfwSwapBuffers (window);
  217. // poll for events (like closing the window)
  218. glfwPollEvents ();
  219. // get the end time of the frame
  220. endTime = clock ();
  221. // ensure the frame time is correct to not overrun FPS
  222. if ((endTime - startTime) < minimumTime)
  223. usleep (static_cast <unsigned int> ((static_cast <double> ((minimumTime - (endTime - startTime))) / CLOCKS_PER_SEC) * 1000));
  224. }
  225. // terminate gl
  226. glfwTerminate ();
  227. // terminate SDL
  228. SDL_Quit ();
  229. // terminate free image
  230. FreeImage_DeInitialise ();
  231. return 0;
  232. }