main.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 "wallpaperengine/shaders/compiler.h"
  9. #include "wallpaperengine/project.h"
  10. #include "wallpaperengine/irrlicht.h"
  11. #include "wallpaperengine/irr/CImageLoaderTEX.h"
  12. int WinID = 0;
  13. irr::SIrrlichtCreationParameters _irr_params;
  14. irr::f32 g_Time = 0;
  15. int init_irrlicht()
  16. {
  17. // prepare basic configuration for irrlicht
  18. _irr_params.AntiAlias = 8;
  19. _irr_params.Bits = 16;
  20. // _irr_params.DeviceType = irr::EIDT_X11;
  21. _irr_params.DriverType = irr::video::EDT_OPENGL;
  22. _irr_params.Doublebuffer = true;
  23. _irr_params.EventReceiver = nullptr;
  24. _irr_params.Fullscreen = false;
  25. _irr_params.HandleSRGB = false;
  26. _irr_params.IgnoreInput = true;
  27. _irr_params.Stencilbuffer = true;
  28. _irr_params.UsePerformanceTimer = false;
  29. _irr_params.Vsync = false;
  30. _irr_params.WithAlphaChannel = false;
  31. _irr_params.ZBufferBits = 24;
  32. _irr_params.LoggingLevel = irr::ELL_DEBUG;
  33. _irr_params.WindowId = reinterpret_cast<void*> (WinID);
  34. wp::irrlicht::device = irr::createDeviceEx (_irr_params);
  35. if (wp::irrlicht::device == nullptr)
  36. {
  37. return 1;
  38. }
  39. wp::irrlicht::device->setWindowCaption (L"Test game");
  40. wp::irrlicht::driver = wp::irrlicht::device->getVideoDriver();
  41. // check for ps and vs support
  42. if (
  43. wp::irrlicht::driver->queryFeature (irr::video::EVDF_PIXEL_SHADER_1_1) == false &&
  44. wp::irrlicht::driver->queryFeature (irr::video::EVDF_ARB_FRAGMENT_PROGRAM_1) == false)
  45. {
  46. wp::irrlicht::device->getLogger ()->log ("WARNING: Pixel shaders disabled because of missing driver/hardware support");
  47. }
  48. if (
  49. wp::irrlicht::driver->queryFeature (irr::video::EVDF_VERTEX_SHADER_1_1) == false &&
  50. wp::irrlicht::driver->queryFeature (irr::video::EVDF_ARB_VERTEX_PROGRAM_1) == false)
  51. {
  52. wp::irrlicht::device->getLogger ()->log ("WARNING: Vertex shaders disabled because of missing driver/hardware support");
  53. }
  54. return 0;
  55. }
  56. void preconfigure_wallpaper_engine ()
  57. {
  58. // load the assets from wallpaper engine
  59. wp::irrlicht::device->getFileSystem ()->addFileArchive ("assets.zip", true, false);
  60. // register custom loaders
  61. wp::irrlicht::driver->addExternalImageLoader (new irr::video::CImageLoaderTex ());
  62. wp::irrlicht::device->getFileSystem ()->addArchiveLoader (new CArchiveLoaderPkg (wp::irrlicht::device->getFileSystem ()));
  63. }
  64. int main (int argc, char* argv[])
  65. {
  66. int mode = 0;
  67. std::string path;
  68. // parse the integer if it exists
  69. if (argc >= 1)
  70. {
  71. std::stringstream ss;
  72. ss << std::hex << argv[1];
  73. ss >> WinID;
  74. }
  75. int option_index = 0;
  76. static struct option long_options [] = {
  77. {"win", required_argument, 0, 'w'},
  78. {"pkg", required_argument, 0, 'p'},
  79. {"dir", required_argument, 0, 'd'},
  80. {nullptr, 0, 0, 0}
  81. };
  82. while (true)
  83. {
  84. int c = getopt_long (argc, argv, "w:p:d:", long_options, &option_index);
  85. if (c == -1)
  86. break;
  87. switch (c)
  88. {
  89. case 'w':
  90. if (optarg)
  91. WinID = atoi (optarg);
  92. break;
  93. case 'p':
  94. mode = 1;
  95. path = optarg;
  96. break;
  97. case 'd':
  98. mode = 2;
  99. path = optarg;
  100. break;
  101. default:
  102. break;
  103. }
  104. }
  105. printf ("Initializing irrlicht to WindowID %d\n", WinID);
  106. if (init_irrlicht())
  107. {
  108. return 1;
  109. }
  110. preconfigure_wallpaper_engine ();
  111. irr::io::path wallpaper_path;
  112. irr::io::path project_path;
  113. irr::io::path scene_path;
  114. switch (mode)
  115. {
  116. // pkg mode
  117. case 1:
  118. wallpaper_path = wp::irrlicht::device->getFileSystem ()->getAbsolutePath (path.c_str ());
  119. project_path = wallpaper_path + "project.json";
  120. scene_path = wallpaper_path + "scene.pkg";
  121. break;
  122. // folder mode
  123. case 2:
  124. wallpaper_path = wp::irrlicht::device->getFileSystem ()->getAbsolutePath (path.c_str ());
  125. project_path = wallpaper_path + "project.json";
  126. break;
  127. default:
  128. break;
  129. }
  130. // set our working directory
  131. wp::irrlicht::device->getFileSystem ()->changeWorkingDirectoryTo (wallpaper_path);
  132. // wp::irrlicht::device->getFileSystem ()->addFileArchive (_wp_engine_folder + "scene.pkg", true, false); // add the pkg file to the lookup list
  133. wp::project* wp_project = new wp::project (project_path);
  134. if (wp_project->getScene ()->isOrthogonal() == true)
  135. {
  136. wp::video::renderer::setupOrthographicCamera (wp_project->getScene ());
  137. }
  138. else
  139. {
  140. wp::irrlicht::device->getLogger ()->log ("Non-orthogonal cameras not supported yet!!");
  141. return 0;
  142. }
  143. // register nodes
  144. wp::video::renderer::queueNode (wp_project->getScene ());
  145. int32_t lastTime = 0;
  146. int32_t minimumTime = 1000 / 90;
  147. int32_t currentTime = 0;
  148. while (wp::irrlicht::device->run () && wp::irrlicht::driver)
  149. {
  150. // if (device->isWindowActive ())
  151. {
  152. currentTime = wp::irrlicht::device->getTimer ()->getTime ();
  153. g_Time = currentTime / 1000.0f;
  154. if (currentTime - lastTime > minimumTime)
  155. {
  156. wp::video::renderer::render ();
  157. lastTime = currentTime;
  158. }
  159. else
  160. {
  161. wp::irrlicht::device->sleep (1, false);
  162. }
  163. }
  164. }
  165. delete wp_project;
  166. return 0;
  167. }