Skip to content

Commit

Permalink
rlgl, core update/improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
CreedVI committed Dec 16, 2021
1 parent 75e3adb commit a23b40a
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 50 deletions.
6 changes: 3 additions & 3 deletions src/com/raylib/java/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/com/raylib/java/JarMainForTesting.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 2 additions & 0 deletions src/com/raylib/java/core/Time.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(){

}
Expand Down
124 changes: 103 additions & 21 deletions src/com/raylib/java/core/rCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}

Expand Down
14 changes: 7 additions & 7 deletions src/com/raylib/java/rlgl/GL_33.java
Original file line number Diff line number Diff line change
Expand Up @@ -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++;

Expand Down Expand Up @@ -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;
Expand All @@ -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);
}

}
21 changes: 14 additions & 7 deletions src/com/raylib/java/rlgl/RLGL.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions src/com/raylib/java/rlgl/data/State.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -129,31 +129,31 @@ public int getColorr(){
return colorr;
}

public void setColorr(int colorr){
public void setColorr(byte colorr){
this.colorr = colorr;
}

public int getColorg(){
return colorg;
}

public void setColorg(int colorg){
public void setColorg(byte colorg){
this.colorg = colorg;
}

public int getColorb(){
return colorb;
}

public void setColorb(int colorb){
public void setColorb(byte colorb){
this.colorb = colorb;
}

public int getColora(){
return colora;
}

public void setColora(int colora){
public void setColora(byte colora){
this.colora = colora;
}

Expand Down
Loading

0 comments on commit a23b40a

Please sign in to comment.