main.cpp 8.9 KB

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