Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

SpritePictureRGBA8.cpp

Go to the documentation of this file.
00001 //------------------------------------------------------------------------------
00002 // Lamp : Open source game middleware
00003 // Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
00004 //
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 //------------------------------------------------------------------------------
00019 
00020 /** @file
00021  * スプライトピクチャRGBA8実装
00022  * @author Junpee
00023  */
00024 
00025 #include "LampBasic.h"
00026 #include "Graphics2D/Sprite/SpritePictureRGBA8.h"
00027 #include "Graphics/Renderer/RenderingDevice.h"
00028 #include "Core/Codec/LinearMinificationFilter/LinearMinificationFilter.h"
00029 
00030 namespace Lamp{
00031 
00032 //------------------------------------------------------------------------------
00033 // 生成、破棄
00034 //------------------------------------------------------------------------------
00035 // コンストラクタ
00036 SpritePictureRGBA8::SpritePictureRGBA8() : image_(NULL){
00037 }
00038 //------------------------------------------------------------------------------
00039 // デストラクタ
00040 SpritePictureRGBA8::~SpritePictureRGBA8(){
00041     SafeArrayDelete(image_);
00042 }
00043 //------------------------------------------------------------------------------
00044 // サイズ
00045 //------------------------------------------------------------------------------
00046 // サイズの設定
00047 void SpritePictureRGBA8::setSize(const DimensionI& size){
00048     SpritePicture::setSize(size);
00049     SafeArrayDelete(image_);
00050     image_ = new Color4c[size.width * size.height];
00051 }
00052 //------------------------------------------------------------------------------
00053 // Direct3Dテクスチャ
00054 //------------------------------------------------------------------------------
00055 // D3Dテクスチャのコンパイル
00056 bool SpritePictureRGBA8::compileD3DTexture(){
00057     Assert(image_ != NULL);
00058     Direct3DTexture* d3dTexture;
00059     const DimensionI& size = getSize();
00060     d3dTexture = RenderingDevice::getInstance()->createTexture(
00061         D3DFMT_A8R8G8B8, size.width, size.height);
00062     setD3DTexture(d3dTexture);
00063     // 書き込み
00064     if(!compileMipmap(image_, size, 0)){ return false; }
00065     return true;
00066 }
00067 //------------------------------------------------------------------------------
00068 // イメージのコンパイル
00069 bool SpritePictureRGBA8::compileImage(
00070     const Color4c* image, const DimensionI& size, int mipmapLevel){
00071     RenderingDevice* device = RenderingDevice::getInstance();
00072     D3DLOCKED_RECT lockedRect =
00073         device->lockTexture(getD3DTexture(), mipmapLevel);
00074     u_char* lineAddr = (u_char*)lockedRect.pBits;
00075     for(int i = 0; i < size.height; i++){
00076         u_int* writeAddr = (u_int*)lineAddr;
00077         int offset = i * size.width;
00078         for(int j = 0; j < size.width; j++){
00079             (*writeAddr) = image[offset + j].getARGB();
00080             writeAddr++;
00081         }
00082         lineAddr += lockedRect.Pitch;
00083     }
00084     device->unlockTexture(getD3DTexture(), mipmapLevel);
00085     return true;
00086 }
00087 //------------------------------------------------------------------------------
00088 // ミップマップのコンパイル
00089 bool SpritePictureRGBA8::compileMipmap(
00090     const Color4c* image, const DimensionI& size, int mipmapLevel){
00091     // イメージのコンパイル
00092     if(!compileImage(image, size, mipmapLevel)){ return false; }
00093     // ミップマップ再帰チェック
00094     mipmapLevel++;
00095     if((size.width == 1) && (size.height == 1)){
00096         Assert(getD3DTexture()->GetLevelCount() == mipmapLevel);
00097         return true;
00098     }
00099     DimensionI nextSize = LinearMinificationFilter::getNextSize(size);
00100     Color4c* nextImage = new Color4c[nextSize.width * nextSize.height];
00101     // ブロックフィルタで縮小
00102     LinearMinificationFilter::filter(image, size, nextImage, nextSize);
00103     bool nextLevelResult = compileMipmap(nextImage, nextSize, mipmapLevel);
00104     delete[] nextImage;
00105     return nextLevelResult;
00106 }
00107 //------------------------------------------------------------------------------
00108 } // End of namespace Lamp
00109 //------------------------------------------------------------------------------

Generated on Wed Mar 16 10:29:36 2005 for Lamp by doxygen 1.3.2