From dfac74ffa7f2f24516ec18a3fa6345a2d238f247 Mon Sep 17 00:00:00 2001 From: Philip Shenk Date: Fri, 21 Jan 2022 02:53:25 -0500 Subject: [PATCH 01/12] updated VSCode project to work with latest raylib makefile and 'working for web' instructions (#2296) --- projects/VSCode/Makefile | 15 +++--- projects/VSCode/main.c | 85 ++++++++++++++++++++----------- projects/VSCode/resources/LICENSE | 1 + 3 files changed, 64 insertions(+), 37 deletions(-) create mode 100644 projects/VSCode/resources/LICENSE diff --git a/projects/VSCode/Makefile b/projects/VSCode/Makefile index d89a80ffe..9907c036d 100644 --- a/projects/VSCode/Makefile +++ b/projects/VSCode/Makefile @@ -117,13 +117,12 @@ endif ifeq ($(PLATFORM),PLATFORM_WEB) # Emscripten required variables - EMSDK_PATH ?= C:/emsdk - EMSCRIPTEN_VERSION ?= 1.38.31 - CLANG_VERSION = e$(EMSCRIPTEN_VERSION)_64bit - PYTHON_VERSION = 2.7.13.1_64bit\python-2.7.13.amd64 - NODE_VERSION = 8.9.1_64bit - export PATH = $(EMSDK_PATH);$(EMSDK_PATH)\clang\$(CLANG_VERSION);$(EMSDK_PATH)\node\$(NODE_VERSION)\bin;$(EMSDK_PATH)\python\$(PYTHON_VERSION);$(EMSDK_PATH)\emscripten\$(EMSCRIPTEN_VERSION);C:\raylib\MinGW\bin:$$(PATH) - EMSCRIPTEN = $(EMSDK_PATH)\emscripten\$(EMSCRIPTEN_VERSION) + EMSDK_PATH ?= C:/emsdk + EMSCRIPTEN_PATH ?= $(EMSDK_PATH)/upstream/emscripten + CLANG_PATH = $(EMSDK_PATH)/upstream/bin + PYTHON_PATH = $(EMSDK_PATH)/python/3.9.2-1_64bit + NODE_PATH = $(EMSDK_PATH)/node/14.18.2_64bit/bin + export PATH = $(EMSDK_PATH);$(EMSCRIPTEN_PATH);$(CLANG_PATH);$(NODE_PATH);$(PYTHON_PATH):$$(PATH) endif # Define raylib release directory for compiled library. @@ -344,7 +343,7 @@ ifeq ($(PLATFORM),PLATFORM_RPI) endif ifeq ($(PLATFORM),PLATFORM_WEB) # Libraries for web (HTML5) compiling - LDLIBS = $(RAYLIB_RELEASE_PATH)/libraylib.bc + LDLIBS = $(RAYLIB_RELEASE_PATH)/libraylib.a endif # Define a recursive wildcard function diff --git a/projects/VSCode/main.c b/projects/VSCode/main.c index 835b4b08f..2d7b121f9 100644 --- a/projects/VSCode/main.c +++ b/projects/VSCode/main.c @@ -15,12 +15,30 @@ * This example has been created using raylib 1.0 (www.raylib.com) * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) * -* Copyright (c) 2013-2020 Ramon Santamaria (@raysan5) +* Copyright (c) 2013-2022 Ramon Santamaria (@raysan5) * ********************************************************************************************/ #include "raylib.h" +#if defined(PLATFORM_WEB) + #include +#endif + +//---------------------------------------------------------------------------------- +// Local Variables Definition (local to this module) +//---------------------------------------------------------------------------------- +Camera camera = { 0 }; +Vector3 cubePosition = { 0 }; + +//---------------------------------------------------------------------------------- +// Local Functions Declaration +//---------------------------------------------------------------------------------- +static void UpdateDrawFrame(void); // Update and draw one frame + +//---------------------------------------------------------------------------------- +// Main entry point +//---------------------------------------------------------------------------------- int main() { // Initialization @@ -30,7 +48,6 @@ int main() InitWindow(screenWidth, screenHeight, "raylib"); - Camera camera = { 0 }; camera.position = (Vector3){ 10.0f, 10.0f, 8.0f }; camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; @@ -39,45 +56,55 @@ int main() SetCameraMode(camera, CAMERA_ORBITAL); - Vector3 cubePosition = { 0 }; + //-------------------------------------------------------------------------------------- +#if defined(PLATFORM_WEB) + emscripten_set_main_loop(UpdateDrawFrame, 60, 1); +#else SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { - // Update - //---------------------------------------------------------------------------------- - UpdateCamera(&camera); - //---------------------------------------------------------------------------------- - - // Draw - //---------------------------------------------------------------------------------- - BeginDrawing(); - - ClearBackground(RAYWHITE); - - BeginMode3D(camera); - - DrawCube(cubePosition, 2.0f, 2.0f, 2.0f, RED); - DrawCubeWires(cubePosition, 2.0f, 2.0f, 2.0f, MAROON); - DrawGrid(10, 1.0f); - - EndMode3D(); - - DrawText("This is a raylib example", 10, 40, 20, DARKGRAY); - - DrawFPS(10, 10); - - EndDrawing(); - //---------------------------------------------------------------------------------- + UpdateDrawFrame(); } +#endif // De-Initialization //-------------------------------------------------------------------------------------- - CloseWindow(); // Close window and OpenGL context + CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; +} + +// Update and draw game frame +static void UpdateDrawFrame(void) +{ + // Update + //---------------------------------------------------------------------------------- + UpdateCamera(&camera); + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + BeginMode3D(camera); + + DrawCube(cubePosition, 2.0f, 2.0f, 2.0f, RED); + DrawCubeWires(cubePosition, 2.0f, 2.0f, 2.0f, MAROON); + DrawGrid(10, 1.0f); + + EndMode3D(); + + DrawText("This is a raylib example", 10, 40, 20, DARKGRAY); + + DrawFPS(10, 10); + + EndDrawing(); + //---------------------------------------------------------------------------------- } \ No newline at end of file diff --git a/projects/VSCode/resources/LICENSE b/projects/VSCode/resources/LICENSE new file mode 100644 index 000000000..ebde2c573 --- /dev/null +++ b/projects/VSCode/resources/LICENSE @@ -0,0 +1 @@ +Assets license. From 45ef46c5e809e4dbfb2dfe2530350f05db7641b8 Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Fri, 21 Jan 2022 19:52:07 +0800 Subject: [PATCH 02/12] fix: material color won't be loaded if there's no texture for that material (#2298) --- src/rmodels.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/rmodels.c b/src/rmodels.c index 22559e34f..601ce234d 100644 --- a/src/rmodels.c +++ b/src/rmodels.c @@ -4692,13 +4692,12 @@ static Model LoadGLTF(const char *fileName) model.materials[j].maps[MATERIAL_MAP_ALBEDO].texture = LoadTextureFromImage(imAlbedo); UnloadImage(imAlbedo); } - - // Load base color factor (tint) - model.materials[j].maps[MATERIAL_MAP_ALBEDO].color.r = (unsigned char)(data->materials[i].pbr_metallic_roughness.base_color_factor[0]*255); - model.materials[j].maps[MATERIAL_MAP_ALBEDO].color.g = (unsigned char)(data->materials[i].pbr_metallic_roughness.base_color_factor[1]*255); - model.materials[j].maps[MATERIAL_MAP_ALBEDO].color.b = (unsigned char)(data->materials[i].pbr_metallic_roughness.base_color_factor[2]*255); - model.materials[j].maps[MATERIAL_MAP_ALBEDO].color.a = (unsigned char)(data->materials[i].pbr_metallic_roughness.base_color_factor[3]*255); } + // Load base color factor (tint) + model.materials[j].maps[MATERIAL_MAP_ALBEDO].color.r = (unsigned char)(data->materials[i].pbr_metallic_roughness.base_color_factor[0]*255); + model.materials[j].maps[MATERIAL_MAP_ALBEDO].color.g = (unsigned char)(data->materials[i].pbr_metallic_roughness.base_color_factor[1]*255); + model.materials[j].maps[MATERIAL_MAP_ALBEDO].color.b = (unsigned char)(data->materials[i].pbr_metallic_roughness.base_color_factor[2]*255); + model.materials[j].maps[MATERIAL_MAP_ALBEDO].color.a = (unsigned char)(data->materials[i].pbr_metallic_roughness.base_color_factor[3]*255); // Load metallic/roughness texture if (data->materials[i].pbr_metallic_roughness.metallic_roughness_texture.texture) From 5b8b24c0c53288a94bfce7cc6e7eb21a9eee3f4a Mon Sep 17 00:00:00 2001 From: Nikolas Date: Sat, 22 Jan 2022 17:25:53 +0100 Subject: [PATCH 03/12] Fix GetApplicationDirectory on macOS (#2304) Previously failed to build with an implicit declaration of `_NSGetExecutablePath`. --- src/rcore.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/rcore.c b/src/rcore.c index eed636675..70cc350bb 100644 --- a/src/rcore.c +++ b/src/rcore.c @@ -173,6 +173,7 @@ #include #elif defined(__APPLE__) #include + #include #endif // OSs #endif // PLATFORM_DESKTOP From b422e407e8ec02156ec83ec9457f7db671e7a9c4 Mon Sep 17 00:00:00 2001 From: Ray Date: Sat, 22 Jan 2022 18:42:44 +0100 Subject: [PATCH 04/12] Update rtextures.c --- src/rtextures.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/rtextures.c b/src/rtextures.c index a7f4323b2..81d6f73e0 100644 --- a/src/rtextures.c +++ b/src/rtextures.c @@ -4324,6 +4324,7 @@ static Image LoadPKM(const unsigned char *fileData, unsigned int fileSize) #if defined(SUPPORT_FILEFORMAT_KTX) // Load KTX compressed image data (ETC1/ETC2 compression) +// TODO: Review KTX loading, many things changed! static Image LoadKTX(const unsigned char *fileData, unsigned int fileSize) { unsigned char *fileDataPtr = (unsigned char *)fileData; @@ -4398,6 +4399,8 @@ static Image LoadKTX(const unsigned char *fileData, unsigned int fileSize) if (ktxHeader->glInternalFormat == 0x8D64) image.format = PIXELFORMAT_COMPRESSED_ETC1_RGB; else if (ktxHeader->glInternalFormat == 0x9274) image.format = PIXELFORMAT_COMPRESSED_ETC2_RGB; else if (ktxHeader->glInternalFormat == 0x9278) image.format = PIXELFORMAT_COMPRESSED_ETC2_EAC_RGBA; + + // TODO: Support uncompressed data formats? Right now it returns format = 0! } } @@ -4406,11 +4409,12 @@ static Image LoadKTX(const unsigned char *fileData, unsigned int fileSize) // Save image data as KTX file // NOTE: By default KTX 1.1 spec is used, 2.0 is still on draft (01Oct2018) +// TODO: Review KTX saving, many things changed! static int SaveKTX(Image image, const char *fileName) { // KTX file Header (64 bytes) // v1.1 - https://www.khronos.org/opengles/sdk/tools/KTX/file_format_spec/ - // v2.0 - http://github.khronos.org/KTX-Specification/ - still on draft, not ready for implementation + // v2.0 - http://github.khronos.org/KTX-Specification/ - Final specs by 2021-04-18 typedef struct { char id[12]; // Identifier: "«KTX 11»\r\n\x1A\n" // KTX 2.0: "«KTX 22»\r\n\x1A\n" unsigned int endianness; // Little endian: 0x01 0x02 0x03 0x04 From b4e0f7ab08dfa950b0a5d643c639d28833536271 Mon Sep 17 00:00:00 2001 From: Tobias Mock Date: Sun, 23 Jan 2022 12:22:25 +0100 Subject: [PATCH 05/12] Update raylib-ocaml to 4.0 (#2305) --- BINDINGS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BINDINGS.md b/BINDINGS.md index b415065d7..ac79a1fa9 100644 --- a/BINDINGS.md +++ b/BINDINGS.md @@ -36,7 +36,7 @@ Some people ported raylib to other languages in form of bindings or wrappers to | raylib_odin_bindings | 4.0-dev | [Odin](https://odin-lang.org/) | MIT | https://github.com/Deathbat2190/raylib_odin_bindings | | raylib-odin | **4.0** | [Odin](https://odin-lang.org/) | BSD-3Clause | https://github.com/odin-lang/Odin/tree/master/vendor/raylib | | raylib.v | **4.0** | [V](https://vlang.io/) | Zlib | https://github.com/irishgreencitrus/raylib.v | -| raylib-ocaml | 3.7 | [OCaml](https://ocaml.org/) | MIT | https://github.com/tjammer/raylib-ocaml | +| raylib-ocaml | **4.0** | [OCaml](https://ocaml.org/) | MIT | https://github.com/tjammer/raylib-ocaml | | raylib-swift | 3.7 | [Swift](https://swift.org/) | MIT | https://github.com/STREGAsGate/Raylib | | hb-raylib | 3.5 | [Harbour](https://harbour.github.io) | MIT | https://github.com/MarcosLeonardoMendezGerencir/hb-raylib | | Relib | 3.5 | [ReCT](https://github.com/RedCubeDev-ByteSpace/ReCT) | ? | https://github.com/RedCubeDev-ByteSpace/Relib | From ebdc34a20ef195919ca22c8bee3fd6f032e87e52 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Tue, 25 Jan 2022 14:04:55 +0100 Subject: [PATCH 06/12] Update minshell.html --- src/minshell.html | 1 + 1 file changed, 1 insertion(+) diff --git a/src/minshell.html b/src/minshell.html index 73c92f2fc..c34637f99 100644 --- a/src/minshell.html +++ b/src/minshell.html @@ -31,6 +31,7 @@ From 76b6efc827ccd90ddfb5d7a0d6890d9f0a5074cf Mon Sep 17 00:00:00 2001 From: Ray Date: Wed, 26 Jan 2022 11:30:38 +0100 Subject: [PATCH 07/12] Support export .jpeg files --- src/rtextures.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/rtextures.c b/src/rtextures.c index 81d6f73e0..12732274f 100644 --- a/src/rtextures.c +++ b/src/rtextures.c @@ -500,7 +500,8 @@ bool ExportImage(Image image, const char *fileName) else if (IsFileExtension(fileName, ".tga")) success = stbi_write_tga(fileName, image.width, image.height, channels, imgData); #endif #if defined(SUPPORT_FILEFORMAT_JPG) - else if (IsFileExtension(fileName, ".jpg")) success = stbi_write_jpg(fileName, image.width, image.height, channels, imgData, 90); // JPG quality: between 1 and 100 + else if (IsFileExtension(fileName, ".jpg") || + IsFileExtension(fileName, ".jpeg")) success = stbi_write_jpg(fileName, image.width, image.height, channels, imgData, 90); // JPG quality: between 1 and 100 #endif #if defined(SUPPORT_FILEFORMAT_QOI) else if (IsFileExtension(fileName, ".qoi")) From e5ee69a0f51e7dc6e3d9c99a185c05fb8e1addb2 Mon Sep 17 00:00:00 2001 From: Siddharth Roy <46083528+siddharthroy12@users.noreply.github.com> Date: Wed, 26 Jan 2022 17:25:34 +0530 Subject: [PATCH 08/12] Add DrawTextCodepoints (#2308) * Add DrawTextCodepoints * Fixed top comment --- src/raylib.h | 1 + src/rtext.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/raylib.h b/src/raylib.h index 6c6baa7b7..228983d54 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1336,6 +1336,7 @@ RLAPI void DrawText(const char *text, int posX, int posY, int fontSize, Color co RLAPI void DrawTextEx(Font font, const char *text, Vector2 position, float fontSize, float spacing, Color tint); // Draw text using font and additional parameters RLAPI void DrawTextPro(Font font, const char *text, Vector2 position, Vector2 origin, float rotation, float fontSize, float spacing, Color tint); // Draw text using Font and pro parameters (rotation) RLAPI void DrawTextCodepoint(Font font, int codepoint, Vector2 position, float fontSize, Color tint); // Draw one character (codepoint) +RLAPI void DrawTextCodepoints(Font font, int *codepoints, int count, Vector2 position, float fontSize, float spacing, Color tint); // Draw multiple character (codepoint) // Text font info functions RLAPI int MeasureText(const char *text, int fontSize); // Measure string width for default font diff --git a/src/rtext.c b/src/rtext.c index 523712a37..f07b8804a 100644 --- a/src/rtext.c +++ b/src/rtext.c @@ -1091,6 +1091,38 @@ void DrawTextCodepoint(Font font, int codepoint, Vector2 position, float fontSiz DrawTexturePro(font.texture, srcRec, dstRec, (Vector2){ 0, 0 }, 0.0f, tint); } +// Draw multiple character (codepoints) +void DrawTextCodepoints(Font font, int *codepoints, int count, Vector2 position, float fontSize, float spacing, Color tint) +{ + int textOffsetY = 0; // Offset between lines (on line break '\n') + float textOffsetX = 0.0f; // Offset X to next character to draw + + float scaleFactor = fontSize/font.baseSize; // Character quad scaling factor + + for (int i = 0; i < count; i++) + { + int index = GetGlyphIndex(font, codepoints[i]); + + if (codepoints[i] == '\n') + { + // NOTE: Fixed line spacing of 1.5 line-height + // TODO: Support custom line spacing defined by user + textOffsetY += (int)((font.baseSize + font.baseSize/2)*scaleFactor); + textOffsetX = 0.0f; + } + else + { + if ((codepoints[i] != ' ') && (codepoints[i] != '\t')) + { + DrawTextCodepoint(font, codepoints[i], (Vector2){ position.x + textOffsetX, position.y + textOffsetY }, fontSize, tint); + } + + if (font.glyphs[index].advanceX == 0) textOffsetX += ((float)font.recs[index].width*scaleFactor + spacing); + else textOffsetX += ((float)font.glyphs[index].advanceX*scaleFactor + spacing); + } + } +} + // Measure string width for default font int MeasureText(const char *text, int fontSize) { From 524bf57b74f443052a90d7a61534dab86a99cda7 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Wed, 26 Jan 2022 16:33:38 +0100 Subject: [PATCH 09/12] Update qoi.h --- src/external/qoi.h | 114 ++++++++++++++++++++++++--------------------- 1 file changed, 60 insertions(+), 54 deletions(-) diff --git a/src/external/qoi.h b/src/external/qoi.h index 5e31076bf..988f9edcb 100644 --- a/src/external/qoi.h +++ b/src/external/qoi.h @@ -28,8 +28,8 @@ SOFTWARE. -- About -QOI encodes and decodes images in a lossless format. Compared to stb_image and -stb_image_write QOI offers 20x-50x faster encoding, 3x-4x faster decoding and +QOI encodes and decodes images in a lossless format. Compared to stb_image and +stb_image_write QOI offers 20x-50x faster encoding, 3x-4x faster decoding and 20% better compression. @@ -45,7 +45,7 @@ stb_image_write QOI offers 20x-50x faster encoding, 3x-4x faster decoding and // the input pixel data. qoi_write("image_new.qoi", rgba_pixels, &(qoi_desc){ .width = 1920, - .height = 1080, + .height = 1080, .channels = 4, .colorspace = QOI_SRGB }); @@ -74,7 +74,7 @@ QOI_NO_STDIO before including this library. This library uses malloc() and free(). To supply your own malloc implementation you can define QOI_MALLOC and QOI_FREE before including this library. -This library uses memset() to zero-initialize the index. To supply your own +This library uses memset() to zero-initialize the index. To supply your own implementation you can define QOI_ZEROARR before including this library. @@ -91,9 +91,9 @@ struct qoi_header_t { uint8_t colorspace; // 0 = sRGB with linear alpha, 1 = all channels linear }; -Images are encoded from top to bottom, left to right. The decoder and encoder -start with {r: 0, g: 0, b: 0, a: 255} as the previous pixel value. An image is -complete when all pixels specified by width * height have been covered. +Images are encoded row by row, left to right, top to bottom. The decoder and +encoder start with {r: 0, g: 0, b: 0, a: 255} as the previous pixel value. An +image is complete when all pixels specified by width * height have been covered. Pixels are encoded as - a run of the previous pixel @@ -101,20 +101,20 @@ Pixels are encoded as - a difference to the previous pixel value in r,g,b - full r,g,b or r,g,b,a values -The color channels are assumed to not be premultiplied with the alpha channel +The color channels are assumed to not be premultiplied with the alpha channel ("un-premultiplied alpha"). -A running array[64] (zero-initialized) of previously seen pixel values is +A running array[64] (zero-initialized) of previously seen pixel values is maintained by the encoder and decoder. Each pixel that is seen by the encoder and decoder is put into this array at the position formed by a hash function of the color value. In the encoder, if the pixel value at the index matches the -current pixel, this index position is written to the stream as QOI_OP_INDEX. +current pixel, this index position is written to the stream as QOI_OP_INDEX. The hash function for the index is: index_position = (r * 3 + g * 5 + b * 7 + a * 11) % 64 -Each chunk starts with a 2- or 8-bit tag, followed by a number of data bits. The -bit length of chunks is divisible by 8 - i.e. all chunks are byte aligned. All +Each chunk starts with a 2- or 8-bit tag, followed by a number of data bits. The +bit length of chunks is divisible by 8 - i.e. all chunks are byte aligned. All values encoded in these data bits have the most significant bit on the left. The 8-bit tags have precedence over the 2-bit tags. A decoder must check for the @@ -135,11 +135,11 @@ The possible chunks are: 2-bit tag b00 6-bit index into the color index array: 0..63 -A valid encoder must not issue 7 or more consecutive QOI_OP_INDEX chunks to the -index 0, to avoid confusion with the 8 byte end marker. +A valid encoder must not issue 2 or more consecutive QOI_OP_INDEX chunks to the +same index. QOI_OP_RUN should be used instead. -.- QOI_OP_DIFF -----------. +.- QOI_OP_DIFF -----------. | Byte[0] | | 7 6 5 4 3 2 1 0 | |-------+-----+-----+-----| @@ -150,14 +150,16 @@ index 0, to avoid confusion with the 8 byte end marker. 2-bit green channel difference from the previous pixel between -2..1 2-bit blue channel difference from the previous pixel between -2..1 -The difference to the current channel values are using a wraparound operation, +The difference to the current channel values are using a wraparound operation, so "1 - 2" will result in 255, while "255 + 1" will result in 0. -Values are stored as unsigned integers with a bias of 2. E.g. -2 is stored as +Values are stored as unsigned integers with a bias of 2. E.g. -2 is stored as 0 (b00). 1 is stored as 3 (b11). +The alpha value remains unchanged from the previous pixel. -.- QOI_OP_LUMA -------------------------------------. + +.- QOI_OP_LUMA -------------------------------------. | Byte[0] | Byte[1] | | 7 6 5 4 3 2 1 0 | 7 6 5 4 3 2 1 0 | |-------+-----------------+-------------+-----------| @@ -168,18 +170,20 @@ Values are stored as unsigned integers with a bias of 2. E.g. -2 is stored as 4-bit red channel difference minus green channel difference -8..7 4-bit blue channel difference minus green channel difference -8..7 -The green channel is used to indicate the general direction of change and is +The green channel is used to indicate the general direction of change and is encoded in 6 bits. The red and blue channels (dr and db) base their diffs off of the green channel difference and are encoded in 4 bits. I.e.: - dr_dg = (last_px.r - cur_px.r) - (last_px.g - cur_px.g) - db_dg = (last_px.b - cur_px.b) - (last_px.g - cur_px.g) + dr_dg = (cur_px.r - prev_px.r) - (cur_px.g - prev_px.g) + db_dg = (cur_px.b - prev_px.b) - (cur_px.g - prev_px.g) -The difference to the current channel values are using a wraparound operation, +The difference to the current channel values are using a wraparound operation, so "10 - 13" will result in 253, while "250 + 7" will result in 1. -Values are stored as unsigned integers with a bias of 32 for the green channel +Values are stored as unsigned integers with a bias of 32 for the green channel and a bias of 8 for the red and blue channel. +The alpha value remains unchanged from the previous pixel. + .- QOI_OP_RUN ------------. | Byte[0] | @@ -190,8 +194,8 @@ and a bias of 8 for the red and blue channel. 2-bit tag b11 6-bit run-length repeating the previous pixel: 1..62 -The run-length is stored with a bias of -1. Note that the run-lengths 63 and 64 -(b111110 and b111111) are illegal as they are occupied by the QOI_OP_RGB and +The run-length is stored with a bias of -1. Note that the run-lengths 63 and 64 +(b111110 and b111111) are illegal as they are occupied by the QOI_OP_RGB and QOI_OP_RGBA tags. @@ -206,6 +210,8 @@ QOI_OP_RGBA tags. 8-bit green channel value 8-bit blue channel value +The alpha value remains unchanged from the previous pixel. + .- QOI_OP_RGBA ---------------------------------------------------. | Byte[0] | Byte[1] | Byte[2] | Byte[3] | Byte[4] | @@ -232,17 +238,17 @@ Header - Public functions */ extern "C" { #endif -/* A pointer to a qoi_desc struct has to be supplied to all of qoi's functions. -It describes either the input format (for qoi_write and qoi_encode), or is +/* A pointer to a qoi_desc struct has to be supplied to all of qoi's functions. +It describes either the input format (for qoi_write and qoi_encode), or is filled with the description read from the file header (for qoi_read and qoi_decode). -The colorspace in this qoi_desc is an enum where +The colorspace in this qoi_desc is an enum where 0 = sRGB, i.e. gamma scaled RGB channels and a linear alpha channel 1 = all channels are linear -You may use the constants QOI_SRGB or QOI_LINEAR. The colorspace is purely +You may use the constants QOI_SRGB or QOI_LINEAR. The colorspace is purely informative. It will be saved to the file header, but does not affect -en-/decoding in any way. */ +how chunks are en-/decoded. */ #define QOI_SRGB 0 #define QOI_LINEAR 1 @@ -256,11 +262,11 @@ typedef struct { #ifndef QOI_NO_STDIO -/* Encode raw RGB or RGBA pixels into a QOI image and write it to the file -system. The qoi_desc struct must be filled with the image width, height, -number of channels (3 = RGB, 4 = RGBA) and the colorspace. +/* Encode raw RGB or RGBA pixels into a QOI image and write it to the file +system. The qoi_desc struct must be filled with the image width, height, +number of channels (3 = RGB, 4 = RGBA) and the colorspace. -The function returns 0 on failure (invalid parameters, or fopen or malloc +The function returns 0 on failure (invalid parameters, or fopen or malloc failed) or the number of bytes written on success. */ int qoi_write(const char *filename, const void *data, const qoi_desc *desc); @@ -271,7 +277,7 @@ number of channels from the file header is used. If channels is 3 or 4 the output format will be forced into this number of channels. The function either returns NULL on failure (invalid data, or malloc or fopen -failed) or a pointer to the decoded pixels. On success, the qoi_desc struct +failed) or a pointer to the decoded pixels. On success, the qoi_desc struct will be filled with the description from the file header. The returned pixel data should be free()d after use. */ @@ -283,8 +289,8 @@ void *qoi_read(const char *filename, qoi_desc *desc, int channels); /* Encode raw RGB or RGBA pixels into a QOI image in memory. -The function either returns NULL on failure (invalid parameters or malloc -failed) or a pointer to the encoded data on success. On success the out_len +The function either returns NULL on failure (invalid parameters or malloc +failed) or a pointer to the encoded data on success. On success the out_len is set to the size in bytes of the encoded data. The returned qoi data should be free()d after use. */ @@ -294,8 +300,8 @@ void *qoi_encode(const void *data, const qoi_desc *desc, int *out_len); /* Decode a QOI image from memory. -The function either returns NULL on failure (invalid parameters or malloc -failed) or a pointer to the decoded pixels. On success, the qoi_desc struct +The function either returns NULL on failure (invalid parameters or malloc +failed) or a pointer to the decoded pixels. On success, the qoi_desc struct is filled with the description from the file header. The returned pixel data should be free()d after use. */ @@ -340,8 +346,8 @@ Implementation */ #define QOI_HEADER_SIZE 14 /* 2GB is the max file size that this implementation can safely handle. We guard -against anything larger than that, assuming the worst case with 5 bytes per -pixel, rounded down to a nice clean value. 400 million pixels ought to be +against anything larger than that, assuming the worst case with 5 bytes per +pixel, rounded down to a nice clean value. 400 million pixels ought to be enough for anybody. */ #define QOI_PIXELS_MAX ((unsigned int)400000000) @@ -352,14 +358,14 @@ typedef union { static const unsigned char qoi_padding[8] = {0,0,0,0,0,0,0,1}; -void qoi_write_32(unsigned char *bytes, int *p, unsigned int v) { +static void qoi_write_32(unsigned char *bytes, int *p, unsigned int v) { bytes[(*p)++] = (0xff000000 & v) >> 24; bytes[(*p)++] = (0x00ff0000 & v) >> 16; bytes[(*p)++] = (0x0000ff00 & v) >> 8; bytes[(*p)++] = (0x000000ff & v); } -unsigned int qoi_read_32(const unsigned char *bytes, int *p) { +static unsigned int qoi_read_32(const unsigned char *bytes, int *p) { unsigned int a = bytes[(*p)++]; unsigned int b = bytes[(*p)++]; unsigned int c = bytes[(*p)++]; @@ -385,8 +391,8 @@ void *qoi_encode(const void *data, const qoi_desc *desc, int *out_len) { return NULL; } - max_size = - desc->width * desc->height * (desc->channels + 1) + + max_size = + desc->width * desc->height * (desc->channels + 1) + QOI_HEADER_SIZE + sizeof(qoi_padding); p = 0; @@ -412,7 +418,7 @@ void *qoi_encode(const void *data, const qoi_desc *desc, int *out_len) { px_prev.rgba.b = 0; px_prev.rgba.a = 255; px = px_prev; - + px_len = desc->width * desc->height * desc->channels; px_end = px_len - desc->channels; channels = desc->channels; @@ -460,14 +466,14 @@ void *qoi_encode(const void *data, const qoi_desc *desc, int *out_len) { if ( vr > -3 && vr < 2 && - vg > -3 && vg < 2 && + vg > -3 && vg < 2 && vb > -3 && vb < 2 ) { bytes[p++] = QOI_OP_DIFF | (vr + 2) << 4 | (vg + 2) << 2 | (vb + 2); } else if ( - vg_r > -9 && vg_r < 8 && - vg > -33 && vg < 32 && + vg_r > -9 && vg_r < 8 && + vg > -33 && vg < 32 && vg_b > -9 && vg_b < 8 ) { bytes[p++] = QOI_OP_LUMA | (vg + 32); @@ -526,7 +532,7 @@ void *qoi_decode(const void *data, int size, qoi_desc *desc, int channels) { desc->colorspace = bytes[p++]; if ( - desc->width == 0 || desc->height == 0 || + desc->width == 0 || desc->height == 0 || desc->channels < 3 || desc->channels > 4 || desc->colorspace > 1 || header_magic != QOI_MAGIC || @@ -592,7 +598,7 @@ void *qoi_decode(const void *data, int size, qoi_desc *desc, int channels) { index[QOI_COLOR_HASH(px) % 64] = px; } - if (channels == 4) { + if (channels == 4) { *(qoi_rgba_t*)(pixels + px_pos) = px; } else { @@ -621,11 +627,11 @@ int qoi_write(const char *filename, const void *data, const qoi_desc *desc) { if (!encoded) { fclose(f); return 0; - } - + } + fwrite(encoded, 1, size, f); fclose(f); - + QOI_FREE(encoded); return size; } From 0f00c41aad3a60f64481172bd11f8b4120963dc3 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Thu, 27 Jan 2022 14:07:05 +0100 Subject: [PATCH 10/12] ADDED: `GetFileSize()` --- src/raylib.h | 1 + src/rcore.c | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/raylib.h b/src/raylib.h index 228983d54..ab8892ccf 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1041,6 +1041,7 @@ RLAPI bool SaveFileText(const char *fileName, char *text); // Save text d RLAPI bool FileExists(const char *fileName); // Check if file exists RLAPI bool DirectoryExists(const char *dirPath); // Check if a directory path exists RLAPI bool IsFileExtension(const char *fileName, const char *ext); // Check file extension (including point: .png, .wav) +RLAPI int GetFileSize(const char *fileName); // Get file size in bytes RLAPI const char *GetFileExtension(const char *fileName); // Get pointer to extension for a filename string (includes dot: '.png') RLAPI const char *GetFileName(const char *filePath); // Get pointer to filename for a path string RLAPI const char *GetFileNameWithoutExt(const char *filePath); // Get filename string without extension (uses static string) diff --git a/src/rcore.c b/src/rcore.c index 70cc350bb..83b6a8844 100644 --- a/src/rcore.c +++ b/src/rcore.c @@ -2851,6 +2851,23 @@ bool DirectoryExists(const char *dirPath) return result; } +// Get file size in bytes +int GetFileSize(const char *fileName) +{ + int size = 0; + + FILE *file = fopen(fileName, "rb"); + + if (file != NULL) + { + fseek(file, 0L, SEEK_END); + size = (int)ftell(file); + fclose(file); + } + + return size; +} + // Get pointer to extension for a filename string (includes the dot: .png) const char *GetFileExtension(const char *fileName) { From d38fe92c3f50fe14f051b4822ae9d24dd59067f5 Mon Sep 17 00:00:00 2001 From: Ray Date: Fri, 28 Jan 2022 00:23:28 +0100 Subject: [PATCH 11/12] RENAMED: `GetFileSize()` to `GetFileLength()` `GetFileSize()` conflicts with the infamous `windows.h` --- src/raylib.h | 2 +- src/rcore.c | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/raylib.h b/src/raylib.h index ab8892ccf..6e6e2e2ac 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1041,7 +1041,7 @@ RLAPI bool SaveFileText(const char *fileName, char *text); // Save text d RLAPI bool FileExists(const char *fileName); // Check if file exists RLAPI bool DirectoryExists(const char *dirPath); // Check if a directory path exists RLAPI bool IsFileExtension(const char *fileName, const char *ext); // Check file extension (including point: .png, .wav) -RLAPI int GetFileSize(const char *fileName); // Get file size in bytes +RLAPI int GetFileLength(const char *fileName); // Get file length in bytes (NOTE: GetFileSize() conflicts with windows.h) RLAPI const char *GetFileExtension(const char *fileName); // Get pointer to extension for a filename string (includes dot: '.png') RLAPI const char *GetFileName(const char *filePath); // Get pointer to filename for a path string RLAPI const char *GetFileNameWithoutExt(const char *filePath); // Get filename string without extension (uses static string) diff --git a/src/rcore.c b/src/rcore.c index 83b6a8844..f0652d3bb 100644 --- a/src/rcore.c +++ b/src/rcore.c @@ -2851,8 +2851,9 @@ bool DirectoryExists(const char *dirPath) return result; } -// Get file size in bytes -int GetFileSize(const char *fileName) +// Get file length in bytes +// NOTE: GetFileSize() conflicts with windows.h +int GetFileLength(const char *fileName) { int size = 0; From 44d3cee5d1e779ba9b62394a351537c557f11276 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Fri, 28 Jan 2022 21:25:40 +0100 Subject: [PATCH 12/12] Minor tweak --- src/raylib.h | 2 +- src/rlgl.h | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/raylib.h b/src/raylib.h index 6e6e2e2ac..70384e281 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -839,7 +839,7 @@ typedef enum { BLEND_MULTIPLIED, // Blend textures multiplying colors BLEND_ADD_COLORS, // Blend textures adding colors (alternative) BLEND_SUBTRACT_COLORS, // Blend textures subtracting colors (alternative) - BLEND_CUSTOM // Belnd textures using custom src/dst factors (use rlSetBlendMode()) + BLEND_CUSTOM // Blend textures using custom src/dst factors (use rlSetBlendMode()) } BlendMode; // Gesture diff --git a/src/rlgl.h b/src/rlgl.h index 16f419021..fa096ca6b 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -1778,7 +1778,11 @@ void rlSetBlendMode(int mode) case RL_BLEND_MULTIPLIED: glBlendFunc(GL_DST_COLOR, GL_ONE_MINUS_SRC_ALPHA); glBlendEquation(GL_FUNC_ADD); break; case RL_BLEND_ADD_COLORS: glBlendFunc(GL_ONE, GL_ONE); glBlendEquation(GL_FUNC_ADD); break; case RL_BLEND_SUBTRACT_COLORS: glBlendFunc(GL_ONE, GL_ONE); glBlendEquation(GL_FUNC_SUBTRACT); break; - case RL_BLEND_CUSTOM: glBlendFunc(RLGL.State.glBlendSrcFactor, RLGL.State.glBlendDstFactor); glBlendEquation(RLGL.State.glBlendEquation); break; + case RL_BLEND_CUSTOM: + { + // NOTE: Using GL blend src/dst factors and GL equation configured with rlSetBlendFactors() + glBlendFunc(RLGL.State.glBlendSrcFactor, RLGL.State.glBlendDstFactor); glBlendEquation(RLGL.State.glBlendEquation); + } break; default: break; }