Gamedev Framework (gf)  0.19.0
A C++17 framework for 2D games
GraphicsHandle.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2021 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_GRAPHICS_HANDLE_H
22 #define GF_GRAPHICS_HANDLE_H
23 
24 #include <utility>
25 
26 #include "Types.h"
27 
28 namespace gf {
29 #ifndef DOXYGEN_SHOULD_SKIP_THIS
30 inline namespace v1 {
31 #endif
32 
37  enum class GraphicsTag {
38  Buffer,
39  Framebuffer,
40  Texture,
41  };
42 
47  template<GraphicsTag Tag>
48  struct GraphicsTrait;
49 
56  template<GraphicsTag Tag>
58  public:
65  : m_name()
66  {
67  GraphicsTrait<Tag>::gen(1, &m_name);
68  }
69 
75  constexpr GraphicsHandle(NoneType) noexcept
76  : m_name(0)
77  {
78  }
79 
83  ~GraphicsHandle() noexcept {
84  if (m_name != 0) {
85  GraphicsTrait<Tag>::del(1, &m_name);
86  }
87  }
88 
92  GraphicsHandle(const GraphicsHandle&) = delete;
93 
97  GraphicsHandle& operator=(const GraphicsHandle&) = delete;
98 
104  GraphicsHandle(GraphicsHandle&& other) noexcept
105  : m_name(std::exchange(other.m_name, 0))
106  {
107  }
108 
116  std::swap(m_name, other.m_name);
117  return *this;
118  }
119 
125  bool isValid() const noexcept {
126  return m_name != 0;
127  }
128 
134  unsigned getName() const noexcept {
135  return m_name;
136  }
137 
143  operator unsigned () const noexcept {
144  return m_name;
145  }
146 
147  private:
148  unsigned m_name;
149  };
150 
151 #ifndef DOXYGEN_SHOULD_SKIP_THIS
152 }
153 #endif
154 }
155 
156 #endif // GF_GRAPHICS_HANDLE_H
A GPU framebuffer.
GraphicsHandle(GraphicsHandle &&other) noexcept
Move constructor.
Definition: GraphicsHandle.h:104
Semantic type to represent "none".
Definition: Types.h:37
A trait to handle creation and deletion of GPU resources.
Definition: GraphicsHandle.h:48
A GL handle.
Definition: GraphicsHandle.h:57
constexpr GraphicsHandle(NoneType) noexcept
Constructor.
Definition: GraphicsHandle.h:75
bool isValid() const noexcept
Check if the handle is valid.
Definition: GraphicsHandle.h:125
GraphicsHandle()
Constructor.
Definition: GraphicsHandle.h:64
The namespace for gf classes.
Definition: Action.h:35
GraphicsHandle & operator=(GraphicsHandle &&other) noexcept
Move assignment.
Definition: GraphicsHandle.h:115
unsigned getName() const noexcept
Get the underlying name of the handle.
Definition: GraphicsHandle.h:134
~GraphicsHandle() noexcept
Destructor.
Definition: GraphicsHandle.h:83
GraphicsTag
A tag to represent various GPU resources.
Definition: GraphicsHandle.h:37