blob: baf75d211c3cecca15974f9b311974b562af2a8a [file] [log] [blame]
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001/*
2 * Copyright © 2010 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Andrew Wedgbury9cd661e2014-04-07 12:40:35 +010023#include "config.h"
24
Kristian Høgsberg012a0072010-10-26 00:02:20 -040025#include <stdint.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <time.h>
Kristian Høgsberg012a0072010-10-26 00:02:20 -040030#include <math.h>
Kristian Høgsberg012a0072010-10-26 00:02:20 -040031#include <cairo.h>
Kristian Høgsberg012a0072010-10-26 00:02:20 -040032
Pekka Paalanen50719bc2011-11-22 14:18:50 +020033#include <wayland-client.h>
Kristian Høgsberg012a0072010-10-26 00:02:20 -040034#include "window.h"
35
36struct smoke {
37 struct display *display;
38 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050039 struct widget *widget;
Kristian Høgsberg02127232012-02-08 14:47:53 -050040 int width, height;
Daniel Stone4eb445a2012-11-07 17:51:36 +110041 int current;
Kristian Høgsberg012a0072010-10-26 00:02:20 -040042 struct { float *d, *u, *v; } b[2];
43};
44
Kristian Høgsberg012a0072010-10-26 00:02:20 -040045static void diffuse(struct smoke *smoke, uint32_t time,
46 float *source, float *dest)
47{
Kristian Høgsberg012a0072010-10-26 00:02:20 -040048 float *s, *d;
49 int x, y, k, stride;
50 float t, a = 0.0002;
51
52 stride = smoke->width;
53
54 for (k = 0; k < 5; k++) {
55 for (y = 1; y < smoke->height - 1; y++) {
56 s = source + y * stride;
57 d = dest + y * stride;
58 for (x = 1; x < smoke->width - 1; x++) {
59 t = d[x - 1] + d[x + 1] +
60 d[x - stride] + d[x + stride];
61 d[x] = (s[x] + a * t) / (1 + 4 * a) * 0.995;
62 }
63 }
64 }
65}
66
67static void advect(struct smoke *smoke, uint32_t time,
68 float *uu, float *vv, float *source, float *dest)
69{
Kristian Høgsberg012a0072010-10-26 00:02:20 -040070 float *s, *d;
71 float *u, *v;
Bryce Harrington40269a62010-11-19 12:15:36 -080072 int x, y, stride;
Kristian Høgsberg012a0072010-10-26 00:02:20 -040073 int i, j;
74 float px, py, fx, fy;
75
76 stride = smoke->width;
77
78 for (y = 1; y < smoke->height - 1; y++) {
79 d = dest + y * stride;
80 u = uu + y * stride;
81 v = vv + y * stride;
82
83 for (x = 1; x < smoke->width - 1; x++) {
84 px = x - u[x];
85 py = y - v[x];
86 if (px < 0.5)
87 px = 0.5;
88 if (py < 0.5)
89 py = 0.5;
Frank Binns77f7dac2014-10-28 10:50:18 +000090 if (px > smoke->width - 1.5)
91 px = smoke->width - 1.5;
92 if (py > smoke->height - 1.5)
93 py = smoke->height - 1.5;
Kristian Høgsberg012a0072010-10-26 00:02:20 -040094 i = (int) px;
95 j = (int) py;
96 fx = px - i;
97 fy = py - j;
98 s = source + j * stride + i;
99 d[x] = (s[0] * (1 - fx) + s[1] * fx) * (1 - fy) +
100 (s[stride] * (1 - fx) + s[stride + 1] * fx) * fy;
101 }
102 }
103}
104
105static void project(struct smoke *smoke, uint32_t time,
106 float *u, float *v, float *p, float *div)
107{
108 int x, y, k, l, s;
Bryce Harrington40269a62010-11-19 12:15:36 -0800109 float h;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400110
111 h = 1.0 / smoke->width;
112 s = smoke->width;
113 memset(p, 0, smoke->height * smoke->width);
114 for (y = 1; y < smoke->height - 1; y++) {
115 l = y * s;
116 for (x = 1; x < smoke->width - 1; x++) {
117 div[l + x] = -0.5 * h * (u[l + x + 1] - u[l + x - 1] +
118 v[l + x + s] - v[l + x - s]);
119 p[l + x] = 0;
120 }
121 }
122
123 for (k = 0; k < 5; k++) {
124 for (y = 1; y < smoke->height - 1; y++) {
125 l = y * s;
126 for (x = 1; x < smoke->width - 1; x++) {
127 p[l + x] = (div[l + x] +
128 p[l + x - 1] +
129 p[l + x + 1] +
130 p[l + x - s] +
131 p[l + x + s]) / 4;
132 }
133 }
134 }
135
136 for (y = 1; y < smoke->height - 1; y++) {
137 l = y * s;
138 for (x = 1; x < smoke->width - 1; x++) {
139 u[l + x] -= 0.5 * (p[l + x + 1] - p[l + x - 1]) / h;
140 v[l + x] -= 0.5 * (p[l + x + s] - p[l + x - s]) / h;
141 }
142 }
143}
144
Kristian Høgsberg02127232012-02-08 14:47:53 -0500145static void render(struct smoke *smoke, cairo_surface_t *surface)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400146{
Bryce Harrington40269a62010-11-19 12:15:36 -0800147 unsigned char *dest;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400148 int x, y, width, height, stride;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400149 float *s;
150 uint32_t *d, c, a;
151
Kristian Høgsberg02127232012-02-08 14:47:53 -0500152 dest = cairo_image_surface_get_data(surface);
153 width = cairo_image_surface_get_width(surface);
154 height = cairo_image_surface_get_height(surface);
155 stride = cairo_image_surface_get_stride(surface);
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400156
157 for (y = 1; y < height - 1; y++) {
158 s = smoke->b[smoke->current].d + y * smoke->height;
159 d = (uint32_t *) (dest + y * stride);
160 for (x = 1; x < width - 1; x++) {
161 c = (int) (s[x] * 800);
162 if (c > 255)
163 c = 255;
164 a = c;
165 if (a < 0x33)
166 a = 0x33;
167 d[x] = (a << 24) | (c << 16) | (c << 8) | c;
168 }
169 }
170}
171
172static void
Kristian Høgsberg02127232012-02-08 14:47:53 -0500173redraw_handler(struct widget *widget, void *data)
174{
175 struct smoke *smoke = data;
Jasper St. Pierrec8e41862014-10-12 18:57:31 -0700176 uint32_t time = widget_get_last_time(smoke->widget);
Kristian Høgsberg02127232012-02-08 14:47:53 -0500177 cairo_surface_t *surface;
178
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400179 diffuse(smoke, time / 30, smoke->b[0].u, smoke->b[1].u);
180 diffuse(smoke, time / 30, smoke->b[0].v, smoke->b[1].v);
181 project(smoke, time / 30,
182 smoke->b[1].u, smoke->b[1].v,
183 smoke->b[0].u, smoke->b[0].v);
184 advect(smoke, time / 30,
185 smoke->b[1].u, smoke->b[1].v,
186 smoke->b[1].u, smoke->b[0].u);
187 advect(smoke, time / 30,
188 smoke->b[1].u, smoke->b[1].v,
189 smoke->b[1].v, smoke->b[0].v);
190 project(smoke, time / 30,
191 smoke->b[0].u, smoke->b[0].v,
192 smoke->b[1].u, smoke->b[1].v);
193
194 diffuse(smoke, time / 30, smoke->b[0].d, smoke->b[1].d);
195 advect(smoke, time / 30,
196 smoke->b[0].u, smoke->b[0].v,
197 smoke->b[1].d, smoke->b[0].d);
198
Kristian Høgsberg02127232012-02-08 14:47:53 -0500199 surface = window_get_surface(smoke->window);
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400200
Kristian Høgsberg02127232012-02-08 14:47:53 -0500201 render(smoke, surface);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400202
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400203 window_damage(smoke->window, 0, 0, smoke->width, smoke->height);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400204
Kristian Høgsberg02127232012-02-08 14:47:53 -0500205 cairo_surface_destroy(surface);
206
Jasper St. Pierrec8e41862014-10-12 18:57:31 -0700207 widget_schedule_redraw(smoke->widget);
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400208}
209
Rusty Lynch3bb2b8c2013-08-08 21:22:40 -0700210static void
211smoke_motion_handler(struct smoke *smoke, float x, float y)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400212{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400213 int i, i0, i1, j, j0, j1, k, d = 5;
214
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500215 if (x - d < 1)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400216 i0 = 1;
217 else
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500218 i0 = x - d;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400219 if (i0 + 2 * d > smoke->width - 1)
220 i1 = smoke->width - 1;
221 else
222 i1 = i0 + 2 * d;
223
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500224 if (y - d < 1)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400225 j0 = 1;
226 else
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500227 j0 = y - d;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400228 if (j0 + 2 * d > smoke->height - 1)
229 j1 = smoke->height - 1;
230 else
231 j1 = j0 + 2 * d;
232
233 for (i = i0; i < i1; i++)
234 for (j = j0; j < j1; j++) {
235 k = j * smoke->width + i;
236 smoke->b[0].u[k] += 256 - (random() & 512);
237 smoke->b[0].v[k] += 256 - (random() & 512);
238 smoke->b[0].d[k] += 1;
239 }
Rusty Lynch3bb2b8c2013-08-08 21:22:40 -0700240}
241
242static int
243mouse_motion_handler(struct widget *widget, struct input *input,
244 uint32_t time, float x, float y, void *data)
245{
246 smoke_motion_handler(data, x, y);
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400247
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300248 return CURSOR_HAND1;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400249}
250
Juan Zhao66650632012-02-02 16:02:06 -0800251static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700252touch_motion_handler(struct widget *widget, struct input *input,
253 uint32_t time, int32_t id, float x, float y, void *data)
Rusty Lynch3bb2b8c2013-08-08 21:22:40 -0700254{
255 smoke_motion_handler(data, x, y);
256}
257
258static void
Juan Zhao66650632012-02-02 16:02:06 -0800259resize_handler(struct widget *widget,
260 int32_t width, int32_t height, void *data)
261{
262 struct smoke *smoke = data;
263
264 /* Dont resize me */
265 widget_set_size(smoke->widget, smoke->width, smoke->height);
266}
267
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400268int main(int argc, char *argv[])
269{
270 struct timespec ts;
271 struct smoke smoke;
272 struct display *d;
Bryce Harrington40269a62010-11-19 12:15:36 -0800273 int size;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400274
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500275 d = display_create(&argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200276 if (d == NULL) {
277 fprintf(stderr, "failed to create display: %m\n");
278 return -1;
279 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400280
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400281 smoke.width = 200;
282 smoke.height = 200;
283 smoke.display = d;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500284 smoke.window = window_create(d);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500285 smoke.widget = window_add_widget(smoke.window, &smoke);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500286 window_set_title(smoke.window, "smoke");
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400287
288 window_set_buffer_type(smoke.window, WINDOW_BUFFER_TYPE_SHM);
289 clock_gettime(CLOCK_MONOTONIC, &ts);
290 srandom(ts.tv_nsec);
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400291
292 smoke.current = 0;
293 size = smoke.height * smoke.width;
294 smoke.b[0].d = calloc(size, sizeof(float));
295 smoke.b[0].u = calloc(size, sizeof(float));
296 smoke.b[0].v = calloc(size, sizeof(float));
297 smoke.b[1].d = calloc(size, sizeof(float));
298 smoke.b[1].u = calloc(size, sizeof(float));
299 smoke.b[1].v = calloc(size, sizeof(float));
300
Rusty Lynch3bb2b8c2013-08-08 21:22:40 -0700301 widget_set_motion_handler(smoke.widget, mouse_motion_handler);
302 widget_set_touch_motion_handler(smoke.widget, touch_motion_handler);
Juan Zhao66650632012-02-02 16:02:06 -0800303 widget_set_resize_handler(smoke.widget, resize_handler);
Kristian Høgsberg02127232012-02-08 14:47:53 -0500304 widget_set_redraw_handler(smoke.widget, redraw_handler);
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400305
306 window_set_user_data(smoke.window, &smoke);
Kristian Høgsberg02127232012-02-08 14:47:53 -0500307
308 widget_schedule_resize(smoke.widget, smoke.width, smoke.height);
309
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400310 display_run(d);
311
vivek31732f72014-05-15 18:58:16 +0530312 widget_destroy(smoke.widget);
313 window_destroy(smoke.window);
314 display_destroy(d);
315
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400316 return 0;
317}