main.cpp 11 KB

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