main.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. #include <iostream>
  2. #include <getopt.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <SDL_mixer.h>
  6. #include <SDL.h>
  7. #include <FreeImage.h>
  8. #include <sys/stat.h>
  9. #include <GL/glew.h>
  10. #include <csignal>
  11. #include <GLFW/glfw3.h>
  12. #include <libgen.h>
  13. #include "WallpaperEngine/Core/CProject.h"
  14. #include "WallpaperEngine/Render/CWallpaper.h"
  15. #include "WallpaperEngine/Render/CContext.h"
  16. #include "WallpaperEngine/Render/CVideo.h"
  17. #include "WallpaperEngine/Assets/CPackage.h"
  18. #include "WallpaperEngine/Assets/CDirectory.h"
  19. #include "WallpaperEngine/Assets/CVirtualContainer.h"
  20. #include "WallpaperEngine/Assets/CCombinedContainer.h"
  21. #include "WallpaperEngine/Assets/CPackageLoadException.h"
  22. float g_Time;
  23. bool g_KeepRunning = true;
  24. int g_AudioVolume = 15;
  25. using namespace WallpaperEngine::Core::Types;
  26. const char* assets_default_paths [] = {
  27. ".steam/steam/steamapps/common/wallpaper_engine/assets",
  28. ".local/share/Steam/steamapps/common/wallpaper_engine/assets",
  29. nullptr
  30. };
  31. const char* backgrounds_default_paths [] = {
  32. ".local/share/Steam/steamapps/workshop/content/431960",
  33. ".steam/steam/steamapps/workshop/content/431960",
  34. nullptr
  35. };
  36. void print_help (const char* route)
  37. {
  38. std::cout
  39. << "Usage: " << route << " [options] background_path/background_id" << std::endl
  40. << std::endl
  41. << "where background_path/background_id can be:" << std::endl
  42. << "\tthe ID of the background (for autodetection on your steam installation)" << std::endl
  43. << "\ta full path to the background's folder" << std::endl
  44. << std::endl
  45. << "options:" << std::endl
  46. << "\t--silent\t\t\t\t\tMutes all the sound the wallpaper might produce" << std::endl
  47. << "\t--volume <amount>\t\t\tSets the volume for all the sounds in the background" << std::endl
  48. << "\t--screen-root <screen name>\tDisplay as screen's background" << std::endl
  49. << "\t--fps <maximum-fps>\t\t\tLimits the FPS to the given number, useful to keep battery consumption low" << std::endl
  50. << "\t--assets-dir <path>\t\t\tFolder where the assets are stored" << std::endl
  51. << "\t--screenshot\t\t\t\tTakes a screenshot of the background" << std::endl
  52. << "\t--list-properties\t\t\tList all the available properties and their possible values" << std::endl
  53. << "\t--set-property <name=value>\tOverrides the default value of the given property" << std::endl;
  54. }
  55. std::string stringPathFixes(const std::string& s)
  56. {
  57. if (s.empty () == true)
  58. return s;
  59. std::string str (s);
  60. // remove single-quotes from the arguments
  61. if (str [0] == '\'' && str [str.size() - 1] == '\'')
  62. str
  63. .erase (str.size() - 1, 1)
  64. .erase (0, 1);
  65. return std::move (str);
  66. }
  67. void signalhandler(int sig)
  68. {
  69. g_KeepRunning = false;
  70. }
  71. int validatePath(const char* path, std::string& final)
  72. {
  73. char finalPath [PATH_MAX];
  74. char* pointer = realpath (path, finalPath);
  75. if (finalPath == nullptr)
  76. return errno;
  77. // ensure the path points to a folder
  78. struct stat pathinfo;
  79. if (stat (finalPath, &pathinfo) != 0)
  80. return errno;
  81. if (!S_ISDIR (pathinfo.st_mode))
  82. return ENOTDIR;
  83. final = finalPath;
  84. return 0;
  85. }
  86. std::string getHomePath ()
  87. {
  88. char* home = getenv ("HOME");
  89. if (home == nullptr)
  90. throw std::runtime_error ("$HOME doesn't exist");
  91. std::string homepath;
  92. int error = validatePath (home, homepath);
  93. if (error == ENOTDIR)
  94. throw std::runtime_error ("Invalid user home path");
  95. else if (error == ENAMETOOLONG)
  96. throw std::runtime_error ("Cannot get user's home folder, path is too long");
  97. else if (error != 0)
  98. throw std::runtime_error ("Cannot find the home folder for the user");
  99. return homepath;
  100. }
  101. void initGLFW ()
  102. {
  103. // first of all, initialize the window
  104. if (glfwInit () == GLFW_FALSE)
  105. throw std::runtime_error ("Failed to initialize GLFW");
  106. // initialize freeimage
  107. FreeImage_Initialise (TRUE);
  108. // set some window hints (opengl version to be used)
  109. glfwWindowHint (GLFW_SAMPLES, 4);
  110. glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 3);
  111. glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 3);
  112. glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  113. if (DEBUG)
  114. glfwWindowHint (GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
  115. }
  116. void addPkg (CCombinedContainer* containers, const std::string& path, std::string pkgfile)
  117. {
  118. try
  119. {
  120. std::string scene_path = path + pkgfile;
  121. // add the package to the list
  122. containers->add (new WallpaperEngine::Assets::CPackage (scene_path));
  123. std::cout << "Detected " << pkgfile << " file at " << scene_path << ". Adding to list of searchable paths" << std::endl;
  124. }
  125. catch (CPackageLoadException& ex)
  126. {
  127. // ignore this error, the package file was not found
  128. std::cout << "No " << pkgfile << " file found at " << path << ". Defaulting to normal folder storage" << std::endl;
  129. }
  130. catch (std::runtime_error& ex)
  131. {
  132. // the package was found but there was an error loading it (wrong header or something)
  133. fprintf (stderr, "Failed to load scene.pkg file: %s\n", ex.what());
  134. throw std::runtime_error ("Cannot load package file");
  135. }
  136. }
  137. CVirtualContainer* buildVirtualContainer ()
  138. {
  139. CVirtualContainer* container = new WallpaperEngine::Assets::CVirtualContainer ();
  140. //
  141. // Had to get a little creative with the effects to achieve the same bloom effect without any custom code
  142. // these virtual files are loaded by an image in the scene that takes current _rt_FullFrameBuffer and
  143. // applies the bloom effect to render it out to the screen
  144. //
  145. // add the effect file for screen bloom
  146. container->add (
  147. "effects/wpenginelinux/bloomeffect.json",
  148. "{"
  149. "\t\"name\":\"camerabloom_wpengine_linux\","
  150. "\t\"group\":\"wpengine_linux_camera\","
  151. "\t\"dependencies\":[],"
  152. "\t\"passes\":"
  153. "\t["
  154. "\t\t{"
  155. "\t\t\t\"material\": \"materials/util/downsample_quarter_bloom.json\","
  156. "\t\t\t\"target\": \"_rt_4FrameBuffer\","
  157. "\t\t\t\"bind\":"
  158. "\t\t\t["
  159. "\t\t\t\t{"
  160. "\t\t\t\t\t\"name\": \"_rt_FullFrameBuffer\","
  161. "\t\t\t\t\t\"index\": 0"
  162. "\t\t\t\t}"
  163. "\t\t\t]"
  164. "\t\t},"
  165. "\t\t{"
  166. "\t\t\t\"material\": \"materials/util/downsample_eighth_blur_v.json\","
  167. "\t\t\t\"target\": \"_rt_8FrameBuffer\","
  168. "\t\t\t\"bind\":"
  169. "\t\t\t["
  170. "\t\t\t\t{"
  171. "\t\t\t\t\t\"name\": \"_rt_4FrameBuffer\","
  172. "\t\t\t\t\t\"index\": 0"
  173. "\t\t\t\t}"
  174. "\t\t\t]"
  175. "\t\t},"
  176. "\t\t{"
  177. "\t\t\t\"material\": \"materials/util/blur_h_bloom.json\","
  178. "\t\t\t\"target\": \"_rt_Bloom\","
  179. "\t\t\t\"bind\":"
  180. "\t\t\t["
  181. "\t\t\t\t{"
  182. "\t\t\t\t\t\"name\": \"_rt_8FrameBuffer\","
  183. "\t\t\t\t\t\"index\": 0"
  184. "\t\t\t\t}"
  185. "\t\t\t]"
  186. "\t\t},"
  187. "\t\t{"
  188. "\t\t\t\"material\": \"materials/util/combine.json\","
  189. "\t\t\t\"target\": \"_rt_FullFrameBuffer\","
  190. "\t\t\t\"bind\":"
  191. "\t\t\t["
  192. "\t\t\t\t{"
  193. "\t\t\t\t\t\"name\": \"_rt_imageLayerComposite_-1_a\","
  194. "\t\t\t\t\t\"index\": 0"
  195. "\t\t\t\t},"
  196. "\t\t\t\t{"
  197. "\t\t\t\t\t\"name\": \"_rt_Bloom\","
  198. "\t\t\t\t\t\"index\": 1"
  199. "\t\t\t\t}"
  200. "\t\t\t]"
  201. "\t\t}"
  202. "\t]"
  203. "}"
  204. );
  205. // add some model for the image element even if it's going to waste rendering cycles
  206. container->add (
  207. "models/wpenginelinux.json",
  208. "{"
  209. "\t\"material\":\"materials/wpenginelinux.json\""
  210. "}"
  211. );
  212. // models require materials, so add that too
  213. container->add (
  214. "materials/wpenginelinux.json",
  215. "{"
  216. "\t\"passes\":"
  217. "\t\t["
  218. "\t\t\t{"
  219. "\t\t\t\t\"blending\": \"normal\","
  220. "\t\t\t\t\"cullmode\": \"nocull\","
  221. "\t\t\t\t\"depthtest\": \"disabled\","
  222. "\t\t\t\t\"depthwrite\": \"disabled\","
  223. "\t\t\t\t\"shader\": \"genericimage2\","
  224. "\t\t\t\t\"textures\": [\"_rt_FullFrameBuffer\"]"
  225. "\t\t\t}"
  226. "\t\t]"
  227. "}"
  228. );
  229. return container;
  230. }
  231. void takeScreenshot (WallpaperEngine::Render::CWallpaper* wp, const std::string& filename, FREE_IMAGE_FORMAT format)
  232. {
  233. GLint width, height;
  234. // bind texture and get the size
  235. glBindFramebuffer (GL_FRAMEBUFFER, wp->getWallpaperFramebuffer ());
  236. glBindTexture (GL_TEXTURE_2D, wp->getWallpaperTexture ());
  237. glGetTexLevelParameteriv (GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
  238. glGetTexLevelParameteriv (GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);
  239. // make room for storing the pixel data
  240. uint8_t* buffer = new uint8_t [width * height * sizeof (uint8_t) * 3];
  241. uint8_t* pixel = buffer;
  242. // read the image into the buffer
  243. glReadPixels (0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, buffer);
  244. // build the output file with FreeImage
  245. FIBITMAP* bitmap = FreeImage_Allocate (width, height, 24);
  246. RGBQUAD color;
  247. // now get access to the pixels
  248. for (int y = height; y > 0; y --)
  249. {
  250. for (int x = 0; x < width; x ++)
  251. {
  252. color.rgbRed = *pixel ++;
  253. color.rgbGreen = *pixel ++;
  254. color.rgbBlue = *pixel ++;
  255. // set the pixel in the destination
  256. FreeImage_SetPixelColor (bitmap, x, y, &color);
  257. }
  258. }
  259. // finally save the file
  260. FreeImage_Save (format, bitmap, filename.c_str (), 0);
  261. // free all the used memory
  262. delete[] buffer;
  263. FreeImage_Unload (bitmap);
  264. // unbind the textures
  265. glBindTexture (GL_TEXTURE_2D, GL_NONE);
  266. }
  267. int main (int argc, char* argv[])
  268. {
  269. std::vector <std::string> screens;
  270. std::map <std::string, std::string> propertyOverrides;
  271. int maximumFPS = 30;
  272. bool shouldEnableAudio = true;
  273. bool shouldTakeScreenshot = false;
  274. bool shouldListPropertiesAndStop = false;
  275. FREE_IMAGE_FORMAT screenshotFormat = FIF_UNKNOWN;
  276. std::string path;
  277. std::string assetsDir;
  278. std::string screenshotPath;
  279. static struct option long_options [] = {
  280. {"screen-root", required_argument, 0, 'r'},
  281. {"pkg", required_argument, 0, 'p'},
  282. {"dir", required_argument, 0, 'd'},
  283. {"silent", no_argument, 0, 's'},
  284. {"volume", required_argument, 0, 'v'},
  285. {"help", no_argument, 0, 'h'},
  286. {"fps", required_argument, 0, 'f'},
  287. {"assets-dir", required_argument, 0, 'a'},
  288. {"screenshot", required_argument, 0, 'c'},
  289. {"list-properties", no_argument, 0, 'l'},
  290. {"set-property", required_argument, 0, 'o'},
  291. {nullptr, 0, 0, 0}
  292. };
  293. while (true)
  294. {
  295. int c = getopt_long (argc, argv, "r:p:d:shf:a:", long_options, nullptr);
  296. if (c == -1)
  297. break;
  298. switch (c)
  299. {
  300. case 'o':
  301. {
  302. std::string value = optarg;
  303. std::string::size_type equals = value.find ('=');
  304. // properties without value are treated as booleans for now
  305. if (equals == std::string::npos)
  306. propertyOverrides.insert_or_assign (value, "1");
  307. else
  308. propertyOverrides.insert_or_assign (
  309. value.substr (0, equals),
  310. value.substr (equals + 1)
  311. );
  312. }
  313. break;
  314. case 'l':
  315. shouldListPropertiesAndStop = true;
  316. break;
  317. case 'r':
  318. screens.emplace_back (optarg);
  319. break;
  320. case 'p':
  321. case 'd':
  322. std::cerr << "--dir/--pkg is deprecated and not used anymore" << std::endl;
  323. path = stringPathFixes (optarg);
  324. break;
  325. case 's':
  326. shouldEnableAudio = false;
  327. break;
  328. case 'h':
  329. print_help (argv [0]);
  330. break;
  331. case 'f':
  332. maximumFPS = atoi (optarg);
  333. break;
  334. case 'a':
  335. assetsDir = optarg;
  336. break;
  337. case 'v':
  338. g_AudioVolume = atoi (optarg);
  339. break;
  340. case 'c':
  341. shouldTakeScreenshot = true;
  342. screenshotPath = stringPathFixes (optarg);
  343. break;
  344. }
  345. }
  346. if (path.empty () == true)
  347. {
  348. if (optind < argc && strlen (argv [optind]) > 0)
  349. {
  350. path = argv [optind];
  351. }
  352. else
  353. {
  354. print_help (argv [0]);
  355. return 0;
  356. }
  357. }
  358. // validate screenshot file just to make sure
  359. if (shouldTakeScreenshot == true)
  360. {
  361. // ensure the file is one of the supported formats
  362. std::string extension = screenshotPath.substr (screenshotPath.find_last_of (".") + 1);
  363. if (extension == "bmp")
  364. screenshotFormat = FIF_BMP;
  365. else if (extension == "png")
  366. screenshotFormat = FIF_PNG;
  367. else if (extension == "jpg" || extension == "jpeg")
  368. screenshotFormat = FIF_JPEG;
  369. else
  370. throw std::runtime_error ("Unsupported screenshot format...");
  371. }
  372. std::string homepath = getHomePath ();
  373. auto containers = new WallpaperEngine::Assets::CCombinedContainer ();
  374. // check if the background might be an ID and try to find the right path in the steam installation folder
  375. if (path.find ('/') == std::string::npos)
  376. {
  377. for (const char** current = backgrounds_default_paths; *current != nullptr; current ++)
  378. {
  379. std::string tmppath = homepath + "/" + *current + "/" + path;
  380. int error = validatePath (tmppath.c_str (), tmppath);
  381. if (error != 0)
  382. continue;
  383. path = tmppath;
  384. }
  385. }
  386. int error = validatePath (path.c_str (), path);
  387. if (error == ENOTDIR)
  388. throw std::runtime_error ("The background path is not a folder");
  389. else if (error == ENAMETOOLONG)
  390. throw std::runtime_error ("Cannot get wallpaper's folder, path is too long");
  391. else if (error != 0)
  392. throw std::runtime_error ("Cannot find the specified folder");
  393. // add a trailing slash to the path so the right file can be found
  394. path += "/";
  395. // the background's path is required to load project.json regardless of the type of background we're using
  396. containers->add (new WallpaperEngine::Assets::CDirectory (path));
  397. // add the virtual container for mocked up files
  398. containers->add (buildVirtualContainer ());
  399. // try to add the common packages
  400. addPkg (containers, path, "scene.pkg");
  401. addPkg (containers, path, "gifscene.pkg");
  402. if (assetsDir.empty () == true)
  403. {
  404. for (const char** current = assets_default_paths; *current != nullptr; current ++)
  405. {
  406. std::string tmppath = homepath + "/" + *current;
  407. error = validatePath (tmppath.c_str (), tmppath);
  408. if (error != 0)
  409. continue;
  410. assetsDir = tmppath;
  411. std::cout << "Found wallpaper engine's assets at " << assetsDir << std::endl;
  412. break;
  413. }
  414. if (assetsDir.empty () == true)
  415. {
  416. unsigned long len = strlen (argv [0]) + 1;
  417. char* copy = new char[len];
  418. strncpy (copy, argv [0], len);
  419. // path still not found, try one last thing on the current binary's folder
  420. std::string exepath = dirname (copy);
  421. exepath += "/assets";
  422. error = validatePath (exepath.c_str (), exepath);
  423. if (error == 0)
  424. {
  425. assetsDir = exepath;
  426. std::cout << "Found assets folder alongside the binary: " << assetsDir << std::endl;
  427. }
  428. delete[] copy;
  429. }
  430. }
  431. else
  432. {
  433. error = validatePath (assetsDir.c_str (), assetsDir);
  434. if (error == ENOTDIR)
  435. throw std::runtime_error ("Invalid assets folder");
  436. else if (error == ENAMETOOLONG)
  437. throw std::runtime_error ("Cannot get assets folder, path is too long");
  438. else if (error != 0)
  439. throw std::runtime_error ("Cannot find the specified assets folder");
  440. std::cout << "Found wallpaper engine's assets at " << assetsDir << " based on --assets-dir parameter" << std::endl;
  441. }
  442. if (assetsDir.empty () == true)
  443. throw std::runtime_error ("Cannot determine a valid path for the wallpaper engine assets");
  444. // add containers to the list
  445. containers->add (new WallpaperEngine::Assets::CDirectory (assetsDir + "/"));
  446. // parse the project.json file
  447. auto project = WallpaperEngine::Core::CProject::fromFile ("project.json", containers);
  448. // go to the right folder so the videos will play
  449. if (project->getWallpaper ()->is <WallpaperEngine::Core::CVideo> () == true)
  450. chdir (path.c_str ());
  451. // show properties if required
  452. for (auto cur : project->getProperties ())
  453. {
  454. // update the value of the property
  455. auto override = propertyOverrides.find (cur->getName ());
  456. if (override != propertyOverrides.end ())
  457. cur->update (override->second);
  458. if (shouldListPropertiesAndStop)
  459. std::cout << cur->dump () << std::endl;
  460. }
  461. // halt if the list-properties option was specified
  462. if (shouldListPropertiesAndStop)
  463. return 0;
  464. // attach signals so if a stop is requested the X11 resources are freed and the program shutsdown gracefully
  465. std::signal(SIGINT, signalhandler);
  466. std::signal(SIGTERM, signalhandler);
  467. // initialize glfw
  468. initGLFW ();
  469. // auto projection = project->getWallpaper ()->as <WallpaperEngine::Core::CScene> ()->getOrthogonalProjection ();
  470. // create the window!
  471. // TODO: DO WE NEED TO PASS MONITOR HERE OR ANYTHING?
  472. // TODO: FIGURE OUT HOW TO PUT THIS WINDOW IN THE BACKGROUND
  473. GLFWwindow* window = glfwCreateWindow (1920, 1080, "WallpaperEngine", NULL, NULL);
  474. if (window == nullptr)
  475. {
  476. fprintf (stderr, "Failed to open a GLFW window");
  477. glfwTerminate ();
  478. return 2;
  479. }
  480. glfwMakeContextCurrent (window);
  481. // TODO: FIGURE THESE OUT BASED ON THE SCREEN
  482. int windowWidth = 1920;
  483. int windowHeight = 1080;
  484. // get the real framebuffer size
  485. glfwGetFramebufferSize (window, &windowWidth, &windowHeight);
  486. GLenum glewInitResult = glewInit ();
  487. // initialize glew
  488. if (glewInitResult != GLEW_OK)
  489. {
  490. fprintf (stderr, "Failed to initialize GLEW: %s", glewGetErrorString (glewInitResult));
  491. glfwTerminate ();
  492. return 3;
  493. }
  494. if (shouldEnableAudio == true && SDL_Init (SDL_INIT_AUDIO) < 0)
  495. {
  496. std::cerr << "Cannot initialize SDL audio system, SDL_GetError: " << SDL_GetError() << std::endl;
  497. std::cerr << "Continuing without audio support" << std::endl;
  498. }
  499. // initialize custom context class
  500. WallpaperEngine::Render::CContext* context = new WallpaperEngine::Render::CContext (screens, window, containers);
  501. // initialize mouse support
  502. context->setMouse (new CMouseInput (window));
  503. // set the default viewport
  504. context->setDefaultViewport ({0, 0, windowWidth, windowHeight});
  505. // ensure the context knows what wallpaper to render
  506. context->setWallpaper (
  507. WallpaperEngine::Render::CWallpaper::fromWallpaper (project->getWallpaper (), context)
  508. );
  509. // update maximum FPS if it's a video
  510. if (context->getWallpaper ()->is <WallpaperEngine::Render::CVideo> () == true)
  511. maximumFPS = context->getWallpaper ()->as <WallpaperEngine::Render::CVideo> ()->getFPS ();
  512. // TODO: FIGURE OUT THE REQUIRED INPUT MODE, AS SOME WALLPAPERS USE THINGS LIKE MOUSE POSITION
  513. // glfwSetInputMode (window, GLFW_STICKY_KEYS, GL_TRUE);
  514. double startTime, endTime, minimumTime = 1.0 / maximumFPS;
  515. uint32_t frameCounter = 0;
  516. while (glfwWindowShouldClose (window) == 0 && g_KeepRunning == true)
  517. {
  518. // get the real framebuffer size
  519. glfwGetFramebufferSize (window, &windowWidth, &windowHeight);
  520. // set the default viewport
  521. context->setDefaultViewport ({0, 0, windowWidth, windowHeight});
  522. // calculate the current time value
  523. g_Time = (float) glfwGetTime ();
  524. // get the start time of the frame
  525. startTime = glfwGetTime ();
  526. // render the scene
  527. context->render ();
  528. // do buffer swapping
  529. glfwSwapBuffers (window);
  530. // poll for events (like closing the window)
  531. glfwPollEvents ();
  532. // get the end time of the frame
  533. endTime = glfwGetTime ();
  534. // ensure the frame time is correct to not overrun FPS
  535. if ((endTime - startTime) < minimumTime)
  536. usleep ((minimumTime - (endTime - startTime)) * CLOCKS_PER_SEC);
  537. frameCounter ++;
  538. if (frameCounter == 5 && shouldTakeScreenshot == true)
  539. {
  540. takeScreenshot (context->getWallpaper (), screenshotPath, screenshotFormat);
  541. // disable screenshot just in case the counter overflows
  542. shouldTakeScreenshot = false;
  543. }
  544. }
  545. // ensure this is updated as sometimes it might not come from a signal
  546. g_KeepRunning = false;
  547. std::cout << "Stop requested" << std::endl;
  548. // terminate gl
  549. glfwTerminate ();
  550. // terminate free image
  551. FreeImage_DeInitialise ();
  552. // terminate SDL
  553. SDL_QuitSubSystem(SDL_INIT_AUDIO);
  554. SDL_Quit ();
  555. // free context
  556. delete context;
  557. return 0;
  558. }