comparison window/picture.h @ 52:15a18fbe6f21

* Known bugs added to the README * Code cleaning (0 -> NULL when needed, indentation, spaces, ...)
author thib
date Sat, 18 Apr 2009 18:35:39 +0000
parents 5ae5533b3a9a
children e16e13d8cd68
comparison
equal deleted inserted replaced
51:cbb301016a4e 52:15a18fbe6f21
26 */ 26 */
27 27
28 #ifndef __PICTURE__ 28 #ifndef __PICTURE__
29 #define __PICTURE__ 29 #define __PICTURE__
30 30
31 #include<vector> 31 #include <vector>
32 #include<list> 32 #include <list>
33 #ifdef HAVE_CONFIG_H 33 #ifdef HAVE_CONFIG_H
34 # include "config.h" 34 # include "config.h"
35 #endif 35 #endif
36 36
37 class PicBase; 37 class PicBase;
44 class Video; 44 class Video;
45 } 45 }
46 46
47 /* PicBase の内容をイベントと連動させるためのインターフェース */ 47 /* PicBase の内容をイベントと連動させるためのインターフェース */
48 class PicAnm { 48 class PicAnm {
49 public: 49 public:
50 typedef std::vector<PicBase*>::iterator iterator; 50 typedef std::vector<PicBase*>::iterator iterator;
51 std::vector<PicBase*> pic; 51 std::vector<PicBase*> pic;
52 PicAnm(PicBase* pic); 52 PicAnm(PicBase* pic);
53 PicAnm(std::vector<PicBase*> pic); 53 PicAnm(std::vector<PicBase*> pic);
54 virtual ~PicAnm(); 54 virtual ~PicAnm();
55 }; 55 };
56 56
57 class PicBase { 57 class PicBase {
58 friend class PicContainer; 58 private:
59 friend class PicWidget; 59 friend class PicContainer;
60 60 friend class PicWidget;
61 typedef std::list<PicBase*> List; 61
62 typedef std::list<PicBase*>::iterator iterator; 62 typedef std::list<PicBase*> List;
63 63 typedef std::list<PicBase*>::iterator iterator;
64 PicContainer* parent; 64
65 class PicWidget* widget; 65 PicContainer* parent;
66 Rect rel_pos; // relative position to the parent 66 class PicWidget* widget;
67 Rect rel_solid_area; // solid area(not alpha-blended) to the parent 67 Rect rel_pos; // relative position to the parent
68 Rect clip_area; // clip area on the parent 68 Rect rel_solid_area; // solid area(not alpha-blended) to the parent
69 bool is_hidden; 69 Rect clip_area; // clip area on the parent
70 bool is_hidden_now; 70 bool is_hidden;
71 bool is_cached; 71 bool is_hidden_now;
72 public: 72 bool is_cached;
73 enum { /*MOBILE=1,*/ CACHE_BACK=2, /* CACHE_SELF=4,*/ NO_PICTURE=8, SOLID = 16, SURFACE_FREE = 32, FIT_SURFACE = 64, BLIT_SATURATE = 128, BLIT_MULTIPLY = 256, ALPHA_FREE=512}; 73 public:
74 private: 74 enum { /*MOBILE=1,*/ CACHE_BACK=2, /* CACHE_SELF=4,*/ NO_PICTURE=8, SOLID = 16, SURFACE_FREE = 32, FIT_SURFACE = 64, BLIT_SATURATE = 128, BLIT_MULTIPLY = 256, ALPHA_FREE=512};
75 int attribute; 75 private:
76 76 int attribute;
77 PicRoot* root; 77
78 iterator z_pos; 78 PicRoot* root;
79 int surface_x, surface_y, surface_w, surface_h; 79 iterator z_pos;
80 Surface* surface_back; 80 int surface_x, surface_y, surface_w, surface_h;
81 Surface* surface_own; 81 Surface* surface_back;
82 const unsigned char* surface_alpha; 82 Surface* surface_own;
83 Rect surface_alpha_rect; 83 const unsigned char* surface_alpha;
84 int distance_root; 84 Rect surface_alpha_rect;
85 85 int distance_root;
86 void Blit(const Rect& rpos); 86
87 void Blit(void) { 87 void Blit(const Rect& rpos);
88 is_cached = true; 88 void Blit(void) {
89 Blit(Rect(0, 0, rel_pos.width(), rel_pos.height())); 89 is_cached = true;
90 } 90 Blit(Rect(0, 0, rel_pos.width(), rel_pos.height()));
91 /* 91 }
92 ** rpos : relative position to the widget 92 /*
93 ** ppos : relative position to the parent 93 ** rpos : relative position to the widget
94 ** ppos = parent_pos(rpos) 94 ** ppos : relative position to the parent
95 ** rpos = child_pos(ppos, parent->this_widget) 95 ** ppos = parent_pos(rpos)
96 ** cpos : relative position to a child widget 96 ** rpos = child_pos(ppos, parent->this_widget)
97 ** cpos = child_pos(rpos, a_child_widget) 97 ** cpos : relative position to a child widget
98 ** apos : absolute position in the screen 98 ** cpos = child_pos(rpos, a_child_widget)
99 ** apos = QueryAbsPos(rpos); 99 ** apos : absolute position in the screen
100 ** or 100 ** apos = QueryAbsPos(rpos);
101 ** Rect ppos = rel_pos; 101 ** or
102 ** apos = parent->QueryAbsPos(ppos); 102 ** Rect ppos = rel_pos;
103 ** the latter form is used for 'rel_pos', 103 ** apos = parent->QueryAbsPos(ppos);
104 ** because rel_pos is defined as the relative position to the parent 104 ** the latter form is used for 'rel_pos',
105 */ 105 ** because rel_pos is defined as the relative position to the parent
106 Rect QueryAbsPos(Rect& ppos); // この picture 内の rel_pos を表示するのに実際に必要な絶対座標を得る 106 */
107 107 Rect QueryAbsPos(Rect& ppos); // この picture 内の rel_pos を表示するのに実際に必要な絶対座標を得る
108 static Rect child_pos(Rect rpos, PicBase* child) { /* return 'cpos' */ 108
109 rpos.intersect(child->rel_pos); 109 static Rect child_pos(Rect rpos, PicBase* child) { /* return 'cpos' */
110 rpos.rmove( -(child->rel_pos.lx), -(child->rel_pos.ty)); 110 rpos.intersect(child->rel_pos);
111 return rpos; 111 rpos.rmove( -(child->rel_pos.lx), -(child->rel_pos.ty));
112 } 112 return rpos;
113 Rect parent_pos(Rect rpos) { /* return 'ppos' */ 113 }
114 rpos.rmove(rel_pos.lx, rel_pos.ty); 114 Rect parent_pos(Rect rpos) { /* return 'ppos' */
115 rpos.intersect(rel_pos); 115 rpos.rmove(rel_pos.lx, rel_pos.ty);
116 return rpos; 116 rpos.intersect(rel_pos);
117 } 117 return rpos;
118 void SetEventWidget(class PicWidget* widget); 118 }
119 public: 119 void SetEventWidget(class PicWidget* widget);
120 PicBase(const Rect& rel_pos, PicContainer* parent, int attr); 120 public:
121 virtual ~PicBase(); 121 PicBase(const Rect& rel_pos, PicContainer* parent, int attr);
122 void InitRoot(PicRoot* r) { root = r;} // only called from PicRoot::PicRoot 122 virtual ~PicBase();
123 123 void InitRoot(PicRoot* r) { root = r;} // only called from PicRoot::PicRoot
124 void ReBlit(const Rect& rpos); 124
125 void ReBlit(void) { ReBlit(Rect(0, 0, rel_pos.width(), rel_pos.height()));} 125 void ReBlit(const Rect& rpos);
126 void ExecReBlit(const Rect& rpos); 126 void ReBlit(void) { ReBlit(Rect(0, 0, rel_pos.width(), rel_pos.height()));}
127 void SimpleBlit(Surface* screen); 127 void ExecReBlit(const Rect& rpos);
128 128 void SimpleBlit(Surface* screen);
129 virtual void RMove(int add_x, int add_y); 129
130 void Move(int new_rx, int new_ry); 130 virtual void RMove(int add_x, int add_y);
131 #define ZMOVE_TOP ((PicBase*)0xffff00ff) /* 最前面へ */ 131 void Move(int new_rx, int new_ry);
132 #define ZMOVE_BOTTOM ((PicBase*)0xffff0fff) /* 最背面へ */ 132 #define ZMOVE_TOP ((PicBase*)0xffff00ff) /* 最前面へ */
133 void ZMove(PicBase* back); // back の前に移動(back と自分は同じ親を持つこと) 133 #define ZMOVE_BOTTOM ((PicBase*)0xffff0fff) /* 最背面へ */
134 134 void ZMove(PicBase* back); // back の前に移動(back と自分は同じ親を持つこと)
135 void SetSurface(Surface* new_surface, int x, int y, int attribute = 0); 135
136 void SetSurface(const char* new_surface, int x, int y); 136 void SetSurface(Surface* new_surface, int x, int y, int attribute = 0);
137 void SetSurfacePos(int x, int y); 137 void SetSurface(const char* new_surface, int x, int y);
138 int SurfacePosX(void); 138 void SetSurfacePos(int x, int y);
139 int SurfacePosY(void); 139 int SurfacePosX(void);
140 void SetSurfaceRect(const Rect& r); 140 int SurfacePosY(void);
141 void SetSurfaceAlpha(const unsigned char* alpha, const Rect& rect); 141 void SetSurfaceRect(const Rect& r);
142 void SetSurfaceAlphaFile(const char* file); 142 void SetSurfaceAlpha(const unsigned char* alpha, const Rect& rect);
143 void SetSurfaceColorKey(int r, int g, int b); 143 void SetSurfaceAlphaFile(const char* file);
144 void SetSurfaceAttribute(int attribute); 144 void SetSurfaceColorKey(int r, int g, int b);
145 void SetSurfaceFreeFlag(bool flag=true); 145 void SetSurfaceAttribute(int attribute);
146 void SetClipArea(const Rect& r); 146 void SetSurfaceFreeFlag(bool flag=true);
147 147 void SetClipArea(const Rect& r);
148 void hide(void); 148
149 void show_all(void); 149 void hide(void);
150 void show(void); 150 void show_all(void);
151 151 void show(void);
152 int PosX(void) const { return rel_pos.lx;} 152
153 int PosY(void) const { return rel_pos.ty;} 153 int PosX(void) const { return rel_pos.lx;}
154 int Width(void) const { return rel_pos.width();} 154 int PosY(void) const { return rel_pos.ty;}
155 int Height(void) const { return rel_pos.height();} 155 int Width(void) const { return rel_pos.width();}
156 int DistanceRoot(void) const { return distance_root; } 156 int Height(void) const { return rel_pos.height();}
157 bool IsHidden(void) { return is_hidden_now;} 157 int DistanceRoot(void) const { return distance_root; }
158 bool IsParent(PicBase* pic); 158 bool IsHidden(void) { return is_hidden_now;}
159 159 bool IsParent(PicBase* pic);
160 std::vector<PicAnm*> anm; 160
161 void ClearAnm(void); 161 std::vector<PicAnm*> anm;
162 void ClearAnm(void);
162 }; 163 };
163 164
164 class PicContainer : public PicBase { 165 class PicContainer : public PicBase {
165 friend class PicBase; 166 private:
166 167 friend class PicBase;
167 void BlitBack(iterator z, Rect rpos); // z より後ろの領域を描画 168
168 void BlitFront(iterator z, Rect rpos); // z を含め、zより前の領域を描画 169 void BlitBack(iterator z, Rect rpos); // z より後ろの領域を描画
169 void BlitChildren(Rect rpos); 170 void BlitFront(iterator z, Rect rpos); // z を含め、zより前の領域を描画
170 void BlitSelf(Rect rpos); 171 void BlitChildren(Rect rpos);
171 void BlitSelf(void) { 172 void BlitSelf(Rect rpos);
172 is_cached = true; 173 void BlitSelf(void) {
173 BlitSelf(Rect(0, 0, rel_pos.width(), rel_pos.height())); 174 is_cached = true;
174 } 175 BlitSelf(Rect(0, 0, rel_pos.width(), rel_pos.height()));
175 public: 176 }
176 List children; 177 public:
177 private: 178 List children;
178 179 private:
179 void set_showflag(void); 180
180 void set_nowhiddenflag(bool is_hide); 181 void set_showflag(void);
181 public: 182 void set_nowhiddenflag(bool is_hide);
182 PicContainer(const Rect& rel_pos, PicContainer* parent, int attr); 183 public:
183 ~PicContainer(); 184 PicContainer(const Rect& rel_pos, PicContainer* parent, int attr);
184 PicBase* create_leaf(const Rect& rel_pos, int attr); 185 ~PicContainer();
185 PicContainer* create_node(const Rect& rel_pos, int attr); 186 PicBase* create_leaf(const Rect& rel_pos, int attr);
186 PicRoot& Root(void) { return *root;} 187 PicContainer* create_node(const Rect& rel_pos, int attr);
187 void RMove(int add_x, int add_y); 188 PicRoot& Root(void) { return *root;}
189 void RMove(int add_x, int add_y);
188 }; 190 };
189 191
190 typedef enum { NO_MASK, ALPHA_MASK, COLOR_MASK} MaskType; 192 typedef enum { NO_MASK, ALPHA_MASK, COLOR_MASK} MaskType;
191 struct PicRoot { 193 struct PicRoot {
192 class PicContainer* root; 194 public:
193 private: 195 class PicContainer* root;
194 class FileToSurface* ftosurface; 196 private:
195 struct UpdateItem { 197 class FileToSurface* ftosurface;
196 PicBase* pic; 198 struct UpdateItem {
197 Rect rpos; 199 PicBase* pic;
198 Rect apos; 200 Rect rpos;
199 static bool less(const UpdateItem&, const UpdateItem&); 201 Rect apos;
200 UpdateItem(PicBase* p, const Rect& _rpos, const Rect& _apos) : pic(p), rpos(_rpos), apos(_apos) {} 202 static bool less(const UpdateItem&, const UpdateItem&);
201 }; 203 UpdateItem(PicBase* p, const Rect& _rpos, const Rect& _apos) : pic(p), rpos(_rpos), apos(_apos) {}
202 std::vector<UpdateItem> update_rects; 204 };
203 205 std::vector<UpdateItem> update_rects;
204 friend class FileToSurface; 206
205 void DeleteSurfaceImpl(Surface* s) const; 207 friend class FileToSurface;
206 public: 208 void DeleteSurfaceImpl(Surface* s) const;
207 void Update(PicBase* pic, const Rect& rpos, const Rect& apos); 209 public:
208 void DeleteUpdatePic(PicBase* pic); 210 void Update(PicBase* pic, const Rect& rpos, const Rect& apos);
209 void ExecUpdate(void); 211 void DeleteUpdatePic(PicBase* pic);
210 void SetWindowCaption(const char* caption); 212 void ExecUpdate(void);
211 213 void SetWindowCaption(const char* caption);
212 // Surface 操作 214
213 Surface* NewSurfaceFromRGBAData(int w, int h, char* data, MaskType with_mask) const; // data は malloc されたものであること(SDLの内部仕様) 215 // Surface 操作
214 Surface* NewSurface(int w, int h, MaskType with_mask) const; 216 Surface* NewSurfaceFromRGBAData(int w, int h, char* data, MaskType with_mask) const; // data は malloc されたものであること(SDLの内部仕様)
215 Surface* NewSurface(const char* filename, MaskType with_mask = ALPHA_MASK); 217 Surface* NewSurface(int w, int h, MaskType with_mask) const;
216 Surface* RotZoomSurface(Surface* from, double zoom, double rotate_angle); 218 Surface* NewSurface(const char* filename, MaskType with_mask = ALPHA_MASK);
217 void DeleteSurface(Surface* s); 219 Surface* RotZoomSurface(Surface* from, double zoom, double rotate_angle);
218 void BlitSurface(Surface* src, const Rect& src_rpos, const unsigned char* alpha, const Rect& alpha_r, Surface* dest, const Rect& dest_rpos, int attribute) const; 220 void DeleteSurface(Surface* s);
219 void BlitSurface(Surface* src, const Rect& src_rpos, Surface* dest, const Rect& dest_rpos) const { 221 void BlitSurface(Surface* src, const Rect& src_rpos, const unsigned char* alpha, const Rect& alpha_r, Surface* dest, const Rect& dest_rpos, int attribute) const;
220 BlitSurface(src, src_rpos, 0, Rect(0,0), dest, dest_rpos, 0); 222 void BlitSurface(Surface* src, const Rect& src_rpos, Surface* dest, const Rect& dest_rpos) const {
221 } 223 BlitSurface(src, src_rpos, 0, Rect(0,0), dest, dest_rpos, 0);
222 static bool with_mask(Surface* src); 224 }
223 225 static bool with_mask(Surface* src);
224 Surface* surface; 226
225 Surface* hw_surface; 227 Surface* surface;
226 int width, height; 228 Surface* hw_surface;
227 PicRoot(void); 229 int width, height;
228 ~PicRoot(); 230 PicRoot(void);
229 PicBase* create_leaf(const Rect& apos, int attr) { 231 ~PicRoot();
230 return root->create_leaf(apos, attr); 232 PicBase* create_leaf(const Rect& apos, int attr) {
231 } 233 return root->create_leaf(apos, attr);
232 PicContainer* create_node(const Rect& apos, int attr) { 234 }
233 return root->create_node(apos, attr); 235 PicContainer* create_node(const Rect& apos, int attr) {
234 } 236 return root->create_node(apos, attr);
237 }
235 }; 238 };
236 239
237 class PicWidget { 240 class PicWidget {
238 PicBase* pic; /* 本来継承するべきだが、遅延初期化したいので instance */ 241 private:
239 public: 242 PicBase* pic; /* 本来継承するべきだが、遅延初期化したいので instance */
240 PicWidget(void); 243 public:
241 virtual ~PicWidget(); 244 PicWidget(void);
242 void SetPic(PicBase* new_pic); 245 virtual ~PicWidget();
243 PicBase* Pic(void); 246 void SetPic(PicBase* new_pic);
244 PicContainer* PicNode(void); 247 PicBase* Pic(void);
245 virtual void activate(void); 248 PicContainer* PicNode(void);
246 virtual void deactivate(void); 249 virtual void activate(void);
247 virtual void SetRegion(const Rect& apos); 250 virtual void deactivate(void);
248 void show(void); 251 virtual void SetRegion(const Rect& apos);
249 void hide(void); 252 void show(void);
250 void show_all(void); 253 void hide(void);
254 void show_all(void);
251 }; 255 };
252 256
253 #endif /* PICTURE */ 257 #endif /* PICTURE */