main.cpp 9.8 KB

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