main.cpp 6.4 KB

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