Skip to content
This repository was archived by the owner on Aug 27, 2024. It is now read-only.

Commit 313a4a0

Browse files
committed
0.1.0 🚀 added sprite drawer
1 parent a845094 commit 313a4a0

File tree

10 files changed

+440
-18
lines changed

10 files changed

+440
-18
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ org.gradle.jvmargs=-Dfile.encoding=UTF-8
33
projGroupId=io.github.over-run
44
projArtifactId=swgl-core
55
projName=swgl-core
6-
projVersion=0.1.0-SNAPSHOT
6+
projVersion=0.1.0
77
projDesc=swgl - A game engine
88
projVcs=Over-Run/swgl-core
99
projBranch=0.x

src/main/java/org/overrun/swgl/core/asset/tex/Texture.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,18 @@ public abstract class Texture extends Asset implements AutoCloseable {
5252
* Get the id of this texture.
5353
*/
5454
public abstract int getId();
55+
56+
/**
57+
* Get the texture width.
58+
*
59+
* @return the texture width
60+
*/
61+
public abstract int getWidth();
62+
63+
/**
64+
* Get the texture height.
65+
*
66+
* @return the texture height
67+
*/
68+
public abstract int getHeight();
5569
}

src/main/java/org/overrun/swgl/core/asset/tex/Texture2D.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ public boolean isFailed() {
269269
*
270270
* @return the texture width
271271
*/
272+
@Override
272273
public int getWidth() {
273274
return width;
274275
}
@@ -278,6 +279,7 @@ public int getWidth() {
278279
*
279280
* @return the texture height
280281
*/
282+
@Override
281283
public int getHeight() {
282284
return height;
283285
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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.gl;
26+
27+
/**
28+
* The tesselator callback to emit vertices.
29+
*
30+
* @author squid233
31+
* @since 0.1.0
32+
*/
33+
@FunctionalInterface
34+
public interface ITessCallback {
35+
/**
36+
* Emit a vertex.
37+
*
38+
* @param x the pos x
39+
* @param y the pos y
40+
* @param z the pos z
41+
* @param w the pos w
42+
* @param r the color red
43+
* @param g the color green
44+
* @param b the color blue
45+
* @param a the color alpha
46+
* @param s the texture-coord s
47+
* @param t the texture-coord t
48+
* @param p the texture-coord r
49+
* @param q the texture-coord q
50+
* @param nx the normal x
51+
* @param ny the normal y
52+
* @param nz the normal z
53+
*/
54+
void emit(float x, float y, float z, float w,
55+
float r, float g, float b, float a,
56+
float s, float t, float p, float q,
57+
float nx, float ny, float nz);
58+
}

src/main/java/org/overrun/swgl/core/io/HeapStackFrame.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
/**
3636
* The heap in auto-closeable.
3737
*
38-
* <h3>Example</h3>
38+
* <h2>Example</h2>
3939
* <pre>{@code try (var heap = new HeapStackFrame()) {
4040
* var bb = heap.utilMemAlloc(8196).put(bytes).flip();
4141
* ProcessBuffer(bb);
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
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+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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.util;
26+
27+
/**
28+
* @author squid233
29+
* @since 0.1.0
30+
*/
31+
public class FloatPair implements IPair<Float, Float> {
32+
private final float left, right;
33+
34+
/**
35+
* Construct a float pair.
36+
*
37+
* @param left The left number.
38+
* @param right The right number.
39+
*/
40+
public FloatPair(float left, float right) {
41+
this.left = left;
42+
this.right = right;
43+
}
44+
45+
/**
46+
* Get the left value.
47+
*
48+
* @return the left value
49+
*/
50+
public float leftFloat() {
51+
return left;
52+
}
53+
54+
/**
55+
* Get the right value.
56+
*
57+
* @return the right value
58+
*/
59+
public float rightFloat() {
60+
return right;
61+
}
62+
63+
/**
64+
* {@inheritDoc}
65+
*
66+
* @deprecated Please use {@link #leftFloat()}
67+
*/
68+
@Override
69+
@Deprecated(since = "0.1.0")
70+
public Float left() {
71+
return left;
72+
}
73+
74+
/**
75+
* {@inheritDoc}
76+
*
77+
* @deprecated Please use {@link #rightFloat()}
78+
*/
79+
@Override
80+
@Deprecated(since = "0.1.0")
81+
public Float right() {
82+
return right;
83+
}
84+
85+
@Override
86+
public String toString() {
87+
return IPair.toString(this);
88+
}
89+
}

0 commit comments

Comments
 (0)