main.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. int main (int argc, char* argv[])
  77. {
  78. int mode = 0;
  79. bool audio_support = true;
  80. std::string path;
  81. int option_index = 0;
  82. static struct option long_options [] = {
  83. {"win", required_argument, 0, 'w'},
  84. {"pkg", required_argument, 0, 'p'},
  85. {"dir", required_argument, 0, 'd'},
  86. {"silent", optional_argument, 0, 's'},
  87. {"help", optional_argument, 0, 'h'},
  88. {nullptr, 0, 0, 0}
  89. };
  90. while (true)
  91. {
  92. int c = getopt_long (argc, argv, "w:p:d:sh", long_options, &option_index);
  93. if (c == -1)
  94. break;
  95. switch (c)
  96. {
  97. case 'w':
  98. if (optarg)
  99. WinID = atoi (optarg);
  100. break;
  101. case 'p':
  102. mode = 1;
  103. path = optarg;
  104. break;
  105. case 'd':
  106. mode = 2;
  107. path = optarg;
  108. break;
  109. case 's':
  110. audio_support = false;
  111. break;
  112. case 'h':
  113. print_help (argv [0]);
  114. return 0;
  115. default:
  116. break;
  117. }
  118. }
  119. std::cout << "Initializing irrlicht to WindowID " << WinID << std::endl;
  120. if (init_irrlicht())
  121. {
  122. return 1;
  123. }
  124. preconfigure_wallpaper_engine ();
  125. irr::io::path wallpaper_path;
  126. irr::io::path project_path;
  127. irr::io::path scene_path;
  128. switch (mode)
  129. {
  130. case 0:
  131. print_help (argv [0]);
  132. return 0;
  133. // pkg mode
  134. case 1:
  135. wallpaper_path = wp::irrlicht::device->getFileSystem ()->getAbsolutePath (path.c_str ());
  136. project_path = wallpaper_path + "project.json";
  137. scene_path = wallpaper_path + "scene.pkg";
  138. wp::irrlicht::device->getFileSystem ()->addFileArchive (scene_path, true, false); // add the pkg file to the lookup list
  139. break;
  140. // folder mode
  141. case 2:
  142. wallpaper_path = wp::irrlicht::device->getFileSystem ()->getAbsolutePath (path.c_str ());
  143. project_path = wallpaper_path + "project.json";
  144. // set our working directory
  145. wp::irrlicht::device->getFileSystem ()->changeWorkingDirectoryTo (wallpaper_path);
  146. break;
  147. default:
  148. break;
  149. }
  150. if (audio_support == true)
  151. {
  152. int mixer_flags = MIX_INIT_MP3 | MIX_INIT_FLAC | MIX_INIT_OGG;
  153. if (SDL_Init (SDL_INIT_AUDIO) < 0 || mixer_flags != Mix_Init (mixer_flags))
  154. {
  155. wp::irrlicht::device->getLogger ()->log ("Cannot initialize SDL audio system", irr::ELL_ERROR);
  156. return -1;
  157. }
  158. // initialize audio engine
  159. Mix_OpenAudio (22050, AUDIO_S16SYS, 2, 640);
  160. }
  161. wp::project* wp_project = new wp::project (project_path);
  162. if (wp_project->getScene ()->isOrthogonal() == true)
  163. {
  164. wp::video::renderer::setupOrthographicCamera (wp_project->getScene ());
  165. }
  166. else
  167. {
  168. wp::irrlicht::device->getLogger ()->log ("Non-orthogonal cameras not supported yet!!", irr::ELL_ERROR);
  169. return -2;
  170. }
  171. // register nodes
  172. wp::video::renderer::queueNode (wp_project->getScene ());
  173. int32_t lastTime = 0;
  174. int32_t minimumTime = 1000 / 90;
  175. int32_t currentTime = 0;
  176. while (wp::irrlicht::device->run () && wp::irrlicht::driver)
  177. {
  178. // if (device->isWindowActive ())
  179. {
  180. currentTime = wp::irrlicht::device->getTimer ()->getTime ();
  181. g_Time = currentTime / 1000.0f;
  182. if (currentTime - lastTime > minimumTime)
  183. {
  184. wp::video::renderer::render ();
  185. lastTime = currentTime;
  186. }
  187. else
  188. {
  189. wp::irrlicht::device->sleep (1, false);
  190. }
  191. }
  192. }
  193. SDL_Quit ();
  194. delete wp_project;
  195. return 0;
  196. }