SdlWindowApi.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * author: Patrick-Christopher Mattulat
  3. * e-mail: webmaster@lynarstudios.com
  4. */
  5. #include <SDL3/SDL.h>
  6. #include <messaging/StatusCodeOk.hpp>
  7. #include <messaging/StatusCodeWindowApiLoadingFailed.hpp>
  8. #include <messaging/StatusCodeWindowCreationFailed.hpp>
  9. #include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
  10. #include <window/SdlApi.hpp>
  11. #include <window/SdlWindowApi.hpp>
  12. using ls::atlantis::messaging::StatusCodeOk;
  13. using ls::atlantis::messaging::StatusCodeWindowApiLoadingFailed;
  14. using ls::atlantis::messaging::StatusCodeWindowCreationFailed;
  15. using ls::atlantis::glossary::WindowApiTypes;
  16. using ls::atlantis::interfaces::AStatusCode;
  17. using ls::atlantis::interfaces::ISdlApi;
  18. using ls::atlantis::window::SdlApi;
  19. using ls::atlantis::window::SdlWindowApi;
  20. using ls::std::core::NullPointerArgumentEvaluator;
  21. using ::std::make_shared;
  22. using ::std::shared_ptr;
  23. SdlWindowApi::SdlWindowApi() : SdlWindowApi(make_shared<SdlApi>())
  24. {}
  25. SdlWindowApi::SdlWindowApi(const shared_ptr<ISdlApi> &_sdlApi)
  26. : sdlApi(_sdlApi),
  27. window(nullptr, [this](SDL_Window *_window) { this->sdlApi->DestroyWindow(_window); }),
  28. windowApiType(WindowApiTypes::SDL_WINDOW_API)
  29. {
  30. NullPointerArgumentEvaluator(_sdlApi).evaluate();
  31. }
  32. SdlWindowApi::~SdlWindowApi()
  33. {
  34. sdlApi->Quit();
  35. }
  36. WindowApiTypes SdlWindowApi::getWindowApiType()
  37. {
  38. return this->windowApiType;
  39. }
  40. shared_ptr<AStatusCode> SdlWindowApi::init()
  41. {
  42. shared_ptr<AStatusCode> statusCode = this->_initApi();
  43. if (statusCode->getId() == StatusCodeOk{}.getId())
  44. {
  45. statusCode = this->_createWindow();
  46. }
  47. return statusCode;
  48. }
  49. shared_ptr<AStatusCode> SdlWindowApi::_createWindow()
  50. {
  51. shared_ptr<AStatusCode> statusCode = make_shared<StatusCodeOk>();
  52. window.reset(sdlApi->CreateWindow("", 0, 0, SDL_WINDOW_FULLSCREEN));
  53. if (window == nullptr)
  54. {
  55. statusCode = make_shared<StatusCodeWindowCreationFailed>();
  56. statusCode->addHint(sdlApi->GetError());
  57. }
  58. return statusCode;
  59. }
  60. shared_ptr<AStatusCode> SdlWindowApi::_initApi() const
  61. {
  62. shared_ptr<AStatusCode> statusCode = make_shared<StatusCodeOk>();
  63. if (!sdlApi->Init(SDL_INIT_VIDEO))
  64. {
  65. statusCode = make_shared<StatusCodeWindowApiLoadingFailed>();
  66. statusCode->addHint(sdlApi->GetError());
  67. }
  68. return statusCode;
  69. }