main.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. 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. // initialize custom context class
  145. WallpaperEngine::Render::CContext* context = new WallpaperEngine::Render::CContext (screens);
  146. // auto projection = project->getWallpaper ()->as <WallpaperEngine::Core::CScene> ()->getOrthogonalProjection ();
  147. // create the window!
  148. // TODO: DO WE NEED TO PASS MONITOR HERE OR ANYTHING?
  149. // TODO: FIGURE OUT HOW TO PUT THIS WINDOW IN THE BACKGROUND
  150. GLFWwindow* window = glfwCreateWindow (1920, 1080, "WallpaperEngine", NULL, NULL);
  151. if (window == nullptr)
  152. {
  153. fprintf (stderr, "Failed to open a GLFW window");
  154. glfwTerminate ();
  155. return 2;
  156. }
  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. if (glewInit () != GLEW_OK)
  164. {
  165. fprintf (stderr, "Failed to initialize GLEW");
  166. glfwTerminate ();
  167. return 3;
  168. }
  169. if (project->getType () == "scene")
  170. {
  171. WallpaperEngine::Core::CScene* scene = project->getWallpaper ()->as <WallpaperEngine::Core::CScene> ();
  172. wallpaper = new WallpaperEngine::Render::CScene (scene, containers);
  173. }
  174. else if (project->getType () == "video")
  175. {
  176. // special steps, running a video needs a root directory change, files are not loaded from the container classes
  177. // as they're streamed from disk
  178. chdir (path.c_str ());
  179. WallpaperEngine::Core::CVideo* video = project->getWallpaper ()->as <WallpaperEngine::Core::CVideo> ();
  180. wallpaper = new WallpaperEngine::Render::CVideo (video, containers);
  181. }
  182. else
  183. {
  184. throw std::runtime_error ("Unsupported wallpaper type");
  185. }
  186. // ensure the context knows what wallpaper to render
  187. context->setWallpaper (wallpaper);
  188. if (shouldEnableAudio == true)
  189. {
  190. int mixer_flags = MIX_INIT_MP3 | MIX_INIT_FLAC | MIX_INIT_OGG;
  191. if (SDL_Init (SDL_INIT_AUDIO) < 0 || mixer_flags != Mix_Init (mixer_flags))
  192. {
  193. // Mix_GetError is an alias for SDL_GetError, so calling it directly will yield the correct result
  194. // it doesn't matter if SDL_Init or Mix_Init failed, both report the errors through the same functions
  195. fprintf (stderr, "Cannot initialize SDL audio system, SDL_GetError: %s", SDL_GetError ());
  196. return 2;
  197. }
  198. // initialize audio engine
  199. Mix_OpenAudio (22050, AUDIO_S16SYS, 2, 640);
  200. }
  201. // TODO: FIGURE OUT THE REQUIRED INPUT MODE, AS SOME WALLPAPERS USE THINGS LIKE MOUSE POSITION
  202. // glfwSetInputMode (window, GLFW_STICKY_KEYS, GL_TRUE);
  203. // enable depth text
  204. glEnable (GL_DEPTH_TEST);
  205. glDepthFunc (GL_LESS);
  206. // cull anything that doesn't look at the camera (might be useful to disable in the future)
  207. //glEnable (GL_CULL_FACE);
  208. clock_t minimumTime = 1000 / maximumFPS;
  209. clock_t startTime = 0;
  210. clock_t endTime = 0;
  211. while (glfwWindowShouldClose (window) == 0)
  212. {
  213. // calculate the current time value
  214. g_Time = (float) glfwGetTime ();
  215. // get the start time of the frame
  216. startTime = clock ();
  217. // render the scene
  218. context->render ();
  219. // do buffer swapping
  220. glfwSwapBuffers (window);
  221. // poll for events (like closing the window)
  222. glfwPollEvents ();
  223. // get the end time of the frame
  224. endTime = clock ();
  225. // ensure the frame time is correct to not overrun FPS
  226. if ((endTime - startTime) < minimumTime)
  227. usleep (static_cast <unsigned int> ((static_cast <double> ((minimumTime - (endTime - startTime))) / CLOCKS_PER_SEC) * 1000));
  228. }
  229. // terminate gl
  230. glfwTerminate ();
  231. // terminate SDL
  232. SDL_Quit ();
  233. // terminate free image
  234. FreeImage_DeInitialise ();
  235. return 0;
  236. }