rolmodl
C++ API for the Simple Directmedia Library 2 (SDL2)
PixelFmt.cpp
1 #include "hpp/PixelFmt.hpp"
2 
3 #include "hpp/Base.hpp"
4 
5 namespace rolmodl::pixelfmt {
6  /*static*/ const SDL_PixelFormat& PixelFmtStorage::get(const Id id) {
7  const size_t i = static_cast<size_t>(id);
8 
9  if (unsafeRaw()[i] == nullptr) {
10  SDL_PixelFormat* tmp = SDL_AllocFormat(id::unsafe::toSDLEnum(id));
11  if (tmp == nullptr)
12  throw sdlexception();
13 
14  unsafeRaw()[i] = tmp;
15  }
16 
17  return *unsafeRaw()[i];
18  }
19 
20  #pragma clang diagnostic push
21  #pragma clang diagnostic ignored "-Wexit-time-destructors"
22  /*static*/ PixelFmtStorage& PixelFmtStorage::instance() noexcept {
23  static PixelFmtStorage res{};
24  return res;
25  }
26  #pragma clang diagnostic pop
27  /*static*/ SDL_PixelFormat** PixelFmtStorage::unsafeRaw() noexcept {
28  return instance().data_;
29  }
30 
31  PixelFmtStorage::PixelFmtStorage() noexcept :
32  data_(new SDL_PixelFormat*[unique_count])
33  {
34  for (size_t i = 0; i < unique_count; ++i)
35  data_[i] = nullptr;
36  }
37  PixelFmtStorage::~PixelFmtStorage() noexcept {
38  if (data_ != nullptr) {
39  for (size_t i = 0; i < unique_count; ++i)
40  if (data_[i] != nullptr)
41  SDL_FreeFormat(data_[i]);
42 
43  delete[] data_;
44  }
45  data_ = nullptr;
46  }
47 }
PixelFmt.hpp
rolmodl::pixelfmt
Pixel format management members.
Definition: PixelFmt.hpp:7
Base.hpp