NLS Engine  v0.1
The Next Logical Step in game engine design.
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Defines
Sprite.cpp
Go to the documentation of this file.
00001 
00012 #include "Sprite.h"
00013 
00014 // System Library Includes
00015 
00016 // Application Library Includes
00017 #include <EngineConfig.h>
00018 
00019 // Local Includes
00020 #include "../../../sharedbase/Entity.h"
00021 #include "../../AssetCreationHelpers.h"
00022 
00023 // Forward Declarations
00024 
00025 // Typedefs and data structures
00026 
00027 // Constants
00028 
00029 // Class methods
00030 namespace GraphicsCore {
00031   Sprite::Sprite(EntitySPTR owner, ModuleInterface* renderModule, LPDIRECT3DDEVICE9 d3dDevice) : RenderableComponent(owner, renderModule) {
00032     D3DXCreateSprite(d3dDevice, &this->sprite);
00033   }
00034 
00035   Sprite::~Sprite( void ) {
00036 
00037   }
00038 
00039   void Sprite::SetSpriteParams( float offX, float offY, std::string texName, unsigned int srcX, unsigned int srcY, unsigned int width, unsigned int height )  {
00040     CreateTexture(NLS_ENGINE_DATA_PATH + texName, &this->texture);
00041     this->srcRect.left = srcX;
00042     this->srcRect.top = srcY;
00043     this->srcRect.right = width + srcX;
00044     this->srcRect.bottom = height + srcY;
00045     this->offsetX = offX;
00046     this->offsetY = offY;
00047   }
00048 
00049   void Sprite::Render( LPDIRECT3DDEVICE9 ) {
00050     D3DXVECTOR3 pos;
00051     unsigned int viewportWidth = 800;
00052     unsigned int viewportHeight = 768;
00053     if (this->owner->GetParent().get() != nullptr) {
00054       pos.x = this->owner->location.x + this->offsetX - this->owner->GetParent()->GetWorldPosition().x; // Add the sprites offset to our entity's location
00055       pos.y = this->owner->location.y + this->offsetY - this->owner->GetParent()->GetWorldPosition().y; // Add the sprites offset to our entity's location
00056 
00057       viewportWidth = this->owner->GetParent()->properties.GetPropertyValue<unsigned int>("viewportWidth");
00058       viewportHeight = this->owner->GetParent()->properties.GetPropertyValue<unsigned int>("viewportHeight");
00059     } else {
00060       pos.x = this->owner->location.x + this->offsetX;
00061       pos.y = this->owner->location.y + this->offsetY;
00062     }
00063     pos.z = this->owner->location.z;
00064 
00065     int sprite_width = this->srcRect.right - this->srcRect.left;
00066     int sprite_height = this->srcRect.bottom - this->srcRect.top;
00067     if (((pos.x >= (sprite_width * -1)) && pos.x < viewportWidth) && ((pos.y >= (sprite_height * -1)) && pos.y < viewportHeight)) {
00068       this->sprite->Begin(D3DXSPRITE_ALPHABLEND);
00069       this->sprite->Draw(this->texture, &this->srcRect, NULL, &pos, 0xFFFFFFFF);
00070       this->sprite->End();
00071     }
00072   }
00073 }