Revision Log
Revision: 1.4 - (view) (download) (as text)
| 1 : | ktanaka | 1.1 | #include "BitmapFont.h" |
| 2 : | ktanaka | 1.4 | |
| 3 : | int BitmapFont::getWidth() const { return width; } | ||
| 4 : | int BitmapFont::getHeight() const { return height; } | ||
| 5 : | int BitmapFont::getSize() const { return width*height; } | ||
| 6 : | int BitmapFont::getPixelSize() const { return pixels.size(); } | ||
| 7 : | pixel BitmapFont::getPixelAt(int index) const { return pixels[index]; } | ||
| 8 : | BitmapFont::BitmapFont(){} | ||
| 9 : | BitmapFont::BitmapFont(const BitmapFont& b){ | ||
| 10 : | width=b.width; | ||
| 11 : | height=b.height; | ||
| 12 : | bitmap=b.bitmap; | ||
| 13 : | pixels=b.pixels; | ||
| 14 : | } | ||
| 15 : | BitmapFont::BitmapFont(int w,int h) :width(w),height(h),bitmap(w*h,false){ | ||
| 16 : | } | ||
| 17 : | BitmapFont::BitmapFont(int w,int h,const std::vector<pixel> &pxls) | ||
| 18 : | :width(w),height(h),bitmap(w*h,false),pixels(pxls){ | ||
| 19 : | std::vector<pixel>::iterator it; | ||
| 20 : | for(it=pixels.begin();it!=pixels.end();it++){ | ||
| 21 : | int x=it->first, y=it->second; | ||
| 22 : | assert(0<= x && x<width && 0<=y && y<height); | ||
| 23 : | bitmap[y*width+x]=true; | ||
| 24 : | } | ||
| 25 : | } | ||
| 26 : | // 暗黙のうちに幅が4ビットの倍数だと仮定している。 | ||
| 27 : | BitmapFont::BitmapFont(int w,int h,const std::string& hex) :width(w),height(h),bitmap(){ | ||
| 28 : | for(std::string::const_iterator it=hex.begin();it!=hex.end();it++){ | ||
| 29 : | std::string s; | ||
| 30 : | s+= *it; | ||
| 31 : | long val=strtol(s.c_str(),0,16); | ||
| 32 : | assert(val>=0 && val<16); | ||
| 33 : | for(int i=0;i<4;i++){ | ||
| 34 : | bool pixel=false; | ||
| 35 : | if((val&(1<<(3-i)))!=0){ | ||
| 36 : | pixel=true; | ||
| 37 : | } | ||
| 38 : | bitmap.push_back(pixel); | ||
| 39 : | } | ||
| 40 : | } | ||
| 41 : | for(int y=0;y<height;y++) | ||
| 42 : | for(int x=0;x<width;x++) | ||
| 43 : | if(get(x,y)) | ||
| 44 : | pixels.push_back(std::pair<int,int>(x,y)); | ||
| 45 : | } | ||
| 46 : | bool BitmapFont::get(int x, int y) const{ | ||
| 47 : | if(x<0 || x>=width || y<0 || y>=height){ | ||
| 48 : | return false; | ||
| 49 : | } | ||
| 50 : | return bitmap[y*width+x]; | ||
| 51 : | } | ||
| 52 : | int BitmapFont::getIndex(const pixel &px) const{ | ||
| 53 : | std::vector<pixel>::const_iterator it=find(pixels.begin(),pixels.end(),px); | ||
| 54 : | if(it!=pixels.end()){ | ||
| 55 : | return it-pixels.begin(); | ||
| 56 : | } | ||
| 57 : | return -1; | ||
| 58 : | } | ||
| 59 : | ktanaka | 1.1 | ostream& operator<<(ostream &os,const BitmapFont &bf){ |
| 60 : | ktanaka | 1.3 | #if 0 |
| 61 : | ktanaka | 1.1 | os << "width= " << bf.getWidth() <<",height=" << bf.getHeight() <<"\n"; |
| 62 : | ktanaka | 1.3 | #endif |
| 63 : | ktanaka | 1.1 | for(int y=0;y<bf.getHeight();y++){ |
| 64 : | for(int x=0;x<bf.getWidth();x++){ | ||
| 65 : | ktanaka | 1.2 | if(bf.get(x,y)) os << '@'; |
| 66 : | else os << '.'; | ||
| 67 : | ktanaka | 1.1 | } |
| 68 : | ktanaka | 1.2 | os << "\n"; |
| 69 : | ktanaka | 1.1 | } |
| 70 : | ktanaka | 1.3 | #if 0 |
| 71 : | ktanaka | 1.2 | vector<pixel>::iterator it; |
| 72 : | for(it=bf.pixels.begin();it!=bf.pixels.end();it++) | ||
| 73 : | os << '(' << (*it).first << ',' << (*it).second << ')'; | ||
| 74 : | os << "\n"; | ||
| 75 : | ktanaka | 1.3 | #endif |
| 76 : | ktanaka | 1.2 | return os; |
| 77 : | ktanaka | 1.1 | } |
|
ktanaka Powered by ViewCVS 1.0-dev |
ViewCVS and CVS Help |