Debian Codegraph @

demos/gtk-demo/example_pixbufs.cc

raw
1 /* Pixbufs
2 *
3 * A Gdk::Pixbuf represents an image, normally in RGB or RGBA format.
4 * Pixbufs are normally used to load files from disk and perform
5 * image scaling.
6 *
7 * This demo is not all that educational, but looks cool. It was written
8 * by Extreme Pixbuf Hacker Federico Mena Quintero. It also shows
9 * off how to use Gtk::DrawingArea to do a simple animation.
10 *
11 * Look at the Image demo for additional pixbuf usage examples.
12 *
13 */
14
15 #include <gtkmm.h>
16 #include <cmath>
17 #include <algorithm>
18
19 namespace
20 {
21 const char* background_name = "background.jpg";
22
23 const char* image_names[] = {
24 "apple-red.png",
25 "gnome-applets.png",
26 "gnome-calendar.png",
27 "gnome-foot.png",
28 "gnome-gmush.png",
29 "gnome-gimp.png",
30 "gnome-gsame.png",
31 "gnu-keys.png"
32 };
33
34 const int n_images = G_N_ELEMENTS(image_names);
35
36 const gint64 cycle_time = 3000000; // microseconds
37
38 } // anonymous namespace
39
40 class Example_Pixbufs : public Gtk::Window
41 {
42 public:
43 Example_Pixbufs();
44 ~Example_Pixbufs() override;
45
46 protected:
47 virtual void load_pixbufs();
48
49 // Signal handler:
50 bool on_drawingarea_draw(const Cairo::RefPtr<Cairo::Context>& cr);
51
52 // Callback:
53 bool on_tick(const Glib::RefPtr<Gdk::FrameClock>& frame_clock);
54
55 //Member widgets:
56 Glib::RefPtr<Gdk::Pixbuf> m_refPixbuf;
57 Glib::RefPtr<Gdk::Pixbuf> m_refPixbuf_Background;
58 Glib::RefPtr<Gdk::Pixbuf> m_images[n_images];
59 Gtk::DrawingArea m_DrawingArea;
60
61 guint m_tick_id = 0;
62 guint m_back_width = 0;
63 guint m_back_height = 0;
64 gint64 m_start_time = 0;
65 };
66
67 //Called by DemoWindow;
68 Gtk::Window* do_pixbufs()
69 {
70 return new Example_Pixbufs();
71 }
72
73 Example_Pixbufs::Example_Pixbufs()
74 {
75 set_title("Pixbufs");
76 set_resizable(false);
77
78 try
79 {
80 load_pixbufs();
81
82 set_size_request(m_back_width, m_back_height);
83 m_refPixbuf = Gdk::Pixbuf::create(Gdk::COLORSPACE_RGB, false, 8, m_back_width, m_back_height);
84 m_DrawingArea.signal_draw().connect(sigc::mem_fun(*this, &Example_Pixbufs::on_drawingarea_draw));
85 add(m_DrawingArea);
86
87 m_tick_id = m_DrawingArea.add_tick_callback(
88 sigc::mem_fun(*this, &Example_Pixbufs::on_tick));
89 }
90 catch (const Glib::Error& error)
91 {
92 Glib::ustring strMsg = "Failed to load an image: ";
93 strMsg += error.what();
94
95 Gtk::MessageDialog dialog(strMsg, false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_CLOSE);
96 dialog.run();
97 }
98
99 show_all();
100 }
101
102 Example_Pixbufs::~Example_Pixbufs()
103 {
104 m_DrawingArea.remove_tick_callback(m_tick_id); // Will probably happen anyway, in the destructor.
105 }
106
107 /* Loads the m_images for the demo and throws an exception if the operation failed */
108 void Example_Pixbufs::load_pixbufs()
109 {
110 if(m_refPixbuf_Background)
111 return; /* already loaded earlier */
112
113 auto resource_name_background = std::string("/pixbufs/") + background_name;
114
115 m_refPixbuf_Background = Gdk::Pixbuf::create_from_resource(resource_name_background);
116
117 m_back_width = m_refPixbuf_Background->get_width();
118 m_back_height = m_refPixbuf_Background->get_height();
119
120 for (int i = 0; i < n_images; ++i)
121 {
122 auto resource_name = std::string("/pixbufs/") + image_names[i];
123
124 auto pixbuf = Gdk::Pixbuf::create_from_resource(resource_name);
125
126 m_images[i] = pixbuf;
127 }
128 }
129
130 /* Draw callback for the drawing area */
131 bool Example_Pixbufs::on_drawingarea_draw(const Cairo::RefPtr<Cairo::Context>& cr)
132 {
133 Gdk::Cairo::set_source_pixbuf(cr, m_refPixbuf);
134 cr->paint();
135
136 return true;
137 }
138
139 /* Handler to regenerate the frame */
140 bool Example_Pixbufs::on_tick(const Glib::RefPtr<Gdk::FrameClock>& frame_clock)
141 {
142 m_refPixbuf_Background->copy_area(0, 0, m_back_width, m_back_height, m_refPixbuf, 0, 0);
143
144 if (m_start_time == 0)
145 m_start_time = frame_clock->get_frame_time();
146
147 const gint64 current_time = frame_clock->get_frame_time();
148 const double f = ((current_time - m_start_time) % cycle_time) / (double)cycle_time;
149
150 const double xmid = m_back_width / 2.0;
151 const double ymid = m_back_height / 2.0;
152
153 const double radius = std::min(xmid, ymid) / 2.0;
154
155 for (int i = 0; i < n_images; i++)
156 {
157 const double ang = 2.0 * G_PI * (double)i / n_images - f * 2.0 * G_PI;
158
159 const int iw = m_images[i]->get_width();
160 const int ih = m_images[i]->get_height();
161
162 const double r = radius + (radius / 3.0) * std::sin(f * 2.0 * G_PI);
163
164 const double xpos = std::floor(xmid + r * std::cos(ang) - iw / 2.0 + 0.5);
165 const double ypos = std::floor(ymid + r * std::sin(ang) - ih / 2.0 + 0.5);
166
167 double k = (i & 1) ? std::sin(f * 2.0 * G_PI) : std::cos(f * 2.0 * G_PI);
168 k = 2.0 * k * k;
169 k = std::max(0.25, k);
170
171 const Gdk::Rectangle r1((int)xpos, (int)ypos, (int)(iw * k), (int)(ih * k));
172 const Gdk::Rectangle r2(0, 0, m_back_width, m_back_height);
173
174 if (r1.intersects(r2))
175 {
176 Gdk::Rectangle dest = r1;
177 dest.intersect(r2);
178 m_images[i]->composite(m_refPixbuf,
179 dest.get_x(), dest.get_y(),
180 dest.get_width(), dest.get_height(),
181 xpos, ypos,
182 k, k,
183 Gdk::INTERP_NEAREST,
184 (int)((i & 1)
185 ? std::max(127.0, std::fabs(255.0 * std::sin(f * 2.0 * G_PI)))
186 : std::max(127.0, std::fabs(255.0 * std::cos(f * 2.0 * G_PI)))));
187 }
188 }
189
190 m_DrawingArea.queue_draw();
191
192 return true;
193 }