main.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. #include "wallpaperengine/shaders/compiler.h"
  11. #include "wallpaperengine/project.h"
  12. #include "wallpaperengine/irrlicht.h"
  13. #include "wallpaperengine/irr/CImageLoaderTEX.h"
  14. int WinID = 0;
  15. irr::SIrrlichtCreationParameters _irr_params;
  16. irr::f32 g_Time = 0;
  17. int init_irrlicht()
  18. {
  19. // prepare basic configuration for irrlicht
  20. _irr_params.AntiAlias = 8;
  21. _irr_params.Bits = 16;
  22. // _irr_params.DeviceType = irr::EIDT_X11;
  23. _irr_params.DriverType = irr::video::EDT_OPENGL;
  24. _irr_params.Doublebuffer = true;
  25. _irr_params.EventReceiver = nullptr;
  26. _irr_params.Fullscreen = false;
  27. _irr_params.HandleSRGB = false;
  28. _irr_params.IgnoreInput = true;
  29. _irr_params.Stencilbuffer = true;
  30. _irr_params.UsePerformanceTimer = false;
  31. _irr_params.Vsync = false;
  32. _irr_params.WithAlphaChannel = false;
  33. _irr_params.ZBufferBits = 24;
  34. _irr_params.LoggingLevel = irr::ELL_DEBUG;
  35. _irr_params.WindowId = reinterpret_cast<void*> (WinID);
  36. wp::irrlicht::device = irr::createDeviceEx (_irr_params);
  37. if (wp::irrlicht::device == nullptr)
  38. {
  39. return 1;
  40. }
  41. wp::irrlicht::device->setWindowCaption (L"Test game");
  42. wp::irrlicht::driver = wp::irrlicht::device->getVideoDriver();
  43. // check for ps and vs support
  44. if (
  45. wp::irrlicht::driver->queryFeature (irr::video::EVDF_PIXEL_SHADER_1_1) == false &&
  46. wp::irrlicht::driver->queryFeature (irr::video::EVDF_ARB_FRAGMENT_PROGRAM_1) == false)
  47. {
  48. wp::irrlicht::device->getLogger ()->log ("WARNING: Pixel shaders disabled because of missing driver/hardware support");
  49. }
  50. if (
  51. wp::irrlicht::driver->queryFeature (irr::video::EVDF_VERTEX_SHADER_1_1) == false &&
  52. wp::irrlicht::driver->queryFeature (irr::video::EVDF_ARB_VERTEX_PROGRAM_1) == false)
  53. {
  54. wp::irrlicht::device->getLogger ()->log ("WARNING: Vertex shaders disabled because of missing driver/hardware support");
  55. }
  56. return 0;
  57. }
  58. void preconfigure_wallpaper_engine ()
  59. {
  60. // load the assets from wallpaper engine
  61. wp::irrlicht::device->getFileSystem ()->addFileArchive ("assets.zip", true, false);
  62. // register custom loaders
  63. wp::irrlicht::driver->addExternalImageLoader (new irr::video::CImageLoaderTex ());
  64. wp::irrlicht::device->getFileSystem ()->addArchiveLoader (new CArchiveLoaderPkg (wp::irrlicht::device->getFileSystem ()));
  65. }
  66. void print_help (const char* route)
  67. {
  68. std::cout
  69. << "Usage:" << route << " [options] " << std::endl
  70. << "options:" << std::endl
  71. << " --silent\t\tMutes all the sound the wallpaper might produce" << std::endl
  72. << " --dir <folder>\tLoads an uncompressed background from the given <folder>" << std::endl
  73. << " --pkg <folder>\tLoads a scene.pkg file from the given <folder>" << std::endl
  74. << " --win <WindowID>\tX Window ID to attach to" << std::endl;
  75. }
  76. std::string stringPathFixes(const std::string& s){
  77. std::string str(s);
  78. if(str.empty())
  79. return s;
  80. if(str[0] == '\'' && str[str.size() - 1] == '\''){
  81. str.erase(str.size() - 1, 1);
  82. str.erase(0,1);
  83. }
  84. if(str[str.size() - 1] != '/')
  85. str += '/';
  86. return std::move(str);
  87. }
  88. int main (int argc, char* argv[])
  89. {
  90. int mode = 0;
  91. bool audio_support = true;
  92. std::string path;
  93. int option_index = 0;
  94. static struct option long_options [] = {
  95. {"win", required_argument, 0, 'w'},
  96. {"pkg", required_argument, 0, 'p'},
  97. {"dir", required_argument, 0, 'd'},
  98. {"silent", optional_argument, 0, 's'},
  99. {"help", optional_argument, 0, 'h'},
  100. {nullptr, 0, 0, 0}
  101. };
  102. while (true)
  103. {
  104. int c = getopt_long (argc, argv, "w:p:d:sh", long_options, &option_index);
  105. if (c == -1)
  106. break;
  107. switch (c)
  108. {
  109. case 'w':
  110. if (optarg)
  111. WinID = atoi (optarg);
  112. break;
  113. case 'p':
  114. mode = 1;
  115. path = optarg;
  116. break;
  117. case 'd':
  118. mode = 2;
  119. path = optarg;
  120. break;
  121. case 's':
  122. audio_support = false;
  123. break;
  124. case 'h':
  125. print_help (argv [0]);
  126. return 0;
  127. default:
  128. break;
  129. }
  130. }
  131. std::cout << "Initializing irrlicht to WindowID " << WinID << std::endl;
  132. if (init_irrlicht())
  133. {
  134. return 1;
  135. }
  136. preconfigure_wallpaper_engine ();
  137. irr::io::path wallpaper_path;
  138. irr::io::path project_path;
  139. irr::io::path scene_path;
  140. switch (mode)
  141. {
  142. case 0:
  143. print_help (argv [0]);
  144. return 0;
  145. // pkg mode
  146. case 1:
  147. path = stringPathFixes(path);
  148. wallpaper_path = wp::irrlicht::device->getFileSystem ()->getAbsolutePath (path.c_str ());
  149. project_path = wallpaper_path + "project.json";
  150. scene_path = wallpaper_path + "scene.pkg";
  151. wp::irrlicht::device->getFileSystem ()->addFileArchive (scene_path, true, false); // add the pkg file to the lookup list
  152. break;
  153. // folder mode
  154. case 2:
  155. path = stringPathFixes(path);
  156. wallpaper_path = wp::irrlicht::device->getFileSystem ()->getAbsolutePath (path.c_str ());
  157. project_path = wallpaper_path + "project.json";
  158. // set our working directory
  159. wp::irrlicht::device->getFileSystem ()->changeWorkingDirectoryTo (wallpaper_path);
  160. break;
  161. default:
  162. break;
  163. }
  164. if (audio_support == true)
  165. {
  166. int mixer_flags = MIX_INIT_MP3 | MIX_INIT_FLAC | MIX_INIT_OGG;
  167. if (SDL_Init (SDL_INIT_AUDIO) < 0 || mixer_flags != Mix_Init (mixer_flags))
  168. {
  169. wp::irrlicht::device->getLogger ()->log ("Cannot initialize SDL audio system", irr::ELL_ERROR);
  170. return -1;
  171. }
  172. // initialize audio engine
  173. Mix_OpenAudio (22050, AUDIO_S16SYS, 2, 640);
  174. }
  175. wp::project* wp_project = new wp::project (project_path);
  176. if (wp_project->getScene ()->isOrthogonal() == true)
  177. {
  178. wp::video::renderer::setupOrthographicCamera (wp_project->getScene ());
  179. }
  180. else
  181. {
  182. wp::irrlicht::device->getLogger ()->log ("Non-orthogonal cameras not supported yet!!", irr::ELL_ERROR);
  183. return -2;
  184. }
  185. // register nodes
  186. wp::video::renderer::queueNode (wp_project->getScene ());
  187. int32_t lastTime = 0;
  188. int32_t minimumTime = 1000 / 90;
  189. int32_t currentTime = 0;
  190. while (wp::irrlicht::device->run () && wp::irrlicht::driver)
  191. {
  192. // if (device->isWindowActive ())
  193. {
  194. currentTime = wp::irrlicht::device->getTimer ()->getTime ();
  195. g_Time = currentTime / 1000.0f;
  196. if (currentTime - lastTime > minimumTime)
  197. {
  198. wp::video::renderer::render ();
  199. lastTime = currentTime;
  200. }
  201. else
  202. {
  203. wp::irrlicht::device->sleep (1, false);
  204. }
  205. }
  206. }
  207. SDL_Quit ();
  208. delete wp_project;
  209. return 0;
  210. }