From 1f0325b52c87af4820b31957d8e40d7bc1f112bb Mon Sep 17 00:00:00 2001 From: Legendary Redfox <128002430+legendaryredfox@users.noreply.github.com> Date: Tue, 3 Dec 2024 09:30:17 -0300 Subject: [PATCH 01/10] [documentation] Removing 404 repos from bindings (#4572) * updated raylib-lua version * Updated some bindings * removed 404 bindings --- BINDINGS.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/BINDINGS.md b/BINDINGS.md index a4e31de3c..5d158c998 100644 --- a/BINDINGS.md +++ b/BINDINGS.md @@ -8,7 +8,6 @@ Some people ported raylib to other languages in the form of bindings or wrappers | :--------------------------------------------------------------------------------------- | :--------------: | :------------------------------------------------------------------: | :------------------: | | [raylib](https://github.com/raysan5/raylib) | **5.5** | [C/C++](https://en.wikipedia.org/wiki/C_(programming_language)) | Zlib | | [raylib-beef](https://github.com/Starpelly/raylib-beef) | **5.5** | [Beef](https://www.beeflang.org) | MIT | -| [raylib-boo](https://github.com/Rabios/raylib-boo) | 3.7 | [Boo](http://boo-language.github.io) | MIT | | [raybit](https://github.com/Alex-Velez/raybit) | **5.0** | [Brainfuck](https://en.wikipedia.org/wiki/Brainfuck) | MIT | | [raylib-c3](https://github.com/c3lang/vendor/tree/main/libraries/raylib55.c3l) | **5.5** | [C3](https://c3-lang.org) | MIT | | [Raylib-cs](https://github.com/ChrisDill/Raylib-cs) | **5.0** | [C#](https://en.wikipedia.org/wiki/C_Sharp_(programming_language)) | Zlib | @@ -43,7 +42,6 @@ Some people ported raylib to other languages in the form of bindings or wrappers | [kaylib](https://github.com/electronstudio/kaylib) | 3.7 | [Kotlin/native](https://kotlinlang.org) | **???** | | [KaylibKit](https://codeberg.org/Kenta/KaylibKit) | 4.5 | [Kotlin/native](https://kotlinlang.org) | Zlib | | [raylib-lua](https://github.com/TSnake41/raylib-lua) | 5.0 | [Lua](http://www.lua.org) | ISC | -| [raylua](https://github.com/Rabios/raylua) | 4.0 | [Lua](http://www.lua.org) | MIT | | [raylib-matte](https://github.com/jcorks/raylib-matte) | 4.6-dev | [Matte](https://github.com/jcorks/matte) | MIT | | [Raylib.nelua](https://github.com/AuzFox/Raylib.nelua) | **5.0** | [nelua](https://nelua.io) | Zlib | | [raylib-bindings](https://github.com/vaiorabbit/raylib-bindings) | 5.6-dev | [Ruby](https://www.ruby-lang.org/en) | Zlib | From 5feccb1babbce13de69b33179a2599e5bc216899 Mon Sep 17 00:00:00 2001 From: Caleb Heydon Date: Tue, 3 Dec 2024 12:35:24 -0500 Subject: [PATCH 02/10] [rmodels] Fixed null pointer dereference in LoadGLTF (#4564) * [rmodels] Fixed null pointer dereference in LoadGLTF * [rmodels] Add parenthesis around conditionals in LoadGLTF --- src/rmodels.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rmodels.c b/src/rmodels.c index 46309f98f..6e33da697 100644 --- a/src/rmodels.c +++ b/src/rmodels.c @@ -5657,7 +5657,7 @@ static Model LoadGLTF(const char *fileName) } // Load primitive indices data (if provided) - if (mesh->primitives[p].indices != NULL) + if ((mesh->primitives[p].indices != NULL) && (mesh->primitives[p].indices->buffer_view != NULL)) { cgltf_accessor *attribute = mesh->primitives[p].indices; From 1f45e7af765e5c292ce1ab038ee0cedffa7408b5 Mon Sep 17 00:00:00 2001 From: Ray Date: Tue, 3 Dec 2024 19:14:14 +0100 Subject: [PATCH 03/10] REVIEWED: Coding conventions --- src/raudio.c | 4 ++-- src/raymath.h | 12 ++++++------ src/rcamera.h | 2 +- src/rcore.c | 33 ++++++++++++++++++++------------- src/rmodels.c | 43 +++++++++++++++++++++++-------------------- src/rtext.c | 2 +- src/rtextures.c | 15 ++++++++------- 7 files changed, 61 insertions(+), 50 deletions(-) diff --git a/src/raudio.c b/src/raudio.c index b2627e2dc..cc596ec11 100644 --- a/src/raudio.c +++ b/src/raudio.c @@ -1550,7 +1550,7 @@ Music LoadMusicStreamFromMemory(const char *fileType, const unsigned char *data, else if ((strcmp(fileType, ".ogg") == 0) || (strcmp(fileType, ".OGG") == 0)) { // Open ogg audio stream - stb_vorbis* ctxOgg = stb_vorbis_open_memory((const unsigned char *)data, dataSize, NULL, NULL); + stb_vorbis *ctxOgg = stb_vorbis_open_memory((const unsigned char *)data, dataSize, NULL, NULL); if (ctxOgg != NULL) { @@ -2462,7 +2462,7 @@ static ma_uint32 ReadAudioBufferFramesInMixingFormat(AudioBuffer *audioBuffer, f float *runningFramesOut = framesOut + (totalOutputFramesProcessed*audioBuffer->converter.channelsOut); // At this point we can convert the data to our mixing format - ma_uint64 inputFramesProcessedThisIteration = ReadAudioBufferFramesInInternalFormat(audioBuffer, inputBuffer, (ma_uint32)inputFramesToProcessThisIteration); /* Safe cast. */ + ma_uint64 inputFramesProcessedThisIteration = ReadAudioBufferFramesInInternalFormat(audioBuffer, inputBuffer, (ma_uint32)inputFramesToProcessThisIteration); ma_uint64 outputFramesProcessedThisIteration = outputFramesToProcessThisIteration; ma_data_converter_process_pcm_frames(&audioBuffer->converter, inputBuffer, &inputFramesProcessedThisIteration, runningFramesOut, &outputFramesProcessedThisIteration); diff --git a/src/raymath.h b/src/raymath.h index caf04a321..9d712027c 100644 --- a/src/raymath.h +++ b/src/raymath.h @@ -2665,12 +2665,12 @@ inline const Vector2& operator *= (Vector2& lhs, const Matrix& rhs) inline Vector2 operator / (const Vector2& lhs, const float& rhs) { - return Vector2Scale(lhs, 1.0f / rhs); + return Vector2Scale(lhs, 1.0f/rhs); } inline const Vector2& operator /= (Vector2& lhs, const float& rhs) { - lhs = Vector2Scale(lhs, 1.0f / rhs); + lhs = Vector2Scale(lhs, 1.0f/rhs); return lhs; } @@ -2759,12 +2759,12 @@ inline const Vector3& operator *= (Vector3& lhs, const Matrix& rhs) inline Vector3 operator / (const Vector3& lhs, const float& rhs) { - return Vector3Scale(lhs, 1.0f / rhs); + return Vector3Scale(lhs, 1.0f/rhs); } inline const Vector3& operator /= (Vector3& lhs, const float& rhs) { - lhs = Vector3Scale(lhs, 1.0f / rhs); + lhs = Vector3Scale(lhs, 1.0f/rhs); return lhs; } @@ -2843,12 +2843,12 @@ inline const Vector4& operator *= (Vector4& lhs, const Vector4& rhs) inline Vector4 operator / (const Vector4& lhs, const float& rhs) { - return Vector4Scale(lhs, 1.0f / rhs); + return Vector4Scale(lhs, 1.0f/rhs); } inline const Vector4& operator /= (Vector4& lhs, const float& rhs) { - lhs = Vector4Scale(lhs, 1.0f / rhs); + lhs = Vector4Scale(lhs, 1.0f/rhs); return lhs; } diff --git a/src/rcamera.h b/src/rcamera.h index bd2b36e03..ab998ae03 100644 --- a/src/rcamera.h +++ b/src/rcamera.h @@ -154,7 +154,7 @@ RLAPI void CameraPitch(Camera *camera, float angle, bool lockView, bool rotateAr RLAPI void CameraRoll(Camera *camera, float angle); RLAPI Matrix GetCameraViewMatrix(Camera *camera); -RLAPI Matrix GetCameraProjectionMatrix(Camera* camera, float aspect); +RLAPI Matrix GetCameraProjectionMatrix(Camera *camera, float aspect); #if defined(__cplusplus) } diff --git a/src/rcore.c b/src/rcore.c index 05e02b14f..bcff5acb0 100644 --- a/src/rcore.c +++ b/src/rcore.c @@ -2765,7 +2765,8 @@ unsigned int *ComputeMD5(unsigned char *data, int dataSize) // Compute SHA-1 hash code // NOTE: Returns a static int[5] array (20 bytes) -unsigned int *ComputeSHA1(unsigned char *data, int dataSize) { +unsigned int *ComputeSHA1(unsigned char *data, int dataSize) +{ #define ROTATE_LEFT(x, c) (((x) << (c)) | ((x) >> (32 - (c)))) static unsigned int hash[5] = { 0 }; // Hash to be returned @@ -2800,17 +2801,16 @@ unsigned int *ComputeSHA1(unsigned char *data, int dataSize) { { // Break chunk into sixteen 32-bit words w[j], 0 <= j <= 15 unsigned int w[80] = {0}; - for (int i = 0; i < 16; i++) { - w[i] = (msg[offset + (i * 4) + 0] << 24) | - (msg[offset + (i * 4) + 1] << 16) | - (msg[offset + (i * 4) + 2] << 8) | - (msg[offset + (i * 4) + 3]); + for (int i = 0; i < 16; i++) + { + w[i] = (msg[offset + (i*4) + 0] << 24) | + (msg[offset + (i*4) + 1] << 16) | + (msg[offset + (i*4) + 2] << 8) | + (msg[offset + (i*4) + 3]); } // Message schedule: extend the sixteen 32-bit words into eighty 32-bit words: - for (int i = 16; i < 80; ++i) { - w[i] = ROTATE_LEFT(w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16], 1); - } + for (int i = 16; i < 80; i++) w[i] = ROTATE_LEFT(w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16], 1); // Initialize hash value for this chunk unsigned int a = hash[0]; @@ -2824,16 +2824,23 @@ unsigned int *ComputeSHA1(unsigned char *data, int dataSize) { unsigned int f = 0; unsigned int k = 0; - if (i < 20) { + if (i < 20) + { f = (b & c) | ((~b) & d); k = 0x5A827999; - } else if (i < 40) { + } + else if (i < 40) + { f = b ^ c ^ d; k = 0x6ED9EBA1; - } else if (i < 60) { + } + else if (i < 60) + { f = (b & c) | (b & d) | (c & d); k = 0x8F1BBCDC; - } else { + } + else + { f = b ^ c ^ d; k = 0xCA62C1D6; } diff --git a/src/rmodels.c b/src/rmodels.c index 5e6d39664..2f9dade83 100644 --- a/src/rmodels.c +++ b/src/rmodels.c @@ -96,9 +96,9 @@ #endif #if defined(SUPPORT_MESH_GENERATION) - #define PAR_MALLOC(T, N) ((T*)RL_MALLOC(N*sizeof(T))) - #define PAR_CALLOC(T, N) ((T*)RL_CALLOC(N*sizeof(T), 1)) - #define PAR_REALLOC(T, BUF, N) ((T*)RL_REALLOC(BUF, sizeof(T)*(N))) + #define PAR_MALLOC(T, N) ((T *)RL_MALLOC(N*sizeof(T))) + #define PAR_CALLOC(T, N) ((T *)RL_CALLOC(N*sizeof(T), 1)) + #define PAR_REALLOC(T, BUF, N) ((T *)RL_REALLOC(BUF, sizeof(T)*(N))) #define PAR_FREE RL_FREE #if defined(_MSC_VER) // Disable some MSVC warning @@ -2308,7 +2308,7 @@ void UpdateModelAnimationBones(Model model, ModelAnimation anim, int frame) } } -// at least 2x speed up vs the old method +// at least 2x speed up vs the old method // Update model animated vertex data (positions and normals) for a given frame // NOTE: Updated data is uploaded to GPU void UpdateModelAnimation(Model model, ModelAnimation anim, int frame) @@ -2340,14 +2340,16 @@ void UpdateModelAnimation(Model model, ModelAnimation anim, int frame) { boneWeight = mesh.boneWeights[boneCounter]; boneId = mesh.boneIds[boneCounter]; + // Early stop when no transformation will be applied if (boneWeight == 0.0f) continue; animVertex = (Vector3){ mesh.vertices[vCounter], mesh.vertices[vCounter + 1], mesh.vertices[vCounter + 2] }; animVertex = Vector3Transform(animVertex,model.meshes[m].boneMatrices[boneId]); - mesh.animVertices[vCounter] += animVertex.x * boneWeight; - mesh.animVertices[vCounter+1] += animVertex.y * boneWeight; - mesh.animVertices[vCounter+2] += animVertex.z * boneWeight; + mesh.animVertices[vCounter] += animVertex.x*boneWeight; + mesh.animVertices[vCounter+1] += animVertex.y*boneWeight; + mesh.animVertices[vCounter+2] += animVertex.z*boneWeight; updated = true; + // Normals processing // NOTE: We use meshes.baseNormals (default normal) to calculate meshes.normals (animated normals) if (mesh.normals != NULL) @@ -2360,6 +2362,7 @@ void UpdateModelAnimation(Model model, ModelAnimation anim, int frame) } } } + if (updated) { rlUpdateVertexBuffer(mesh.vboId[0], mesh.animVertices, mesh.vertexCount*3*sizeof(float), 0); // Update vertex position @@ -2725,11 +2728,11 @@ Mesh GenMeshCube(float width, float height, float length) #else // Use par_shapes library to generate cube mesh /* // Platonic solids: -par_shapes_mesh* par_shapes_create_tetrahedron(); // 4 sides polyhedron (pyramid) -par_shapes_mesh* par_shapes_create_cube(); // 6 sides polyhedron (cube) -par_shapes_mesh* par_shapes_create_octahedron(); // 8 sides polyhedron (diamond) -par_shapes_mesh* par_shapes_create_dodecahedron(); // 12 sides polyhedron -par_shapes_mesh* par_shapes_create_icosahedron(); // 20 sides polyhedron +par_shapes_mesh *par_shapes_create_tetrahedron(); // 4 sides polyhedron (pyramid) +par_shapes_mesh *par_shapes_create_cube(); // 6 sides polyhedron (cube) +par_shapes_mesh *par_shapes_create_octahedron(); // 8 sides polyhedron (diamond) +par_shapes_mesh *par_shapes_create_dodecahedron(); // 12 sides polyhedron +par_shapes_mesh *par_shapes_create_icosahedron(); // 20 sides polyhedron */ // Platonic solid generation: cube (6 sides) // NOTE: No normals/texcoords generated by default @@ -3840,7 +3843,7 @@ void DrawBillboardPro(Camera camera, Texture2D texture, Rectangle source, Vector for (int i = 0; i < 4; i++) { points[i] = Vector3Subtract(points[i], origin3D); - if (rotation != 0.0) points[i] = Vector3RotateByAxisAngle(points[i], forward, rotation * DEG2RAD); + if (rotation != 0.0) points[i] = Vector3RotateByAxisAngle(points[i], forward, rotation*DEG2RAD); points[i] = Vector3Add(points[i], position); } @@ -4049,7 +4052,7 @@ RayCollision GetRayCollisionMesh(Ray ray, Mesh mesh, Matrix transform) for (int i = 0; i < triangleCount; i++) { Vector3 a, b, c; - Vector3* vertdata = (Vector3*)mesh.vertices; + Vector3 *vertdata = (Vector3 *)mesh.vertices; if (mesh.indices) { @@ -4213,7 +4216,7 @@ static Model LoadOBJ(const char *fileName) if (CHDIR(workingDir) != 0) TRACELOG(LOG_WARNING, "MODEL: [%s] Failed to change working directory", workingDir); unsigned int dataSize = (unsigned int)strlen(fileText); - + unsigned int flags = TINYOBJ_FLAG_TRIANGULATE; int ret = tinyobj_parse_obj(&objAttributes, &objShapes, &objShapeCount, &objMaterials, &objMaterialCount, fileText, dataSize, flags); @@ -4316,7 +4319,7 @@ static Model LoadOBJ(const char *fileName) faceVertIndex += objAttributes.face_num_verts[faceId]; localMeshVertexCount += objAttributes.face_num_verts[faceId]; } - + localMeshVertexCounts[meshIndex] = localMeshVertexCount; for (int i = 0; i < model.meshCount; i++) @@ -4325,7 +4328,7 @@ static Model LoadOBJ(const char *fileName) unsigned int vertexCount = localMeshVertexCounts[i]; model.meshes[i].vertexCount = vertexCount; - model.meshes[i].triangleCount = vertexCount / 3; + model.meshes[i].triangleCount = vertexCount/3; model.meshes[i].vertices = (float *)MemAlloc(sizeof(float)*vertexCount*3); model.meshes[i].normals = (float *)MemAlloc(sizeof(float)*vertexCount*3); @@ -4360,7 +4363,7 @@ static Model LoadOBJ(const char *fileName) else nextShapeEnd = objAttributes.num_face_num_verts; // This is actually the total number of face verts in the file, not faces newMesh = true; } - + // If this is a new material, we need to allocate a new mesh if (lastMaterial != -1 && objAttributes.material_ids[faceId] != lastMaterial) newMesh = true; lastMaterial = objAttributes.material_ids[faceId]; @@ -5672,7 +5675,7 @@ static Model LoadGLTF(const char *fileName) else if (attribute->component_type == cgltf_component_type_r_8u) { // Init raylib mesh indices to copy glTF attribute data - model.meshes[meshIndex].indices = RL_MALLOC(attribute->count * sizeof(unsigned short)); + model.meshes[meshIndex].indices = RL_MALLOC(attribute->count*sizeof(unsigned short)); LOAD_ATTRIBUTE_CAST(attribute, 1, unsigned char, model.meshes[meshIndex].indices, unsigned short) } @@ -5727,7 +5730,7 @@ static Model LoadGLTF(const char *fileName) for (int i = 0; i < model.boneCount; i++) { - cgltf_node* node = skin.joints[i]; + cgltf_node *node = skin.joints[i]; cgltf_float worldTransform[16]; cgltf_node_transform_world(node, worldTransform); Matrix worldMatrix = { diff --git a/src/rtext.c b/src/rtext.c index 5c7c01fbe..005568dbf 100644 --- a/src/rtext.c +++ b/src/rtext.c @@ -1282,7 +1282,7 @@ Vector2 MeasureTextEx(Font font, const char *text, float fontSize, float spacing { Vector2 textSize = { 0 }; - if ((isGpuReady && (font.texture.id == 0)) || + if ((isGpuReady && (font.texture.id == 0)) || (text == NULL) || (text[0] == '\0')) return textSize; // Security check int size = TextLength(text); // Get size in bytes of text diff --git a/src/rtextures.c b/src/rtextures.c index aea6ab46c..57ee57604 100644 --- a/src/rtextures.c +++ b/src/rtextures.c @@ -829,11 +829,11 @@ Image GenImageGradientLinear(int width, int height, int direction, Color start, // Calculate how far the top-left pixel is along the gradient direction from the center of said gradient float startingPos = 0.5f - (cosDir*width/2) - (sinDir*height/2); - // With directions that lie in the first or third quadrant (i.e. from top-left to + // With directions that lie in the first or third quadrant (i.e. from top-left to // bottom-right or vice-versa), pixel (0, 0) is the farthest point on the gradient // (i.e. the pixel which should become one of the gradient's ends color); while for // directions that lie in the second or fourth quadrant, that point is pixel (width, 0). - float maxPosValue = + float maxPosValue = ((signbit(sinDir) != 0) == (signbit(cosDir) != 0)) ? fabsf(startingPos) : fabsf(startingPos+width*cosDir); @@ -842,12 +842,12 @@ Image GenImageGradientLinear(int width, int height, int direction, Color start, for (int j = 0; j < height; j++) { // Calculate the relative position of the pixel along the gradient direction - float pos = (startingPos + (i*cosDir + j*sinDir)) / maxPosValue; + float pos = (startingPos + (i*cosDir + j*sinDir))/maxPosValue; float factor = pos; factor = (factor > 1.0f)? 1.0f : factor; // Clamp to [-1,1] factor = (factor < -1.0f)? -1.0f : factor; // Clamp to [-1,1] - factor = factor / 2 + 0.5f; + factor = factor/2.0f + 0.5f; // Generate the color for this pixel pixels[j*width + i].r = (int)((float)end.r*factor + (float)start.r*(1.0f - factor)); @@ -1007,7 +1007,8 @@ Image GenImagePerlinNoise(int width, int height, int offsetX, int offsetY, float { Color *pixels = (Color *)RL_MALLOC(width*height*sizeof(Color)); - float aspectRatio = (float)width / (float)height; + float aspectRatio = (float)width/(float)height; + for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) @@ -5387,7 +5388,7 @@ static float HalfToFloat(unsigned short x) const unsigned int e = (x & 0x7C00) >> 10; // Exponent const unsigned int m = (x & 0x03FF) << 13; // Mantissa const float fm = (float)m; - const unsigned int v = (*(unsigned int*)&fm) >> 23; // Evil log2 bit hack to count leading zeros in denormalized format + const unsigned int v = (*(unsigned int *)&fm) >> 23; // Evil log2 bit hack to count leading zeros in denormalized format const unsigned int r = (x & 0x8000) << 16 | (e != 0)*((e + 112) << 23 | m) | ((e == 0)&(m != 0))*((v - 37) << 23 | ((m << (150 - v)) & 0x007FE000)); // sign : normalized : denormalized result = *(float *)&r; @@ -5400,7 +5401,7 @@ static unsigned short FloatToHalf(float x) { unsigned short result = 0; - const unsigned int b = (*(unsigned int*) & x) + 0x00001000; // Round-to-nearest-even: add last bit after truncated mantissa + const unsigned int b = (*(unsigned int *) & x) + 0x00001000; // Round-to-nearest-even: add last bit after truncated mantissa const unsigned int e = (b & 0x7F800000) >> 23; // Exponent const unsigned int m = b & 0x007FFFFF; // Mantissa; in line below: 0x007FF000 = 0x00800000-0x00001000 = decimal indicator flag - initial rounding From ca6c5f4f3c88b3a1091756ae70565335f5f734d6 Mon Sep 17 00:00:00 2001 From: 0riginaln0 <74508026+0riginaln0@users.noreply.github.com> Date: Sat, 7 Dec 2024 23:28:08 +0300 Subject: [PATCH 04/10] Update BINDINGS.md | Fennel Bindings (#4585) --- BINDINGS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/BINDINGS.md b/BINDINGS.md index 5d158c998..873e6af0e 100644 --- a/BINDINGS.md +++ b/BINDINGS.md @@ -85,6 +85,7 @@ Some people ported raylib to other languages in the form of bindings or wrappers | [raylib-cobol](https://codeberg.org/glowiak/raylib-cobol) | **auto** | [COBOL](https://gnucobol.sourceforge.io) | Public domain | | [raylib-apl](https://github.com/Brian-ED/raylib-apl) | **5.0** | [Dyalog APL](https://www.dyalog.com/) | MIT | | [raylib-jai](https://github.com/ahmedqarmout2/raylib-jai) | **5.5** | [Jai](https://github.com/BSVino/JaiPrimer/blob/master/JaiPrimer.md) | MIT | +| [fnl-raylib](https://github.com/0riginaln0/fnl-raylib) | **5.5** | [Fennel](https://fennel-lang.org/) | MIT | ### Utility Wrapers From 2820fcc29e91aac8155ffef45560070ecfc5752d Mon Sep 17 00:00:00 2001 From: danil <61111955+danilwhale@users.noreply.github.com> Date: Sun, 8 Dec 2024 09:52:09 +0200 Subject: [PATCH 05/10] [examples] improve input_virtual_controls example (#4584) --- examples/core/core_input_virtual_controls.c | 175 +++++++++++------- examples/core/core_input_virtual_controls.png | Bin 9094 -> 1802 bytes 2 files changed, 113 insertions(+), 62 deletions(-) diff --git a/examples/core/core_input_virtual_controls.c b/examples/core/core_input_virtual_controls.c index bbbad208f..76eeafee4 100644 --- a/examples/core/core_input_virtual_controls.c +++ b/examples/core/core_input_virtual_controls.c @@ -6,7 +6,8 @@ * * Example create by GreenSnakeLinux (@GreenSnakeLinux), * lighter by oblerion (@oblerion) and -* reviewed by Ramon Santamaria (@raysan5) +* reviewed by Ramon Santamaria (@raysan5) and +* improved by danilwhale (@danilwhale) * * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, * BSD-like license that allows static linking with closed source software @@ -17,6 +18,16 @@ #include "raylib.h" #include + +typedef enum { + BUTTON_NONE = -1, + BUTTON_UP, + BUTTON_LEFT, + BUTTON_RIGHT, + BUTTON_DOWN, + BUTTON_MAX +} PadButton; + //------------------------------------------------------------------------------------ // Program main entry point //------------------------------------------------------------------------------------ @@ -29,24 +40,38 @@ int main(void) InitWindow(screenWidth, screenHeight, "raylib [core] example - input virtual controls"); - const float dpadX = 90; - const float dpadY = 300; - const float dpadRad = 25.0f;//radius of each pad - Color dpadColor = BLUE; - int dpadKeydown = -1;//-1 if not down, else 0,1,2,3 + Vector2 padPosition = { 100, 350 }; + float buttonRadius = 30; - - const float dpadCollider[4][2]= // collider array with x,y position + Vector2 buttonPositions[BUTTON_MAX] = { - {dpadX,dpadY-dpadRad*1.5f},//up - {dpadX-dpadRad*1.5f,dpadY},//left - {dpadX+dpadRad*1.5f,dpadY},//right - {dpadX,dpadY+dpadRad*1.5f}//down + { padPosition.x,padPosition.y - buttonRadius*1.5f }, // Up + { padPosition.x - buttonRadius*1.5f, padPosition.y }, // Left + { padPosition.x + buttonRadius*1.5f, padPosition.y }, // Right + { padPosition.x, padPosition.y + buttonRadius*1.5f } // Down }; - const char dpadLabel[4]="XYBA";//label of Dpad - float playerX=100; - float playerY=100; + const char *buttonLabels[BUTTON_MAX] = + { + "Y", // Up + "X", // Left + "B", // Right + "A" // Down + }; + + Color buttonLabelColors[BUTTON_MAX] = + { + YELLOW, // Up + BLUE, // Left + RED, // Right + GREEN // Down + }; + + int pressedButton = BUTTON_NONE; + Vector2 inputPosition = { 0, 0 }; + + Vector2 playerPosition = { (float)screenWidth/2, (float)screenHeight/2 }; + float playerSpeed = 75; SetTargetFPS(60); //-------------------------------------------------------------------------------------- @@ -54,63 +79,89 @@ int main(void) // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { - // Update - //-------------------------------------------------------------------------- - dpadKeydown = -1; //reset - int inputX = 0; - int inputY = 0; - if(GetTouchPointCount()>0) - {//use touch pos - inputX = GetTouchX(); - inputY = GetTouchY(); + // Update + //-------------------------------------------------------------------------- + if ((GetTouchPointCount() > 0)) + { + // Use touch position + inputPosition = GetTouchPosition(0); } else - {//use mouse pos - inputX = GetMouseX(); - inputY = GetMouseY(); - } - for(int i=0;i<4;i++) { - //test distance each collider and input < radius - if( fabsf(dpadCollider[i][1]-inputY) + fabsf(dpadCollider[i][0]-inputX) < dpadRad) - { - dpadKeydown = i; - break; - } + // Use mouse position + inputPosition = GetMousePosition(); } - // move player - switch(dpadKeydown){ - case 0: playerY -= 50*GetFrameTime(); - break; - case 1: playerX -= 50*GetFrameTime(); - break; - case 2: playerX += 50*GetFrameTime(); - break; - case 3: playerY += 50*GetFrameTime(); - default:; - }; - //-------------------------------------------------------------------------- - // Draw - //-------------------------------------------------------------------------- - BeginDrawing(); - ClearBackground(RAYWHITE); - for(int i=0;i<4;i++) + + // Reset pressed button to none + pressedButton = BUTTON_NONE; + + // Make sure user is pressing left mouse button if they're from desktop + if ((GetTouchPointCount() > 0) || ((GetTouchPointCount() == 0) && IsMouseButtonDown(MOUSE_BUTTON_LEFT))) + { + // Find nearest D-Pad button to the input position + for (int i = 0; i < BUTTON_MAX; i++) { - //draw all pad - DrawCircleV((Vector2) { dpadCollider[i][0], dpadCollider[i][1] }, dpadRad, dpadColor); - if(i!=dpadKeydown) + float distX = fabsf(buttonPositions[i].x - inputPosition.x); + float distY = fabsf(buttonPositions[i].y - inputPosition.y); + + if ((distX + distY < buttonRadius)) { - //draw label - DrawText(TextSubtext(dpadLabel,i,1), - (int)dpadCollider[i][0]-7, - (int)dpadCollider[i][1]-8,20,BLACK); + pressedButton = i; + break; } } + } + + // Move player according to pressed button + switch (pressedButton) + { + case BUTTON_UP: + { + playerPosition.y -= playerSpeed*GetFrameTime(); + break; + } + case BUTTON_LEFT: + { + playerPosition.x -= playerSpeed*GetFrameTime(); + break; + } + case BUTTON_RIGHT: + { + playerPosition.x += playerSpeed*GetFrameTime(); + break; + } + case BUTTON_DOWN: + { + playerPosition.y += playerSpeed*GetFrameTime(); + break; + } + default: break; + }; + + //-------------------------------------------------------------------------- + // Draw + //-------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + // Draw world + DrawCircleV(playerPosition, 50, MAROON); + + // Draw GUI + for (int i = 0; i < BUTTON_MAX; i++) + { + DrawCircleV(buttonPositions[i], buttonRadius, (i == pressedButton)? DARKGRAY : BLACK); + + DrawText(buttonLabels[i], + (int)buttonPositions[i].x - 7, (int)buttonPositions[i].y - 8, + 20, buttonLabelColors[i]); + } + + DrawText("move the player with D-Pad buttons", 10, 10, 20, DARKGRAY); - DrawRectangleRec((Rectangle) { playerX - 4, playerY - 4, 75, 28 }, RED); - DrawText("Player", (int)playerX, (int)playerY, 20, WHITE); EndDrawing(); - //-------------------------------------------------------------------------- + //-------------------------------------------------------------------------- } // De-Initialization diff --git a/examples/core/core_input_virtual_controls.png b/examples/core/core_input_virtual_controls.png index cb1444598017b9d7ff83480f66909c8523ece519..83097a54ca46ebed8c1958e4ed7854cbbd2286a6 100644 GIT binary patch literal 1802 zcmbVMdpJ~E6hC8T@+zP+%e_WKxE0ZxyhfA4(Bm@X?laUM_s_e(Z=d~JzqR&WXRZA^=d7!<{URkTB?y8R z(P`V~I>}7;fhozAKm&ew-QV z54kb}+2MxHcJ8p5p|PQ{Z-v`J5F_ySq_gPsb&vuuh>#YJ2;l&708@J&gExlAIJ}IP z1K1cR2dRP88_d8&f{Z@|FgC^`aL{{D*pKx;Ng?3k$wLGXiEJ=Ia5v^o#WE6a%$;-z zU~I^NbLC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$=!uac6JkCWcmb=Viy;*!`R)ga(JWsXmWDCRD8{n5Iy z(ElrlGqx$gq*eI$vFA4>nT8iOFrVslS_UZY=w`uFW9;U2xWSdE+YqoCB@WF$UU2B( z%1PV(OX=ov$6+NLl^$KtZp{C~&hj3T`@nW-F;z)yQFV;!XLMBeQXFb`^QEYEp1|Ey z=RiCkKk^3r$46wfz2T58FU9l5e{T1FQ*Nh$Yd`Gzl0Ap|;6d@;EKIQQ^VZ2C!pp1l z0Eh2aE9?U7G++ty( ze|LQTI#}Xu`}f#)k#qo}@%vZ@*DVE(r%f-9B$PhB2EbH$_LagiwGXGGh+;*Ny0J3j z$MgZwhIZ|}2R86-C;BDmiS4hx0) zK>Y046heF6>h`)9zLcjZy51NTS|4rTy>#+k^OV>~BwHEos{%zl7~35&>kYegtJ8oe z`!%x~zuruZdI~cLdDx==<;CI=K-7KmB&B@ZX@xc9zp+>DNv8wU)zuH6;ec;hs#6G2 zn;R-F%;u1zF$G3o{5Z+OL6y`@OOkZv4a5G0MW2%cZM`-&r_TVl3NHx=DC5vSNzz{c z6ua~)Etg`Jbh#MHjfmqOcmSNP=DJ3hIFec*jz|7Q!ygoB=bSpYWBkTg|Euno z)*r6_z=i)V3qPsxKX>VK)<#Yk?mWg@=d#S8R+yLRy;h|JKZONif*a z1MVB{jrLe-JwgM_4)#_JkhqAU=M@ycq4zn(}LeH~8rh&Fc+U{snNKpPe1Kd;5yj`GV H{`@}x#HYs# From 93a67417b336a4e0814d37dc37b4591f03b9588c Mon Sep 17 00:00:00 2001 From: Asdqwe Date: Sun, 8 Dec 2024 08:46:43 -0300 Subject: [PATCH 06/10] Fix examples Makefile PLATFORM define (#4582) --- examples/Makefile | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/examples/Makefile b/examples/Makefile index a65a4961e..34cf36f04 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -54,10 +54,15 @@ # Define target platform: PLATFORM_DESKTOP, PLATFORM_DESKTOP_SDL, PLATFORM_DRM, PLATFORM_ANDROID, PLATFORM_WEB PLATFORM ?= PLATFORM_DESKTOP -ifeq ($(PLATFORM), PLATFORM_DESKTOP) - TARGET_PLATFORM = PLATFORM_DESKTOP_GLFW +ifeq ($(PLATFORM),$(filter $(PLATFORM),PLATFORM_DESKTOP_GLFW PLATFORM_DESKTOP_SDL PLATFORM_DESKTOP_RGFW)) + TARGET_PLATFORM := $(PLATFORM) + override PLATFORM = PLATFORM_DESKTOP else - TARGET_PLATFORM = $(PLATFORM) + ifeq ($(PLATFORM), PLATFORM_DESKTOP) + TARGET_PLATFORM = PLATFORM_DESKTOP_GLFW + else + TARGET_PLATFORM = $(PLATFORM) + endif endif # Define required raylib variables @@ -653,7 +658,7 @@ OTHERS = \ ifeq ($(TARGET_PLATFORM), PLATFORM_DESKTOP_GFLW) OTHERS += others/rlgl_standalone endif - + CURRENT_MAKEFILE = $(lastword $(MAKEFILE_LIST)) From d2cd2a01524ca5b14cfa6d72255e891c76b29bd0 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 8 Dec 2024 12:48:54 +0100 Subject: [PATCH 07/10] [rlgl][rmodels] Add instranceTransform shader location index #4538 (#4579) --- .../resources/shaders/glsl330/lighting_instancing.vs | 2 +- examples/shaders/shaders_mesh_instancing.c | 3 +-- src/config.h | 2 ++ src/raylib.h | 3 ++- src/rcore.c | 1 + src/rlgl.h | 7 +++++++ src/rmodels.c | 8 ++++---- 7 files changed, 18 insertions(+), 8 deletions(-) diff --git a/examples/shaders/resources/shaders/glsl330/lighting_instancing.vs b/examples/shaders/resources/shaders/glsl330/lighting_instancing.vs index 3e4da1e28..32db8cdd4 100644 --- a/examples/shaders/resources/shaders/glsl330/lighting_instancing.vs +++ b/examples/shaders/resources/shaders/glsl330/lighting_instancing.vs @@ -25,7 +25,7 @@ void main() // Send vertex attributes to fragment shader fragPosition = vec3(instanceTransform*vec4(vertexPosition, 1.0)); fragTexCoord = vertexTexCoord; - //fragColor = vertexColor; + fragColor = vec4(1.0); fragNormal = normalize(vec3(matNormal*vec4(vertexNormal, 1.0))); // Calculate final vertex position, note that we multiply mvp by instanceTransform diff --git a/examples/shaders/shaders_mesh_instancing.c b/examples/shaders/shaders_mesh_instancing.c index eb42cb471..1e0bf0696 100644 --- a/examples/shaders/shaders_mesh_instancing.c +++ b/examples/shaders/shaders_mesh_instancing.c @@ -61,7 +61,7 @@ int main(void) { Matrix translation = MatrixTranslate((float)GetRandomValue(-50, 50), (float)GetRandomValue(-50, 50), (float)GetRandomValue(-50, 50)); Vector3 axis = Vector3Normalize((Vector3){ (float)GetRandomValue(0, 360), (float)GetRandomValue(0, 360), (float)GetRandomValue(0, 360) }); - float angle = (float)GetRandomValue(0, 10)*DEG2RAD; + float angle = (float)GetRandomValue(0, 180)*DEG2RAD; Matrix rotation = MatrixRotate(axis, angle); transforms[i] = MatrixMultiply(rotation, translation); @@ -73,7 +73,6 @@ int main(void) // Get shader locations shader.locs[SHADER_LOC_MATRIX_MVP] = GetShaderLocation(shader, "mvp"); shader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos"); - shader.locs[SHADER_LOC_MATRIX_MODEL] = GetShaderLocationAttrib(shader, "instanceTransform"); // Set shader value: ambient light level int ambientLoc = GetShaderLocation(shader, "ambient"); diff --git a/src/config.h b/src/config.h index d8f7112eb..74e0a1353 100644 --- a/src/config.h +++ b/src/config.h @@ -151,6 +151,8 @@ #define RL_DEFAULT_SHADER_ATTRIB_LOCATION_BONEIDS 7 #define RL_DEFAULT_SHADER_ATTRIB_LOCATION_BONEWEIGHTS 8 #endif +#define RL_DEFAULT_SHADER_ATTRIB_LOCATION_INSTANCE_TX 9 + // Default shader vertex attribute names to set location points // NOTE: When a new shader is loaded, the following locations are tried to be set for convenience diff --git a/src/raylib.h b/src/raylib.h index 641bd10e0..73c8cca5d 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -801,7 +801,8 @@ typedef enum { SHADER_LOC_MAP_BRDF, // Shader location: sampler2d texture: brdf SHADER_LOC_VERTEX_BONEIDS, // Shader location: vertex attribute: boneIds SHADER_LOC_VERTEX_BONEWEIGHTS, // Shader location: vertex attribute: boneWeights - SHADER_LOC_BONE_MATRICES // Shader location: array of matrices uniform: boneMatrices + SHADER_LOC_BONE_MATRICES, // Shader location: array of matrices uniform: boneMatrices + SHADER_LOC_VERTEX_INSTANCE_TX // Shader location: vertex attribute: instanceTransform } ShaderLocationIndex; #define SHADER_LOC_MAP_DIFFUSE SHADER_LOC_MAP_ALBEDO diff --git a/src/rcore.c b/src/rcore.c index bcff5acb0..d69fa0f6c 100644 --- a/src/rcore.c +++ b/src/rcore.c @@ -1346,6 +1346,7 @@ Shader LoadShaderFromMemory(const char *vsCode, const char *fsCode) shader.locs[SHADER_LOC_VERTEX_COLOR] = rlGetLocationAttrib(shader.id, RL_DEFAULT_SHADER_ATTRIB_NAME_COLOR); shader.locs[SHADER_LOC_VERTEX_BONEIDS] = rlGetLocationAttrib(shader.id, RL_DEFAULT_SHADER_ATTRIB_NAME_BONEIDS); shader.locs[SHADER_LOC_VERTEX_BONEWEIGHTS] = rlGetLocationAttrib(shader.id, RL_DEFAULT_SHADER_ATTRIB_NAME_BONEWEIGHTS); + shader.locs[SHADER_LOC_VERTEX_INSTANCE_TX] = rlGetLocationAttrib(shader.id, RL_DEFAULT_SHADER_ATTRIB_NAME_INSTANCE_TX); // Get handles to GLSL uniform locations (vertex shader) shader.locs[SHADER_LOC_MATRIX_MVP] = rlGetLocationUniform(shader.id, RL_DEFAULT_SHADER_UNIFORM_NAME_MVP); diff --git a/src/rlgl.h b/src/rlgl.h index c08623de3..b09b04b18 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -355,6 +355,9 @@ #define RL_DEFAULT_SHADER_ATTRIB_LOCATION_BONEWEIGHTS 8 #endif #endif +#ifndef RL_DEFAULT_SHADER_ATTRIB_LOCATION_INSTANCE_TX + #define RL_DEFAULT_SHADER_ATTRIB_LOCATION_INSTANCE_TX 9 +#endif //---------------------------------------------------------------------------------- // Types and Structures Definition @@ -998,6 +1001,9 @@ RLAPI void rlLoadDrawQuad(void); // Load and draw a quad #ifndef RL_DEFAULT_SHADER_ATTRIB_NAME_BONEWEIGHTS #define RL_DEFAULT_SHADER_ATTRIB_NAME_BONEWEIGHTS "vertexBoneWeights" // Bound by default to shader location: RL_DEFAULT_SHADER_ATTRIB_NAME_BONEWEIGHTS #endif +#ifndef RL_DEFAULT_SHADER_ATTRIB_NAME_INSTANCE_TX + #define RL_DEFAULT_SHADER_ATTRIB_NAME_INSTANCE_TX "instanceTransform" // Bound by default to shader location: RL_DEFAULT_SHADER_ATTRIB_NAME_INSTANCE_TX +#endif #ifndef RL_DEFAULT_SHADER_UNIFORM_NAME_MVP #define RL_DEFAULT_SHADER_UNIFORM_NAME_MVP "mvp" // model-view-projection matrix @@ -4216,6 +4222,7 @@ unsigned int rlLoadShaderProgram(unsigned int vShaderId, unsigned int fShaderId) glBindAttribLocation(program, RL_DEFAULT_SHADER_ATTRIB_LOCATION_COLOR, RL_DEFAULT_SHADER_ATTRIB_NAME_COLOR); glBindAttribLocation(program, RL_DEFAULT_SHADER_ATTRIB_LOCATION_TANGENT, RL_DEFAULT_SHADER_ATTRIB_NAME_TANGENT); glBindAttribLocation(program, RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD2, RL_DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD2); + glBindAttribLocation(program, RL_DEFAULT_SHADER_ATTRIB_LOCATION_INSTANCE_TX, RL_DEFAULT_SHADER_ATTRIB_NAME_INSTANCE_TX); #ifdef RL_SUPPORT_MESH_GPU_SKINNING glBindAttribLocation(program, RL_DEFAULT_SHADER_ATTRIB_LOCATION_BONEIDS, RL_DEFAULT_SHADER_ATTRIB_NAME_BONEIDS); diff --git a/src/rmodels.c b/src/rmodels.c index 24f4a4fc9..a159d5432 100644 --- a/src/rmodels.c +++ b/src/rmodels.c @@ -1734,12 +1734,12 @@ void DrawMeshInstanced(Mesh mesh, Material material, const Matrix *transforms, i // no faster, since we're transferring all the transform matrices anyway instancesVboId = rlLoadVertexBuffer(instanceTransforms, instances*sizeof(float16), false); - // Instances transformation matrices are send to shader attribute location: SHADER_LOC_MATRIX_MODEL + // Instances transformation matrices are sent to shader attribute location: SHADER_LOC_VERTEX_INSTANCE_TX for (unsigned int i = 0; i < 4; i++) { - rlEnableVertexAttribute(material.shader.locs[SHADER_LOC_MATRIX_MODEL] + i); - rlSetVertexAttribute(material.shader.locs[SHADER_LOC_MATRIX_MODEL] + i, 4, RL_FLOAT, 0, sizeof(Matrix), i*sizeof(Vector4)); - rlSetVertexAttributeDivisor(material.shader.locs[SHADER_LOC_MATRIX_MODEL] + i, 1); + rlEnableVertexAttribute(material.shader.locs[SHADER_LOC_VERTEX_INSTANCE_TX] + i); + rlSetVertexAttribute(material.shader.locs[SHADER_LOC_VERTEX_INSTANCE_TX] + i, 4, RL_FLOAT, 0, sizeof(Matrix), i*sizeof(Vector4)); + rlSetVertexAttributeDivisor(material.shader.locs[SHADER_LOC_VERTEX_INSTANCE_TX] + i, 1); } rlDisableVertexBuffer(); From 732da949b72a629fbd6b7e7f9fcab3d88f8355ee Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Sun, 8 Dec 2024 11:49:07 +0000 Subject: [PATCH 08/10] Update raylib_api.* by CI --- parser/output/raylib_api.json | 5 +++++ parser/output/raylib_api.lua | 5 +++++ parser/output/raylib_api.txt | 3 ++- parser/output/raylib_api.xml | 3 ++- 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/parser/output/raylib_api.json b/parser/output/raylib_api.json index 3eb438392..9614f0cac 100644 --- a/parser/output/raylib_api.json +++ b/parser/output/raylib_api.json @@ -2543,6 +2543,11 @@ "name": "SHADER_LOC_BONE_MATRICES", "value": 28, "description": "Shader location: array of matrices uniform: boneMatrices" + }, + { + "name": "SHADER_LOC_VERTEX_INSTANCE_TX", + "value": 29, + "description": "Shader location: vertex attribute: instanceTransform" } ] }, diff --git a/parser/output/raylib_api.lua b/parser/output/raylib_api.lua index da3faf073..00547334e 100644 --- a/parser/output/raylib_api.lua +++ b/parser/output/raylib_api.lua @@ -2543,6 +2543,11 @@ return { name = "SHADER_LOC_BONE_MATRICES", value = 28, description = "Shader location: array of matrices uniform: boneMatrices" + }, + { + name = "SHADER_LOC_VERTEX_INSTANCE_TX", + value = 29, + description = "Shader location: vertex attribute: instanceTransform" } } }, diff --git a/parser/output/raylib_api.txt b/parser/output/raylib_api.txt index 5c7c38701..09dc8cfeb 100644 --- a/parser/output/raylib_api.txt +++ b/parser/output/raylib_api.txt @@ -795,7 +795,7 @@ Enum 08: MaterialMapIndex (11 values) Value[MATERIAL_MAP_IRRADIANCE]: 8 Value[MATERIAL_MAP_PREFILTER]: 9 Value[MATERIAL_MAP_BRDF]: 10 -Enum 09: ShaderLocationIndex (29 values) +Enum 09: ShaderLocationIndex (30 values) Name: ShaderLocationIndex Description: Shader location index Value[SHADER_LOC_VERTEX_POSITION]: 0 @@ -827,6 +827,7 @@ Enum 09: ShaderLocationIndex (29 values) Value[SHADER_LOC_VERTEX_BONEIDS]: 26 Value[SHADER_LOC_VERTEX_BONEWEIGHTS]: 27 Value[SHADER_LOC_BONE_MATRICES]: 28 + Value[SHADER_LOC_VERTEX_INSTANCE_TX]: 29 Enum 10: ShaderUniformDataType (9 values) Name: ShaderUniformDataType Description: Shader uniform data type diff --git a/parser/output/raylib_api.xml b/parser/output/raylib_api.xml index 81cb6a371..9e870437f 100644 --- a/parser/output/raylib_api.xml +++ b/parser/output/raylib_api.xml @@ -507,7 +507,7 @@ - + @@ -537,6 +537,7 @@ + From 2a2acff2d5208b443f6303fc18efe8946c35be06 Mon Sep 17 00:00:00 2001 From: Rico P Date: Sun, 8 Dec 2024 12:52:06 +0100 Subject: [PATCH 09/10] Make sure ShaderUniformDataType matches rlShaderUniformDataType (#4577) * Make sure ShaderUniformDataType matches rlShaderUniformDataType * Update raylib_api.* by CI --------- Co-authored-by: github-actions[bot] Co-authored-by: Ray --- parser/output/raylib_api.json | 22 +++++++++++++++++++++- parser/output/raylib_api.lua | 22 +++++++++++++++++++++- parser/output/raylib_api.txt | 9 ++++++--- parser/output/raylib_api.xml | 8 ++++++-- src/raylib.h | 4 ++++ 5 files changed, 58 insertions(+), 7 deletions(-) diff --git a/parser/output/raylib_api.json b/parser/output/raylib_api.json index 9614f0cac..e05d8a748 100644 --- a/parser/output/raylib_api.json +++ b/parser/output/raylib_api.json @@ -2596,8 +2596,28 @@ "description": "Shader uniform type: ivec4 (4 int)" }, { - "name": "SHADER_UNIFORM_SAMPLER2D", + "name": "SHADER_UNIFORM_UINT", "value": 8, + "description": "Shader uniform type: unsigned int" + }, + { + "name": "SHADER_UNIFORM_UIVEC2", + "value": 9, + "description": "Shader uniform type: uivec2 (2 unsigned int)" + }, + { + "name": "SHADER_UNIFORM_UIVEC3", + "value": 10, + "description": "Shader uniform type: uivec3 (3 unsigned int)" + }, + { + "name": "SHADER_UNIFORM_UIVEC4", + "value": 11, + "description": "Shader uniform type: uivec4 (4 unsigned int)" + }, + { + "name": "SHADER_UNIFORM_SAMPLER2D", + "value": 12, "description": "Shader uniform type: sampler2d" } ] diff --git a/parser/output/raylib_api.lua b/parser/output/raylib_api.lua index 00547334e..d8a848b45 100644 --- a/parser/output/raylib_api.lua +++ b/parser/output/raylib_api.lua @@ -2596,8 +2596,28 @@ return { description = "Shader uniform type: ivec4 (4 int)" }, { - name = "SHADER_UNIFORM_SAMPLER2D", + name = "SHADER_UNIFORM_UINT", value = 8, + description = "Shader uniform type: unsigned int" + }, + { + name = "SHADER_UNIFORM_UIVEC2", + value = 9, + description = "Shader uniform type: uivec2 (2 unsigned int)" + }, + { + name = "SHADER_UNIFORM_UIVEC3", + value = 10, + description = "Shader uniform type: uivec3 (3 unsigned int)" + }, + { + name = "SHADER_UNIFORM_UIVEC4", + value = 11, + description = "Shader uniform type: uivec4 (4 unsigned int)" + }, + { + name = "SHADER_UNIFORM_SAMPLER2D", + value = 12, description = "Shader uniform type: sampler2d" } } diff --git a/parser/output/raylib_api.txt b/parser/output/raylib_api.txt index 09dc8cfeb..d3b6eb895 100644 --- a/parser/output/raylib_api.txt +++ b/parser/output/raylib_api.txt @@ -827,8 +827,7 @@ Enum 09: ShaderLocationIndex (30 values) Value[SHADER_LOC_VERTEX_BONEIDS]: 26 Value[SHADER_LOC_VERTEX_BONEWEIGHTS]: 27 Value[SHADER_LOC_BONE_MATRICES]: 28 - Value[SHADER_LOC_VERTEX_INSTANCE_TX]: 29 -Enum 10: ShaderUniformDataType (9 values) +Enum 10: ShaderUniformDataType (13 values) Name: ShaderUniformDataType Description: Shader uniform data type Value[SHADER_UNIFORM_FLOAT]: 0 @@ -839,7 +838,11 @@ Enum 10: ShaderUniformDataType (9 values) Value[SHADER_UNIFORM_IVEC2]: 5 Value[SHADER_UNIFORM_IVEC3]: 6 Value[SHADER_UNIFORM_IVEC4]: 7 - Value[SHADER_UNIFORM_SAMPLER2D]: 8 + Value[SHADER_UNIFORM_UINT]: 8 + Value[SHADER_UNIFORM_UIVEC2]: 9 + Value[SHADER_UNIFORM_UIVEC3]: 10 + Value[SHADER_UNIFORM_UIVEC4]: 11 + Value[SHADER_UNIFORM_SAMPLER2D]: 12 Enum 11: ShaderAttributeDataType (4 values) Name: ShaderAttributeDataType Description: Shader attribute data types diff --git a/parser/output/raylib_api.xml b/parser/output/raylib_api.xml index 9e870437f..e1b1f35e2 100644 --- a/parser/output/raylib_api.xml +++ b/parser/output/raylib_api.xml @@ -539,7 +539,7 @@ - + @@ -548,7 +548,11 @@ - + + + + + diff --git a/src/raylib.h b/src/raylib.h index 73c8cca5d..7482d4392 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -818,6 +818,10 @@ typedef enum { SHADER_UNIFORM_IVEC2, // Shader uniform type: ivec2 (2 int) SHADER_UNIFORM_IVEC3, // Shader uniform type: ivec3 (3 int) SHADER_UNIFORM_IVEC4, // Shader uniform type: ivec4 (4 int) + SHADER_UNIFORM_UINT, // Shader uniform type: unsigned int + SHADER_UNIFORM_UIVEC2, // Shader uniform type: uivec2 (2 unsigned int) + SHADER_UNIFORM_UIVEC3, // Shader uniform type: uivec3 (3 unsigned int) + SHADER_UNIFORM_UIVEC4, // Shader uniform type: uivec4 (4 unsigned int) SHADER_UNIFORM_SAMPLER2D // Shader uniform type: sampler2d } ShaderUniformDataType; From 83f6f3dabeb14b8f5268bf3abdcc898f50cfeb17 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Sun, 8 Dec 2024 11:52:21 +0000 Subject: [PATCH 10/10] Update raylib_api.* by CI --- parser/output/raylib_api.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/parser/output/raylib_api.txt b/parser/output/raylib_api.txt index d3b6eb895..7c7b003c4 100644 --- a/parser/output/raylib_api.txt +++ b/parser/output/raylib_api.txt @@ -827,6 +827,7 @@ Enum 09: ShaderLocationIndex (30 values) Value[SHADER_LOC_VERTEX_BONEIDS]: 26 Value[SHADER_LOC_VERTEX_BONEWEIGHTS]: 27 Value[SHADER_LOC_BONE_MATRICES]: 28 + Value[SHADER_LOC_VERTEX_INSTANCE_TX]: 29 Enum 10: ShaderUniformDataType (13 values) Name: ShaderUniformDataType Description: Shader uniform data type