main.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. }
  124. std::string stringPathFixes(const std::string& s){
  125. std::string str(s);
  126. if(str.empty())
  127. return s;
  128. if(str[0] == '\'' && str[str.size() - 1] == '\''){
  129. str.erase(str.size() - 1, 1);
  130. str.erase(0,1);
  131. }
  132. if(str[str.size() - 1] != '/')
  133. str += '/';
  134. return std::move(str);
  135. }
  136. int main (int argc, char* argv[])
  137. {
  138. int mode = 0;
  139. bool audio_support = true;
  140. std::string path;
  141. int option_index = 0;
  142. static struct option long_options [] = {
  143. {"screen-root", required_argument, 0, 'r'},
  144. {"pkg", required_argument, 0, 'p'},
  145. {"dir", required_argument, 0, 'd'},
  146. {"silent", optional_argument, 0, 's'},
  147. {"help", optional_argument, 0, 'h'},
  148. {nullptr, 0, 0, 0}
  149. };
  150. while (true)
  151. {
  152. int c = getopt_long (argc, argv, "r:p:d:sh", long_options, &option_index);
  153. if (c == -1)
  154. break;
  155. switch (c)
  156. {
  157. case 'r':
  158. IsRootWindow = true;
  159. Screens.push_back (optarg);
  160. break;
  161. case 'p':
  162. mode = 1;
  163. path = optarg;
  164. break;
  165. case 'd':
  166. mode = 2;
  167. path = optarg;
  168. break;
  169. case 's':
  170. audio_support = false;
  171. break;
  172. case 'h':
  173. print_help (argv [0]);
  174. return 0;
  175. default:
  176. break;
  177. }
  178. }
  179. if (init_irrlicht())
  180. {
  181. return 1;
  182. }
  183. preconfigure_wallpaper_engine ();
  184. irr::io::path wallpaper_path;
  185. irr::io::path project_path;
  186. irr::io::path scene_path;
  187. switch (mode)
  188. {
  189. case 0:
  190. print_help (argv [0]);
  191. return 0;
  192. // pkg mode
  193. case 1:
  194. path = stringPathFixes(path);
  195. wallpaper_path = wp::irrlicht::device->getFileSystem ()->getAbsolutePath (path.c_str ());
  196. project_path = wallpaper_path + "project.json";
  197. scene_path = wallpaper_path + "scene.pkg";
  198. wp::irrlicht::device->getFileSystem ()->addFileArchive (scene_path, true, false); // add the pkg file to the lookup list
  199. break;
  200. // folder mode
  201. case 2:
  202. path = stringPathFixes(path);
  203. wallpaper_path = wp::irrlicht::device->getFileSystem ()->getAbsolutePath (path.c_str ());
  204. project_path = wallpaper_path + "project.json";
  205. // set our working directory
  206. wp::irrlicht::device->getFileSystem ()->changeWorkingDirectoryTo (wallpaper_path);
  207. break;
  208. default:
  209. break;
  210. }
  211. if (audio_support == true)
  212. {
  213. int mixer_flags = MIX_INIT_MP3 | MIX_INIT_FLAC | MIX_INIT_OGG;
  214. if (SDL_Init (SDL_INIT_AUDIO) < 0 || mixer_flags != Mix_Init (mixer_flags))
  215. {
  216. wp::irrlicht::device->getLogger ()->log ("Cannot initialize SDL audio system", irr::ELL_ERROR);
  217. return -1;
  218. }
  219. // initialize audio engine
  220. Mix_OpenAudio (22050, AUDIO_S16SYS, 2, 640);
  221. }
  222. wp::project* wp_project = new wp::project (project_path);
  223. if (wp_project->getScene ()->isOrthogonal() == true)
  224. {
  225. wp::video::renderer::setupOrthographicCamera (wp_project->getScene ());
  226. }
  227. else
  228. {
  229. wp::irrlicht::device->getLogger ()->log ("Non-orthogonal cameras not supported yet!!", irr::ELL_ERROR);
  230. return -2;
  231. }
  232. // register nodes
  233. wp::video::renderer::queueNode (wp_project->getScene ());
  234. int32_t lastTime = 0;
  235. int32_t minimumTime = 1000 / 90;
  236. int32_t currentTime = 0;
  237. while (wp::irrlicht::device->run () && wp::irrlicht::driver)
  238. {
  239. // if (device->isWindowActive ())
  240. {
  241. currentTime = wp::irrlicht::device->getTimer ()->getTime ();
  242. g_Time = currentTime / 1000.0f;
  243. if (currentTime - lastTime > minimumTime)
  244. {
  245. std::vector<irr::core::rect<irr::s32>>::iterator cur = Viewports.begin ();
  246. std::vector<irr::core::rect<irr::s32>>::iterator end = Viewports.end ();
  247. for (; cur != end; cur ++)
  248. {
  249. // change viewport to render to the correct portion of the display
  250. wp::irrlicht::driver->setViewPort (*cur);
  251. wp::video::renderer::render ();
  252. }
  253. lastTime = currentTime;
  254. }
  255. else
  256. {
  257. wp::irrlicht::device->sleep (1, false);
  258. }
  259. }
  260. }
  261. SDL_Quit ();
  262. delete wp_project;
  263. return 0;
  264. }