OSDN Git Service

fix cmake/Macros.cmake
[moflib/moflib.git] / moflib-1.0 / moflib / moflib / mof / Texture.cpp
1 #include "mof/private/TextureImpl.hpp"
2 #include "mof/ConsoleIO.hpp"
3 #include "mof/private/FileTextureBuilder.hpp"
4 #include "mof/private/PixelMapTextureBuilder.hpp"
5
6 mof::Texture::Texture( const tstring& filename)
7 : m_pImpl(new Impl())
8 {
9         m_pImpl->pTexture = mof::FileTextureBuilder( filename).create();
10         D3DSURFACE_DESC desc;
11         m_pImpl->pTexture->GetLevelDesc(0 , &desc);
12         m_pImpl->width = desc.Width;
13         m_pImpl->height = desc.Height;
14 }
15
16 mof::Texture::Texture( const std::shared_ptr<mof::PixelMap>& pPixelMap)
17 : m_pImpl(new Impl())
18 {
19         m_pImpl->pTexture = mof::PixelMapTextureBuilder(pPixelMap).create();
20         D3DSURFACE_DESC desc;
21         m_pImpl->pTexture->GetLevelDesc(0 , &desc);
22         m_pImpl->width = desc.Width;
23         m_pImpl->height = desc.Height;
24 }
25
26 mof::Texture::~Texture(){}
27
28 std::shared_ptr<mof::PixelMap> mof::Texture::getPixelMap(){
29         typedef mof::PixelMap::size_type size_type;
30         boost::array<size_type , 2> sizes = {{getWidth() , getHeight()}};
31         
32         std::shared_ptr<mof::PixelMap> pMap(new mof::PixelMap(sizes));
33         D3DLOCKED_RECT rect;
34         m_pImpl->pTexture->LockRect(0 , &rect , NULL , 0);
35         for(int y = 0 ; y < getHeight() ; y++){ 
36                 for(int x = 0 ; x < getWidth() ; x++){
37                         BYTE* pPixel = (BYTE*)rect.pBits + x * 4 + y * rect.Pitch;
38                         (*pMap)[x][y] = mof::createColor(*(pPixel + 3) , *(pPixel+2) , *(pPixel+1) , *(pPixel));
39                 }
40         }
41         m_pImpl->pTexture->UnlockRect(0);
42         return pMap;
43 }
44
45 int mof::Texture::getWidth(){
46         return m_pImpl->width;
47 }
48
49 int mof::Texture::getHeight(){
50         return m_pImpl->height;
51 }
52
53