main.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. #include <iostream>
  2. #include <irrlicht/irrlicht.h>
  3. #include <sstream>
  4. #include <wallpaperengine/video/renderer.h>
  5. #include <wallpaperengine/video/material.h>
  6. #include <wallpaperengine/irr/CPkgReader.h>
  7. #include <getopt.h>
  8. #include <SDL_mixer.h>
  9. #include <SDL.h>
  10. // support for randr extended screens
  11. #include <X11/Xlib.h>
  12. #include <X11/extensions/Xrandr.h>
  13. #include "wallpaperengine/shaders/compiler.h"
  14. #include "wallpaperengine/project.h"
  15. #include "wallpaperengine/irrlicht.h"
  16. #include "wallpaperengine/irr/CImageLoaderTEX.h"
  17. bool IsRootWindow = false;
  18. std::vector<std::string> Screens;
  19. std::vector<irr::core::rect<irr::s32>> Viewports;
  20. irr::SIrrlichtCreationParameters _irr_params;
  21. irr::f32 g_Time = 0;
  22. void initialize_viewports ()
  23. {
  24. if (IsRootWindow == false || Screens.size () == 0)
  25. return;
  26. Display* display = XOpenDisplay (NULL);
  27. int xrandr_result, xrandr_error;
  28. if (!XRRQueryExtension (display, &xrandr_result, &xrandr_error))
  29. {
  30. std::cerr << "XRandr is not present, cannot detect specified screens, running in window mode" << std::endl;
  31. return;
  32. }
  33. XRRScreenResources* screenResources = XRRGetScreenResources (display, DefaultRootWindow (display));
  34. // there are some situations where xrandr returns null (like screen not using the extension)
  35. if (screenResources == nullptr)
  36. return;
  37. for (int i = 0; i < screenResources->noutput; i ++)
  38. {
  39. XRROutputInfo* info = XRRGetOutputInfo (display, screenResources, screenResources->outputs [i]);
  40. // there are some situations where xrandr returns null (like screen not using the extension)
  41. if (info == nullptr)
  42. continue;
  43. std::vector<std::string>::iterator cur = Screens.begin ();
  44. std::vector<std::string>::iterator end = Screens.end ();
  45. for (; cur != end; cur ++)
  46. {
  47. if (info->connection == RR_Connected && strcmp (info->name, (*cur).c_str ()) == 0)
  48. {
  49. XRRCrtcInfo* crtc = XRRGetCrtcInfo (display, screenResources, info->crtc);
  50. std::cout << "Found requested screen: " << info->name << " -> " << crtc->x << "x" << crtc->y << ":" << crtc->width << "x" << crtc->height << std::endl;
  51. irr::core::rect<irr::s32> viewport;
  52. viewport.UpperLeftCorner.X = crtc->x;
  53. viewport.UpperLeftCorner.Y = crtc->y;
  54. viewport.LowerRightCorner.X = crtc->x + crtc->width;
  55. viewport.LowerRightCorner.Y = crtc->y + crtc->height;
  56. Viewports.push_back (viewport);
  57. XRRFreeCrtcInfo (crtc);
  58. }
  59. }
  60. XRRFreeOutputInfo (info);
  61. }
  62. XRRFreeScreenResources (screenResources);
  63. _irr_params.WindowId = reinterpret_cast<void*> (DefaultRootWindow (display));
  64. }
  65. int init_irrlicht()
  66. {
  67. // prepare basic configuration for irrlicht
  68. _irr_params.AntiAlias = 8;
  69. _irr_params.Bits = 16;
  70. // _irr_params.DeviceType = irr::EIDT_X11;
  71. _irr_params.DriverType = irr::video::EDT_OPENGL;
  72. _irr_params.Doublebuffer = false;
  73. _irr_params.EventReceiver = nullptr;
  74. _irr_params.Fullscreen = false;
  75. _irr_params.HandleSRGB = false;
  76. _irr_params.IgnoreInput = true;
  77. _irr_params.Stencilbuffer = true;
  78. _irr_params.UsePerformanceTimer = false;
  79. _irr_params.Vsync = false;
  80. _irr_params.WithAlphaChannel = false;
  81. _irr_params.ZBufferBits = 24;
  82. _irr_params.LoggingLevel = irr::ELL_DEBUG;
  83. initialize_viewports ();
  84. wp::irrlicht::device = irr::createDeviceEx (_irr_params);
  85. if (wp::irrlicht::device == nullptr)
  86. {
  87. return 1;
  88. }
  89. wp::irrlicht::device->setWindowCaption (L"Test game");
  90. wp::irrlicht::driver = wp::irrlicht::device->getVideoDriver();
  91. // check for ps and vs support
  92. if (
  93. wp::irrlicht::driver->queryFeature (irr::video::EVDF_PIXEL_SHADER_1_1) == false &&
  94. wp::irrlicht::driver->queryFeature (irr::video::EVDF_ARB_FRAGMENT_PROGRAM_1) == false)
  95. {
  96. wp::irrlicht::device->getLogger ()->log ("WARNING: Pixel shaders disabled because of missing driver/hardware support");
  97. }
  98. if (
  99. wp::irrlicht::driver->queryFeature (irr::video::EVDF_VERTEX_SHADER_1_1) == false &&
  100. wp::irrlicht::driver->queryFeature (irr::video::EVDF_ARB_VERTEX_PROGRAM_1) == false)
  101. {
  102. wp::irrlicht::device->getLogger ()->log ("WARNING: Vertex shaders disabled because of missing driver/hardware support");
  103. }
  104. return 0;
  105. }
  106. void preconfigure_wallpaper_engine ()
  107. {
  108. // load the assets from wallpaper engine
  109. wp::irrlicht::device->getFileSystem ()->addFileArchive ("assets.zip", true, false);
  110. // register custom loaders
  111. wp::irrlicht::driver->addExternalImageLoader (new irr::video::CImageLoaderTex ());
  112. wp::irrlicht::device->getFileSystem ()->addArchiveLoader (new CArchiveLoaderPkg (wp::irrlicht::device->getFileSystem ()));
  113. }
  114. void print_help (const char* route)
  115. {
  116. std::cout
  117. << "Usage:" << route << " [options] " << std::endl
  118. << "options:" << std::endl
  119. << " --silent\t\tMutes all the sound the wallpaper might produce" << std::endl
  120. << " --dir <folder>\tLoads an uncompressed background from the given <folder>" << std::endl
  121. << " --pkg <folder>\tLoads a scene.pkg file from the given <folder>" << std::endl
  122. << " --screen-root <screen name>\tDisplay as screen's background" << std::endl
  123. << " --fps <maximum-fps>\tLimits the FPS to the given number, useful to keep battery consumption low" << std::endl;
  124. }
  125. std::string stringPathFixes(const std::string& s){
  126. std::string str(s);
  127. if(str.empty())
  128. return s;
  129. if(str[0] == '\'' && str[str.size() - 1] == '\''){
  130. str.erase(str.size() - 1, 1);
  131. str.erase(0,1);
  132. }
  133. if(str[str.size() - 1] != '/')
  134. str += '/';
  135. return std::move(str);
  136. }
  137. int main (int argc, char* argv[])
  138. {
  139. int mode = 0;
  140. int max_fps = 30;
  141. bool audio_support = true;
  142. std::string path;
  143. int option_index = 0;
  144. static struct option long_options [] = {
  145. {"screen-root", required_argument, 0, 'r'},
  146. {"pkg", required_argument, 0, 'p'},
  147. {"dir", required_argument, 0, 'd'},
  148. {"silent", no_argument, 0, 's'},
  149. {"help", no_argument, 0, 'h'},
  150. {"fps", required_argument, 0, 'f'},
  151. {nullptr, 0, 0, 0}
  152. };
  153. while (true)
  154. {
  155. int c = getopt_long (argc, argv, "r:p:d:shf:", long_options, &option_index);
  156. if (c == -1)
  157. break;
  158. switch (c)
  159. {
  160. case 'r':
  161. IsRootWindow = true;
  162. Screens.push_back (optarg);
  163. break;
  164. case 'p':
  165. mode = 1;
  166. path = optarg;
  167. break;
  168. case 'd':
  169. mode = 2;
  170. path = optarg;
  171. break;
  172. case 's':
  173. audio_support = false;
  174. break;
  175. case 'h':
  176. print_help (argv [0]);
  177. return 0;
  178. case 'f':
  179. max_fps = atoi (optarg);
  180. break;
  181. default:
  182. break;
  183. }
  184. }
  185. if (init_irrlicht())
  186. {
  187. return 1;
  188. }
  189. preconfigure_wallpaper_engine ();
  190. irr::io::path wallpaper_path;
  191. irr::io::path project_path;
  192. irr::io::path scene_path;
  193. switch (mode)
  194. {
  195. case 0:
  196. print_help (argv [0]);
  197. return 0;
  198. // pkg mode
  199. case 1:
  200. path = stringPathFixes(path);
  201. wallpaper_path = wp::irrlicht::device->getFileSystem ()->getAbsolutePath (path.c_str ());
  202. project_path = wallpaper_path + "project.json";
  203. scene_path = wallpaper_path + "scene.pkg";
  204. wp::irrlicht::device->getFileSystem ()->addFileArchive (scene_path, true, false); // add the pkg file to the lookup list
  205. break;
  206. // folder mode
  207. case 2:
  208. path = stringPathFixes(path);
  209. wallpaper_path = wp::irrlicht::device->getFileSystem ()->getAbsolutePath (path.c_str ());
  210. project_path = wallpaper_path + "project.json";
  211. // set our working directory
  212. wp::irrlicht::device->getFileSystem ()->changeWorkingDirectoryTo (wallpaper_path);
  213. break;
  214. default:
  215. break;
  216. }
  217. if (audio_support == true)
  218. {
  219. int mixer_flags = MIX_INIT_MP3 | MIX_INIT_FLAC | MIX_INIT_OGG;
  220. if (SDL_Init (SDL_INIT_AUDIO) < 0 || mixer_flags != Mix_Init (mixer_flags))
  221. {
  222. wp::irrlicht::device->getLogger ()->log ("Cannot initialize SDL audio system", irr::ELL_ERROR);
  223. return -1;
  224. }
  225. // initialize audio engine
  226. Mix_OpenAudio (22050, AUDIO_S16SYS, 2, 640);
  227. }
  228. wp::project* wp_project = new wp::project (project_path);
  229. if (wp_project->getScene ()->isOrthogonal() == true)
  230. {
  231. wp::video::renderer::setupOrthographicCamera (wp_project->getScene ());
  232. }
  233. else
  234. {
  235. wp::irrlicht::device->getLogger ()->log ("Non-orthogonal cameras not supported yet!!", irr::ELL_ERROR);
  236. return -2;
  237. }
  238. // register nodes
  239. wp::video::renderer::queueNode (wp_project->getScene ());
  240. irr::u32 lastTime = 0;
  241. irr::u32 minimumTime = 1000 / max_fps;
  242. irr::u32 currentTime = 0;
  243. irr::u32 startTime = 0;
  244. irr::u32 endTime = 0;
  245. while (wp::irrlicht::device->run () && wp::irrlicht::driver)
  246. {
  247. // if (device->isWindowActive ())
  248. {
  249. currentTime = startTime = wp::irrlicht::device->getTimer ()->getTime ();
  250. g_Time = currentTime / 1000.0f;
  251. if (Viewports.size () > 0)
  252. {
  253. std::vector<irr::core::rect<irr::s32>>::iterator cur = Viewports.begin ();
  254. std::vector<irr::core::rect<irr::s32>>::iterator end = Viewports.end ();
  255. for (; cur != end; cur ++)
  256. {
  257. // change viewport to render to the correct portion of the display
  258. wp::irrlicht::driver->setViewPort (*cur);
  259. wp::video::renderer::render ();
  260. }
  261. }
  262. else
  263. {
  264. wp::video::renderer::render ();
  265. }
  266. endTime = wp::irrlicht::device->getTimer ()->getTime ();
  267. wp::irrlicht::device->sleep (minimumTime - (endTime - startTime), false);
  268. }
  269. }
  270. SDL_Quit ();
  271. delete wp_project;
  272. return 0;
  273. }