/
circlesaver.c
173 lines (141 loc) · 3.79 KB
/
circlesaver.c
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
#include "util.h"
static gchar *geometry = NULL;
static gboolean countdown = FALSE;
static GOptionEntry options[] = {
{"geometry",
0,
0,
G_OPTION_ARG_STRING,
&geometry,
"The initial size and position of window", "WIDTHxHEIGHT+X+Y"},
{"countdown",
'c',
0,
G_OPTION_ARG_NONE,
&countdown,
"If set, the clock counts down to 1234567890", NULL},
{NULL}
};
#define NUM_CIRCLES 200
#define UPDATE_FREQ 10
static circle_t
gen_circle(int i)
{
int clr;
circle_t c;
c.x = 0.2 + sin(i*0.01)*2.0 + next_random()*0.1;
c.y = -0.2 + cos(i*0.001)*0.5 + next_random()*0.1;
c.r = 0.005 + next_random()*0.03;
c.t = 0.0;
c.dx = 0.03 * sin(next_random());
c.dy = -0.01 * cos(next_random());
clr = rand()%4;
switch (clr) {
case 0: c.fill = color(0x3fa080cf); break;
case 1: c.fill = color(0x3fb9b5fc); break;
case 2: c.fill = color(0x0f784f56); break;
default:
case 3: c.fill = color(0x0fb1103c); break;
};
c.stroke = color(0x3f705391);
return c;
}
static void
update_circle(circle_t* c, double dt)
{
c->x += c->dx * dt;
c->y -= c->dy * dt;
// boundschecks
double border = 0.1;
if (c->x - c->r > 1.0+border)
c->x = -border-c->r;
if (c->x < -border-c->r)
c->x = 1.0+border + c->r;
if (c->y - c->r > 1.0+border)
c->y = -border-c->r;
if (c->y < -border-c->r)
c->y = 1.0+border + c->r;
}
static void
draw_circle(cairo_t* cr, gint width, gint height, circle_t* c)
{
cairo_set_source_rgba(cr, c->fill.r, c->fill.g, c->fill.b, c->fill.a);
cairo_arc(cr, c->x*width, c->y*(double)height, c->r*(double)width, 0.0, 2 * M_PI);
//cairo_stroke_preserve(cr);
cairo_fill_preserve(cr);
cairo_set_source_rgba(cr, c->stroke.r, c->stroke.g, c->stroke.b, c->stroke.a);
cairo_set_line_width(cr, 10.0);
cairo_stroke(cr);
}
circle_t circles[NUM_CIRCLES];
cairo_pattern_t* bgpattern;
static gboolean
on_expose_event(GtkWidget *widget,
GdkEventExpose *event,
gpointer data)
{
cairo_t *cr;
gint x, y, width, height;
int i;
cr = gdk_cairo_create(widget->window);
gdk_window_get_position(widget->window, &x, &y);
gdk_drawable_get_size(widget->window, &width, &height);
cairo_set_source_rgb (cr, 0, 0, 0);
//cairo_set_source(cr, bgpattern);
cairo_paint (cr);
for (i = 0; i < NUM_CIRCLES; ++i) {
draw_circle(cr, width, height, circles + i);
}
cairo_destroy(cr);
return FALSE;
}
static gint
timeout_callback (gpointer data)
{
double dt;
int i;
dt = (double)UPDATE_FREQ / 1000.0;
for (i = 0; i < NUM_CIRCLES; ++i) {
update_circle(circles + i, dt);
}
GtkWidget *darea = (GtkWidget*)data;
gtk_widget_queue_draw (darea);
return TRUE;
}
int
main (int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *darea;
GError *error;
int i;
rgba_t bgcolor1, bgcolor2;
error = NULL;
gtk_init_with_args (&argc, &argv,
"circlesaver",
options, NULL, &error);
window = gs_theme_window_new();
darea = gtk_drawing_area_new();
gtk_container_add(GTK_CONTAINER (window), darea);
g_signal_connect(darea, "expose-event",
G_CALLBACK (on_expose_event), NULL);
g_signal_connect(window, "destroy",
G_CALLBACK (gtk_main_quit), NULL);
for (i = 0; i < NUM_CIRCLES; ++i) {
circles[i] = gen_circle(i);
}
bgcolor1 = color(0xFF000000);
bgcolor2 = color(0xFF705391);
bgpattern = cairo_pattern_create_linear(100.5, 0.0, 100.5, 500.0);
cairo_pattern_add_color_stop_rgb(bgpattern, 0.0, bgcolor1.r, bgcolor1.g, bgcolor1.b);
cairo_pattern_add_color_stop_rgb(bgpattern, 1.0, bgcolor2.r, bgcolor2.g, bgcolor2.b);
g_timeout_add_full (G_PRIORITY_HIGH, UPDATE_FREQ, timeout_callback, darea, NULL);
//g_timeout_add (UPDATE_FREQ, timeout_callback, darea);
if ((geometry == NULL)
|| !gtk_window_parse_geometry (GTK_WINDOW (window), geometry))
gtk_window_set_default_size (GTK_WINDOW (window), 640, 480);
gtk_widget_show_all(window);
gtk_main();
cairo_pattern_destroy(bgpattern);
return 0;
}