main.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. // support for randr extended screens
  11. #include <X11/Xlib.h>
  12. #include <X11/extensions/Xrandr.h>
  13. #include "WallpaperEngine/Render/Shaders/Compiler.h"
  14. #include "WallpaperEngine/project.h"
  15. #include "WallpaperEngine/Irrlicht/Irrlicht.h"
  16. #include "WallpaperEngine/Irrlicht/CImageLoaderTEX.h"
  17. #include "WallpaperEngine/Core/CProject.h"
  18. bool IsRootWindow = false;
  19. std::vector<std::string> Screens;
  20. std::vector<irr::core::rect<irr::s32>> Viewports;
  21. irr::SIrrlichtCreationParameters _irr_params;
  22. irr::f32 g_Time = 0;
  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. // prepare basic configuration for irrlicht
  69. _irr_params.AntiAlias = 8;
  70. _irr_params.Bits = 16;
  71. // _irr_params.DeviceType = Irrlicht::EIDT_X11;
  72. _irr_params.DriverType = irr::video::EDT_OPENGL;
  73. _irr_params.Doublebuffer = false;
  74. _irr_params.EventReceiver = nullptr;
  75. _irr_params.Fullscreen = false;
  76. _irr_params.HandleSRGB = false;
  77. _irr_params.IgnoreInput = true;
  78. _irr_params.Stencilbuffer = true;
  79. _irr_params.UsePerformanceTimer = false;
  80. _irr_params.Vsync = false;
  81. _irr_params.WithAlphaChannel = false;
  82. _irr_params.ZBufferBits = 24;
  83. _irr_params.LoggingLevel = irr::ELL_DEBUG;
  84. initialize_viewports ();
  85. WallpaperEngine::Irrlicht::device = irr::createDeviceEx (_irr_params);
  86. if (WallpaperEngine::Irrlicht::device == nullptr)
  87. {
  88. return 1;
  89. }
  90. WallpaperEngine::Irrlicht::device->setWindowCaption (L"Test game");
  91. WallpaperEngine::Irrlicht::driver = WallpaperEngine::Irrlicht::device->getVideoDriver();
  92. // check for ps and vs support
  93. if (
  94. WallpaperEngine::Irrlicht::driver->queryFeature (irr::video::EVDF_PIXEL_SHADER_1_1) == false &&
  95. WallpaperEngine::Irrlicht::driver->queryFeature (irr::video::EVDF_ARB_FRAGMENT_PROGRAM_1) == false)
  96. {
  97. WallpaperEngine::Irrlicht::device->getLogger ()->log ("WARNING: Pixel shaders disabled because of missing driver/hardware support");
  98. }
  99. if (
  100. WallpaperEngine::Irrlicht::driver->queryFeature (irr::video::EVDF_VERTEX_SHADER_1_1) == false &&
  101. WallpaperEngine::Irrlicht::driver->queryFeature (irr::video::EVDF_ARB_VERTEX_PROGRAM_1) == false)
  102. {
  103. WallpaperEngine::Irrlicht::device->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. WallpaperEngine::Irrlicht::device->getFileSystem ()->addFileArchive ("assets.zip", true, false);
  111. // register custom loaders
  112. WallpaperEngine::Irrlicht::driver->addExternalImageLoader (new WallpaperEngine::Irrlicht::CImageLoaderTex ());
  113. WallpaperEngine::Irrlicht::device->getFileSystem ()->addArchiveLoader (new WallpaperEngine::Irrlicht::CArchiveLoaderPkg (WallpaperEngine::Irrlicht::device->getFileSystem ()));
  114. }
  115. void print_help (const char* route)
  116. {
  117. std::cout
  118. << "Usage:" << route << " [options] " << std::endl
  119. << "options:" << std::endl
  120. << " --silent\t\tMutes all the sound the wallpaper might produce" << std::endl
  121. << " --dir <folder>\tLoads an uncompressed background from the given <folder>" << std::endl
  122. << " --pkg <folder>\tLoads a scene.pkg file from the given <folder>" << std::endl
  123. << " --screen-root <screen name>\tDisplay as screen's background" << 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. bool audio_support = true;
  141. std::string path;
  142. int option_index = 0;
  143. static struct option long_options [] = {
  144. {"screen-root", required_argument, 0, 'r'},
  145. {"pkg", required_argument, 0, 'p'},
  146. {"dir", required_argument, 0, 'd'},
  147. {"silent", optional_argument, 0, 's'},
  148. {"help", optional_argument, 0, 'h'},
  149. {nullptr, 0, 0, 0}
  150. };
  151. while (true)
  152. {
  153. int c = getopt_long (argc, argv, "r:p:d:sh", long_options, &option_index);
  154. if (c == -1)
  155. break;
  156. switch (c)
  157. {
  158. case 'r':
  159. IsRootWindow = true;
  160. Screens.push_back (optarg);
  161. break;
  162. case 'p':
  163. mode = 1;
  164. path = optarg;
  165. break;
  166. case 'd':
  167. mode = 2;
  168. path = optarg;
  169. break;
  170. case 's':
  171. audio_support = false;
  172. break;
  173. case 'h':
  174. print_help (argv [0]);
  175. return 0;
  176. default:
  177. break;
  178. }
  179. }
  180. if (init_irrlicht())
  181. {
  182. return 1;
  183. }
  184. preconfigure_wallpaper_engine ();
  185. irr::io::path wallpaper_path;
  186. irr::io::path project_path;
  187. irr::io::path scene_path;
  188. switch (mode)
  189. {
  190. case 0:
  191. print_help (argv [0]);
  192. return 0;
  193. // pkg mode
  194. case 1:
  195. path = stringPathFixes(path);
  196. wallpaper_path = WallpaperEngine::Irrlicht::device->getFileSystem ()->getAbsolutePath (path.c_str ());
  197. project_path = wallpaper_path + "project.json";
  198. scene_path = wallpaper_path + "scene.pkg";
  199. WallpaperEngine::Irrlicht::device->getFileSystem ()->addFileArchive (scene_path, true, false); // add the pkg file to the lookup list
  200. break;
  201. // folder mode
  202. case 2:
  203. path = stringPathFixes(path);
  204. wallpaper_path = WallpaperEngine::Irrlicht::device->getFileSystem ()->getAbsolutePath (path.c_str ());
  205. project_path = wallpaper_path + "project.json";
  206. // set our working directory
  207. WallpaperEngine::Irrlicht::device->getFileSystem ()->changeWorkingDirectoryTo (wallpaper_path);
  208. break;
  209. default:
  210. break;
  211. }
  212. if (audio_support == true)
  213. {
  214. int mixer_flags = MIX_INIT_MP3 | MIX_INIT_FLAC | MIX_INIT_OGG;
  215. if (SDL_Init (SDL_INIT_AUDIO) < 0 || mixer_flags != Mix_Init (mixer_flags))
  216. {
  217. WallpaperEngine::Irrlicht::device->getLogger ()->log ("Cannot initialize SDL audio system", irr::ELL_ERROR);
  218. return -1;
  219. }
  220. // initialize audio engine
  221. Mix_OpenAudio (22050, AUDIO_S16SYS, 2, 640);
  222. }
  223. WallpaperEngine::project* wp_project = new WallpaperEngine::project (project_path);
  224. if (wp_project->getScene ()->isOrthogonal() == true)
  225. {
  226. WallpaperEngine::video::renderer::setupOrthographicCamera (wp_project->getScene ());
  227. }
  228. else
  229. {
  230. WallpaperEngine::Irrlicht::device->getLogger ()->log ("Non-orthogonal cameras not supported yet!!", irr::ELL_ERROR);
  231. return -2;
  232. }
  233. // register nodes
  234. WallpaperEngine::video::renderer::queueNode (wp_project->getScene ());
  235. int32_t lastTime = 0;
  236. int32_t minimumTime = 1000 / 90;
  237. int32_t currentTime = 0;
  238. while (WallpaperEngine::Irrlicht::device->run () && WallpaperEngine::Irrlicht::driver)
  239. {
  240. // if (device->isWindowActive ())
  241. {
  242. currentTime = WallpaperEngine::Irrlicht::device->getTimer ()->getTime ();
  243. g_Time = currentTime / 1000.0f;
  244. if (currentTime - lastTime > minimumTime)
  245. {
  246. std::vector<irr::core::rect<irr::s32>>::iterator cur = Viewports.begin ();
  247. std::vector<irr::core::rect<irr::s32>>::iterator end = Viewports.end ();
  248. for (; cur != end; cur ++)
  249. {
  250. // change viewport to render to the correct portion of the display
  251. WallpaperEngine::Irrlicht::driver->setViewPort (*cur);
  252. WallpaperEngine::video::renderer::render ();
  253. }
  254. lastTime = currentTime;
  255. }
  256. else
  257. {
  258. WallpaperEngine::Irrlicht::device->sleep (1, false);
  259. }
  260. }
  261. }
  262. SDL_Quit ();
  263. delete wp_project;
  264. return 0;
  265. }