From a23b40a0ec1319cdf49d33a4336702cdd57ab608 Mon Sep 17 00:00:00 2001 From: creedvi Date: Wed, 15 Dec 2021 21:49:56 -0500 Subject: [PATCH] rlgl, core update/improvement --- src/com/raylib/java/Config.java | 6 +- src/com/raylib/java/JarMainForTesting.java | 1 + src/com/raylib/java/core/Time.java | 2 + src/com/raylib/java/core/rCore.java | 124 +++++++++++++++---- src/com/raylib/java/rlgl/GL_33.java | 14 +-- src/com/raylib/java/rlgl/RLGL.java | 21 ++-- src/com/raylib/java/rlgl/data/State.java | 16 +-- src/com/raylib/java/rlgl/rlVertexBuffer.java | 8 +- 8 files changed, 142 insertions(+), 50 deletions(-) diff --git a/src/com/raylib/java/Config.java b/src/com/raylib/java/Config.java index 8bbc760..941a722 100644 --- a/src/com/raylib/java/Config.java +++ b/src/com/raylib/java/Config.java @@ -38,7 +38,7 @@ public class Config{ /** * Draw a mouse pointer on screen */ - static boolean SUPPORT_MOUSE_CURSOR_NATIVE = true; + public static boolean SUPPORT_MOUSE_CURSOR_POINT = true; /** * Use busy wait loop for timing sync, if not defined, a high-resolution timer is setup and used */ @@ -78,7 +78,7 @@ public class Config{ // Support custom frame control, only for advance users // By default EndDrawing() does this job: draws everything + SwapScreenBuffer() + manage frame timming + PollInputEvents() // Enabling this flag allows manual control of the frame processes, use at your own risk - //#define SUPPORT_CUSTOM_FRAME_CONTROL 1 + public static boolean SUPPORT_CUSTOM_FRAME_CONTROL = false; // core: Configuration values @@ -361,7 +361,7 @@ public void setSupportSshKeyboardRpi(boolean supportSshKeyboardRpi){ * @param supportMouseCursorNative flag for support */ public void setSupportMouseCursorNative(boolean supportMouseCursorNative){ - SUPPORT_MOUSE_CURSOR_NATIVE = supportMouseCursorNative; + SUPPORT_MOUSE_CURSOR_POINT = supportMouseCursorNative; } /** diff --git a/src/com/raylib/java/JarMainForTesting.java b/src/com/raylib/java/JarMainForTesting.java index f18b9bd..0ad6114 100644 --- a/src/com/raylib/java/JarMainForTesting.java +++ b/src/com/raylib/java/JarMainForTesting.java @@ -24,6 +24,7 @@ private static void update(){ private static void render(){ rlj.core.BeginDrawing(); rlj.core.ClearBackground(Color.WHITE); + rlj.shapes.DrawRectangle(0,0,200,200,Color.BLACK); rlj.text.DrawText("Hello, World!", 400 - (rlj.text.MeasureText("Hello, World!", 20)/2), 300, 20, Color.DARKGRAY); rlj.core.EndDrawing(); } diff --git a/src/com/raylib/java/core/Time.java b/src/com/raylib/java/core/Time.java index e7ef038..4335e31 100644 --- a/src/com/raylib/java/core/Time.java +++ b/src/com/raylib/java/core/Time.java @@ -9,6 +9,8 @@ public class Time{ double frame; // Time measure for one frame double target; // Desired time for one frame, if 0 not applied + int frameCounter; // Frame counter + public Time(){ } diff --git a/src/com/raylib/java/core/rCore.java b/src/com/raylib/java/core/rCore.java index b21cc47..1f061cc 100644 --- a/src/com/raylib/java/core/rCore.java +++ b/src/com/raylib/java/core/rCore.java @@ -36,7 +36,6 @@ import static com.raylib.java.Config.ConfigFlag.*; import static com.raylib.java.Config.*; -import static com.raylib.java.Config.RL_DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD; import static com.raylib.java.core.AutomationEvent.AutomationEventType.*; import static com.raylib.java.core.Color.RAYWHITE; import static com.raylib.java.core.camera.rCamera.CameraProjection.CAMERA_ORTHOGRAPHIC; @@ -854,44 +853,127 @@ public void ClearBackground(Color color){ * Setup canvas (framebuffer) to start drawing */ public void BeginDrawing(){ - time.setCurrent(GetTime()); // Number of elapsed seconds since InitTimer() - time.setUpdate(time.getCurrent() - time.getPrevious()); - time.setPrevious(time.getCurrent()); + // WARNING: Previously to BeginDrawing() other render textures drawing could happen, + // consequently the measure for update vs draw is not accurate (only the total frame time is accurate) - RLGL.rlLoadIdentity(); // Reset current matrix (modelview) - RLGL.rlMultMatrixf(MatrixToFloat(window.getScreenScale())); // Apply screen scaling + time.current = GetTime(); // Number of elapsed seconds since InitTimer() + time.update = time.current - time.previous; + time.previous = time.current; + + rlLoadIdentity(); // Reset current matrix (modelview) + rlMultMatrixf(MatrixToFloat(window.screenScale)); // Apply screen scaling //rlTranslatef(0.375, 0.375, 0); // HACK to have 2D pixel-perfect drawing on OpenGL 1.1 // NOTE: Not required with OpenGL 3.3+ + } /** * End canvas drawing and swap buffers (double buffering) */ public void EndDrawing(){ + rlgl.rlDrawRenderBatchActive(); // Update and draw internal render batch + + if(SUPPORT_MOUSE_CURSOR_POINT) { + // Draw a small rectangle on mouse position for user reference + /*TODO + if (!input.mouse.cursorHidden) { + rShapes.DrawRectangle((int)input.mouse.currentPosition.x, (int)input.mouse.currentPosition.y, 3, 3, Color.MAROON); + rlgl.rlDrawRenderBatchActive(); // Update and draw internal render batch + } + */ + } - rlgl.rlDrawRenderBatchActive(); // Draw Buffers (Only OpenGL 3+ and ES2) - SwapScreenBuffers(); // Copy back buffer to front buffer + if(SUPPORT_GIF_RECORDING) { + // Draw record indicator + /*TODO + if (gifRecording) { + #define GIF_RECORD_FRAMERATE 10 + gifFrameCounter++; - // Frame time control system - time.setCurrent(GetTime()); - time.setDraw(time.getCurrent() - time.getPrevious()); - time.previous = time.current; + // NOTE: We record one gif frame every 10 game frames + if ((gifFrameCounter%GIF_RECORD_FRAMERATE) == 0) + { + // Get image data for the current frame (from backbuffer) + // NOTE: This process is quite slow... :( + unsigned char *screenData = rlReadScreenPixels(CORE.Window.screen.width, CORE.Window.screen.height); + msf_gif_frame(&gifState, screenData, 10, 16, CORE.Window.screen.width*4); + + RL_FREE(screenData); // Free image data + } + + if (((gifFrameCounter/15)%2) == 1) + { + DrawCircle(30, CORE.Window.screen.height - 20, 10, MAROON); + DrawText("GIF RECORDING", 50, CORE.Window.screen.height - 25, 10, RED); + } + + rlDrawRenderBatchActive(); // Update and draw internal render batch + }*/ + } + + if(SUPPORT_EVENTS_AUTOMATION) { + // Draw record/play indicator + /* TODO + if (eventsRecording) { + gifFrameCounter++; + + if (((gifFrameCounter/15)%2) == 1) { + DrawCircle(30, CORE.Window.screen.height - 20, 10, MAROON); + DrawText("EVENTS RECORDING", 50, CORE.Window.screen.height - 25, 10, RED); + } + + rlDrawRenderBatchActive(); // Update and draw internal render batch + } + else if (eventsPlaying) { + gifFrameCounter++; + + if (((gifFrameCounter/15)%2) == 1) { + DrawCircle(30, CORE.Window.screen.height - 20, 10, LIME); + DrawText("EVENTS PLAYING", 50, CORE.Window.screen.height - 25, 10, GREEN); + } - time.setFrame(time.getUpdate() + time.getDraw()); + rlgl.rlDrawRenderBatchActive(); // Update and draw internal render batch + }*/ + } - // Wait for some milliseconds... - if (time.frame < time.target){ - WaitTime((float) ((time.target - time.frame) * 1000.0f)); + if(!SUPPORT_CUSTOM_FRAME_CONTROL) { + SwapScreenBuffer(); // Copy back buffer to front buffer (screen) + // Frame time control system time.current = GetTime(); - double waitTime = time.current - time.previous; + time.draw = time.current - time.previous; time.previous = time.current; - time.frame += waitTime; // Total frame time: update + draw + wait + time.frame = time.update + time.draw; + + // Wait for some milliseconds... + if (time.frame < time.target) { + WaitTime((float)(time.target - time.frame)*1000.0f); + + time.current = GetTime(); + double waitTime = time.current - time.previous; + time.previous = time.current; + + time.frame += waitTime; // Total frame time: update + draw + wait + } + + PollInputEvents(); // Poll user events (before next frame update) + } + + if(SUPPORT_EVENTS_AUTOMATION) { + // Events recording and playing logic + if (eventsRecording) { + RecordAutomationEvent(time.frameCounter); + } + else if (eventsPlaying) { + // TODO: When should we play? After/before/replace PollInputEvents()? + if (time.frameCounter >= eventCount) eventsPlaying = false; + PlayAutomationEvent(time.frameCounter); + } } - PollInputEvents(); // Poll user events + time.frameCounter++; } // Initialize 2D mode with custom camera (2D) @@ -2802,9 +2884,9 @@ void PollInputEvents(){ } /** - * Copy back buffer to front buffers + * Swap back buffer with front buffer (screen drawing) */ - void SwapScreenBuffers(){ + void SwapScreenBuffer(){ glfwSwapBuffers(window.handle); } diff --git a/src/com/raylib/java/rlgl/GL_33.java b/src/com/raylib/java/rlgl/GL_33.java index 13b1cad..71090fd 100644 --- a/src/com/raylib/java/rlgl/GL_33.java +++ b/src/com/raylib/java/rlgl/GL_33.java @@ -245,10 +245,10 @@ public static void rlVertex3f(float x, float y, float z){ // By default rlVertexBuffer type does not store normals // Add current color - rlglData.getCurrentBatch().rlVertexBuffer[rlglData.getCurrentBatch().currentBuffer].colors[4*rlglData.getState().vertexCounter] = (float) rlglData.getState().colorr/255; - rlglData.getCurrentBatch().rlVertexBuffer[rlglData.getCurrentBatch().currentBuffer].colors[4*rlglData.getState().vertexCounter + 1] = (float) rlglData.getState().colorg/255; - rlglData.getCurrentBatch().rlVertexBuffer[rlglData.getCurrentBatch().currentBuffer].colors[4*rlglData.getState().vertexCounter + 2] = (float) rlglData.getState().colorb/255; - rlglData.getCurrentBatch().rlVertexBuffer[rlglData.getCurrentBatch().currentBuffer].colors[4*rlglData.getState().vertexCounter + 3] = (float) rlglData.getState().colora/255; + rlglData.getCurrentBatch().rlVertexBuffer[rlglData.getCurrentBatch().currentBuffer].colors[4*rlglData.getState().vertexCounter] = rlglData.getState().colorr; + rlglData.getCurrentBatch().rlVertexBuffer[rlglData.getCurrentBatch().currentBuffer].colors[4*rlglData.getState().vertexCounter + 1] = rlglData.getState().colorg; + rlglData.getCurrentBatch().rlVertexBuffer[rlglData.getCurrentBatch().currentBuffer].colors[4*rlglData.getState().vertexCounter + 2] = rlglData.getState().colorb; + rlglData.getCurrentBatch().rlVertexBuffer[rlglData.getCurrentBatch().currentBuffer].colors[4*rlglData.getState().vertexCounter + 3] = rlglData.getState().colora; rlglData.getState().vertexCounter++; @@ -285,7 +285,7 @@ static void rlNormal3f(float x, float y, float z){ } // Define one vertex (color) - static void rlColor4ub(int x, int y, int z, int w){ + static void rlColor4ub(byte x, byte y, byte z, byte w){ rlglData.getState().colorr = x; rlglData.getState().colorg = y; rlglData.getState().colorb = z; @@ -294,12 +294,12 @@ static void rlColor4ub(int x, int y, int z, int w){ // Define one vertex (color) static void rlColor4f(float r, float g, float b, float a){ - rlColor4ub((int)(r * 255), (int)(g * 255), (int)(b * 255), (int)(a * 255)); + rlColor4ub((byte)(r * 255), (byte)(g * 255), (byte)(b * 255), (byte)(a * 255)); } // Define one vertex (color) public static void rlColor3f(float x, float y, float z){ - rlColor4ub((int)(x * 255), (int)(y * 255), (int)(z * 255), 255); + rlColor4ub((byte)(x * 255), (byte)(y * 255), (byte)(z * 255), (byte)255); } } \ No newline at end of file diff --git a/src/com/raylib/java/rlgl/RLGL.java b/src/com/raylib/java/rlgl/RLGL.java index 8b11435..acf7edb 100644 --- a/src/com/raylib/java/rlgl/RLGL.java +++ b/src/com/raylib/java/rlgl/RLGL.java @@ -479,7 +479,7 @@ public static void rlNormal3f(float x, float y, float z){ // Define one vertex (color) public static void rlColor4ub(int x, int y, int z, int w){ if (GRAPHICS_API_OPENGL_33){ - GL_33.rlColor4ub(x, y, z, w); + GL_33.rlColor4ub((byte)x, (byte)y, (byte)z, (byte)w); } else{ GL_11.rlColor4ub(x, y, z, w); @@ -1381,11 +1381,11 @@ static rlRenderBatch rlLoadRenderBatch(int numBuffers, int bufferElements){ for(int i = 0; i < numBuffers; i++){ batch.rlVertexBuffer[i].elementCount = bufferElements; - batch.rlVertexBuffer[i].setVertices(new float[bufferElements * 3 * Float.BYTES]); + batch.rlVertexBuffer[i].setVertices(new float[bufferElements * 3 * 4 * Float.BYTES]); // 3 float by vertex, 4 vertex by quad - batch.rlVertexBuffer[i].setTexcoords(new float[bufferElements * 2 * Float.BYTES]); + batch.rlVertexBuffer[i].setTexcoords(new float[bufferElements * 4 * 2 * Float.BYTES]); // 2 float by texcoord, 4 texcoord by quad - batch.rlVertexBuffer[i].setColors(new float[bufferElements * 4 * Float.BYTES]); + batch.rlVertexBuffer[i].setColors(new byte[bufferElements * 4 * 4 * Byte.BYTES]); // 4 float by color, 4 colors by quad if(GRAPHICS_API_OPENGL_33){ batch.getVertexBuffer()[i].setIndices_GL11(new int[bufferElements * 6 * Integer.BYTES]); @@ -1442,6 +1442,7 @@ else if(GRAPHICS_API_OPENGL_ES2){ //-------------------------------------------------------------------------------------------- // Upload to GPU (VRAM) vertex data and initialize VAOs/VBOs //-------------------------------------------------------------------------------------------- + for(int i = 0; i < numBuffers; i++){ if(rlglData.getExtSupported().isVao()){ // Initialize Quads VAO @@ -1467,12 +1468,15 @@ else if(GRAPHICS_API_OPENGL_ES2){ 2, GL_FLOAT, false, 0, 0); // Vertex color buffer (shader-location = 3) + ByteBuffer colours = ByteBuffer.allocateDirect(batch.rlVertexBuffer[i].colors.length); + colours.put(batch.rlVertexBuffer[i].colors).flip(); + batch.rlVertexBuffer[i].vboId[2] = glGenBuffers(); glBindBuffer(GL_ARRAY_BUFFER, batch.rlVertexBuffer[i].vboId[2]); - glBufferData(GL_ARRAY_BUFFER, batch.rlVertexBuffer[i].colors, GL_DYNAMIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, colours, GL_DYNAMIC_DRAW); glEnableVertexAttribArray(rlglData.getState().currentShaderLocs[RL_SHADER_LOC_VERTEX_COLOR]); glVertexAttribPointer(rlglData.getState().currentShaderLocs[RL_SHADER_LOC_VERTEX_COLOR], - 4, GL_FLOAT, false, 0, 0); + 4, GL_UNSIGNED_BYTE, true, 0, 0); // Fill index buffer batch.rlVertexBuffer[i].vboId[3] = glGenBuffers(); @@ -1574,9 +1578,12 @@ static void rlDrawRenderBatch(rlRenderBatch batch){ // batch->rlVertexBuffer[batch->currentBuffer].texcoords, GL_DYNAMIC_DRAW); // Update all buffer + ByteBuffer colours = ByteBuffer.allocateDirect(batch.rlVertexBuffer[batch.getCurrentBuffer()].colors.length); + colours.put(batch.rlVertexBuffer[batch.getCurrentBuffer()].colors).flip(); + // Colors buffer glBindBuffer(GL_ARRAY_BUFFER, batch.rlVertexBuffer[batch.getCurrentBuffer()].vboId[2]); - glBufferSubData(GL_ARRAY_BUFFER, 0, batch.rlVertexBuffer[batch.getCurrentBuffer()].colors); + glBufferSubData(GL_ARRAY_BUFFER, 0, colours); //glBufferData(GL_ARRAY_BUFFER, sizeof(float)*4*4*batch->rlVertexBuffer[batch->currentBuffer].elementsCount, // batch->rlVertexBuffer[batch->currentBuffer].colors, GL_DYNAMIC_DRAW); // Update all buffer diff --git a/src/com/raylib/java/rlgl/data/State.java b/src/com/raylib/java/rlgl/data/State.java index 5b9d004..9242c14 100644 --- a/src/com/raylib/java/rlgl/data/State.java +++ b/src/com/raylib/java/rlgl/data/State.java @@ -9,15 +9,15 @@ public class State{ // Renderer state public int vertexCounter; // Current active render batch vertex counter (generic, used for all batches) public float texcoordx, texcoordy; // Current active texture coordinate (added on glVertex*()) public float normalx, normaly, normalz; // Current active normal (added on glVertex*()) - public int colorr, colorg, colorb, colora; // Current active color (added on glVertex*()) + public byte colorr, colorg, colorb, colora; // Current active color (added on glVertex*()) public int currentMatrixMode; // Current matrix mode - public Matrix currentMatrix; // Current matrix pointer + public Matrix currentMatrix; // Current matrix pointer public Matrix modelview; // Default modelview matrix public Matrix projection; // Default projection matrix public Matrix transform; // Transform matrix to be used with rlTranslate, rlRotate, rlScale - public boolean transformRequired; // Require transform matrix application to current draw-call vertex (if required) - public Matrix[] stack;// Matrix stack for push/pop + public boolean transformRequired; // Require transform matrix application to current draw-call vertex (if required) + public Matrix[] stack; // Matrix stack for push/pop public int stackCounter; // Matrix stack counter public int defaultTextureId; // Default texture used on shapes/poly drawing (required by shader) @@ -129,7 +129,7 @@ public int getColorr(){ return colorr; } - public void setColorr(int colorr){ + public void setColorr(byte colorr){ this.colorr = colorr; } @@ -137,7 +137,7 @@ public int getColorg(){ return colorg; } - public void setColorg(int colorg){ + public void setColorg(byte colorg){ this.colorg = colorg; } @@ -145,7 +145,7 @@ public int getColorb(){ return colorb; } - public void setColorb(int colorb){ + public void setColorb(byte colorb){ this.colorb = colorb; } @@ -153,7 +153,7 @@ public int getColora(){ return colora; } - public void setColora(int colora){ + public void setColora(byte colora){ this.colora = colora; } diff --git a/src/com/raylib/java/rlgl/rlVertexBuffer.java b/src/com/raylib/java/rlgl/rlVertexBuffer.java index f288dc0..a11130f 100644 --- a/src/com/raylib/java/rlgl/rlVertexBuffer.java +++ b/src/com/raylib/java/rlgl/rlVertexBuffer.java @@ -5,7 +5,7 @@ public class rlVertexBuffer{ int elementCount; // Number of elements in the buffer (QUADS) float[] vertices; // Vertex position (XYZ - 3 components per vertex) (shader-location = 0) float[] texcoords; // Vertex texture coordinates (UV - 2 components per vertex) (shader-location = 1) - float[] colors; // Vertex colors (RGBA - 4 components per vertex) (shader-location = 3) + byte[] colors; // Vertex colors (RGBA - 4 components per vertex) (shader-location = 3) int[] indices_GL11; // Vertex indices (in case vertex data comes indexed) (6 indices per quad) short[] indices_ES20; // Vertex indices (in case vertex data comes indexed) (6 indices per quad) int vaoId; // OpenGL Vertex Array Object id @@ -14,7 +14,7 @@ public class rlVertexBuffer{ public rlVertexBuffer(){ vertices = new float[3]; texcoords = new float[2]; - colors = new float[64]; + colors = new byte[64]; indices_GL11 = new int[6]; indices_ES20 = new short[6]; vboId = new int[4]; @@ -44,11 +44,11 @@ public void setTexcoords(float[] texcoords){ this.texcoords = texcoords; } - public float[] getColors(){ + public byte[] getColors(){ return colors; } - public void setColors(float[] colors){ + public void setColors(byte[] colors){ this.colors = colors; }