NLS Engine  v0.1
The Next Logical Step in game engine design.
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Defines
CubeRenderable.cpp
Go to the documentation of this file.
00001 
00013 #include "CubeRenderable.h"
00014 
00015 // Standard Includes
00016 
00017 // Library Includes
00018 #include <d3d9.h>
00019 
00020 // Local Includes
00021 #include "../../../sharedbase/EventLogger.h"
00022 
00023 #include "../../material/MaterialInterface.h"
00024 #include "../../material/BasicMaterial.h"
00025 
00026 // Typedefs
00027 
00028 // Constants
00029 
00030 // Class Methods
00031 namespace GraphicsCore {
00032   CubeRenderable::CubeRenderable(EntitySPTR owner, ModuleInterface* renderModule, LPDIRECT3DDEVICE9 d3dDevice) :
00033       RenderableComponent(owner, renderModule) {
00034     D3DXCreateBox(d3dDevice, 1.0, 1.0, 1.0, &(this->mesh), NULL);
00035     
00036     material = nullptr;
00037   }
00038   
00039   CubeRenderable::~CubeRenderable() {
00040     this->RemoveMaterial(0);
00041   }
00042   
00043   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00044   void CubeRenderable::Render(LPDIRECT3DDEVICE9 d3dDevice) {
00045     if (this->mesh != nullptr) {
00046       if (this->material != nullptr) {
00047         this->material->Apply(d3dDevice);
00048       }
00049       
00050       this->mesh->DrawSubset(0);
00051     }
00052   }
00053   
00054   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00055   bool CubeRenderable::InsertMaterial(unsigned int index, EnvelopeSPTR envelope) {
00056     MaterialInterface* newMaterial = nullptr;
00057     
00058     // Cube only has one material.
00059     if (index == 0) {
00060       newMaterial = MaterialsSystemInterface::CreateMaterial(envelope);
00061       
00062       // Make sure a new material was created!
00063       if (newMaterial != nullptr) {
00064         // Remove any pre-existing material
00065         this->RemoveMaterial(index); // Return value doesn't need to be tested: if there was no material, we can continue.  If there was, this will remove it and we can continue.
00066         
00067         // Place the new material
00068         this->material = newMaterial;
00069         
00070         return true;
00071       }
00072       else {
00073         LOG(LOG_PRIORITY::INFO, "Material could not be created; not replacing existing material.");
00074       }
00075     }
00076     else {
00077       LOG(LOG_PRIORITY::INFO, "Material index out of bounds: CubeRenderable only supports 1 material at index 0." );
00078     }
00079     
00080     return false;
00081   }
00082   
00083   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00084   bool CubeRenderable::RemoveMaterial(unsigned int index) {
00085     // Cube only has one material.
00086     if (index == 0) {
00087       MaterialsSystemInterface::DestroyMaterial(this->material);
00088       
00089       this->material = nullptr;
00090       
00091       return true;
00092     }
00093     else {
00094       LOG(LOG_PRIORITY::INFO, "Material index out of bounds: CubeRenderable only supports 1 material at index 0." );
00095     }
00096     
00097     return false;
00098   }
00099 }