main.cpp 7.8 KB

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