1 | #include <iostream> |
2 | #include <sstream> |
3 | #include <fstream> |
4 | |
5 | #include "sdl_painter_demo.hpp" |
6 | #include "simple_time.hpp" |
7 | #include "PanZoomTracker.hpp" |
8 | #include "read_path.hpp" |
9 | #include "ImageLoader.hpp" |
10 | #include "colorstop_command_line.hpp" |
11 | |
12 | using namespace fastuidraw; |
13 | |
14 | class painter_clip_test:public sdl_painter_demo |
15 | { |
16 | public: |
17 | painter_clip_test(void); |
18 | |
19 | protected: |
20 | void |
21 | derived_init(int, int); |
22 | |
23 | void |
24 | draw_frame(void); |
25 | |
26 | void |
27 | handle_event(const SDL_Event &ev); |
28 | |
29 | private: |
30 | |
31 | void |
32 | draw_scene(bool with_clipping); |
33 | |
34 | bool m_draw_overlay, m_use_matrices; |
35 | PanZoomTrackerSDLEvent m_zoomer; |
36 | }; |
37 | |
38 | painter_clip_test:: |
39 | painter_clip_test(): |
40 | m_draw_overlay(false), |
41 | m_use_matrices(false) |
42 | { |
43 | } |
44 | |
45 | void |
46 | painter_clip_test:: |
47 | derived_init(int, int) |
48 | { |
49 | } |
50 | |
51 | |
52 | void |
53 | painter_clip_test:: |
54 | draw_scene(bool with_clipping) |
55 | { |
56 | vec2 wh(dimensions()); |
57 | |
58 | /* set clipping to screen center |
59 | */ |
60 | if (with_clipping) |
61 | { |
62 | m_painter->clip_in_rect(Rect() |
63 | .min_point(wh * 0.25f) |
64 | .max_point(wh * 0.75f)); |
65 | } |
66 | |
67 | /* draw a green quad over the clipped region |
68 | */ |
69 | PainterBrush brush; |
70 | brush.color(0.0f, 1.0f, 0.0f, 0.5f); |
71 | m_painter->fill_rect(PainterData(&brush), |
72 | Rect().size(wh)); |
73 | |
74 | /* draw half size. |
75 | */ |
76 | if (m_use_matrices) |
77 | { |
78 | ScaleTranslate<float> sc(0.5f); |
79 | m_painter->concat(sc.matrix3()); |
80 | } |
81 | else |
82 | { |
83 | m_painter->scale(0.5f); |
84 | } |
85 | |
86 | /* move (0,0) to wh*0.5 |
87 | */ |
88 | if (m_use_matrices) |
89 | { |
90 | ScaleTranslate<float> sc(wh * 0.5f); |
91 | m_painter->concat(sc.matrix3()); |
92 | } |
93 | else |
94 | { |
95 | m_painter->translate(wh * 0.5f); |
96 | } |
97 | |
98 | /* clip again |
99 | */ |
100 | if (with_clipping) |
101 | { |
102 | m_painter->clip_in_rect(Rect() |
103 | .min_point(wh * 0.125f) |
104 | .size(wh * 0.25f)); |
105 | } |
106 | |
107 | /* draw a blue quad |
108 | */ |
109 | brush.color(0.0f, 0.0f, 1.0f, 0.5f); |
110 | m_painter->fill_rect(PainterData(&brush), |
111 | Rect().size(wh * 0.5f)); |
112 | |
113 | /* rotate by 30 degrees |
114 | */ |
115 | float r = (0.125f + 0.25f) * 0.5f; |
116 | |
117 | m_painter->translate(wh * r); |
118 | m_painter->rotate(30.0f * float(FASTUIDRAW_PI) / 180.0f); |
119 | brush.color(1.0f, 1.0f, 1.0f, 0.5f); |
120 | m_painter->fill_rect(PainterData(&brush), |
121 | Rect() |
122 | .min_point(wh * (r * 0.25f)) |
123 | .size(wh * (r * 0.25f))); |
124 | } |
125 | |
126 | void |
127 | painter_clip_test:: |
128 | draw_frame(void) |
129 | { |
130 | m_painter->begin(m_surface, Painter::y_increases_downwards); |
131 | m_zoomer.transformation().concat_to_painter(m_painter); |
132 | |
133 | m_painter->save(); |
134 | draw_scene(true); |
135 | m_painter->restore(); |
136 | if (m_draw_overlay) |
137 | { |
138 | draw_scene(false); |
139 | } |
140 | |
141 | m_painter->end(); |
142 | fastuidraw_glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); |
143 | fastuidraw_glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
144 | m_surface->blit_surface(GL_NEAREST); |
145 | } |
146 | |
147 | void |
148 | painter_clip_test:: |
149 | handle_event(const SDL_Event &ev) |
150 | { |
151 | m_zoomer.handle_event(ev); |
152 | switch(ev.type) |
153 | { |
154 | case SDL_QUIT: |
155 | end_demo(0); |
156 | break; |
157 | |
158 | case SDL_KEYUP: |
159 | switch(ev.key.keysym.sym) |
160 | { |
161 | case SDLK_ESCAPE: |
162 | end_demo(0); |
163 | break; |
164 | case SDLK_o: |
165 | m_draw_overlay = !m_draw_overlay; |
166 | break; |
167 | case SDLK_m: |
168 | m_use_matrices = !m_use_matrices; |
169 | std::cout << "Use matrics: " << m_use_matrices << "\n" ; |
170 | break; |
171 | } |
172 | break; |
173 | }; |
174 | } |
175 | |
176 | int |
177 | main(int argc, char **argv) |
178 | { |
179 | painter_clip_test P; |
180 | return P.main(argc, argv); |
181 | } |
182 | |