blob: 80b8c58ee25b2f3cc46cd70eb9444546d78c1f7f [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
23#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <time.h>
Kristian Høgsberg012a0072010-10-26 00:02:20 -040028#include <math.h>
29#include <time.h>
30#include <cairo.h>
Kristian Høgsberg012a0072010-10-26 00:02:20 -040031
Pekka Paalanen50719bc2011-11-22 14:18:50 +020032#include <wayland-client.h>
Kristian Høgsberg012a0072010-10-26 00:02:20 -040033#include "window.h"
34
35struct smoke {
36 struct display *display;
37 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050038 struct widget *widget;
Kristian Høgsberg02127232012-02-08 14:47:53 -050039 int width, height;
Daniel Stone4eb445a2012-11-07 17:51:36 +110040 int current;
Kristian Høgsberg02127232012-02-08 14:47:53 -050041 uint32_t time;
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;
90 if (px > smoke->width - 0.5)
91 px = smoke->width - 0.5;
92 if (py > smoke->height - 0.5)
93 py = smoke->height - 0.5;
94 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øgsberga8d1fa72011-08-23 18:14:06 -0400173frame_callback(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400174{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400175 struct smoke *smoke = data;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400176
Kristian Høgsberg02127232012-02-08 14:47:53 -0500177 window_schedule_redraw(smoke->window);
178 smoke->time = time;
179
180 if (callback)
181 wl_callback_destroy(callback);
182}
183
184static const struct wl_callback_listener listener = {
185 frame_callback,
186};
187
188static void
189redraw_handler(struct widget *widget, void *data)
190{
191 struct smoke *smoke = data;
192 uint32_t time = smoke->time;
193 struct wl_callback *callback;
194 cairo_surface_t *surface;
195
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400196 diffuse(smoke, time / 30, smoke->b[0].u, smoke->b[1].u);
197 diffuse(smoke, time / 30, smoke->b[0].v, smoke->b[1].v);
198 project(smoke, time / 30,
199 smoke->b[1].u, smoke->b[1].v,
200 smoke->b[0].u, smoke->b[0].v);
201 advect(smoke, time / 30,
202 smoke->b[1].u, smoke->b[1].v,
203 smoke->b[1].u, smoke->b[0].u);
204 advect(smoke, time / 30,
205 smoke->b[1].u, smoke->b[1].v,
206 smoke->b[1].v, smoke->b[0].v);
207 project(smoke, time / 30,
208 smoke->b[0].u, smoke->b[0].v,
209 smoke->b[1].u, smoke->b[1].v);
210
211 diffuse(smoke, time / 30, smoke->b[0].d, smoke->b[1].d);
212 advect(smoke, time / 30,
213 smoke->b[0].u, smoke->b[0].v,
214 smoke->b[1].d, smoke->b[0].d);
215
Kristian Høgsberg02127232012-02-08 14:47:53 -0500216 surface = window_get_surface(smoke->window);
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400217
Kristian Høgsberg02127232012-02-08 14:47:53 -0500218 render(smoke, surface);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400219
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400220 window_damage(smoke->window, 0, 0, smoke->width, smoke->height);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400221
Kristian Høgsberg02127232012-02-08 14:47:53 -0500222 cairo_surface_destroy(surface);
223
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400224 callback = wl_surface_frame(window_get_wl_surface(smoke->window));
225 wl_callback_add_listener(callback, &listener, smoke);
Pekka Paalanen8e159182012-10-10 12:49:25 +0300226 wl_surface_commit(window_get_wl_surface(smoke->window));
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400227}
228
229static int
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500230smoke_motion_handler(struct widget *widget, struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400231 uint32_t time, float x, float y, void *data)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400232{
233 struct smoke *smoke = data;
234 int i, i0, i1, j, j0, j1, k, d = 5;
235
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500236 if (x - d < 1)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400237 i0 = 1;
238 else
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500239 i0 = x - d;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400240 if (i0 + 2 * d > smoke->width - 1)
241 i1 = smoke->width - 1;
242 else
243 i1 = i0 + 2 * d;
244
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500245 if (y - d < 1)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400246 j0 = 1;
247 else
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500248 j0 = y - d;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400249 if (j0 + 2 * d > smoke->height - 1)
250 j1 = smoke->height - 1;
251 else
252 j1 = j0 + 2 * d;
253
254 for (i = i0; i < i1; i++)
255 for (j = j0; j < j1; j++) {
256 k = j * smoke->width + i;
257 smoke->b[0].u[k] += 256 - (random() & 512);
258 smoke->b[0].v[k] += 256 - (random() & 512);
259 smoke->b[0].d[k] += 1;
260 }
261
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300262 return CURSOR_HAND1;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400263}
264
Juan Zhao66650632012-02-02 16:02:06 -0800265static void
266resize_handler(struct widget *widget,
267 int32_t width, int32_t height, void *data)
268{
269 struct smoke *smoke = data;
270
271 /* Dont resize me */
272 widget_set_size(smoke->widget, smoke->width, smoke->height);
273}
274
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400275int main(int argc, char *argv[])
276{
277 struct timespec ts;
278 struct smoke smoke;
279 struct display *d;
Bryce Harrington40269a62010-11-19 12:15:36 -0800280 int size;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400281
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400282 d = display_create(argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200283 if (d == NULL) {
284 fprintf(stderr, "failed to create display: %m\n");
285 return -1;
286 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400287
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400288 smoke.width = 200;
289 smoke.height = 200;
290 smoke.display = d;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500291 smoke.window = window_create(d);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500292 smoke.widget = window_add_widget(smoke.window, &smoke);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500293 window_set_title(smoke.window, "smoke");
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400294
295 window_set_buffer_type(smoke.window, WINDOW_BUFFER_TYPE_SHM);
296 clock_gettime(CLOCK_MONOTONIC, &ts);
297 srandom(ts.tv_nsec);
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400298
299 smoke.current = 0;
300 size = smoke.height * smoke.width;
301 smoke.b[0].d = calloc(size, sizeof(float));
302 smoke.b[0].u = calloc(size, sizeof(float));
303 smoke.b[0].v = calloc(size, sizeof(float));
304 smoke.b[1].d = calloc(size, sizeof(float));
305 smoke.b[1].u = calloc(size, sizeof(float));
306 smoke.b[1].v = calloc(size, sizeof(float));
307
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500308 widget_set_motion_handler(smoke.widget, smoke_motion_handler);
Juan Zhao66650632012-02-02 16:02:06 -0800309 widget_set_resize_handler(smoke.widget, resize_handler);
Kristian Høgsberg02127232012-02-08 14:47:53 -0500310 widget_set_redraw_handler(smoke.widget, redraw_handler);
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400311
312 window_set_user_data(smoke.window, &smoke);
Kristian Høgsberg02127232012-02-08 14:47:53 -0500313
314 widget_schedule_resize(smoke.widget, smoke.width, smoke.height);
315
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400316 display_run(d);
317
318 return 0;
319}