|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2022 Overrun Organization |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package org.overrun.swgl.core.level; |
| 26 | + |
| 27 | +import org.overrun.swgl.core.asset.tex.Texture; |
| 28 | +import org.overrun.swgl.core.gl.ITessCallback; |
| 29 | +import org.overrun.swgl.core.util.FloatPair; |
| 30 | + |
| 31 | +/** |
| 32 | + * The sprite drawer. |
| 33 | + * |
| 34 | + * @author squid233 |
| 35 | + * @since 0.1.0 |
| 36 | + */ |
| 37 | +public class SpriteDrawer { |
| 38 | + /** |
| 39 | + * Draw a sprite from a callback. |
| 40 | + * |
| 41 | + * @param x the pos x |
| 42 | + * @param y the pos y |
| 43 | + * @param w the size width |
| 44 | + * @param h the size height |
| 45 | + * @param texU the sprite UV-coord u |
| 46 | + * @param texV the sprite UV-coord v |
| 47 | + * @param spriteW the sprite size width |
| 48 | + * @param spriteH the sprite size height |
| 49 | + * @param texW the texture size width |
| 50 | + * @param texH the texture size width |
| 51 | + * @param flipY flip y axis to [bottom-to-up] |
| 52 | + * @param cb the callback, color is always white and normal is always positive-z |
| 53 | + */ |
| 54 | + public static void draw( |
| 55 | + float x, |
| 56 | + float y, |
| 57 | + float w, |
| 58 | + float h, |
| 59 | + float texU, |
| 60 | + float texV, |
| 61 | + float spriteW, |
| 62 | + float spriteH, |
| 63 | + float texW, |
| 64 | + float texH, |
| 65 | + boolean flipY, |
| 66 | + ITessCallback cb |
| 67 | + ) { |
| 68 | + float x1 = x + w; |
| 69 | + float y0 = flipY ? (y + h) : y; |
| 70 | + float y1 = flipY ? y : (y + h); |
| 71 | + float u0 = texU / texW; |
| 72 | + float u1 = (texU + spriteW) / texW; |
| 73 | + float v0 = texV / texH; |
| 74 | + float v1 = (texV + spriteH) / texH; |
| 75 | + |
| 76 | + cb.emit(x, y0, 0, 1, |
| 77 | + 1, 1, 1, 1, |
| 78 | + u0, v0, 0, 1, |
| 79 | + 0, 0, 1); |
| 80 | + cb.emit(x, y1, 0, 1, |
| 81 | + 1, 1, 1, 1, |
| 82 | + u0, v1, 0, 1, |
| 83 | + 0, 0, 1); |
| 84 | + cb.emit(x1, y1, 0, 1, |
| 85 | + 1, 1, 1, 1, |
| 86 | + u1, v1, 0, 1, |
| 87 | + 0, 0, 1); |
| 88 | + cb.emit(x1, y0, 0, 1, |
| 89 | + 1, 1, 1, 1, |
| 90 | + u1, v0, 0, 1, |
| 91 | + 0, 0, 1); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Draw a sprite from a callback. |
| 96 | + * |
| 97 | + * @param texture the texture which has size |
| 98 | + * @param x the pos x |
| 99 | + * @param y the pos y |
| 100 | + * @param w the size width |
| 101 | + * @param h the size height |
| 102 | + * @param texU the sprite UV-coord u |
| 103 | + * @param texV the sprite UV-coord v |
| 104 | + * @param spriteW the sprite size width |
| 105 | + * @param spriteH the sprite size height |
| 106 | + * @param flipY flip y axis to [bottom-to-up] |
| 107 | + * @param cb the callback, color is always white and normal is always positive-z |
| 108 | + * @see #draw(float, float, float, float, float, float, float, float, float, float, boolean, ITessCallback) |
| 109 | + */ |
| 110 | + public static void draw( |
| 111 | + Texture texture, |
| 112 | + float x, |
| 113 | + float y, |
| 114 | + float w, |
| 115 | + float h, |
| 116 | + float texU, |
| 117 | + float texV, |
| 118 | + float spriteW, |
| 119 | + float spriteH, |
| 120 | + boolean flipY, |
| 121 | + ITessCallback cb |
| 122 | + ) { |
| 123 | + draw(x, y, w, h, texU, texV, spriteW, spriteH, texture.getWidth(), texture.getHeight(), flipY, cb); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Draw a sprite from a callback. |
| 128 | + * |
| 129 | + * @param pos the pos |
| 130 | + * @param size the size |
| 131 | + * @param texUV the sprite UV-coord |
| 132 | + * @param spriteSize the sprite size |
| 133 | + * @param texSize the texture size |
| 134 | + * @param flipY flip y axis to [bottom-to-up] |
| 135 | + * @param cb the callback, color is always white and normal is always positive-z |
| 136 | + * @see #draw(float, float, float, float, float, float, float, float, float, float, boolean, ITessCallback) |
| 137 | + */ |
| 138 | + public static void draw( |
| 139 | + FloatPair pos, |
| 140 | + FloatPair size, |
| 141 | + FloatPair texUV, |
| 142 | + FloatPair spriteSize, |
| 143 | + FloatPair texSize, |
| 144 | + boolean flipY, |
| 145 | + ITessCallback cb |
| 146 | + ) { |
| 147 | + float x = pos.leftFloat(); |
| 148 | + float y = pos.rightFloat(); |
| 149 | + float w = size.leftFloat(); |
| 150 | + float h = size.rightFloat(); |
| 151 | + float texU = texUV.leftFloat(); |
| 152 | + float texV = texUV.rightFloat(); |
| 153 | + float spriteW = spriteSize.leftFloat(); |
| 154 | + float spriteH = spriteSize.rightFloat(); |
| 155 | + float texW = texSize.leftFloat(); |
| 156 | + float texH = texSize.rightFloat(); |
| 157 | + |
| 158 | + draw(x, y, w, h, texU, texV, spriteW, spriteH, texW, texH, flipY, cb); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Draw a sprite from a callback. |
| 163 | + * |
| 164 | + * @param texture the texture which has size |
| 165 | + * @param pos the pos |
| 166 | + * @param size the size |
| 167 | + * @param texUV the sprite UV-coord |
| 168 | + * @param spriteSize the sprite size |
| 169 | + * @param flipY flip y axis to [bottom-to-up] |
| 170 | + * @param cb the callback, color is always white and normal is always positive-z |
| 171 | + * @see #draw(float, float, float, float, float, float, float, float, float, float, boolean, ITessCallback) |
| 172 | + */ |
| 173 | + public static void draw( |
| 174 | + Texture texture, |
| 175 | + FloatPair pos, |
| 176 | + FloatPair size, |
| 177 | + FloatPair texUV, |
| 178 | + FloatPair spriteSize, |
| 179 | + boolean flipY, |
| 180 | + ITessCallback cb |
| 181 | + ) { |
| 182 | + float x = pos.leftFloat(); |
| 183 | + float y = pos.rightFloat(); |
| 184 | + float w = size.leftFloat(); |
| 185 | + float h = size.rightFloat(); |
| 186 | + float texU = texUV.leftFloat(); |
| 187 | + float texV = texUV.rightFloat(); |
| 188 | + float spriteW = spriteSize.leftFloat(); |
| 189 | + float spriteH = spriteSize.rightFloat(); |
| 190 | + float texW = texture.getWidth(); |
| 191 | + float texH = texture.getHeight(); |
| 192 | + |
| 193 | + draw(x, y, w, h, texU, texV, spriteW, spriteH, texW, texH, flipY, cb); |
| 194 | + } |
| 195 | +} |
0 commit comments