forked from flugi/ITK_graphicslib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics.hpp
264 lines (211 loc) · 5.92 KB
/
graphics.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#ifndef GRAPHICS_HPP_INCLUDED
#define GRAPHICS_HPP_INCLUDED
#include <string>
struct SDL_Surface;
struct _TTF_Font;
namespace genv
{
/*********** Graphical output device definition ***********/
class canvas {
public:
canvas();
virtual ~canvas();
canvas(int w, int h);
canvas(const canvas & c);
bool open(unsigned width, unsigned height);
bool save(const std::string& file) const;
void transparent(bool t) {transp=t;}
void set_color(int r, int g, int b);
bool move_point(int x, int y);
void draw_dot();
void draw_line(int x, int y);
void draw_box(int x, int y);
void draw_text(const std::string& str);
void blitfrom(const canvas &c,int x1,int y1, int x2, int y2, int x3, int y3);
bool load_font(const std::string& fname, int fontsize = 16, bool antialias=true);
void set_antialias(bool antialias) {antialiastext=antialias;}
int x() const { return pt_x; }
int y() const { return pt_y; }
int cascent() const;
int cdescent() const;
int twidth(const std::string& s) const;
virtual void refresh() {}
template <typename T>
inline void call_with_rel(T meth, int vec_x, int vec_y) {
if (vec_x || vec_y) {
int dx=vec_x-sgn(vec_x);
int dy=vec_y-sgn(vec_y);
(this->*meth)(dx, dy);
}
}
protected:
template <typename T>
inline int sgn(const T& a) {
if (a<0) return -1; if (a>0) return 1; return 0;
}
int pt_x;
int pt_y;
SDL_Surface* buf;
int draw_clr;
bool transp;
_TTF_Font* font;
bool antialiastext;
std::string loaded_font_file_name;
int font_size;
};
// Class of output device (singleton)
class groutput : public canvas
{
public:
static groutput& instance();
virtual ~groutput();
void showmouse(bool toggle);
void movemouse(int x, int y);
bool open(unsigned width, unsigned height, bool fullscreen=false);
virtual void refresh();
void set_title(const std::string& title);
private:
groutput();
};
// Global accessor for the output device instance
extern groutput& gout;
// Generic operator for applying global manipulators
template <typename Op>
inline canvas& operator << (canvas& out, Op oper)
{ oper(out); return out; }
// Global manipulators for frequently used operations
inline void refresh(canvas& out) { out.refresh(); }
inline void dot(canvas& out) { out.draw_dot(); }
struct stamp
{
canvas &c;
int x1,y1,x2,y2,x3,y3;
stamp(canvas&cc, int sx1, int sy1, int xsize, int ysize, int tx1, int ty1) :
c(cc), x1(sx1), y1(sy1),x2(xsize), y2(ysize), x3(tx1), y3(ty1) {}
stamp(canvas&cc, int tx1, int ty1) :
c(cc), x1(-1), y1(-1),x2(-1), y2(-1), x3(tx1), y3(ty1) {}
void operator () (canvas& out)
{ out.blitfrom(c,x1,y1,x2,y2,x3,y3); }
};
struct color
{
int red, green, blue;
color(int r, int g, int b) : red(r), green(g), blue(b) {}
void operator () (canvas& out)
{ out.set_color(red, green, blue); }
};
struct move
{
int vec_x, vec_y;
move(int x, int y) : vec_x(x), vec_y(y) {}
void operator () (canvas& out)
{ out.move_point(vec_x, vec_y); }
};
struct move_to
{
int pos_x, pos_y;
move_to(unsigned x, unsigned y) : pos_x(x), pos_y(y) {}
void operator () (canvas& out)
{ out.move_point(pos_x - out.x(), pos_y - out.y()); }
};
struct line
{
int vec_x, vec_y;
line(int x, int y) : vec_x(x), vec_y(y) {}
void operator () (canvas& out)
{ out.call_with_rel(&canvas::draw_line,vec_x, vec_y); }
};
struct line_to
{
int pos_x, pos_y;
line_to(unsigned x, unsigned y) : pos_x(x), pos_y(y) {}
void operator () (canvas& out)
{ out.draw_line(pos_x - out.x(), pos_y - out.y()); }
};
struct box
{
int vec_x, vec_y;
box(int x, int y) : vec_x(x), vec_y(y) {}
void operator () (canvas& out)
{ out.call_with_rel(&canvas::draw_box, vec_x, vec_y); }
};
struct box_to
{
int pos_x, pos_y;
box_to(unsigned x, unsigned y) : pos_x(x), pos_y(y) {}
void operator () (canvas& out)
{ out.draw_box(pos_x - out.x(), pos_y - out.y()); }
};
struct text
{
std::string str;
text(const std::string& s) : str(s) {}
text(char c) : str(1, c) {}
void operator () (canvas& out)
{ out.draw_text(str); }
};
/*
struct title
{
std::string str;
title(const std::string& s) : str(s) {}
void operator () (canvas& out)
{ out.set_title(str); }
};
*/
struct font
{
std::string font_name;
int font_size;
bool antialias;
font(const std::string& s, int fs, bool a=true) : font_name(s), font_size(fs), antialias(a) {}
void operator () (canvas& out)
{ out.load_font(font_name, font_size, antialias); }
};
/*********** Input device definition **********/
// Keycode constants
enum keycode_t {
key_tab = '\t', key_backspace = '\b', key_enter = '\r',
key_escape = '\033', key_space = ' ',
key_up = 0x10000, key_down, key_right, key_left,
key_insert, key_delete, key_home, key_end, key_pgup, key_pgdn,
key_lshift, key_rshift, key_lctrl, key_rctrl, key_lalt, key_ralt,
key_lwin, key_rwin, key_menu, key_numl, key_capsl, key_scrl,
key_f0 = 0x20000, key_f1, key_f2, key_f3, key_f4, key_f5, key_f6, key_f7,
key_f8, key_f9, key_f10, key_f11, key_f12, key_f13, key_f14, key_f15
};
enum button_t {
btn_left = 1, btn_middle, btn_right, btn_wheelup, btn_wheeldown
};
enum event_type {
ev_key = 1, ev_mouse, ev_timer
};
// Event descriptor
struct event
{
int keycode;
int pos_x, pos_y;
int button;
int time;
int type;
};
// Class of input device (singleton)
class grinput
{
public:
static grinput& instance();
grinput& wait_event(event&);
void timer(int wait);
operator const void*() const
{ if (quit) return 0; else return this; }
private:
grinput() : quit(false) {}
bool quit;
};
// Global accessor for the input device instance
extern grinput& gin;
// Event reader operator
inline grinput& operator >> (grinput& inp, event& ev)
{ return inp.wait_event(ev); }
}
#endif // GRAPHICS_HPP_INCLUDED