GAM531 DPS931 – Week 2 Into the Engine
Last Time in GAM531… Engine Core Client Interface Operating System API Device API Engine DX11 Device GL 4.3 Device ControllerManagerModel DX11 Object GL 4.3 Object DX 11 API GL 4.3 API DX 11 API GL 4.3 API 1 m 1 1 or 1 1 or 1 1 iEngine iController iModel
Back to the Engine iEngine.hpp namespace Emperor { class iEngine {…}; } Engine.hpp namespace Emperor { template class Engine : public iEngine, public Singleton > {…}; }
The Singleton Engine DeviceMeshVertexShader Buffer TextureMaterialActorNodeBlendStateRasterizerSceneObject Controller ManagerLightCamera Engine * engine; x10,000 class Engine { static Engine* self; public: Engine() {self = this;} static Engine* getPtr() { return self; } void stuff(); }; Engine::getPtr()->stuff();
Our Singleton Class (hold on…) template class Singleton { private: static D* self; protected: public: Singleton() { if(!self) self = (D*)this; else EMP_FATAL_ERROR(“…"); } virtual ~Singleton() {self = 0;} static D* getPtr() {return self;} }; template D* Singleton ::self = nullptr; template class Engine : public iEngine, public Singleton > {…}; } Engine ::getPtr()->initialize();
Deeper into the Engine Engine.hpp namespace Emperor { template class Engine : public iEngine, public Singleton > { SceneController sCont; ResourceController rCont; … public: iSceneController* getSceneController() {return &sCont;} iResourceController* getResourceController() {return &rCont;} }; } EngineController 1 m
Why do we use controllers? Convenience & Separation Resource Scene Window
Scene vs. Resource Statue1.obj Summoner.obj BigDude.obj
Into the Controller SceneController.hpp namespace Emperor { template class SceneController : public iSceneController { public: ActorManager actMan; CameraManager camMan; LightManager lgtMan; NodeManager nodMan; … }; } ControllerManager 1 m iSceneController.hpp namespace Emperor { … class iSceneController { … public: virtual iActor* createActor() = 0; virtual iCamera* createCamera() = 0; virtual iLight* createLight() = 0; virtual iNode* createNode() = 0; }; }
What does a Manager Do? Hiring – Creating the object Firing – Destroying the object Keeping Tabs – Having a reference to all objects Organizing/Scheduling – Dealing with tasks that deal with all objects Damage Control – Releasing resources if they have not been released by shutdown
Into the Manager BaseManager.hpp template class BaseManager { protected: ArrayList objects; ArrayList activeObjects; public: BaseManager() {} virtual ~BaseManager() {…} T* createObject() {…} void removeObject(T* o) {…} void activateObject(T* o) {…} void deactivateObject(T* o) {…} }; ManagerModel 1 m #define ArrayList std::vector
Std::Vector Std::Vector is a dynamic array Common Functions: push_back(T) – add a value to the back pop_back() – removes the last value size() – returns the number of values front() – returns first value (ref) back() – returns last value (ref) [N] (subscript) – returns the Nth value (ref) begin() – returns an iterator to the beginning beg end end() – returns an iterator to the end
Std::Iterator Std::Iterator is a unified method of iterating over many STL data structures //iterating over a std::vector //assume ar is a std::vector for(auto i = ar.begin(), end = ar.end(); i!=end; i++) { std::cout << *i; } i end std::vector ::iterator i = ar.begin(); i += 3; std::cout << i – ar.begin(); //outputs 3 auto i = ar.begin(); ar.push_back(25); std::cout << *i; //throws exception, iterator not valid
Std::Iterator Cont Many STL functions use the std::iterator std::vector intvec; intvec.push_back(20); intvec.push_back(93); intvec.insert(intvec.begin() + 1, 50);//inserts between 20 and 93 intvec.erase(intvec.begin());//removes 20 from the list auto i = std::find(intvec.begin(), intvec.end(), 93); //returns iterator to the second element i = std::find(intvec.begin(), intvec.end(), 108); //returns iterator to the end of the array
Into the Manager BaseManager.hpp template class BaseManager { protected: ArrayList objects; ArrayList activeObjects; public: BaseManager() {} virtual ~BaseManager() {…} T* createObject() {…} void removeObject(T* o) {…} void activateObject(T* o) {…} void deactivateObject(T* o) {…} }; ManagerModel 1 m #define ArrayList std::vector
Scene vs. Resource Statue1.obj Summoner.obj BigDude.obj
Into the Resource Manager ResourceManager.hpp template class ResourceManager { protected: HashMap resources; virtual T* createResource(const String& r) = 0; public: ResourceManager() {} virtual ~ResourceManager() {…} T* newResource(const String& r) {…} T* getResource(const String& resource) {…} void removeResource(T* a) {…} }; #define HashMap std::map
std::map 0x248F std::map is an associative container (key-value pairs) which relies on unique keys Common Functions: insert(KEY,VALUE) – insert key and value erase(KEY) – removes key and associated values size() – returns the number of elements clear() – empties the container find(KEY) – returns an iterator to the element [KEY] (subscript) – returns the value associated with the key (ref) test.obj begin() – returns an iterator to the beginning end() – returns an iterator to the end 0x1C8AHello? 0x52CEeng.exe Key (String) Value (Pointer)
std::map usage 0x248F template T* findOrAdd(std::map & res, const std::string& key) { if( res.find(key) == res.end()) { res[key] = new T(); } return res[key]; } int main() { std::map im; *findOrAdd(im, “Test”) = 15; *findOrAdd(im, “Snap”) = 20; *findOrAdd(im, “Test”) = 33; return 0; } Test 0x1C8ASnap Key (String) Value (Pointer)
Into the Resource Manager ResourceManager.hpp template class ResourceManager { protected: HashMap resources; virtual T* createResource(const String& r) = 0; public: ResourceManager() {} virtual ~ResourceManager() {…} T* newResource(const String& r) {…} T* getResource(const String& resource) {…} void removeResource(T* a) {…} }; #define HashMap std::map
Scene vs. Resource Statue1.obj Summoner.obj BigDude.obj
Engine DX11 Device GL 4.3 Device ControllerManagerModel DX11 Object GL 4.3 Object DX 11 API GL 4.3 API DX 11 API GL 4.3 API 1 m 1 1 or 1 1 or 1 1 iEngine iController iModel To Recap
To Do Create zenit wiki accounts if you don’t already have one Add your user information to the student list on the wiki Read over Assignment 1 (will be released Friday) Begin to look for group partners (2-3 per group) Bookmark the GAM531 website Make an account on Bit Bucket