00001 #include <stdlib.h>
00002 #include <ctype.h>
00003 #include <stdio.h>
00004 #include "agg_basics.h"
00005 #include "agg_rendering_buffer.h"
00006 #include "agg_rasterizer_scanline_aa.h"
00007 #include "agg_renderer_outline_aa.h"
00008 #include "agg_rasterizer_outline_aa.h"
00009 #include "agg_scanline_p.h"
00010 #include "agg_renderer_scanline.h"
00011 #include "agg_path_storage.h"
00012 #include "agg_conv_transform.h"
00013 #include "agg_bounding_rect.h"
00014 #include "ctrl/agg_slider_ctrl.h"
00015 #include "ctrl/agg_cbox_ctrl.h"
00016 #include "platform/agg_platform_support.h"
00017
00018 #define AGG_BGR24
00019
00020
00021
00022
00023
00024
00025
00026 #include "pixel_formats.h"
00027
00028 enum flip_y_e { flip_y = true };
00029
00030
00031 agg::rasterizer_scanline_aa<> g_rasterizer;
00032 agg::scanline_p8 g_scanline;
00033 agg::path_storage g_path;
00034 agg::rgba8 g_colors[100];
00035 unsigned g_path_idx[100];
00036 unsigned g_npaths = 0;
00037 double g_x1 = 0;
00038 double g_y1 = 0;
00039 double g_x2 = 0;
00040 double g_y2 = 0;
00041 double g_base_dx = 0;
00042 double g_base_dy = 0;
00043 double g_angle = 0;
00044 double g_scale = 1.0;
00045 double g_skew_x = 0;
00046 double g_skew_y = 0;
00047 int g_nclick = 0;
00048
00049
00050 unsigned parse_lion(agg::path_storage& ps, agg::rgba8* colors, unsigned* path_idx);
00051 void parse_lion()
00052 {
00053 g_npaths = parse_lion(g_path, g_colors, g_path_idx);
00054 agg::pod_array_adaptor<unsigned> path_idx(g_path_idx, 100);
00055 agg::bounding_rect(g_path, path_idx, 0, g_npaths, &g_x1, &g_y1, &g_x2, &g_y2);
00056 g_base_dx = (g_x2 - g_x1) / 2.0;
00057 g_base_dy = (g_y2 - g_y1) / 2.0;
00058 }
00059
00060
00061 class the_application : public agg::platform_support
00062 {
00063 agg::slider_ctrl<agg::rgba8> m_width_slider;
00064 agg::cbox_ctrl<agg::rgba8> m_scanline;
00065
00066 public:
00067 the_application(agg::pix_format_e format, bool flip_y) :
00068 agg::platform_support(format, flip_y),
00069 m_width_slider(5, 5, 150, 12, !flip_y),
00070 m_scanline(160, 5, "Use Scanline Rasterizer", !flip_y)
00071 {
00072 parse_lion();
00073 add_ctrl(m_width_slider);
00074 m_width_slider.no_transform();
00075 m_width_slider.range(0.0, 4.0);
00076 m_width_slider.value(1.0);
00077 m_width_slider.label("Width %3.2f");
00078
00079 add_ctrl(m_scanline);
00080 m_scanline.no_transform();
00081 }
00082
00083
00084 virtual void on_draw()
00085 {
00086 int width = rbuf_window().width();
00087 int height = rbuf_window().height();
00088
00089 typedef agg::renderer_base<pixfmt> renderer_base;
00090 typedef agg::renderer_scanline_aa_solid<renderer_base> renderer_solid;
00091
00092 pixfmt pixf(rbuf_window());
00093 renderer_base rb(pixf);
00094 renderer_solid r(rb);
00095 rb.clear(agg::rgba(1,1,1));
00096
00097 agg::trans_affine mtx;
00098 mtx *= agg::trans_affine_translation(-g_base_dx, -g_base_dy);
00099 mtx *= agg::trans_affine_scaling(g_scale, g_scale);
00100 mtx *= agg::trans_affine_rotation(g_angle + agg::pi);
00101 mtx *= agg::trans_affine_skewing(g_skew_x/1000.0, g_skew_y/1000.0);
00102 mtx *= agg::trans_affine_translation(width/2, height/2);
00103
00104 if(m_scanline.status())
00105 {
00106 agg::conv_stroke<agg::path_storage> stroke(g_path);
00107 stroke.width(m_width_slider.value());
00108 stroke.line_join(agg::round_join);
00109 agg::conv_transform<agg::conv_stroke<agg::path_storage> > trans(stroke, mtx);
00110 agg::render_all_paths(g_rasterizer, g_scanline, r, trans, g_colors, g_path_idx, g_npaths);
00111 }
00112 else
00113 {
00114 typedef agg::renderer_outline_aa<renderer_base> renderer_type;
00115 typedef agg::rasterizer_outline_aa<renderer_type> rasterizer_type;
00116
00117 double w = m_width_slider.value() * mtx.scale();
00118
00119 agg::line_profile_aa profile(w, agg::gamma_none());
00120 renderer_type ren(rb, profile);
00121 rasterizer_type ras(ren);
00122
00123 agg::conv_transform<agg::path_storage> trans(g_path, mtx);
00124
00125 ras.render_all_paths(trans, g_colors, g_path_idx, g_npaths);
00126 }
00127
00128
00129 agg::render_ctrl(g_rasterizer, g_scanline, rb, m_width_slider);
00130 agg::render_ctrl(g_rasterizer, g_scanline, rb, m_scanline);
00131 }
00132
00133
00134 void transform(double width, double height, double x, double y)
00135 {
00136 x -= width / 2;
00137 y -= height / 2;
00138 g_angle = atan2(y, x);
00139 g_scale = sqrt(y * y + x * x) / 100.0;
00140 }
00141
00142
00143 virtual void on_mouse_button_down(int x, int y, unsigned flags)
00144 {
00145 if(flags & agg::mouse_left)
00146 {
00147 int width = rbuf_window().width();
00148 int height = rbuf_window().height();
00149 transform(width, height, x, y);
00150 force_redraw();
00151 }
00152
00153 if(flags & agg::mouse_right)
00154 {
00155 g_skew_x = x;
00156 g_skew_y = y;
00157 force_redraw();
00158 }
00159 }
00160
00161
00162 virtual void on_mouse_move(int x, int y, unsigned flags)
00163 {
00164 on_mouse_button_down(x, y, flags);
00165 }
00166
00167 };
00168
00169
00170
00171
00172
00173
00174 int agg_main(int argc, char* argv[])
00175 {
00176 the_application app(pix_format, flip_y);
00177 app.caption("AGG Example. Lion");
00178
00179 if(app.init(512, 512, agg::window_resize))
00180 {
00181 return app.run();
00182 }
00183 return 1;
00184 }
00185
00186
00187
00188
00189
00190