AnimEngine
OOOGL_Core.hpp
1 #pragma once
2 #ifndef KJY_OBJECT_ORIENTED_OPENGL_CORE_HPP_
3 #define KJY_OBJECT_ORIENTED_OPENGL_CORE_HPP_
4 #include <glad/glad.h>
5 #include <memory>
6 #include <exception>
7 #include <string>
8 #include "common.h"
9 
10 /* OOOGL := Object Oriented OpenGL */
11 namespace OOOGL{
12 
13 /* A namespace static flag used as indicator for marking whether or not any
14  * OpenGL bindings (such as framebuffer or VAO bindings have been changed
15  * indirectly by a function internal to OOOGL) */
16 enum oglbindstatus : unsigned int {BINDINGS_CLEAN = 0U, BINDINGS_UNCLEAN = 1U, BINDINGS_UNKNOWN = 2U};
17 static unsigned int binding_purity = BINDINGS_CLEAN;
18 
19 enum class GpuSyncStatus : unsigned int {GPU_CURRENT = 1, GPU_STALE, GPU_ONLY, GPU_UNINITIALIZED};
20 
21 /* --- Section 1: Core object oriented types and smart pointers --- */
22 
23 typedef void (*OglObjectDestroyer)(const GLuint* id);
24 
26  virtual ~OglNameInterface(){}
27  virtual GLuint getName() const = 0;
28  operator GLuint() const{return(getName());}
29 };
30 
31 class ConstOglName;
32 
34  public:
35  MutableOglName(OglObjectDestroyer destroyer) : destroyer(destroyer){}
36  MutableOglName(GLuint id, OglObjectDestroyer destroyer) : oglID(id), destroyer(destroyer){}
37  ~MutableOglName() {if(destroyer != nullptr) destroyer(&oglID);}
38  GLuint* getIdPtr(){return(&oglID);}
39  const GLuint* getIdPtr() const {return(&oglID);}
40  virtual GLuint getName() const override {return(oglID);}
41  inline GLuint getNameDirect() const {return(oglID);}
42  const OglObjectDestroyer getDestroyer(){return(destroyer);}
43 
44  /* Prematurely delete this OpenGL object from the OpenGL state, but keep the object itself around. */
45  virtual void makeZombie() {if(destroyer != nullptr) {destroyer(&oglID);} oglID = 0; _is_zombie_ = true;}
46  virtual bool isZombie() const {return(_is_zombie_);}
47  protected:
48  friend ConstOglName;
49  GLuint oglID = 0;
50  const OglObjectDestroyer destroyer = nullptr;
51  bool _is_zombie_ = false;
52 };
53 
55  public:
56  ConstOglName(OglObjectDestroyer destroyer) : destroyer(destroyer) {}
57  ConstOglName(GLuint id, OglObjectDestroyer destroyer) : oglID(id), destroyer(destroyer) {}
58  ConstOglName(const MutableOglName& othername) : oglID(othername.oglID), destroyer(othername.destroyer) {}
59  ~ConstOglName() {if(destroyer != nullptr) destroyer(&oglID);}
60  const GLuint* getIdPtr() const {return(&oglID);}
61  virtual GLuint getName() const override {return(oglID);}
62  inline GLuint getNameDirect() const {return(oglID);}
63  const OglObjectDestroyer getDestroyer(){return(destroyer);}
64  protected:
65  const GLuint oglID = 0;
66  const OglObjectDestroyer destroyer = nullptr;
67 };
68 
69 /* Marks a class as being worth possessing (aka it will prevent an object from automatic deletion) */
71  virtual bool isPossessible() const = 0;
72 };
73 
74 class PersistentOglName : public std::shared_ptr<MutableOglName>, public OglNameInterface, public PossessibleInteraface{
75  public:
76  PersistentOglName() : std::shared_ptr<MutableOglName>(nullptr){}
77  PersistentOglName(OglObjectDestroyer destroyer) : std::shared_ptr<MutableOglName>(std::make_shared<MutableOglName>(destroyer)) {}
78  PersistentOglName(GLuint id, OglObjectDestroyer destroyer) : std::shared_ptr<MutableOglName>(std::make_shared<MutableOglName>(id, destroyer)) {}
79  virtual ~PersistentOglName() noexcept {}
80  GLuint* getIdPtr(){return(get()->getIdPtr());}
81  const GLuint* getIdPtr() const {return(get()->getIdPtr());}
82  virtual GLuint getName() const override {return(get()->getNameDirect());}
83  inline GLuint getNameDirect() const {return(get()->getNameDirect());}
84  virtual bool isPossessible() const override {return(true);}
85 
86  virtual void makeZombie() {get()->makeZombie();}
87  virtual bool isZombie() const {return(get() == nullptr || get()->isZombie());}
88 };
89 
90 static PersistentOglName _zombie_name;
91 
92 class ConstPersistentOglName : public std::shared_ptr<ConstOglName>, public OglNameInterface, public PossessibleInteraface {
93  public:
94  ConstPersistentOglName() : std::shared_ptr<ConstOglName>(nullptr){}
95  ConstPersistentOglName(OglObjectDestroyer destroyer) : std::shared_ptr<ConstOglName>(std::make_shared<ConstOglName>(destroyer)) {}
96  ConstPersistentOglName(GLuint id, OglObjectDestroyer destroyer) : std::shared_ptr<ConstOglName>(std::make_shared<ConstOglName>(id, destroyer)) {}
97  ConstPersistentOglName(const PersistentOglName& other) : std::shared_ptr<ConstOglName>(std::make_shared<ConstOglName>(*other)) {}
98  virtual ~ConstPersistentOglName(){}
99  const GLuint* getIdPtr()const {return(get()->getIdPtr());}
100  virtual GLuint getName() const override {return(get()->getNameDirect());}
101  inline GLuint getNameDirect() const {return(get()->getNameDirect());}
102  virtual bool isPossessible() const override {return(true);}
103 };
104 
106  public:
107  OglObject(OglObjectDestroyer destroyer) : PersistentOglName(destroyer) {}
108 
109  protected:
110 
111  OglObject(const PersistentOglName& zombie) : PersistentOglName(zombie) {}
112 };
113 
114 
115 
116 
117 /* --- Section 2: Interfaces for behaviors, relationships, and interactions with OpenGL objects --- */
118 
119 template<class... Possessibles> class OglPossessorInterface{};
120 
121 template<class Possessible, class... Possessibles>
122 class OglPossessorInterface<Possessible, Possessibles...> : public OglPossessorInterface<Possessibles...>{
123  static_assert(std::is_base_of<PossessibleInteraface, Possessible>::value);
124 
125  public:
126 
127  virtual size_t numPossessed() const = 0;
128  virtual void possessObject(Possessible t) = 0;
129  virtual bool doesPossessObject(Possessible t) const = 0;
130  virtual void dispossessObject(const Possessible& object) = 0;
131 };
132 
133 class ObjectTag {
134  public:
135  virtual const std::string& getTag() const {return(tag);}
136  virtual void setTag(const std::string& tag) {this->tag = tag;}
137  protected:
138  std::string tag;
139 };
140 
141 
142 /* --- Section 3: Exceptions and Errors --- */
143 
144 class BaseGraphicsException : public std::exception{
145  public:
146  virtual const char* what() const noexcept override{
147  return("Base OOOGL graphics exception");
148  }
149 };
150 
152  public:
153  NeedsImplementingException(const std::string& file, const int line, const std::string& tag)
154  : file(file), linenum(line), tag(tag),
155  _whatstr_("Missing implementation for " + tag + " at " + file + ":" + "line")
156  {}
157  virtual const char* what() const noexcept override{
158  return(_whatstr_.c_str());
159  }
160 
161  protected:
162  const std::string file;
163  const int linenum;
164  const std::string tag;
165 
166  private:
167  const std::string _whatstr_;
168 };
169 
171  public:
172  GenericIndexOutOfBoundsException(size_t index, const std::string& note = std::string()) : index(index), _whatstr_("Index out of bounds exception with index '" + std::to_string(index) + "'" + (note.empty() ? "" : " with note: " + note)) {}
173  virtual const char* what() const noexcept override{
174  return(_whatstr_.c_str());
175  }
176  protected:
177  const size_t index;
178  const std::string _whatstr_;
179 };
180 
181 }
182 
183 #endif
Definition: OOOGL_Core.hpp:33
Definition: OOOGL_Core.hpp:92
Definition: OOOGL_Core.hpp:119
Definition: OOOGL_Core.hpp:133
Definition: OOOGL_Core.hpp:144
Definition: OOOGL_Core.hpp:105
Definition: OOOGL_Core.hpp:151
Definition: OOOGL_Core.hpp:170
Definition: OOOGL_Core.hpp:74
Definition: OOOGL_Core.hpp:70
Definition: OOOGL_Core.hpp:54
Definition: OOOGL_Core.hpp:25
Definition: Core_Utilities.cpp:3