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