main.cpp 8.3 KB

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