blob: 5d976af1571674af7299a8788eab813242f1f504 [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øgsberg02127232012-02-08 14:47:53 -050042 uint32_t time;
Kristian Høgsberg012a0072010-10-26 00:02:20 -040043 struct { float *d, *u, *v; } b[2];
44};
45
Kristian Høgsberg012a0072010-10-26 00:02:20 -040046static void diffuse(struct smoke *smoke, uint32_t time,
47 float *source, float *dest)
48{
Kristian Høgsberg012a0072010-10-26 00:02:20 -040049 float *s, *d;
50 int x, y, k, stride;
51 float t, a = 0.0002;
52
53 stride = smoke->width;
54
55 for (k = 0; k < 5; k++) {
56 for (y = 1; y < smoke->height - 1; y++) {
57 s = source + y * stride;
58 d = dest + y * stride;
59 for (x = 1; x < smoke->width - 1; x++) {
60 t = d[x - 1] + d[x + 1] +
61 d[x - stride] + d[x + stride];
62 d[x] = (s[x] + a * t) / (1 + 4 * a) * 0.995;
63 }
64 }
65 }
66}
67
68static void advect(struct smoke *smoke, uint32_t time,
69 float *uu, float *vv, float *source, float *dest)
70{
Kristian Høgsberg012a0072010-10-26 00:02:20 -040071 float *s, *d;
72 float *u, *v;
Bryce Harrington40269a62010-11-19 12:15:36 -080073 int x, y, stride;
Kristian Høgsberg012a0072010-10-26 00:02:20 -040074 int i, j;
75 float px, py, fx, fy;
76
77 stride = smoke->width;
78
79 for (y = 1; y < smoke->height - 1; y++) {
80 d = dest + y * stride;
81 u = uu + y * stride;
82 v = vv + y * stride;
83
84 for (x = 1; x < smoke->width - 1; x++) {
85 px = x - u[x];
86 py = y - v[x];
87 if (px < 0.5)
88 px = 0.5;
89 if (py < 0.5)
90 py = 0.5;
91 if (px > smoke->width - 0.5)
92 px = smoke->width - 0.5;
93 if (py > smoke->height - 0.5)
94 py = smoke->height - 0.5;
95 i = (int) px;
96 j = (int) py;
97 fx = px - i;
98 fy = py - j;
99 s = source + j * stride + i;
100 d[x] = (s[0] * (1 - fx) + s[1] * fx) * (1 - fy) +
101 (s[stride] * (1 - fx) + s[stride + 1] * fx) * fy;
102 }
103 }
104}
105
106static void project(struct smoke *smoke, uint32_t time,
107 float *u, float *v, float *p, float *div)
108{
109 int x, y, k, l, s;
Bryce Harrington40269a62010-11-19 12:15:36 -0800110 float h;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400111
112 h = 1.0 / smoke->width;
113 s = smoke->width;
114 memset(p, 0, smoke->height * smoke->width);
115 for (y = 1; y < smoke->height - 1; y++) {
116 l = y * s;
117 for (x = 1; x < smoke->width - 1; x++) {
118 div[l + x] = -0.5 * h * (u[l + x + 1] - u[l + x - 1] +
119 v[l + x + s] - v[l + x - s]);
120 p[l + x] = 0;
121 }
122 }
123
124 for (k = 0; k < 5; k++) {
125 for (y = 1; y < smoke->height - 1; y++) {
126 l = y * s;
127 for (x = 1; x < smoke->width - 1; x++) {
128 p[l + x] = (div[l + x] +
129 p[l + x - 1] +
130 p[l + x + 1] +
131 p[l + x - s] +
132 p[l + x + s]) / 4;
133 }
134 }
135 }
136
137 for (y = 1; y < smoke->height - 1; y++) {
138 l = y * s;
139 for (x = 1; x < smoke->width - 1; x++) {
140 u[l + x] -= 0.5 * (p[l + x + 1] - p[l + x - 1]) / h;
141 v[l + x] -= 0.5 * (p[l + x + s] - p[l + x - s]) / h;
142 }
143 }
144}
145
Kristian Høgsberg02127232012-02-08 14:47:53 -0500146static void render(struct smoke *smoke, cairo_surface_t *surface)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400147{
Bryce Harrington40269a62010-11-19 12:15:36 -0800148 unsigned char *dest;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400149 int x, y, width, height, stride;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400150 float *s;
151 uint32_t *d, c, a;
152
Kristian Høgsberg02127232012-02-08 14:47:53 -0500153 dest = cairo_image_surface_get_data(surface);
154 width = cairo_image_surface_get_width(surface);
155 height = cairo_image_surface_get_height(surface);
156 stride = cairo_image_surface_get_stride(surface);
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400157
158 for (y = 1; y < height - 1; y++) {
159 s = smoke->b[smoke->current].d + y * smoke->height;
160 d = (uint32_t *) (dest + y * stride);
161 for (x = 1; x < width - 1; x++) {
162 c = (int) (s[x] * 800);
163 if (c > 255)
164 c = 255;
165 a = c;
166 if (a < 0x33)
167 a = 0x33;
168 d[x] = (a << 24) | (c << 16) | (c << 8) | c;
169 }
170 }
171}
172
173static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400174frame_callback(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400175{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400176 struct smoke *smoke = data;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400177
Kristian Høgsberg02127232012-02-08 14:47:53 -0500178 window_schedule_redraw(smoke->window);
179 smoke->time = time;
180
181 if (callback)
182 wl_callback_destroy(callback);
183}
184
185static const struct wl_callback_listener listener = {
186 frame_callback,
187};
188
189static void
190redraw_handler(struct widget *widget, void *data)
191{
192 struct smoke *smoke = data;
193 uint32_t time = smoke->time;
194 struct wl_callback *callback;
195 cairo_surface_t *surface;
196
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400197 diffuse(smoke, time / 30, smoke->b[0].u, smoke->b[1].u);
198 diffuse(smoke, time / 30, smoke->b[0].v, smoke->b[1].v);
199 project(smoke, time / 30,
200 smoke->b[1].u, smoke->b[1].v,
201 smoke->b[0].u, smoke->b[0].v);
202 advect(smoke, time / 30,
203 smoke->b[1].u, smoke->b[1].v,
204 smoke->b[1].u, smoke->b[0].u);
205 advect(smoke, time / 30,
206 smoke->b[1].u, smoke->b[1].v,
207 smoke->b[1].v, smoke->b[0].v);
208 project(smoke, time / 30,
209 smoke->b[0].u, smoke->b[0].v,
210 smoke->b[1].u, smoke->b[1].v);
211
212 diffuse(smoke, time / 30, smoke->b[0].d, smoke->b[1].d);
213 advect(smoke, time / 30,
214 smoke->b[0].u, smoke->b[0].v,
215 smoke->b[1].d, smoke->b[0].d);
216
Kristian Høgsberg02127232012-02-08 14:47:53 -0500217 surface = window_get_surface(smoke->window);
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400218
Kristian Høgsberg02127232012-02-08 14:47:53 -0500219 render(smoke, surface);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400220
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400221 window_damage(smoke->window, 0, 0, smoke->width, smoke->height);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400222
Kristian Høgsberg02127232012-02-08 14:47:53 -0500223 cairo_surface_destroy(surface);
224
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400225 callback = wl_surface_frame(window_get_wl_surface(smoke->window));
226 wl_callback_add_listener(callback, &listener, smoke);
Pekka Paalanen8e159182012-10-10 12:49:25 +0300227 wl_surface_commit(window_get_wl_surface(smoke->window));
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400228}
229
Rusty Lynch3bb2b8c2013-08-08 21:22:40 -0700230static void
231smoke_motion_handler(struct smoke *smoke, float x, float y)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400232{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400233 int i, i0, i1, j, j0, j1, k, d = 5;
234
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500235 if (x - d < 1)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400236 i0 = 1;
237 else
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500238 i0 = x - d;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400239 if (i0 + 2 * d > smoke->width - 1)
240 i1 = smoke->width - 1;
241 else
242 i1 = i0 + 2 * d;
243
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500244 if (y - d < 1)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400245 j0 = 1;
246 else
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500247 j0 = y - d;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400248 if (j0 + 2 * d > smoke->height - 1)
249 j1 = smoke->height - 1;
250 else
251 j1 = j0 + 2 * d;
252
253 for (i = i0; i < i1; i++)
254 for (j = j0; j < j1; j++) {
255 k = j * smoke->width + i;
256 smoke->b[0].u[k] += 256 - (random() & 512);
257 smoke->b[0].v[k] += 256 - (random() & 512);
258 smoke->b[0].d[k] += 1;
259 }
Rusty Lynch3bb2b8c2013-08-08 21:22:40 -0700260}
261
262static int
263mouse_motion_handler(struct widget *widget, struct input *input,
264 uint32_t time, float x, float y, void *data)
265{
266 smoke_motion_handler(data, x, y);
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400267
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300268 return CURSOR_HAND1;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400269}
270
Juan Zhao66650632012-02-02 16:02:06 -0800271static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700272touch_motion_handler(struct widget *widget, struct input *input,
273 uint32_t time, int32_t id, float x, float y, void *data)
Rusty Lynch3bb2b8c2013-08-08 21:22:40 -0700274{
275 smoke_motion_handler(data, x, y);
276}
277
278static void
Juan Zhao66650632012-02-02 16:02:06 -0800279resize_handler(struct widget *widget,
280 int32_t width, int32_t height, void *data)
281{
282 struct smoke *smoke = data;
283
284 /* Dont resize me */
285 widget_set_size(smoke->widget, smoke->width, smoke->height);
286}
287
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400288int main(int argc, char *argv[])
289{
290 struct timespec ts;
291 struct smoke smoke;
292 struct display *d;
Bryce Harrington40269a62010-11-19 12:15:36 -0800293 int size;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400294
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500295 d = display_create(&argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200296 if (d == NULL) {
297 fprintf(stderr, "failed to create display: %m\n");
298 return -1;
299 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400300
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400301 smoke.width = 200;
302 smoke.height = 200;
303 smoke.display = d;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500304 smoke.window = window_create(d);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500305 smoke.widget = window_add_widget(smoke.window, &smoke);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500306 window_set_title(smoke.window, "smoke");
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400307
308 window_set_buffer_type(smoke.window, WINDOW_BUFFER_TYPE_SHM);
309 clock_gettime(CLOCK_MONOTONIC, &ts);
310 srandom(ts.tv_nsec);
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400311
312 smoke.current = 0;
313 size = smoke.height * smoke.width;
314 smoke.b[0].d = calloc(size, sizeof(float));
315 smoke.b[0].u = calloc(size, sizeof(float));
316 smoke.b[0].v = calloc(size, sizeof(float));
317 smoke.b[1].d = calloc(size, sizeof(float));
318 smoke.b[1].u = calloc(size, sizeof(float));
319 smoke.b[1].v = calloc(size, sizeof(float));
320
Rusty Lynch3bb2b8c2013-08-08 21:22:40 -0700321 widget_set_motion_handler(smoke.widget, mouse_motion_handler);
322 widget_set_touch_motion_handler(smoke.widget, touch_motion_handler);
Juan Zhao66650632012-02-02 16:02:06 -0800323 widget_set_resize_handler(smoke.widget, resize_handler);
Kristian Høgsberg02127232012-02-08 14:47:53 -0500324 widget_set_redraw_handler(smoke.widget, redraw_handler);
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400325
326 window_set_user_data(smoke.window, &smoke);
Kristian Høgsberg02127232012-02-08 14:47:53 -0500327
328 widget_schedule_resize(smoke.widget, smoke.width, smoke.height);
329
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400330 display_run(d);
331
332 return 0;
333}