main.cpp 9.4 KB

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