main.cpp 7.5 KB

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