kick
/Users/morten/Programmering/cpp/kick/src/kick/2d/sprite.cpp
00001 //
00002 // Created by morten on 30/06/14.
00003 //
00004 
00005 #include "kick/2d/sprite.h"
00006 #include "kick/core/debug.h"
00007 
00008 #include <iostream>
00009 #include <glm/gtx/string_cast.hpp>
00010 
00011 using namespace std;
00012 using namespace glm;
00013 
00014 namespace kick {
00015 
00016     Sprite::Sprite(GameObject *gameObject, std::shared_ptr<Canvas> canvas) : Component2D(gameObject, canvas) {
00017     }
00018 
00019     std::shared_ptr<TextureAtlas> Sprite::textureAtlas() const {
00020         return mTextureAtlas;
00021     }
00022 
00023     void Sprite::setTextureAtlas(std::shared_ptr<TextureAtlas> textureAtlas) {
00024         Sprite::mTextureAtlas = textureAtlas;
00025         if (textureAtlas && mSpriteName.length()>0){
00026             mEntry = textureAtlas->get(mSpriteName);
00027         }
00028     }
00029 
00030 
00031     std::string const &Sprite::spriteName() const {
00032         return mSpriteName;
00033     }
00034 
00035     void Sprite::setSpriteName(std::string const &spriteName) {
00036         Sprite::mSpriteName = spriteName;
00037         if (mTextureAtlas && spriteName != ""){
00038             mEntry = mTextureAtlas->get(spriteName);
00039         }
00040     }
00041 
00042     TextureAtlasEntry Sprite::entry() const {
00043         return mEntry;
00044     }
00045 
00046     glm::vec2 Sprite::sliceX() const {
00047         return mSliceX;
00048     }
00049 
00050     void Sprite::setSliceX(glm::vec2 sliceX) {
00051         assert (sliceX.x <= sliceX.y);
00052         assert (0 <= sliceX.x);
00053         assert (sliceX.y <= 1);
00054         Sprite::mSliceX = sliceX;
00055     }
00056 
00057     glm::vec2 Sprite::sliceY() const {
00058         return mSliceY;
00059     }
00060 
00061     void Sprite::setSliceY(glm::vec2 sliceY) {
00062         assert (sliceY.x <= sliceY.y);
00063         assert (0 <= sliceY.x);
00064         assert (sliceY.y <= 1);
00065         Sprite::mSliceY = sliceY;
00066     }
00067 
00068     glm::vec2 Sprite::anchor() const {
00069         return mAnchor;
00070     }
00071 
00072     void Sprite::setAnchor(glm::vec2 anchor) {
00073         Sprite::mAnchor = anchor;
00074     }
00075 
00076     glm::vec2 Sprite::scale() const {
00077         return mScale;
00078     }
00079 
00080     void Sprite::setScale(glm::vec2 scale) {
00081         Sprite::mScale = scale;
00082     }
00083 
00084     void Sprite::setType(SpriteType type) {
00085         Sprite::mType = type;
00086     }
00087 
00088     SpriteType Sprite::type() const {
00089         return mType;
00090     }
00091 
00092     Bounds2 Sprite::bounds() const {
00093         Bounds2 bounds;
00094 
00095         float w = mEntry.spriteSourceSize.z * mScale.x;
00096         float h = mEntry.spriteSourceSize.w * mScale.y;
00097         bounds.min = -vec2{mEntry.spriteSourceSize.z, mEntry.spriteSourceSize.w} * mAnchor * mScale;
00098         bounds.max = bounds.min + glm::vec2{w,h};
00099         return bounds;
00100     }
00101 
00102     Bounds2 Sprite::trimmedBounds() const {
00103         Bounds2 bounds;
00104 
00105         float w = mEntry.frame.z * mScale.x;
00106         float h = mEntry.frame.w * mScale.y;
00107         bounds.min = {mEntry.spriteSourceSize.x, mEntry.spriteSourceSize.w - mEntry.spriteSourceSize.y - mEntry.frame.w};
00108         bounds.min -= vec2{mEntry.spriteSourceSize.z, mEntry.spriteSourceSize.w} * mAnchor * mScale;
00109         bounds.max = bounds.min + glm::vec2{w,h};
00110         return bounds;
00111     }
00112 
00113     glm::vec4 Sprite::color() const {
00114         return mColor;
00115     }
00116 
00117     void Sprite::setColor(glm::vec4 color) {
00118         Sprite::mColor = color;
00119     }
00120 
00121     Shader *Sprite::shader() const {
00122         if (mTextureAtlas){
00123             return mTextureAtlas->shader().get();
00124         }
00125         return nullptr;
00126     }
00127 
00128     void Sprite::setBounds(Bounds2 b) {
00129         cout << "Todo implement"<<endl;
00130     }
00131 }
 All Classes Functions Variables