Revision: 1.2 - (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 : | BitmapFont(){} | ||
20 : | BitmapFont(const BitmapFont& b){ | ||
21 : | width=b.width; | ||
22 : | height=b.height; | ||
23 : | bitmap=b.bitmap; | ||
24 : | ktanaka | 1.2 | pixels=b.pixels; |
25 : | ktanaka | 1.1 | } |
26 : | BitmapFont(int w,int h) :width(w),height(h),bitmap(w*h,false){ | ||
27 : | } | ||
28 : | // 暗黙のうちに幅が4ビットの倍数だと仮定している。 | ||
29 : | BitmapFont(int w,int h,const string& hex) :width(w),height(h),bitmap(){ | ||
30 : | for(string::const_iterator it=hex.begin();it!=hex.end();it++){ | ||
31 : | string s; | ||
32 : | s+= *it; | ||
33 : | long val=strtol(s.c_str(),0,16); | ||
34 : | assert(val>=0 && val<16); | ||
35 : | for(int i=0;i<4;i++){ | ||
36 : | bool pixel=false; | ||
37 : | ktanaka | 1.2 | if((val&(1<<(3-i)))!=0){ |
38 : | pixel=true; | ||
39 : | } | ||
40 : | ktanaka | 1.1 | bitmap.push_back(pixel); |
41 : | } | ||
42 : | } | ||
43 : | ktanaka | 1.2 | for(int y=0;y<height;y++) |
44 : | for(int x=0;x<width;x++) | ||
45 : | if(get(x,y)) | ||
46 : | pixels.push_back(pair<int,int>(x,y)); | ||
47 : | ktanaka | 1.1 | } |
48 : | bool get(int x, int y) const{ | ||
49 : | if(x<0 || x>=width || y<0 || y>=height){ | ||
50 : | return false; | ||
51 : | } | ||
52 : | return bitmap[y*width+x]; | ||
53 : | ktanaka | 1.2 | } |
54 : | int getIndex(const pixel &px){ | ||
55 : | vector<pixel>::iterator it=find(pixels.begin(),pixels.end(),px); | ||
56 : | if(it!=pixels.end()){ | ||
57 : | return it-pixels.begin(); | ||
58 : | } | ||
59 : | return -1; | ||
60 : | ktanaka | 1.1 | } |
61 : | }; | ||
62 : | #endif /* _BITMAP_FONT_H */ |
ktanaka Powered by ViewCVS 1.0-dev |
ViewCVS and CVS Help |