|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2018 Laurent Caron and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * Josh83 - Original Version |
| 13 | + * Laurent Caron (laurent.caron at gmail dot com) - Conversion to SWT |
| 14 | + * IBM Corporation - adaptation to GraphicsExample |
| 15 | + *******************************************************************************/ |
| 16 | + |
| 17 | +package org.eclipse.swt.examples.graphics; |
| 18 | + |
| 19 | +import org.eclipse.swt.SWT; |
| 20 | +import org.eclipse.swt.graphics.GC; |
| 21 | +import org.eclipse.swt.graphics.Image; |
| 22 | +import org.eclipse.swt.graphics.ImageData; |
| 23 | +import org.eclipse.swt.graphics.PaletteData; |
| 24 | +import org.eclipse.swt.graphics.RGB; |
| 25 | + |
| 26 | +/** |
| 27 | + * This tab displays an animated burning sea effect with fire reflections |
| 28 | + * on water, using sine-based wave distortion and random sparks. |
| 29 | + */ |
| 30 | +public class BurningSeaTab extends AnimatedGraphicsTab { |
| 31 | + |
| 32 | + private static final int YSCROLL = 10; |
| 33 | + |
| 34 | + private int[] sinp; |
| 35 | + private int[] ran1; |
| 36 | + private int[] xfall, yfall, sfall; |
| 37 | + private short t; |
| 38 | + private int r1; |
| 39 | + private ImageData imageData; |
| 40 | + private Image outputImage; |
| 41 | + private int lastWidth; |
| 42 | + private int lastHeight; |
| 43 | + |
| 44 | + public BurningSeaTab(GraphicsExample example) { |
| 45 | + super(example); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public String getCategory() { |
| 50 | + return GraphicsExample.getResourceString("Misc"); //$NON-NLS-1$ |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public String getText() { |
| 55 | + return GraphicsExample.getResourceString("BurningSea"); //$NON-NLS-1$ |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public String getDescription() { |
| 60 | + return GraphicsExample.getResourceString("BurningSeaDescription"); //$NON-NLS-1$ |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public int getInitialAnimationTime() { |
| 65 | + return 10; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void dispose() { |
| 70 | + if (outputImage != null) { |
| 71 | + outputImage.dispose(); |
| 72 | + outputImage = null; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void next(int width, int height) { |
| 78 | + if (sinp == null) { |
| 79 | + return; |
| 80 | + } |
| 81 | + if (width != lastWidth || height != lastHeight) { |
| 82 | + init(width, height); |
| 83 | + } |
| 84 | + |
| 85 | + t += 8; |
| 86 | + if (t > 30000) { |
| 87 | + t = 0; |
| 88 | + } |
| 89 | + |
| 90 | + // Generate fire at the horizon line |
| 91 | + int horizonLine = Math.min(height - 3, (int) (height * 0.65)); |
| 92 | + for (int x = 0; x < width; x++) { |
| 93 | + for (int y = horizonLine; y < Math.min(horizonLine + 3, height); y++) { |
| 94 | + imageData.setPixel(x, y, (int) (Math.random() * 100 + 80)); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // Animate falling sparks |
| 99 | + for (int i = 0; i < 10; i++) { |
| 100 | + yfall[i] += sfall[i]; |
| 101 | + if (yfall[i] > horizonLine - 5) { |
| 102 | + xfall[i] = (int) (Math.random() * width); |
| 103 | + yfall[i] = 1; |
| 104 | + sfall[i] = (int) (Math.random() * 4 + 1); |
| 105 | + } |
| 106 | + for (int x = 0; x < sfall[i] + 2; x++) { |
| 107 | + for (int y = 0; y < sfall[i] + 2; y++) { |
| 108 | + int px = x + xfall[i]; |
| 109 | + int py = y + yfall[i]; |
| 110 | + if (px >= 0 && px < width && py >= 0 && py < height) { |
| 111 | + imageData.setPixel(px, py, (int) (Math.random() * 50 + (sfall[i] << 5))); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // Fire propagation with blur |
| 118 | + for (int ab = width - 2; ab > 0; ab--) { |
| 119 | + for (int bb = horizonLine; bb > 0; bb--) { |
| 120 | + if (bb < YSCROLL - 1 || bb > YSCROLL + 12) { |
| 121 | + if (imageData.getPixel(ab, bb + 1) > 0 || imageData.getPixel(ab, bb) > 0) { |
| 122 | + r1++; |
| 123 | + if (r1 > 9000) { |
| 124 | + r1 = 0; |
| 125 | + } |
| 126 | + int cblur = imageData.getPixel(ab - 1, bb + 1) + imageData.getPixel(ab + 1, bb + 1) |
| 127 | + + ran1[r1] * imageData.getPixel(ab, bb + 1) + imageData.getPixel(ab, bb) >> 2; |
| 128 | + if (cblur > 190) { |
| 129 | + cblur = 190; |
| 130 | + } |
| 131 | + imageData.setPixel(ab, bb, cblur); |
| 132 | + } |
| 133 | + } |
| 134 | + if (imageData.getPixel(ab, bb) > 0) { |
| 135 | + imageData.setPixel(ab, bb, imageData.getPixel(ab, bb) - 1); |
| 136 | + } |
| 137 | + |
| 138 | + // Water reflection with sine distortion |
| 139 | + int abnew = ab + (sinp[(t + bb * 20) % 628] * (bb - (horizonLine - 20) >> 2) >> 7); |
| 140 | + int bbnew = (horizonLine * 2) - bb + 5; |
| 141 | + |
| 142 | + if (abnew > 0 && abnew < width && bbnew > 0 && bbnew < height) { |
| 143 | + imageData.setPixel(ab, bbnew, imageData.getPixel(Math.min(abnew, width - 1), bb) >> 1); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + // Clear sky area |
| 149 | + for (int x = 0; x < width; x++) { |
| 150 | + for (int y = 0; y < 30; y++) { |
| 151 | + imageData.setPixel(x, y, 0); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public void paint(GC gc, int width, int height) { |
| 158 | + if (sinp == null) { |
| 159 | + init(width, height); |
| 160 | + } |
| 161 | + |
| 162 | + gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_BLACK)); |
| 163 | + gc.fillRectangle(0, 0, width, height); |
| 164 | + |
| 165 | + if (imageData == null) { |
| 166 | + return; |
| 167 | + } |
| 168 | + |
| 169 | + if (outputImage != null) { |
| 170 | + outputImage.dispose(); |
| 171 | + } |
| 172 | + outputImage = new Image(gc.getDevice(), imageData); |
| 173 | + gc.drawImage(outputImage, 0, 0); |
| 174 | + } |
| 175 | + |
| 176 | + private void init(int width, int height) { |
| 177 | + lastWidth = width; |
| 178 | + lastHeight = height; |
| 179 | + |
| 180 | + sinp = new int[628]; |
| 181 | + for (int i = 0; i < 628; i++) { |
| 182 | + sinp[i] = (int) (Math.sin(i * 0.01) * 128); |
| 183 | + } |
| 184 | + |
| 185 | + ran1 = new int[10000]; |
| 186 | + for (int i = 0; i < 10000; i++) { |
| 187 | + ran1[i] = (int) (Math.random() * 3); |
| 188 | + } |
| 189 | + |
| 190 | + // Fire palette |
| 191 | + final RGB[] colors = new RGB[256]; |
| 192 | + int r = 0, g = 0, b = 0; |
| 193 | + for (int i = 0; i < 256; i++) { |
| 194 | + colors[i] = new RGB(0, 0, 0); |
| 195 | + } |
| 196 | + for (int i = 0; i < 64; i++) { |
| 197 | + colors[i] = new RGB(r, 0, 0); |
| 198 | + r++; |
| 199 | + } |
| 200 | + for (int i = 64; i < 128; i++) { |
| 201 | + colors[i] = new RGB(63, g, 0); |
| 202 | + g++; |
| 203 | + } |
| 204 | + for (int i = 128; i < 192; i++) { |
| 205 | + colors[i] = new RGB(63, 63, b); |
| 206 | + b++; |
| 207 | + } |
| 208 | + b = 0; |
| 209 | + for (int i = 192; i < 256; i++) { |
| 210 | + colors[i] = new RGB(0, 0, b); |
| 211 | + b++; |
| 212 | + } |
| 213 | + // Increase brightness |
| 214 | + for (int i = 1; i < 256; i++) { |
| 215 | + final RGB color = colors[i]; |
| 216 | + final float[] temp = color.getHSB(); |
| 217 | + colors[i] = new RGB(temp[0], temp[1], Math.min(1.0f, temp[2] * 4f)); |
| 218 | + } |
| 219 | + |
| 220 | + xfall = new int[10]; |
| 221 | + yfall = new int[10]; |
| 222 | + sfall = new int[10]; |
| 223 | + for (int x = 0; x < 10; x++) { |
| 224 | + xfall[x] = (int) (Math.random() * width); |
| 225 | + yfall[x] = (int) (Math.random() * 50); |
| 226 | + sfall[x] = (int) (Math.random() * 4) + 1; |
| 227 | + } |
| 228 | + |
| 229 | + imageData = new ImageData(width, height, 8, new PaletteData(colors)); |
| 230 | + t = 0; |
| 231 | + r1 = 0; |
| 232 | + } |
| 233 | +} |
0 commit comments