main.cpp 11 KB

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