main.cpp 8.1 KB

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