Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 1 | /* |
| 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> |
| 28 | #include <fcntl.h> |
| 29 | #include <unistd.h> |
| 30 | #include <math.h> |
| 31 | #include <time.h> |
| 32 | #include <cairo.h> |
| 33 | #include <glib.h> |
| 34 | |
Pekka Paalanen | 50719bc | 2011-11-22 14:18:50 +0200 | [diff] [blame] | 35 | #include <wayland-client.h> |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 36 | #include "window.h" |
| 37 | |
| 38 | struct smoke { |
| 39 | struct display *display; |
| 40 | struct window *window; |
Kristian Høgsberg | 75bc667 | 2012-01-10 09:43:58 -0500 | [diff] [blame] | 41 | struct widget *widget; |
Kristian Høgsberg | 0212723 | 2012-02-08 14:47:53 -0500 | [diff] [blame^] | 42 | int width, height; |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 43 | int offset, current; |
Kristian Høgsberg | 0212723 | 2012-02-08 14:47:53 -0500 | [diff] [blame^] | 44 | uint32_t time; |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 45 | struct { float *d, *u, *v; } b[2]; |
| 46 | }; |
| 47 | |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 48 | static void diffuse(struct smoke *smoke, uint32_t time, |
| 49 | float *source, float *dest) |
| 50 | { |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 51 | float *s, *d; |
| 52 | int x, y, k, stride; |
| 53 | float t, a = 0.0002; |
| 54 | |
| 55 | stride = smoke->width; |
| 56 | |
| 57 | for (k = 0; k < 5; k++) { |
| 58 | for (y = 1; y < smoke->height - 1; y++) { |
| 59 | s = source + y * stride; |
| 60 | d = dest + y * stride; |
| 61 | for (x = 1; x < smoke->width - 1; x++) { |
| 62 | t = d[x - 1] + d[x + 1] + |
| 63 | d[x - stride] + d[x + stride]; |
| 64 | d[x] = (s[x] + a * t) / (1 + 4 * a) * 0.995; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | static void advect(struct smoke *smoke, uint32_t time, |
| 71 | float *uu, float *vv, float *source, float *dest) |
| 72 | { |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 73 | float *s, *d; |
| 74 | float *u, *v; |
Bryce Harrington | 40269a6 | 2010-11-19 12:15:36 -0800 | [diff] [blame] | 75 | int x, y, stride; |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 76 | int i, j; |
| 77 | float px, py, fx, fy; |
| 78 | |
| 79 | stride = smoke->width; |
| 80 | |
| 81 | for (y = 1; y < smoke->height - 1; y++) { |
| 82 | d = dest + y * stride; |
| 83 | u = uu + y * stride; |
| 84 | v = vv + y * stride; |
| 85 | |
| 86 | for (x = 1; x < smoke->width - 1; x++) { |
| 87 | px = x - u[x]; |
| 88 | py = y - v[x]; |
| 89 | if (px < 0.5) |
| 90 | px = 0.5; |
| 91 | if (py < 0.5) |
| 92 | py = 0.5; |
| 93 | if (px > smoke->width - 0.5) |
| 94 | px = smoke->width - 0.5; |
| 95 | if (py > smoke->height - 0.5) |
| 96 | py = smoke->height - 0.5; |
| 97 | i = (int) px; |
| 98 | j = (int) py; |
| 99 | fx = px - i; |
| 100 | fy = py - j; |
| 101 | s = source + j * stride + i; |
| 102 | d[x] = (s[0] * (1 - fx) + s[1] * fx) * (1 - fy) + |
| 103 | (s[stride] * (1 - fx) + s[stride + 1] * fx) * fy; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | static void project(struct smoke *smoke, uint32_t time, |
| 109 | float *u, float *v, float *p, float *div) |
| 110 | { |
| 111 | int x, y, k, l, s; |
Bryce Harrington | 40269a6 | 2010-11-19 12:15:36 -0800 | [diff] [blame] | 112 | float h; |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 113 | |
| 114 | h = 1.0 / smoke->width; |
| 115 | s = smoke->width; |
| 116 | memset(p, 0, smoke->height * smoke->width); |
| 117 | for (y = 1; y < smoke->height - 1; y++) { |
| 118 | l = y * s; |
| 119 | for (x = 1; x < smoke->width - 1; x++) { |
| 120 | div[l + x] = -0.5 * h * (u[l + x + 1] - u[l + x - 1] + |
| 121 | v[l + x + s] - v[l + x - s]); |
| 122 | p[l + x] = 0; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | for (k = 0; k < 5; k++) { |
| 127 | for (y = 1; y < smoke->height - 1; y++) { |
| 128 | l = y * s; |
| 129 | for (x = 1; x < smoke->width - 1; x++) { |
| 130 | p[l + x] = (div[l + x] + |
| 131 | p[l + x - 1] + |
| 132 | p[l + x + 1] + |
| 133 | p[l + x - s] + |
| 134 | p[l + x + s]) / 4; |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | for (y = 1; y < smoke->height - 1; y++) { |
| 140 | l = y * s; |
| 141 | for (x = 1; x < smoke->width - 1; x++) { |
| 142 | u[l + x] -= 0.5 * (p[l + x + 1] - p[l + x - 1]) / h; |
| 143 | v[l + x] -= 0.5 * (p[l + x + s] - p[l + x - s]) / h; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
Kristian Høgsberg | 0212723 | 2012-02-08 14:47:53 -0500 | [diff] [blame^] | 148 | static void render(struct smoke *smoke, cairo_surface_t *surface) |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 149 | { |
Bryce Harrington | 40269a6 | 2010-11-19 12:15:36 -0800 | [diff] [blame] | 150 | unsigned char *dest; |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 151 | int x, y, width, height, stride; |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 152 | float *s; |
| 153 | uint32_t *d, c, a; |
| 154 | |
Kristian Høgsberg | 0212723 | 2012-02-08 14:47:53 -0500 | [diff] [blame^] | 155 | dest = cairo_image_surface_get_data(surface); |
| 156 | width = cairo_image_surface_get_width(surface); |
| 157 | height = cairo_image_surface_get_height(surface); |
| 158 | stride = cairo_image_surface_get_stride(surface); |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 159 | |
| 160 | for (y = 1; y < height - 1; y++) { |
| 161 | s = smoke->b[smoke->current].d + y * smoke->height; |
| 162 | d = (uint32_t *) (dest + y * stride); |
| 163 | for (x = 1; x < width - 1; x++) { |
| 164 | c = (int) (s[x] * 800); |
| 165 | if (c > 255) |
| 166 | c = 255; |
| 167 | a = c; |
| 168 | if (a < 0x33) |
| 169 | a = 0x33; |
| 170 | d[x] = (a << 24) | (c << 16) | (c << 8) | c; |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | static void |
Kristian Høgsberg | a8d1fa7 | 2011-08-23 18:14:06 -0400 | [diff] [blame] | 176 | frame_callback(void *data, struct wl_callback *callback, uint32_t time) |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 177 | { |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 178 | struct smoke *smoke = data; |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 179 | |
Kristian Høgsberg | 0212723 | 2012-02-08 14:47:53 -0500 | [diff] [blame^] | 180 | window_schedule_redraw(smoke->window); |
| 181 | smoke->time = time; |
| 182 | |
| 183 | if (callback) |
| 184 | wl_callback_destroy(callback); |
| 185 | } |
| 186 | |
| 187 | static const struct wl_callback_listener listener = { |
| 188 | frame_callback, |
| 189 | }; |
| 190 | |
| 191 | static void |
| 192 | redraw_handler(struct widget *widget, void *data) |
| 193 | { |
| 194 | struct smoke *smoke = data; |
| 195 | uint32_t time = smoke->time; |
| 196 | struct wl_callback *callback; |
| 197 | cairo_surface_t *surface; |
| 198 | |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 199 | diffuse(smoke, time / 30, smoke->b[0].u, smoke->b[1].u); |
| 200 | diffuse(smoke, time / 30, smoke->b[0].v, smoke->b[1].v); |
| 201 | project(smoke, time / 30, |
| 202 | smoke->b[1].u, smoke->b[1].v, |
| 203 | smoke->b[0].u, smoke->b[0].v); |
| 204 | advect(smoke, time / 30, |
| 205 | smoke->b[1].u, smoke->b[1].v, |
| 206 | smoke->b[1].u, smoke->b[0].u); |
| 207 | advect(smoke, time / 30, |
| 208 | smoke->b[1].u, smoke->b[1].v, |
| 209 | smoke->b[1].v, smoke->b[0].v); |
| 210 | project(smoke, time / 30, |
| 211 | smoke->b[0].u, smoke->b[0].v, |
| 212 | smoke->b[1].u, smoke->b[1].v); |
| 213 | |
| 214 | diffuse(smoke, time / 30, smoke->b[0].d, smoke->b[1].d); |
| 215 | advect(smoke, time / 30, |
| 216 | smoke->b[0].u, smoke->b[0].v, |
| 217 | smoke->b[1].d, smoke->b[0].d); |
| 218 | |
Kristian Høgsberg | 0212723 | 2012-02-08 14:47:53 -0500 | [diff] [blame^] | 219 | surface = window_get_surface(smoke->window); |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 220 | |
Kristian Høgsberg | 0212723 | 2012-02-08 14:47:53 -0500 | [diff] [blame^] | 221 | render(smoke, surface); |
Kristian Høgsberg | a8d1fa7 | 2011-08-23 18:14:06 -0400 | [diff] [blame] | 222 | |
Kristian Høgsberg | 0212723 | 2012-02-08 14:47:53 -0500 | [diff] [blame^] | 223 | display_surface_damage(smoke->display, surface, |
Benjamin Franzke | bde55ec | 2011-03-07 15:08:09 +0100 | [diff] [blame] | 224 | 0, 0, smoke->width, smoke->height); |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 225 | window_damage(smoke->window, 0, 0, smoke->width, smoke->height); |
Kristian Høgsberg | a8d1fa7 | 2011-08-23 18:14:06 -0400 | [diff] [blame] | 226 | |
Kristian Høgsberg | 0212723 | 2012-02-08 14:47:53 -0500 | [diff] [blame^] | 227 | cairo_surface_destroy(surface); |
| 228 | |
Kristian Høgsberg | a8d1fa7 | 2011-08-23 18:14:06 -0400 | [diff] [blame] | 229 | callback = wl_surface_frame(window_get_wl_surface(smoke->window)); |
| 230 | wl_callback_add_listener(callback, &listener, smoke); |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | static int |
Kristian Høgsberg | 87a57bb | 2012-01-09 10:34:35 -0500 | [diff] [blame] | 234 | smoke_motion_handler(struct widget *widget, struct input *input, |
| 235 | uint32_t time, int32_t x, int32_t y, void *data) |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 236 | { |
| 237 | struct smoke *smoke = data; |
| 238 | int i, i0, i1, j, j0, j1, k, d = 5; |
| 239 | |
Kristian Høgsberg | 87a57bb | 2012-01-09 10:34:35 -0500 | [diff] [blame] | 240 | if (x - d < 1) |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 241 | i0 = 1; |
| 242 | else |
Kristian Høgsberg | 87a57bb | 2012-01-09 10:34:35 -0500 | [diff] [blame] | 243 | i0 = x - d; |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 244 | if (i0 + 2 * d > smoke->width - 1) |
| 245 | i1 = smoke->width - 1; |
| 246 | else |
| 247 | i1 = i0 + 2 * d; |
| 248 | |
Kristian Høgsberg | 87a57bb | 2012-01-09 10:34:35 -0500 | [diff] [blame] | 249 | if (y - d < 1) |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 250 | j0 = 1; |
| 251 | else |
Kristian Høgsberg | 87a57bb | 2012-01-09 10:34:35 -0500 | [diff] [blame] | 252 | j0 = y - d; |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 253 | if (j0 + 2 * d > smoke->height - 1) |
| 254 | j1 = smoke->height - 1; |
| 255 | else |
| 256 | j1 = j0 + 2 * d; |
| 257 | |
| 258 | for (i = i0; i < i1; i++) |
| 259 | for (j = j0; j < j1; j++) { |
| 260 | k = j * smoke->width + i; |
| 261 | smoke->b[0].u[k] += 256 - (random() & 512); |
| 262 | smoke->b[0].v[k] += 256 - (random() & 512); |
| 263 | smoke->b[0].d[k] += 1; |
| 264 | } |
| 265 | |
| 266 | return POINTER_HAND1; |
| 267 | } |
| 268 | |
Juan Zhao | 6665063 | 2012-02-02 16:02:06 -0800 | [diff] [blame] | 269 | static void |
| 270 | resize_handler(struct widget *widget, |
| 271 | int32_t width, int32_t height, void *data) |
| 272 | { |
| 273 | struct smoke *smoke = data; |
| 274 | |
| 275 | /* Dont resize me */ |
| 276 | widget_set_size(smoke->widget, smoke->width, smoke->height); |
| 277 | } |
| 278 | |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 279 | int main(int argc, char *argv[]) |
| 280 | { |
| 281 | struct timespec ts; |
| 282 | struct smoke smoke; |
| 283 | struct display *d; |
Bryce Harrington | 40269a6 | 2010-11-19 12:15:36 -0800 | [diff] [blame] | 284 | int size; |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 285 | |
Kristian Høgsberg | 9de79a9 | 2011-08-24 11:09:53 -0400 | [diff] [blame] | 286 | d = display_create(&argc, &argv, NULL); |
Yuval Fledel | e9f5e36 | 2010-11-22 21:34:19 +0200 | [diff] [blame] | 287 | if (d == NULL) { |
| 288 | fprintf(stderr, "failed to create display: %m\n"); |
| 289 | return -1; |
| 290 | } |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 291 | |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 292 | smoke.width = 200; |
| 293 | smoke.height = 200; |
| 294 | smoke.display = d; |
Kristian Høgsberg | 009ac0a | 2012-01-31 15:24:48 -0500 | [diff] [blame] | 295 | smoke.window = window_create(d); |
Kristian Høgsberg | 75bc667 | 2012-01-10 09:43:58 -0500 | [diff] [blame] | 296 | smoke.widget = window_add_widget(smoke.window, &smoke); |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 297 | window_set_title(smoke.window, "smoke"); |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 298 | |
| 299 | window_set_buffer_type(smoke.window, WINDOW_BUFFER_TYPE_SHM); |
| 300 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 301 | srandom(ts.tv_nsec); |
| 302 | smoke.offset = random(); |
| 303 | |
| 304 | smoke.current = 0; |
| 305 | size = smoke.height * smoke.width; |
| 306 | smoke.b[0].d = calloc(size, sizeof(float)); |
| 307 | smoke.b[0].u = calloc(size, sizeof(float)); |
| 308 | smoke.b[0].v = calloc(size, sizeof(float)); |
| 309 | smoke.b[1].d = calloc(size, sizeof(float)); |
| 310 | smoke.b[1].u = calloc(size, sizeof(float)); |
| 311 | smoke.b[1].v = calloc(size, sizeof(float)); |
| 312 | |
Kristian Høgsberg | 75bc667 | 2012-01-10 09:43:58 -0500 | [diff] [blame] | 313 | widget_set_motion_handler(smoke.widget, smoke_motion_handler); |
Juan Zhao | 6665063 | 2012-02-02 16:02:06 -0800 | [diff] [blame] | 314 | widget_set_resize_handler(smoke.widget, resize_handler); |
Kristian Høgsberg | 0212723 | 2012-02-08 14:47:53 -0500 | [diff] [blame^] | 315 | widget_set_redraw_handler(smoke.widget, redraw_handler); |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 316 | |
| 317 | window_set_user_data(smoke.window, &smoke); |
Kristian Høgsberg | 0212723 | 2012-02-08 14:47:53 -0500 | [diff] [blame^] | 318 | |
| 319 | widget_schedule_resize(smoke.widget, smoke.width, smoke.height); |
| 320 | |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 321 | display_run(d); |
| 322 | |
| 323 | return 0; |
| 324 | } |