From a362ad10016273c916b2e6da938cebf38b709cc2 Mon Sep 17 00:00:00 2001 From: Artem Borovik Date: Sat, 17 Aug 2024 18:22:34 +0300 Subject: [PATCH 1/5] webrogue support --- cmake/LibraryConfigurations.cmake | 6 ++++- src/rtextures.c | 44 ++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/cmake/LibraryConfigurations.cmake b/cmake/LibraryConfigurations.cmake index 6d2250c715be..9fda38bb720e 100644 --- a/cmake/LibraryConfigurations.cmake +++ b/cmake/LibraryConfigurations.cmake @@ -44,7 +44,11 @@ if (${PLATFORM} MATCHES "Desktop") set(OPENGL_LIBRARIES "GL") endif () - set(LIBS_PRIVATE m atomic pthread ${OPENGL_LIBRARIES} ${OSS_LIBRARY}) + if (NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "WASI") + set(LIBS_PRIVATE m atomic) + endif () + + set(LIBS_PRIVATE ${LIBS_PRIVATE} pthread ${OPENGL_LIBRARIES} ${OSS_LIBRARY}) if ("${CMAKE_SYSTEM_NAME}" MATCHES "(Net|Open)BSD") find_library(OSS_LIBRARY ossaudio) diff --git a/src/rtextures.c b/src/rtextures.c index 5fa01c9dff2d..882050b21f00 100644 --- a/src/rtextures.c +++ b/src/rtextures.c @@ -4599,6 +4599,7 @@ void DrawTexturePro(Texture2D texture, Rectangle source, Rectangle dest, Vector2 } rlSetTexture(texture.id); +#if defined(SUPPORT_QUADS_DRAW_MODE) rlBegin(RL_QUADS); rlColor4ub(tint.r, tint.g, tint.b, tint.a); @@ -4625,7 +4626,6 @@ void DrawTexturePro(Texture2D texture, Rectangle source, Rectangle dest, Vector2 rlVertex2f(topRight.x, topRight.y); rlEnd(); - rlSetTexture(0); // NOTE: Vertex position can be transformed using matrices // but the process is way more costly than just calculating @@ -4633,7 +4633,6 @@ void DrawTexturePro(Texture2D texture, Rectangle source, Rectangle dest, Vector2 // I leave here the old implementation for educational purposes, // just in case someone wants to do some performance test /* - rlSetTexture(texture.id); rlPushMatrix(); rlTranslatef(dest.x, dest.y, 0.0f); if (rotation != 0.0f) rlRotatef(rotation, 0.0f, 0.0f, 1.0f); @@ -4664,8 +4663,47 @@ void DrawTexturePro(Texture2D texture, Rectangle source, Rectangle dest, Vector2 rlVertex2f(dest.width, 0.0f); rlEnd(); rlPopMatrix(); - rlSetTexture(0); */ + +#else + rlBegin(RL_TRIANGLES); + + rlColor4ub(tint.r, tint.g, tint.b, tint.a); + rlNormal3f(0.0f, 0.0f, 1.0f); // Normal vector pointing towards viewer + + // Top-left corner for texture + if (flipX) rlTexCoord2f((source.x + source.width)/width, source.y/height); + else rlTexCoord2f(source.x/width, source.y/height); + rlVertex2f(topLeft.x, topLeft.y); + + // Bottom-left corner for texture + if (flipX) rlTexCoord2f((source.x + source.width)/width, (source.y + source.height)/height); + else rlTexCoord2f(source.x/width, (source.y + source.height)/height); + rlVertex2f(bottomLeft.x, bottomLeft.y); + + // Bottom-right corner for texture + if (flipX) rlTexCoord2f(source.x/width, (source.y + source.height)/height); + else rlTexCoord2f((source.x + source.width)/width, (source.y + source.height)/height); + rlVertex2f(bottomRight.x, bottomRight.y); + + // Top-left corner for texture + if (flipX) rlTexCoord2f((source.x + source.width)/width, source.y/height); + else rlTexCoord2f(source.x/width, source.y/height); + rlVertex2f(topLeft.x, topLeft.y); + + // Bottom-right corner for texture + if (flipX) rlTexCoord2f(source.x/width, (source.y + source.height)/height); + else rlTexCoord2f((source.x + source.width)/width, (source.y + source.height)/height); + rlVertex2f(bottomRight.x, bottomRight.y); + + // Top-right corner for texture + if (flipX) rlTexCoord2f(source.x/width, source.y/height); + else rlTexCoord2f((source.x + source.width)/width, source.y/height); + rlVertex2f(topRight.x, topRight.y); + + rlEnd(); +#endif + rlSetTexture(0); } } From d331278450912a8e38aab3c2ac54cdf6ba02ae28 Mon Sep 17 00:00:00 2001 From: Artem Borovik Date: Tue, 20 Aug 2024 00:43:10 +0300 Subject: [PATCH 2/5] WaitTime fix --- src/rcore.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rcore.c b/src/rcore.c index 5485ce8ce032..3f087e80f993 100644 --- a/src/rcore.c +++ b/src/rcore.c @@ -1689,7 +1689,7 @@ void WaitTime(double seconds) #if defined(_WIN32) Sleep((unsigned long)(sleepSeconds*1000.0)); #endif - #if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__EMSCRIPTEN__) + #if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__EMSCRIPTEN__) || defined(__wasi__) struct timespec req = { 0 }; time_t sec = sleepSeconds; long nsec = (sleepSeconds - sec)*1000000000L; From 732c1fcb26030d15dfaba838043d24358b10f352 Mon Sep 17 00:00:00 2001 From: Artem Borovik Date: Fri, 30 Aug 2024 18:08:39 +0300 Subject: [PATCH 3/5] texture fix --- src/rtextures.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/rtextures.c b/src/rtextures.c index 882050b21f00..d9c00ff6588f 100644 --- a/src/rtextures.c +++ b/src/rtextures.c @@ -4598,9 +4598,9 @@ void DrawTexturePro(Texture2D texture, Rectangle source, Rectangle dest, Vector2 bottomRight.y = y + (dx + dest.width)*sinRotation + (dy + dest.height)*cosRotation; } - rlSetTexture(texture.id); #if defined(SUPPORT_QUADS_DRAW_MODE) rlBegin(RL_QUADS); + rlSetTexture(texture.id); rlColor4ub(tint.r, tint.g, tint.b, tint.a); rlNormal3f(0.0f, 0.0f, 1.0f); // Normal vector pointing towards viewer @@ -4667,6 +4667,7 @@ void DrawTexturePro(Texture2D texture, Rectangle source, Rectangle dest, Vector2 #else rlBegin(RL_TRIANGLES); + rlSetTexture(texture.id); rlColor4ub(tint.r, tint.g, tint.b, tint.a); rlNormal3f(0.0f, 0.0f, 1.0f); // Normal vector pointing towards viewer From 617e0e51dfd2f712757c01726016681c1c307616 Mon Sep 17 00:00:00 2001 From: Artem Borovik Date: Fri, 30 Aug 2024 20:34:42 +0300 Subject: [PATCH 4/5] fix --- src/rlgl.h | 3 ++- src/rshapes.c | 17 ++++++++++++++--- src/rtextures.c | 3 +-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/rlgl.h b/src/rlgl.h index 5fb9497cd20a..58bb241a6f0d 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -1424,6 +1424,7 @@ void rlBegin(int mode) // NOTE: In all three cases, vertex are accumulated over default internal vertex buffer if (RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode != mode) { + int currentTexture = RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].textureId; if (RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount > 0) { // Make sure current RLGL.currentBatch->draws[i].vertexCount is aligned a multiple of 4, @@ -1446,7 +1447,7 @@ void rlBegin(int mode) RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode = mode; RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount = 0; - RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].textureId = RLGL.State.defaultTextureId; + RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].textureId = currentTexture; } } diff --git a/src/rshapes.c b/src/rshapes.c index 676b2521a06b..68ff50492fb1 100644 --- a/src/rshapes.c +++ b/src/rshapes.c @@ -719,10 +719,10 @@ void DrawRectanglePro(Rectangle rec, Vector2 origin, float rotation, Color color bottomRight.y = y + (dx + rec.width)*sinRotation + (dy + rec.height)*cosRotation; } -#if defined(SUPPORT_QUADS_DRAW_MODE) rlSetTexture(GetShapesTexture().id); Rectangle shapeRect = GetShapesTextureRectangle(); +#if defined(SUPPORT_QUADS_DRAW_MODE) rlBegin(RL_QUADS); rlNormal3f(0.0f, 0.0f, 1.0f); @@ -741,23 +741,34 @@ void DrawRectanglePro(Rectangle rec, Vector2 origin, float rotation, Color color rlVertex2f(topRight.x, topRight.y); rlEnd(); - - rlSetTexture(0); #else rlBegin(RL_TRIANGLES); + rlNormal3f(0.0f, 0.0f, 1.0f); rlColor4ub(color.r, color.g, color.b, color.a); + rlTexCoord2f(shapeRect.x/texShapes.width, shapeRect.y/texShapes.height); rlVertex2f(topLeft.x, topLeft.y); + + rlTexCoord2f(shapeRect.x/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height); rlVertex2f(bottomLeft.x, bottomLeft.y); + + rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, shapeRect.y/texShapes.height); rlVertex2f(topRight.x, topRight.y); + rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, shapeRect.y/texShapes.height); rlVertex2f(topRight.x, topRight.y); + + rlTexCoord2f(shapeRect.x/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height); rlVertex2f(bottomLeft.x, bottomLeft.y); + + rlTexCoord2f((shapeRect.x + shapeRect.width)/texShapes.width, (shapeRect.y + shapeRect.height)/texShapes.height); rlVertex2f(bottomRight.x, bottomRight.y); rlEnd(); #endif + + rlSetTexture(0); } // Draw a vertical-gradient-filled rectangle diff --git a/src/rtextures.c b/src/rtextures.c index d9c00ff6588f..882050b21f00 100644 --- a/src/rtextures.c +++ b/src/rtextures.c @@ -4598,9 +4598,9 @@ void DrawTexturePro(Texture2D texture, Rectangle source, Rectangle dest, Vector2 bottomRight.y = y + (dx + dest.width)*sinRotation + (dy + dest.height)*cosRotation; } + rlSetTexture(texture.id); #if defined(SUPPORT_QUADS_DRAW_MODE) rlBegin(RL_QUADS); - rlSetTexture(texture.id); rlColor4ub(tint.r, tint.g, tint.b, tint.a); rlNormal3f(0.0f, 0.0f, 1.0f); // Normal vector pointing towards viewer @@ -4667,7 +4667,6 @@ void DrawTexturePro(Texture2D texture, Rectangle source, Rectangle dest, Vector2 #else rlBegin(RL_TRIANGLES); - rlSetTexture(texture.id); rlColor4ub(tint.r, tint.g, tint.b, tint.a); rlNormal3f(0.0f, 0.0f, 1.0f); // Normal vector pointing towards viewer From 64f3990e939df27f60f0835292718b512c4b1737 Mon Sep 17 00:00:00 2001 From: Artem Borovik Date: Fri, 30 Aug 2024 20:40:07 +0300 Subject: [PATCH 5/5] reset webrogue-specific changes to upstream --- cmake/LibraryConfigurations.cmake | 6 +----- src/rcore.c | 2 +- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/cmake/LibraryConfigurations.cmake b/cmake/LibraryConfigurations.cmake index 9fda38bb720e..6d2250c715be 100644 --- a/cmake/LibraryConfigurations.cmake +++ b/cmake/LibraryConfigurations.cmake @@ -44,11 +44,7 @@ if (${PLATFORM} MATCHES "Desktop") set(OPENGL_LIBRARIES "GL") endif () - if (NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "WASI") - set(LIBS_PRIVATE m atomic) - endif () - - set(LIBS_PRIVATE ${LIBS_PRIVATE} pthread ${OPENGL_LIBRARIES} ${OSS_LIBRARY}) + set(LIBS_PRIVATE m atomic pthread ${OPENGL_LIBRARIES} ${OSS_LIBRARY}) if ("${CMAKE_SYSTEM_NAME}" MATCHES "(Net|Open)BSD") find_library(OSS_LIBRARY ossaudio) diff --git a/src/rcore.c b/src/rcore.c index d8ff89b2f58a..972d5860074c 100644 --- a/src/rcore.c +++ b/src/rcore.c @@ -1696,7 +1696,7 @@ void WaitTime(double seconds) #if defined(_WIN32) Sleep((unsigned long)(sleepSeconds*1000.0)); #endif - #if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__EMSCRIPTEN__) || defined(__wasi__) + #if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__EMSCRIPTEN__) struct timespec req = { 0 }; time_t sec = sleepSeconds; long nsec = (sleepSeconds - sec)*1000000000L;