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