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