cocos2dx之CCImage
阿新 • • 發佈:2019-02-09
CCImage在"CCImage.h"中定義,表示一張載入到記憶體的紋理圖片。在其內部的實現中,紋理以每個畫素的顏色值儲存在記憶體之中。CCImage通常作為檔案和顯示卡間資料交換的一個工具,因此主要提供了兩個方面的功能:一方面是檔案的載入與儲存,另一方面是記憶體緩衝區的讀寫。
我們可以使用CCImage輕鬆地讀寫圖片檔案。目前,CCImage支援PNG、JPEG和TIFF三種主流的圖片格式。下面列舉與檔案讀寫相關的方法:
/** @brief Load the image from the specified path. @param strPath the absolute file path @param imageType the type of image, now only support tow types. @return true if load correctly */ bool initWithImageFile(const char * strPath, EImageFormat imageType = kFmtPng); /* @brief The same meaning as initWithImageFile, but it is thread safe. It is casued by loadImage() in CCTextureCache.cpp. @param fullpath full path of the file @param imageType the type of image, now only support tow types. @return true if load correctly */ bool initWithImageFileThreadSafe(const char *fullpath, EImageFormat imageType = kFmtPng);
/** @brief Save the CCImage data to specified file with specified format. @param pszFilePath the file's absolute path, including file subfix @param bIsToRGB if the image is saved as RGB format */ bool saveToFile(const char *pszFilePath, bool bIsToRGB = true);
CCImage也提供了讀寫記憶體的介面。getData和getDataLen這兩個方法提供了獲取當前紋理的緩衝區的功能,而initWithImageData方法提供了使用畫素資料初始化圖片的功能。相關的方法定義如下:
unsigned char * getData() { return m_pData; }
int getDataLen() { return m_nWidth * m_nHeight; }
/** @brief Load image from stream buffer. @warning kFmtRawData only support RGBA8888 @param pBuffer stream buffer that hold the image data @param nLength the length of data(managed in byte) @param nWidth, nHeight, nBitsPerComponent are used for kFmtRawData @return true if load correctly */ bool initWithImageData(void * pData, int nDataLen, EImageFormat eFmt = kFmtUnKnown, int nWidth = 0, int nHeight = 0, int nBitsPerComponent = 8);