LynarMoon.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. /*
  2. * author: Patrick-Christopher Mattulat
  3. * dev-tools: JetBrains CLion
  4. * created: 10.08.2018
  5. * last updated: 08.09.2018
  6. */
  7. #include "LynarMoon.h"
  8. #include "../engine/Error/ErrorCodes.h"
  9. #include "Global.h"
  10. #include "../tools/Date/DateHandler.h"
  11. #include "../tools/File/FileCreator.h"
  12. #include "../tools/File/LogFileHandler.h"
  13. #include "Module/Module.h"
  14. #include "SetUp/SetUp.h"
  15. #include "SDLCommunicator/SDLCommunicator.h"
  16. #include <iostream>
  17. #include <SDL.h>
  18. #include <sstream>
  19. #include <DirectoryHandler.h>
  20. #include <Clock.h>
  21. #include <fstream>
  22. #include <SDL_mixer.h>
  23. #include <SDL_ttf.h>
  24. #include <SDL_image.h>
  25. //////////////////////////////////////////////////
  26. //////////////////////////////////////////////////
  27. //// constructor / destructor
  28. //////////////////////////////////////////////////
  29. //////////////////////////////////////////////////
  30. LynarMoon::LynarMoon():
  31. fps(0),
  32. initialized(false),
  33. initializedIMG(false),
  34. initializedSound(false),
  35. initializedTTF(false),
  36. numberOfSoundChannels(0),
  37. operatingSystem(OS_NOT_SELECTED),
  38. timestamp(0),
  39. timestep(0.0f)
  40. {
  41. this->setup = SetUp("SetUp.txt");
  42. this->Keyboard = std::make_shared<KeyboardModule>();
  43. this->Mouse = std::make_shared<MouseModule>();
  44. }
  45. LynarMoon::~LynarMoon()
  46. {
  47. if(this->initializedIMG.load()) {
  48. IMG_Quit();
  49. }
  50. if(this->initializedSound.load()) {
  51. Mix_Quit();
  52. }
  53. if(this->initializedTTF.load()) {
  54. // TODO: TTF_Quit();
  55. }
  56. if(this->initialized.load()) {
  57. SDL_Quit();
  58. }
  59. std::cout << "engine shut down...\n" << std::endl;
  60. }
  61. //////////////////////////////////////////////////
  62. //////////////////////////////////////////////////
  63. //// private
  64. //////////////////////////////////////////////////
  65. //////////////////////////////////////////////////
  66. void LynarMoon::createPreferencePath()
  67. {
  68. std::string prefPathBuffer;
  69. #if OS == OS_ANDROID
  70. prefPathBuffer = SDL_AndroidGetExternalStoragePath() << "/";
  71. #else
  72. prefPathBuffer = SDL_GetPrefPath("LynarMoonEngine", this->appName.c_str());
  73. #endif
  74. this->prefPath = prefPathBuffer;
  75. }
  76. int LynarMoon::drawLineModule(std::shared_ptr<LineModule>& lineModule)
  77. {
  78. int result = LE_NO_ERROR;
  79. std::string functionName = "LynarMoon::drawLineModule()";
  80. LineRendererInfo lineRendererInfo = lineModule.get()->getRendererInfo();
  81. if(lineRendererInfo.visible)
  82. {
  83. if(SDL_SetRenderDrawColor(this->renderer.get(), (uint8_t) lineRendererInfo.colorValue.red, (uint8_t) lineRendererInfo.colorValue.green, (uint8_t) lineRendererInfo.colorValue.blue, (uint8_t) lineRendererInfo.colorValue.alpha)) {
  84. result = LE_SDL_RENDER_DRAW_COLOR;
  85. this->errorHandler->output(result, functionName);
  86. }
  87. if(!result) {
  88. if(SDL_SetRenderDrawBlendMode(this->renderer.get(), SDL_BLENDMODE_BLEND)) {
  89. result = LE_SDL_SET_RENDER_DRAW_BLEND_MODE;
  90. this->errorHandler->output(result, functionName);
  91. }
  92. }
  93. if(!result) {
  94. if(SDL_RenderSetClipRect(this->renderer.get(), nullptr)) {
  95. result = LE_SDL_RENDER_SET_CLIP_RECT;
  96. this->errorHandler->output(result, functionName);
  97. }
  98. }
  99. if(!result) {
  100. if(SDL_RenderDrawLine(this->renderer.get(), (int) lineRendererInfo.line.p1.x, (int) lineRendererInfo.line.p1.y, (int) lineRendererInfo.line.p2.x, (int) lineRendererInfo.line.p2.y)) {
  101. result = LE_SDL_RENDER_DRAW_LINE;
  102. this->errorHandler->output(result, functionName);
  103. }
  104. }
  105. }
  106. return result;
  107. }
  108. int LynarMoon::drawModelModule(std::shared_ptr<ModelModule>& model)
  109. {
  110. int result = LE_NO_ERROR;
  111. if(model.get()->getRendererInfo().visible) {
  112. result = this->drawModelModuleOriginal(model);
  113. if(!result) {
  114. result = this->drawModelModuleClones(model);
  115. }
  116. }
  117. return result;
  118. }
  119. int LynarMoon::drawModelModuleClones(std::shared_ptr<ModelModule>& model)
  120. {
  121. int result = LE_NO_ERROR;
  122. std::string functionName = "LynarMoon::drawModelModuleClones()";
  123. ModelRendererInfo rendererInfo = model.get()->getRendererInfo();
  124. ModelCloneRendererInfo cloneRendererInfo;
  125. LynarMath::_Point_d center;
  126. SDL_Rect sdlRenderingArea;
  127. SDL_Point sdlCenter;
  128. for(uint32_t i = 0 ; i < model.get()->getAmountOfClones() ; i++) {
  129. sdlRenderingArea = SDLCommunicator::_SDL_Rect(rendererInfo.renderingArea);
  130. cloneRendererInfo = model.get()->getCloneRendererInfoAtIndex(i);
  131. // new position
  132. sdlRenderingArea.x = (int) cloneRendererInfo.position.x;
  133. sdlRenderingArea.y = (int) cloneRendererInfo.position.y;
  134. // new center
  135. center = {cloneRendererInfo.position.x + rendererInfo.renderingArea.w * 0.5f, cloneRendererInfo.position.y + rendererInfo.renderingArea.h * 0.5f};
  136. sdlCenter = SDLCommunicator::_SDL_Point(center);
  137. // render
  138. if(SDL_RenderCopyEx(this->renderer.get(), rendererInfo.texture, nullptr, &sdlRenderingArea, rendererInfo.angle, &sdlCenter, SDL_FLIP_NONE)) {
  139. result = LE_SDL_RENDER_COPY_EX;
  140. this->errorHandler->output(result, functionName);
  141. break;
  142. }
  143. }
  144. return result;
  145. }
  146. int LynarMoon::drawModelModuleOriginal(std::shared_ptr<ModelModule>& model)
  147. {
  148. int result = LE_NO_ERROR;
  149. std::string functionName = "LynarMoon::drawModelModuleOriginal()";
  150. ModelRendererInfo rendererInfo = model.get()->getRendererInfo();
  151. LynarMath::_Point_d center;
  152. SDL_Rect sdlRenderingArea;
  153. SDL_Point sdlCenter;
  154. sdlRenderingArea = SDLCommunicator::_SDL_Rect(rendererInfo.renderingArea);
  155. center = {rendererInfo.renderingArea.x + rendererInfo.renderingArea.w * 0.5f, rendererInfo.renderingArea.y + rendererInfo.renderingArea.h * 0.5f};
  156. sdlCenter = SDLCommunicator::_SDL_Point(center);
  157. if(SDL_RenderCopyEx(this->renderer.get(), rendererInfo.texture, nullptr, &sdlRenderingArea, rendererInfo.angle, &sdlCenter, SDL_FLIP_NONE)) {
  158. result = LE_SDL_RENDER_COPY_EX;
  159. this->errorHandler->output(result, functionName);
  160. }
  161. return result;
  162. }
  163. int LynarMoon::drawPointModule(std::shared_ptr<PointModule>& pointModule)
  164. {
  165. int result = LE_NO_ERROR;
  166. std::string functionName = "LynarMoon::drawPointModule()";
  167. PointRendererInfo pointRendererInfo = pointModule.get()->getRendererInfo();
  168. if(pointRendererInfo.visible)
  169. {
  170. if(SDL_SetRenderDrawColor(this->renderer.get(), (uint8_t) pointRendererInfo.colorValue.red, (uint8_t) pointRendererInfo.colorValue.green, (uint8_t) pointRendererInfo.colorValue.blue, (uint8_t) pointRendererInfo.colorValue.alpha)) {
  171. result = LE_SDL_RENDER_DRAW_COLOR;
  172. this->errorHandler->output(result, functionName);
  173. }
  174. }
  175. if(!result) {
  176. if(SDL_SetRenderDrawBlendMode(this->renderer.get(), SDL_BLENDMODE_BLEND)) {
  177. result = LE_SDL_SET_RENDER_DRAW_BLEND_MODE;
  178. this->errorHandler->output(result, functionName);
  179. }
  180. }
  181. if(!result) {
  182. if(SDL_RenderSetClipRect(this->renderer.get(), nullptr)) {
  183. result = LE_SDL_RENDER_SET_CLIP_RECT;
  184. this->errorHandler->output(result, functionName);
  185. }
  186. }
  187. if(!result) {
  188. if(SDL_RenderDrawPoint(this->renderer.get(), (int) pointRendererInfo.point.x, (int) pointRendererInfo.point.y)) {
  189. result = LE_SDL_RENDER_DRAW_POINT;
  190. this->errorHandler->output(result, functionName);
  191. }
  192. }
  193. return result;
  194. }
  195. int LynarMoon::drawTextModule(std::shared_ptr<TextModule>& text)
  196. {
  197. int result = LE_NO_ERROR;
  198. std::string functionName = "LynarMoon::drawTextModule()";
  199. TextRendererInfo rendererInfo = text.get()->getRendererInfo();
  200. LynarMath::_Rectangle_d renderingArea;
  201. SDL_Rect sdlRenderingArea;
  202. if(rendererInfo.visible) {
  203. renderingArea = rendererInfo.texture.get()->getRenderingArea();
  204. sdlRenderingArea = SDLCommunicator::_SDL_Rect(renderingArea);
  205. if(SDL_RenderSetClipRect(this->renderer.get(), nullptr)) {
  206. result = LE_SDL_RENDER_SET_CLIP_RECT;
  207. this->errorHandler->output(result, functionName);
  208. }
  209. if(!result) {
  210. if(SDL_RenderCopyEx(this->renderer.get(), rendererInfo.texture.get()->getTexture(), nullptr, &sdlRenderingArea, 0.0f, nullptr, SDL_FLIP_NONE)) {
  211. result = LE_SDL_RENDER_COPY_EX;
  212. this->errorHandler->output(result, functionName);
  213. }
  214. }
  215. }
  216. return result;
  217. }
  218. int LynarMoon::drawModules()
  219. {
  220. int result = LE_NO_ERROR;
  221. std::vector<ZindexInfo> zindexList = this->zindexHandler.getZindexList();
  222. std::shared_ptr<LineModule> lineModule;
  223. std::shared_ptr<ModelModule> modelModule;
  224. std::shared_ptr<PointModule> pointModule;
  225. std::shared_ptr<TextModule> textModule;
  226. std::string functionName = "LynarMoon::drawModules()";
  227. for(ZindexInfo zindexInfo : zindexList) {
  228. switch(zindexInfo.module) {
  229. case MODULE_LINE:
  230. {
  231. lineModule = this->lineModuleList.get()->getElement(zindexInfo.id);
  232. if(lineModule != nullptr) {
  233. result = this->drawLineModule(lineModule);
  234. }
  235. else {
  236. result = LE_ID_NOEXIST;
  237. this->errorHandler->output(result, functionName);
  238. }
  239. } break;
  240. case MODULE_MODEL:
  241. {
  242. modelModule = this->modelModuleList.get()->getElement(zindexInfo.id);
  243. if(modelModule != nullptr) {
  244. result = this->drawModelModule(modelModule);
  245. }
  246. else {
  247. result = LE_ID_NOEXIST;
  248. this->errorHandler->output(result, functionName);
  249. }
  250. } break;
  251. case MODULE_POINT:
  252. {
  253. pointModule = this->pointModuleList.get()->getElement(zindexInfo.id);
  254. if(pointModule != nullptr) {
  255. result = this->drawPointModule(pointModule);
  256. }
  257. else {
  258. result = LE_ID_NOEXIST;
  259. this->errorHandler->output(result, functionName);
  260. }
  261. } break;
  262. case MODULE_TEXT:
  263. {
  264. textModule = this->textModuleList.get()->getElement(zindexInfo.id);
  265. if(textModule != nullptr) {
  266. result = this->drawTextModule(textModule);
  267. }
  268. else {
  269. result = LE_ID_NOEXIST;
  270. this->errorHandler->output(result, functionName);
  271. }
  272. } break;
  273. default:
  274. {} break;
  275. }
  276. if(result) {
  277. break;
  278. }
  279. }
  280. return result;
  281. }
  282. void LynarMoon::initModules()
  283. {
  284. this->Font = std::make_shared<FontLayer>(this, "Font", this->errorHandler);
  285. this->Line = std::make_shared<LineLayer>(this, "Line", this->errorHandler);
  286. this->Model = std::make_shared<ModelLayer>(this, "Model", this->errorHandler);
  287. this->Music = std::make_shared<MusicLayer>(this, "Music", this->errorHandler);
  288. this->Point = std::make_shared<PointLayer>(this, "Point", this->errorHandler);
  289. this->Sound = std::make_shared<SoundLayer>(this, "Sound", this->errorHandler);
  290. this->Text = std::make_shared<TextLayer>(this, "Text", this->errorHandler);
  291. this->TimeEvent = std::make_shared<TimeEventLayer>(this, "Time Event", this->errorHandler);
  292. }
  293. int LynarMoon::initRenderer()
  294. {
  295. int result = LE_NO_ERROR;
  296. std::shared_ptr<SDL_Renderer> r(SDL_CreateRenderer(this->window->getWindowHandle().get(), -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC), SDL_DestroyRenderer);
  297. this->renderer = r;
  298. if(this->renderer == nullptr) {
  299. result = LE_SDL_RENDERER;
  300. }
  301. this->errorHandler->output(result, "LynarMoon::initRenderer()");
  302. return result;
  303. }
  304. int LynarMoon::initSDL()
  305. {
  306. int result = LE_NO_ERROR;
  307. if(SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_EVENTS)) {
  308. result = LE_SDL_INIT;
  309. }
  310. this->errorHandler->output(result, "LynarMoon::initSDL()");
  311. return result;
  312. }
  313. int LynarMoon::writeVersion()
  314. {
  315. int result = LE_NO_ERROR;
  316. if(this->operatingSystem == OS_WINDOWS) {
  317. result = system("cls");
  318. std::cout << " _..._\n";
  319. std::cout << " .' `::.\n";
  320. std::cout << " : :::\n";
  321. std::cout << "Lynar Moon Engine : :::\n";
  322. std::cout << " `. .::'\n";
  323. std::cout << " `-..:''\n\n\n";
  324. std::cout << "version:\t\t" << LYNAR_MOON_VERSION_STR << "\n";
  325. std::cout << "release-date:\t\t" << RELEASE_DATE << "\n";
  326. std::cout << "website:\t\twww.lynarstudios.de\n";
  327. std::cout << "\n";
  328. std::cout << "author:\t\t\tPatrick-Christopher Mattulat\n";
  329. std::cout << "co-author:\t\tSebastian Bruchhold\n\n";
  330. std::cout << "Application: " << this->appName.c_str() << "(" << BUILD_MODE << ")\n";
  331. }
  332. if(this->operatingSystem == OS_LINUX) {
  333. std::cout << "\x1B[2J\x1B[H";
  334. std::cout << " _..._\n";
  335. std::cout << " .' `::.\n";
  336. std::cout << " : :::\n";
  337. std::cout << "Lynar Moon Engine : :::\n";
  338. std::cout << " `. .::'\n";
  339. std::cout << " `-..:''\n\n\n";
  340. std::cout << "version:\t\t\t" << LYNAR_MOON_VERSION_STR << "\n";
  341. std::cout << "release-date:\t\t" << RELEASE_DATE << "\n";
  342. std::cout << "website:\t\t\twww.lynarstudios.de\n";
  343. std::cout << "\n";
  344. std::cout << "author:\t\t\t\tPatrick-Christopher Mattulat\n";
  345. std::cout << "co-author:\t\t\tSebastian Bruchhold\n\n";
  346. std::cout << "Application: " << this->appName.c_str() << "(" << BUILD_MODE << ")\n";
  347. }
  348. std::cout << "with preference path: " << this->prefPath << std::endl;
  349. if(BUILD_MODE == BUILD_RELEASE) {
  350. std::cout << BUILD_MODE << " built!\n";
  351. }
  352. if(BUILD_MODE == BUILD_RELEASE_LOG) {
  353. std::cout << BUILD_MODE << " built!\n";
  354. std::cout << "Logging will be saved in preferenced directory!\n";
  355. }
  356. std::cout << "\nextern libraries:\n" << std::endl;
  357. std::cout << "### tinydir ###\n\n" << "Copyright (c) 2013-2016, tinydir authors:\n"
  358. "- Cong Xu\n"
  359. "- Lautis Sun\n"
  360. "- Baudouin Feildel\n"
  361. "- Andargor <andargor@yahoo.com>\n";
  362. std::cout << std::endl;
  363. return result;
  364. }
  365. //////////////////////////////////////////////////
  366. //////////////////////////////////////////////////
  367. //// public
  368. //////////////////////////////////////////////////
  369. //////////////////////////////////////////////////
  370. void LynarMoon::beginFrame()
  371. {
  372. this->timestamp.store(this->getTimestamp());
  373. this->Keyboard->scanKeys(this->event);
  374. this->Mouse->scanKeys(this->event);
  375. this->Mouse->getMouseState();
  376. }
  377. void LynarMoon::delay(uint32_t milliseconds)
  378. {
  379. SDL_Delay(milliseconds);
  380. }
  381. int LynarMoon::drawFrame()
  382. {
  383. int result = LE_NO_ERROR;
  384. std::string functionName = "LynarMoon::drawFrame()";
  385. // background color
  386. if(SDL_SetRenderDrawColor(this->renderer.get(), (uint8_t) this->backgroundColor.red, (uint8_t) this->backgroundColor.green, (uint8_t) this->backgroundColor.blue, (uint8_t) this->backgroundColor.alpha))
  387. {
  388. result = LE_SDL_RENDER_DRAW_COLOR;
  389. this->errorHandler->output(result, functionName);
  390. }
  391. // clear renderer
  392. if(!result)
  393. {
  394. if(SDL_RenderClear(this->renderer.get()))
  395. {
  396. if(this->renderer == nullptr)
  397. {
  398. result = LE_SDL_RENDERER_NULLPTR;
  399. this->errorHandler->output(result, functionName);
  400. }
  401. else
  402. {
  403. result = LE_SDL_CLEAR_RENDERER;
  404. this->errorHandler->output(result, functionName);
  405. }
  406. }
  407. }
  408. // render modules
  409. if(!result) {
  410. result = this->drawModules();
  411. }
  412. // render present
  413. if(!result) {
  414. SDL_RenderPresent(this->renderer.get());
  415. }
  416. return result;
  417. }
  418. void LynarMoon::endFrame()
  419. {
  420. this->timestep.store((this->getTimestamp() - this->timestamp.load()) * 0.001f);
  421. if(this->timestep.load() > 0.0f) {
  422. this->fps.store(1000 / (uint32_t) (this->timestep.load() * 1000));
  423. }
  424. this->Keyboard->freeKeys();
  425. this->Mouse->freeKeys();
  426. }
  427. uint32_t LynarMoon::getAmountOfSoundChannels()
  428. {
  429. return this->numberOfSoundChannels.load();
  430. }
  431. const std::string LynarMoon::getAppName()
  432. {
  433. return this->appName;
  434. }
  435. _ColorValue_d LynarMoon::getBackgroundColor()
  436. {
  437. return this->backgroundColor;
  438. }
  439. uint32_t LynarMoon::getFps()
  440. {
  441. return (this->fps.load() >= this->getMaxFrameRate()) ? this->getMaxFrameRate() : this->fps.load();
  442. }
  443. int LynarMoon::getMaxFrameRate()
  444. {
  445. return this->window->getDisplayMode().refresh_rate;
  446. }
  447. const std::string LynarMoon::getPrefPath()
  448. {
  449. return this->prefPath;
  450. }
  451. int LynarMoon::getScreenHeight()
  452. {
  453. return this->window->getDisplayMode().h;
  454. }
  455. int LynarMoon::getScreenWidth()
  456. {
  457. return this->window->getDisplayMode().w;
  458. }
  459. uint32_t LynarMoon::getTimestamp()
  460. {
  461. return SDL_GetTicks();
  462. }
  463. double LynarMoon::getTimestep()
  464. {
  465. return this->timestep.load();
  466. }
  467. int LynarMoon::init(const std::string applicationName, uint8_t os)
  468. {
  469. int result;
  470. this->appName = applicationName;
  471. std::shared_ptr<LogFileHandler> engineLogFileHandler;
  472. this->operatingSystem.store(os);
  473. std::stringstream logFilePathStream;
  474. std::string logFilePath;
  475. // log file / Error Handler
  476. this->createPreferencePath();
  477. engineLogFileHandler = std::make_shared<LogFileHandler>(this->setup.getConfiguration("ENGINE_PATH"));
  478. this->logFileName = engineLogFileHandler->createLogFile(this->prefPath);
  479. logFilePathStream << this->prefPath << "logging/" << this->logFileName;
  480. logFilePath = logFilePathStream.str();
  481. this->errorHandler = std::make_shared<ErrorHandler>(logFilePath, engineLogFileHandler);
  482. // clean up old log files
  483. if(!this->logFileName.empty()) {
  484. engineLogFileHandler->cleanUpOldFiles(this->prefPath + "logging");
  485. }
  486. // init SDL
  487. result = this->initSDL();
  488. // init fullscreen Window
  489. if(!result) {
  490. this->window = std::make_shared<Window>(this->appName, this->errorHandler);
  491. result = this->window.get()->create();
  492. }
  493. // create renderer
  494. if(!result) {
  495. result = this->initRenderer();
  496. }
  497. // init object lists
  498. if(!result) {
  499. this->initModules();
  500. }
  501. // write version
  502. if(!result) {
  503. result = this->writeVersion();
  504. this->initialized.store(true);
  505. }
  506. return result;
  507. }
  508. int LynarMoon::initIMG()
  509. {
  510. int result = LE_NO_ERROR;
  511. if(!(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG) || !(IMG_Init(IMG_INIT_JPG) & IMG_INIT_JPG)) {
  512. result = LE_SDL_IMG_INIT;
  513. }
  514. else {
  515. this->initializedIMG.store(true);
  516. }
  517. this->errorHandler->output(result, "LynarMoon::initIMG()");
  518. return result;
  519. }
  520. int LynarMoon::initSound(uint32_t numChannels)
  521. {
  522. int result = LE_NO_ERROR;
  523. std::string functionName = "LynarMoon::initSound()";
  524. int mixFlags = 0;
  525. if(SDL_InitSubSystem(SDL_INIT_AUDIO)) {
  526. result = LE_SDL_INIT_SUBSYSTEM;
  527. }
  528. if(!result) {
  529. if((Mix_Init(mixFlags) & mixFlags) != mixFlags) {
  530. result = LE_SDL_MIX_INIT;
  531. }
  532. }
  533. if(!result) {
  534. if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024)) {
  535. result = LE_SDL_OPEN_AUDIO;
  536. }
  537. }
  538. if(!result) {
  539. this->numberOfSoundChannels.store(numChannels);
  540. if(Mix_AllocateChannels(this->numberOfSoundChannels.load()) != this->numberOfSoundChannels.load()) {
  541. result = LE_SDL_MIX_ALLOC_CHANNELS;
  542. }
  543. else {
  544. this->initializedSound.store(true);
  545. }
  546. }
  547. this->errorHandler->output(result, functionName);
  548. return result;
  549. }
  550. int LynarMoon::initTTF()
  551. {
  552. int result = LE_NO_ERROR;
  553. std::string functionName = "LynarMoon::initTTF()";
  554. if(TTF_Init()) {
  555. result = LE_SDL_TTF_INIT;
  556. }
  557. else {
  558. this->initializedTTF.store(true);
  559. }
  560. this->errorHandler->output(result, functionName);
  561. return result;
  562. }
  563. int LynarMoon::pollEvent()
  564. {
  565. return SDL_PollEvent(&(this->event));
  566. }
  567. void LynarMoon::setBackgroundColor(_ColorValue_d color)
  568. {
  569. this->backgroundColor = color;
  570. }
  571. int LynarMoon::showCursor(bool show)
  572. {
  573. int result = LE_NO_ERROR;
  574. std::string functionName = "LynarMoon::showCursor()";
  575. if(show)
  576. {
  577. if(SDL_ShowCursor(SDL_ENABLE) < 0)
  578. {
  579. result = LE_SDL_SHOW_CURSOR;
  580. this->errorHandler->output(result, functionName);
  581. }
  582. }
  583. else
  584. {
  585. if(SDL_ShowCursor(SDL_DISABLE) < 0)
  586. {
  587. result = LE_SDL_SHOW_CURSOR;
  588. this->errorHandler->output(result, functionName);
  589. }
  590. }
  591. return result;
  592. }
  593. double LynarMoon::step(double value)
  594. {
  595. return value * this->timestep.load();
  596. }