Adjusted for the web port

This commit is contained in:
2025-04-14 17:05:14 +03:00
parent 44546b1d72
commit f6d85c5315
19 changed files with 598 additions and 35 deletions

View File

@@ -24,6 +24,8 @@ package com.watabou.glwrap;
import com.badlogic.gdx.Gdx;
import java.nio.FloatBuffer;
import com.badlogic.gdx.graphics.GL20;
import com.watabou.utils.DeviceCompat;
public class Attribute {
@@ -36,20 +38,22 @@ public class Attribute {
public int location() {
return location;
}
private static final GL20 activeGL = (DeviceCompat.isWeb()) ? Gdx.gl30 : Gdx.gl;
public void enable() {
Gdx.gl.glEnableVertexAttribArray( location );
activeGL.glEnableVertexAttribArray( location );
}
public void disable() {
Gdx.gl.glDisableVertexAttribArray( location );
activeGL.glDisableVertexAttribArray( location );
}
public void vertexPointer( int size, int stride, FloatBuffer ptr ) {
Gdx.gl.glVertexAttribPointer( location, size, Gdx.gl.GL_FLOAT, false, stride * 4, ptr );
activeGL.glVertexAttribPointer( location, size, GL20.GL_FLOAT, false, stride * 4, ptr );
}
public void vertexBuffer( int size, int stride, int offset) {
Gdx.gl.glVertexAttribPointer(location, size, Gdx.gl.GL_FLOAT, false, stride * 4, offset * 4);
activeGL.glVertexAttribPointer( location, size, GL20.GL_FLOAT, false, stride * 4, offset * 4 );
}
}

View File

@@ -22,6 +22,8 @@
package com.watabou.noosa;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.watabou.utils.DeviceCompat;
import com.watabou.glscripts.Script;
import com.watabou.glwrap.Attribute;
import com.watabou.glwrap.Quad;
@@ -34,6 +36,8 @@ import java.nio.ShortBuffer;
public class NoosaScript extends Script {
private static final GL20 activeGL = (DeviceCompat.isWeb()) ? Gdx.gl30 : Gdx.gl20;
public Uniform uCamera;
public Uniform uModel;
public Uniform uTex;
@@ -44,6 +48,7 @@ public class NoosaScript extends Script {
private Camera lastCamera;
private int vertexBufferId;
public NoosaScript() {
super();
@@ -60,6 +65,12 @@ public class NoosaScript extends Script {
Quad.setupIndices();
Quad.bindIndices();
if (DeviceCompat.isWeb()) {
vertexBufferId = Gdx.gl30.glGenBuffer();
Gdx.gl30.glBindBuffer(Gdx.gl30.GL_ARRAY_BUFFER, vertexBufferId);
Gdx.gl30.glBufferData(Gdx.gl30.GL_ARRAY_BUFFER, 16384, null, Gdx.gl30.GL_DYNAMIC_DRAW);
Gdx.gl30.glBindBuffer(Gdx.gl30.GL_ARRAY_BUFFER, 0);
}
}
@Override
@@ -73,27 +84,50 @@ public class NoosaScript extends Script {
}
public void drawElements( FloatBuffer vertices, ShortBuffer indices, int size ) {
if (DeviceCompat.isWeb()) {
Gdx.gl30.glBindBuffer(Gdx.gl30.GL_ARRAY_BUFFER, vertexBufferId);
((Buffer)vertices).position( 0 );
Gdx.gl30.glBufferSubData(Gdx.gl30.GL_ARRAY_BUFFER, 0, vertices.remaining() * 4, vertices);
((Buffer)vertices).position( 0 );
aXY.vertexPointer( 2, 4, vertices );
aXY.vertexBuffer(2, 4, 0);
aUV.vertexBuffer(2, 4, 2);
((Buffer)vertices).position( 2 );
aUV.vertexPointer( 2, 4, vertices );
Quad.releaseIndices();
Gdx.gl30.glDrawElements( Gdx.gl30.GL_TRIANGLES, size, Gdx.gl30.GL_UNSIGNED_SHORT, indices );
Quad.bindIndices();
Gdx.gl30.glBindBuffer(Gdx.gl30.GL_ARRAY_BUFFER, 0);
} else {
((Buffer)vertices).position( 0 );
aXY.vertexPointer( 2, 4, vertices );
Quad.releaseIndices();
Gdx.gl20.glDrawElements( Gdx.gl20.GL_TRIANGLES, size, Gdx.gl20.GL_UNSIGNED_SHORT, indices );
Quad.bindIndices();
((Buffer)vertices).position( 2 );
aUV.vertexPointer( 2, 4, vertices );
Quad.releaseIndices();
Gdx.gl20.glDrawElements( Gdx.gl20.GL_TRIANGLES, size, Gdx.gl20.GL_UNSIGNED_SHORT, indices );
Quad.bindIndices();
}
}
public void drawQuad( FloatBuffer vertices ) {
if (DeviceCompat.isWeb()) {
Gdx.gl30.glBindBuffer(Gdx.gl30.GL_ARRAY_BUFFER, vertexBufferId);
((Buffer)vertices).position( 0 );
Gdx.gl30.glBufferSubData(Gdx.gl30.GL_ARRAY_BUFFER, 0, vertices.remaining() * 4, vertices);
((Buffer)vertices).position( 0 );
aXY.vertexPointer( 2, 4, vertices );
Gdx.gl30.glVertexAttribPointer(aXY.location(), 2, Gdx.gl30.GL_FLOAT, false, 4 * 4, 0);
Gdx.gl30.glVertexAttribPointer(aUV.location(), 2, Gdx.gl30.GL_FLOAT, false, 4 * 4, 2 * 4);
Gdx.gl30.glDrawElements( Gdx.gl30.GL_TRIANGLES, Quad.SIZE, Gdx.gl30.GL_UNSIGNED_SHORT, 0 );
Gdx.gl30.glBindBuffer(Gdx.gl30.GL_ARRAY_BUFFER, 0);
} else {
((Buffer)vertices).position( 0 );
aXY.vertexPointer( 2, 4, vertices );
((Buffer)vertices).position( 2 );
aUV.vertexPointer( 2, 4, vertices );
((Buffer)vertices).position( 2 );
aUV.vertexPointer( 2, 4, vertices );
Gdx.gl20.glDrawElements( Gdx.gl20.GL_TRIANGLES, Quad.SIZE, Gdx.gl20.GL_UNSIGNED_SHORT, 0 );
Gdx.gl20.glDrawElements( Gdx.gl20.GL_TRIANGLES, Quad.SIZE, Gdx.gl20.GL_UNSIGNED_SHORT, 0 );
}
}
public void drawQuad( Vertexbuffer buffer ) {
@@ -107,7 +141,7 @@ public class NoosaScript extends Script {
buffer.release();
Gdx.gl20.glDrawElements( Gdx.gl20.GL_TRIANGLES, Quad.SIZE, Gdx.gl20.GL_UNSIGNED_SHORT, 0 );
activeGL.glDrawElements( GL20.GL_TRIANGLES, Quad.SIZE, GL20.GL_UNSIGNED_SHORT, 0 );
}
public void drawQuadSet( FloatBuffer vertices, int size ) {
@@ -116,13 +150,26 @@ public class NoosaScript extends Script {
return;
}
((Buffer)vertices).position( 0 );
aXY.vertexPointer( 2, 4, vertices );
if (DeviceCompat.isWeb()) {
Gdx.gl30.glBindBuffer(Gdx.gl30.GL_ARRAY_BUFFER, vertexBufferId);
((Buffer)vertices).position( 0 );
Gdx.gl30.glBufferSubData(Gdx.gl30.GL_ARRAY_BUFFER, 0, vertices.remaining() * 4, vertices);
((Buffer)vertices).position( 2 );
aUV.vertexPointer( 2, 4, vertices );
Gdx.gl30.glVertexAttribPointer(aXY.location(), 2, Gdx.gl30.GL_FLOAT, false, 4 * 4, 0);
Gdx.gl30.glVertexAttribPointer(aUV.location(), 2, Gdx.gl30.GL_FLOAT, false, 4 * 4, 2 * 4);
Gdx.gl30.glDrawElements( Gdx.gl30.GL_TRIANGLES, Quad.SIZE * size, Gdx.gl30.GL_UNSIGNED_SHORT, 0 );
Gdx.gl30.glBindBuffer(Gdx.gl30.GL_ARRAY_BUFFER, 0);
} else {
((Buffer)vertices).position( 0 );
aXY.vertexPointer( 2, 4, vertices );
((Buffer)vertices).position( 2 );
aUV.vertexPointer( 2, 4, vertices );
Gdx.gl20.glDrawElements( Gdx.gl20.GL_TRIANGLES, Quad.SIZE * size, Gdx.gl20.GL_UNSIGNED_SHORT, 0 );
Gdx.gl20.glDrawElements( Gdx.gl20.GL_TRIANGLES, Quad.SIZE * size, Gdx.gl20.GL_UNSIGNED_SHORT, 0 );
}
}
public void drawQuadSet( Vertexbuffer buffer, int length, int offset ){
@@ -140,7 +187,7 @@ public class NoosaScript extends Script {
buffer.release();
Gdx.gl20.glDrawElements( Gdx.gl20.GL_TRIANGLES, Quad.SIZE * length, Gdx.gl20.GL_UNSIGNED_SHORT, Quad.SIZE * Short.SIZE/8 * offset );
activeGL.glDrawElements( GL20.GL_TRIANGLES, Quad.SIZE * length, GL20.GL_UNSIGNED_SHORT, Quad.SIZE * Short.SIZE/8 * offset );
}
public void lighting( float rm, float gm, float bm, float am, float ra, float ga, float ba, float aa ) {
@@ -161,7 +208,7 @@ public class NoosaScript extends Script {
uCamera.valueM4( camera.matrix );
if (!camera.fullScreen) {
Gdx.gl20.glEnable( Gdx.gl20.GL_SCISSOR_TEST );
activeGL.glEnable( GL20.GL_SCISSOR_TEST );
//This fixes pixel scaling issues on some hidpi displays (mainly on macOS)
// because for some reason all other openGL operations work on virtual pixels
@@ -169,13 +216,13 @@ public class NoosaScript extends Script {
float xScale = (Gdx.graphics.getBackBufferWidth() / (float)Game.width );
float yScale = ((Gdx.graphics.getBackBufferHeight()-Game.bottomInset) / (float)Game.height );
Gdx.gl20.glScissor(
activeGL.glScissor(
Math.round(camera.x * xScale),
Math.round((Game.height - camera.screenHeight - camera.y) * yScale) + Game.bottomInset,
Math.round(camera.screenWidth * xScale),
Math.round(camera.screenHeight * yScale));
} else {
Gdx.gl20.glDisable( Gdx.gl20.GL_SCISSOR_TEST );
activeGL.glDisable( GL20.GL_SCISSOR_TEST );
}
}
}
@@ -189,8 +236,41 @@ public class NoosaScript extends Script {
return SHADER;
}
private static final String SHADER =
private static String SHADER;
static {
if (DeviceCompat.isWeb()) {
SHADER =
//vertex shader
"#version 300 es\n" +
"uniform mat4 uCamera;\n" +
"uniform mat4 uModel;\n" +
"in vec4 aXYZW;\n" +
"in vec2 aUV;\n" +
"out vec2 vUV;\n" +
"void main() {\n" +
" gl_Position = uCamera * uModel * aXYZW;\n" +
" vUV = aUV;\n" +
"}\n" +
//this symbol separates the vertex and fragment shaders (see Script.compile)
"//\n" +
//fragment shader
//preprocessor directives let us define precision on GLES platforms, and ignore it elsewhere
"#version 300 es\n" +
"#ifdef GL_ES\n" +
" precision mediump float;\n" +
"#endif\n" +
"in vec2 vUV;\n" +
"uniform sampler2D uTex;\n" +
"uniform vec4 uColorM;\n" +
"uniform vec4 uColorA;\n" +
"out vec4 fragColor;\n" +
"void main() {\n" +
" fragColor = texture( uTex, vUV ) * uColorM + uColorA;\n" +
"}\n";
} else {
SHADER =
//vertex shader
"uniform mat4 uCamera;\n" +
"uniform mat4 uModel;\n" +
@@ -217,4 +297,6 @@ public class NoosaScript extends Script {
"void main() {\n" +
" gl_FragColor = texture2D( uTex, vUV ) * uColorM + uColorA;\n" +
"}\n";
}
}
}

View File

@@ -22,6 +22,7 @@
package com.watabou.noosa;
import com.watabou.glscripts.Script;
import com.watabou.utils.DeviceCompat;
//This class should be used on heavy pixel-fill based loads when lighting is not needed.
// It skips the lighting component of the fragment shader, giving a significant performance boost
@@ -44,8 +45,40 @@ public class NoosaScriptNoLighting extends NoosaScript {
return SHADER;
}
private static final String SHADER =
private static String SHADER;
static {
if (DeviceCompat.isWeb()) {
SHADER =
//vertex shader
"#version 300 es\n" +
"uniform mat4 uCamera;\n" +
"uniform mat4 uModel;\n" +
"in vec4 aXYZW;\n" +
"in vec2 aUV;\n" +
"out vec2 vUV;\n" +
"void main() {\n" +
" gl_Position = uCamera * uModel * aXYZW;\n" +
" vUV = aUV;\n" +
"}\n" +
//this symbol separates the vertex and fragment shaders (see Script.compile)
"//\n" +
//fragment shader
//preprocessor directives let us define precision on GLES platforms, and ignore it elsewhere
"#version 300 es\n" +
"#ifdef GL_ES\n" +
" precision mediump float;\n" +
"#endif\n" +
"in vec2 vUV;\n" +
"uniform sampler2D uTex;\n" +
"out vec4 fragColor;\n" +
"void main() {\n" +
" fragColor = texture( uTex, vUV );\n" +
"}\n";
} else {
SHADER =
//vertex shader
"uniform mat4 uCamera;\n" +
"uniform mat4 uModel;\n" +
@@ -70,4 +103,6 @@ public class NoosaScriptNoLighting extends NoosaScript {
"void main() {\n" +
" gl_FragColor = texture2D( uTex, vUV );\n" +
"}\n";
}
}
}

View File

@@ -32,6 +32,7 @@ import com.badlogic.gdx.math.Affine2;
import com.badlogic.gdx.math.Matrix4;
import com.watabou.glwrap.Matrix;
import com.watabou.glwrap.Quad;
import com.watabou.utils.DeviceCompat;
import java.nio.Buffer;
import java.nio.FloatBuffer;
@@ -79,9 +80,11 @@ public class RenderedText extends Image {
private synchronized void measure(){
if (!DeviceCompat.isWeb()) {
if (Thread.currentThread().getName().equals("SHPD Actor Thread")){
throw new RuntimeException("Text measured from the actor thread!");
}
}
if ( text == null || text.equals("") ) {
text = "";

View File

@@ -27,6 +27,7 @@ import com.watabou.glwrap.Quad;
import com.watabou.glwrap.Vertexbuffer;
import com.watabou.utils.Rect;
import com.watabou.utils.RectF;
import com.watabou.utils.DeviceCompat;;
import java.nio.Buffer;
import java.nio.FloatBuffer;
@@ -197,7 +198,9 @@ public class Tilemap extends Visual {
public void draw() {
super.draw();
if (DeviceCompat.isWeb()) {
fullUpdate = true;
}
if (!updated.isEmpty()) {
updateVertices();
if (buffer == null)

View File

@@ -25,6 +25,7 @@ import com.badlogic.gdx.utils.JsonReader;
import com.badlogic.gdx.utils.JsonValue;
import com.badlogic.gdx.utils.JsonWriter;
import com.watabou.noosa.Game;
import com.watabou.utils.DeviceCompat;
import org.json.JSONArray;
import org.json.JSONException;
@@ -484,7 +485,15 @@ public class Bundle {
}
//useful to turn this off for save data debugging.
private static final boolean compressByDefault = true;
private static final boolean compressByDefault;
static {
if (DeviceCompat.isWeb()) {
compressByDefault = false;
} else {
compressByDefault = true;
}
}
private static final int GZIP_BUFFER = 1024*4; //4 kb

View File

@@ -25,6 +25,7 @@ import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.utils.SharedLibraryLoader;
import com.watabou.noosa.Game;
import com.badlogic.gdx.Application;
//TODO migrate to platformSupport class
public class DeviceCompat {
@@ -49,15 +50,22 @@ public class DeviceCompat {
}
public static boolean isAndroid(){
return SharedLibraryLoader.isAndroid;
return Gdx.app.getType() == Application.ApplicationType.Android;
}
public static boolean isiOS(){
return SharedLibraryLoader.isIos;
return Gdx.app.getType() == Application.ApplicationType.iOS;
}
public static boolean isDesktop(){
return SharedLibraryLoader.isWindows || SharedLibraryLoader.isMac || SharedLibraryLoader.isLinux;
return System.getProperty("os.name").toLowerCase().contains("win") ||
System.getProperty("os.name").toLowerCase().contains("mac") ||
System.getProperty("os.name").toLowerCase().contains("nux") ||
Gdx.app.getType() == Application.ApplicationType.WebGL;
}
public static boolean isWeb(){
return Gdx.app.getType() == Application.ApplicationType.WebGL;
}
public static boolean hasHardKeyboard(){