From baab9b52cd90dd5be1ea7974802523165ffb990e Mon Sep 17 00:00:00 2001 From: Etienne Rey-bethbeder Date: Wed, 26 Apr 2023 14:03:40 +0200 Subject: [PATCH] Add MLX42 V3 --- MLX42/.gitattributes | 8 -- MLX42/.github/ISSUE_TEMPLATE/bug_report.md | 34 -------- .../.github/ISSUE_TEMPLATE/feature_request.md | 20 ----- MLX42/.github/workflows/ci.yml | 78 ------------------- MLX42/.gitignore | 68 ---------------- MLX42/CMakeLists.txt | 4 +- MLX42/README.md | 13 ++-- MLX42/Testing/Temporary/CTestCostData.txt | 1 + MLX42/Testing/Temporary/LastTest.log | 3 + MLX42/cmake/Findglfw3.cmake | 6 +- MLX42/docs/42.md | 2 - MLX42/include/MLX42/MLX42.h | 42 +++++++--- MLX42/include/MLX42/MLX42_Int.h | 6 +- MLX42/src/font/font.h | 8 +- MLX42/src/font/mlx_font.c | 2 +- MLX42/src/mlx_cursor.c | 17 ++-- MLX42/src/mlx_images.c | 20 ++--- MLX42/src/mlx_loop.c | 9 +-- MLX42/src/textures/mlx_texture.c | 54 +++++++++++-- MLX42/tests/CMakeLists.txt | 8 +- MLX42/tests/WindowFixture.hpp | 8 +- MLX42/tests/tests.cpp | 12 +-- 22 files changed, 125 insertions(+), 298 deletions(-) delete mode 100644 MLX42/.gitattributes delete mode 100644 MLX42/.github/ISSUE_TEMPLATE/bug_report.md delete mode 100644 MLX42/.github/ISSUE_TEMPLATE/feature_request.md delete mode 100644 MLX42/.github/workflows/ci.yml delete mode 100644 MLX42/.gitignore create mode 100644 MLX42/Testing/Temporary/CTestCostData.txt create mode 100644 MLX42/Testing/Temporary/LastTest.log diff --git a/MLX42/.gitattributes b/MLX42/.gitattributes deleted file mode 100644 index ccbbc0d..0000000 --- a/MLX42/.gitattributes +++ /dev/null @@ -1,8 +0,0 @@ -# See https://docs.github.com/en/get-started/getting-started-with-git/configuring-git-to-handle-line-endings - -# Set the default behavior, in case people don't have core.autocrlf set -* text=auto - -# Declare files that will always have a certain EOL -*.sh text eol=lf -shaders/** text eol=lf diff --git a/MLX42/.github/ISSUE_TEMPLATE/bug_report.md b/MLX42/.github/ISSUE_TEMPLATE/bug_report.md deleted file mode 100644 index 4c8efdb..0000000 --- a/MLX42/.github/ISSUE_TEMPLATE/bug_report.md +++ /dev/null @@ -1,34 +0,0 @@ ---- -name: Bug report -about: Create a report to help us improve -title: "[BUG]" -labels: '' -assignees: '' - ---- - -**NOTE** -Before creating a bug report! Make sure you git pull from master and check if the bug still exists! - -**Describe the bug** -A clear and concise description of what the bug is. - -**To Reproduce** -Steps to reproduce the behavior: -1. Go to '...' -2. Click on '...' -3. Scroll down to '...' -4. See error - -**Expected behavior** -A clear and concise description of what you expected to happen. - -**Screenshots** -If applicable, add screenshots to help explain your problem. - -**Desktop (please complete the following information):** - - OS: [e.g. MacOS] - - Version: [e.g. BigSur] - -**Additional context** -Add any other context about the problem here. diff --git a/MLX42/.github/ISSUE_TEMPLATE/feature_request.md b/MLX42/.github/ISSUE_TEMPLATE/feature_request.md deleted file mode 100644 index d36cbc7..0000000 --- a/MLX42/.github/ISSUE_TEMPLATE/feature_request.md +++ /dev/null @@ -1,20 +0,0 @@ ---- -name: Feature request -about: Suggest an idea for this project -title: "[REQUEST]" -labels: '' -assignees: '' - ---- - -**Is your feature request related to a problem? Please describe.** -A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] - -**Describe the solution you'd like** -A clear and concise description of what you want to happen. - -**Describe alternatives you've considered** -A clear and concise description of any alternative solutions or features you've considered. - -**Additional context** -Add any other context or screenshots about the feature request here. diff --git a/MLX42/.github/workflows/ci.yml b/MLX42/.github/workflows/ci.yml deleted file mode 100644 index 448432a..0000000 --- a/MLX42/.github/workflows/ci.yml +++ /dev/null @@ -1,78 +0,0 @@ -# ----------------------------------------------------------------------------- -# Codam Coding College, Amsterdam @ 2022-2023-2023 by W2Wizard. -# See README in the root project for more information. -# ----------------------------------------------------------------------------- - -name: Build - -#=============================================================================# - -on: - push: - branches: [ master ] - pull_request: - branches: [ master ] - -#=============================================================================# - -jobs: - -# Tests -#=============================================================================# - - unit-test: - timeout-minutes: 10 - runs-on: ubuntu-latest - env: - DISPLAY: ":99" - - steps: - - name: Clone repository - uses: actions/checkout@v3 - - - name: Install Dependencies - run: | - sudo apt-get update -qq - sudo apt-get install -y -qq xorg-dev xvfb - - - name: Setup virtual screen - run: Xvfb $DISPLAY -screen 0 1280x1024x24 & - - - name: Build MLX42 & tests - run: cmake -DBUILD_TESTS=YES -B ${{github.workspace}}/build && cmake --build ${{github.workspace}}/build --parallel - - - name: Run tests - run: ctest --output-on-failure --test-dir ${{github.workspace}}/build - -# Unix -#=============================================================================# - - build: - timeout-minutes: 10 - runs-on: ${{ matrix.os }} - strategy: - matrix: - os: [ubuntu-latest, macos-latest, windows-latest] - - steps: - - name: Clone repository - uses: actions/checkout@v3 - - # Windows will just fetch glfw with cmake automatically. - # This avoids doing extra work like installing a package manager. - - name: Install Dependencies - if: matrix.os != 'windows-latest' - run: | - set -x - if [ "$RUNNER_OS" == "Linux" ]; then - sudo apt-get update -qq - sudo apt-get install -y -qq xorg-dev - elif [ "$RUNNER_OS" == "macOS" ]; then - brew update - brew install glfw - fi - - - name: Build - run: cmake -B build && cmake --build build --parallel - -#=============================================================================# diff --git a/MLX42/.gitignore b/MLX42/.gitignore deleted file mode 100644 index cd85961..0000000 --- a/MLX42/.gitignore +++ /dev/null @@ -1,68 +0,0 @@ -# Prerequisites -*.d - -# Object files -*.o -*.ko -*.obj -*.elf - -# Linker output -*.ilk -*.map -*.exp - -# Precompiled Headers -*.gch -*.pch - -# Libraries -*.lib -*.a -*.la -*.lo - -# Shared objects (inc. Windows DLLs) -*.dll -*.so -*.so.* -*.dylib - -# Executables -*.exe -*.out -*.app -*.i*86 -*.x86_64 -*.hex - -# Debug files -*.dSYM/ -*.su -*.idb -*.pdb - -# Kernel Module Compile Results -*.mod* -*.cmd -.tmp_versions/ -modules.order -Module.symvers -Mkfile.old -dkms.conf - -# Misc -main.c -temp/ -.vscode/ -lib/glfw/ -.DS_Store - -# Special shader files -mlx_*_shader.c -build/ -main.c -test - -# Automatic downloaded deps -_deps/ \ No newline at end of file diff --git a/MLX42/CMakeLists.txt b/MLX42/CMakeLists.txt index fb4d924..1174c94 100644 --- a/MLX42/CMakeLists.txt +++ b/MLX42/CMakeLists.txt @@ -5,8 +5,8 @@ # CMake specifications # ----------------------------------------------------------------------------- -cmake_minimum_required (VERSION 3.18.0) -project(mlx42 VERSION 2.3.1) +cmake_minimum_required (VERSION 3.16.0) +project(mlx42 VERSION 2.3.0) message(STATUS "MLX42 @ ${CMAKE_PROJECT_VERSION}") # Variables diff --git a/MLX42/README.md b/MLX42/README.md index c6c8832..761290c 100644 --- a/MLX42/README.md +++ b/MLX42/README.md @@ -79,6 +79,11 @@ Then simply run them with: ctest --output-on-failure --test-dir build ``` +### Available Options + +You can pass build options to cmake ... `cmake -DDEBUG=1 -DGLFW_FETCH=0` ... to customise the install, more at [installation](./docs/index.md#available-options). +You can find an example makefile in the documentation [here](https://github.com/codam-coding-college/MLX42/blob/master/docs/Basics.md). + ---- ## Download and build - MLX42 @@ -92,12 +97,6 @@ ctest --output-on-failure --test-dir build The output library file is called `libmlx42.a` and is located in the `build` folder that you specified. -### Available Options - -You can pass build [options](./docs/index.md#available-options) to cmake, e.g: `cmake -DDEBUG=1 -DGLFW_FETCH=0...`. These will for instance let you build it in DEBUG mode or alter any sort of behaviour at build-time. - -You can find an example makefile in the documentation [here](https://github.com/codam-coding-college/MLX42/blob/master/docs/Basics.md). - ---- ## For MacOS: @@ -250,7 +249,7 @@ int32_t ft_pixel(int32_t r, int32_t g, int32_t b, int32_t a) return (r << 24 | g << 16 | b << 8 | a); } -void ft_randomize(void* param) +void ft_randomize(void* img) { for (int32_t i = 0; i < image->width; ++i) { diff --git a/MLX42/Testing/Temporary/CTestCostData.txt b/MLX42/Testing/Temporary/CTestCostData.txt new file mode 100644 index 0000000..ed97d53 --- /dev/null +++ b/MLX42/Testing/Temporary/CTestCostData.txt @@ -0,0 +1 @@ +--- diff --git a/MLX42/Testing/Temporary/LastTest.log b/MLX42/Testing/Temporary/LastTest.log new file mode 100644 index 0000000..4f14789 --- /dev/null +++ b/MLX42/Testing/Temporary/LastTest.log @@ -0,0 +1,3 @@ +Start testing: Mar 03 14:00 CET +---------------------------------------------------------- +End testing: Mar 03 14:00 CET diff --git a/MLX42/cmake/Findglfw3.cmake b/MLX42/cmake/Findglfw3.cmake index d99fc5e..2d586b0 100644 --- a/MLX42/cmake/Findglfw3.cmake +++ b/MLX42/cmake/Findglfw3.cmake @@ -13,9 +13,8 @@ set(_glfw3_HEADER_SEARCH_DIRS "/usr/include" "/usr/local/include" -"C:/Program Files/GLFW/include" -"C:/Program Files (x86)/GLFW/include" -"$ENV{HOME}/.brew/include/" +"C:/Program Files/GLFW" +"$ENV{HOME}/.brew/lib/" ) # Possbile library locations @@ -23,7 +22,6 @@ set(_glfw3_LIB_SEARCH_DIRS "/usr/lib" "/usr/local/lib" "C:/Program Files/GLFW" -"C:/Program Files (x86)/GLFW" "$ENV{HOME}/.brew/lib/" ) diff --git a/MLX42/docs/42.md b/MLX42/docs/42.md index ad31106..55c4ce8 100644 --- a/MLX42/docs/42.md +++ b/MLX42/docs/42.md @@ -120,8 +120,6 @@ Q: **_"Are the libraries 1:1 identical?"_** A: No, there are 100% breaking changes, and changing from miniLibX to MLX42 will not be a simple drop-in replacement. As in their API is slightly different. That was not the idea eitherway, as it would be impossible to fix the problems present in the current miniLibX. -In terms of usage, it's practically identical, while the API Prototypes are slightly different, the usage is roughly the same and the only core difference is the way images are handled. - My suggestion is for students who are using miniLibX to keep using it to finish their project and for new students to prefer the new one or until they reach the next graphical project. --- diff --git a/MLX42/include/MLX42/MLX42.h b/MLX42/include/MLX42/MLX42.h index 0abd9ba..003ddbb 100644 --- a/MLX42/include/MLX42/MLX42.h +++ b/MLX42/include/MLX42/MLX42.h @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* MLX42_Keys.h :+: :+: */ +/* MLX42.h :+: :+: */ /* +:+ */ /* By: W2Wizard +#+ */ /* +#+ */ -/* Created: 2021/12/28 02:29:06 by W2Wizard #+# #+# */ -/* Updated: 2023/03/30 16:23:19 by ntamayo- ######## odam.nl */ +/* Created: 2021/12/28 00:33:01 by W2Wizard #+# #+# */ +/* Updated: 2023/02/01 11:33:26 by W2Wizard ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -469,13 +469,6 @@ mlx_win_cursor_t* mlx_create_std_cursor(cursor_t type); */ mlx_win_cursor_t* mlx_create_cursor(mlx_texture_t* texture); -/** - * Destroys the given cursor object. - * - * @param[in] cursor The cursor object to destroy. - */ -void mlx_destroy_cursor(mlx_win_cursor_t* cursor); - /** * Sets the current cursor to the given custom cursor. * @@ -601,6 +594,31 @@ void mlx_delete_xpm42(xpm_t* xpm); */ mlx_image_t* mlx_texture_to_image(mlx_t* mlx, mlx_texture_t* texture); +/** + * Given an X & Y coordinate and a Width and Height from a section of a + * texture a new image is created, useful for texture atlases. + * + * Basically a cropping tool. + * + * @param[in] mlx The MLX instance handle. + * @param[in] texture The texture to use to create the image from. + * @param[in] xy The X & Y location. + * @param[in] wh The Width & Height. + * @return mlx_image_t* The image created from the texture area. + */ +mlx_image_t* mlx_texture_area_to_image(mlx_t* mlx, mlx_texture_t* texture, uint32_t xy[2], uint32_t wh[2]); + +/** + * Draws the texture on an already existing image. + * + * @param[in] image The image to draw on. + * @param[in] texture The texture to use to draw on the image. + * @param[in] x X position relative to the image. + * @param[in] y Y position relative to the image. + * @return In case of any issues false, else true. + */ +bool mlx_draw_texture(mlx_image_t* image, mlx_texture_t* texture, uint32_t x, uint32_t y); + //= Image Functions =// /** @@ -659,8 +677,8 @@ int32_t mlx_image_to_window(mlx_t* mlx, mlx_image_t* img, int32_t x, int32_t y); void mlx_delete_image(mlx_t* mlx, mlx_image_t* image); /** - * Allows you to resize an image, a new pixel buffer is allocated - * to fit & the previous data is scaled to fit the new size. + * Allows you to resize an image, the pixel buffer is re-allocated + * to fit & the previous data is copied over. New pixels are zeroed. * * @param[in] img The image to resize. * @param[in] nwidth The new width. diff --git a/MLX42/include/MLX42/MLX42_Int.h b/MLX42/include/MLX42/MLX42_Int.h index e5a450a..5e27f12 100644 --- a/MLX42/include/MLX42/MLX42_Int.h +++ b/MLX42/include/MLX42/MLX42_Int.h @@ -1,19 +1,19 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* MLX42_Int.h :+: :+: */ +/* MLX42_Int.h :+: :+: :+: */ /* +:+ */ /* By: W2Wizard +#+ */ /* +#+ */ /* Created: 2021/12/27 23:55:34 by W2Wizard #+# #+# */ -/* Updated: 2022/07/21 10:46:43 by sbos ######## odam.nl */ +/* Updated: 2023/03/03 11:13:25 by ulayus ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef MLX42_INT_H # define MLX42_INT_H # define LODEPNG_NO_COMPILE_ALLOCATORS -# include "MLX42/MLX42.h" +# include "MLX42.h" # include "lodepng/lodepng.h" # include "glad/glad.h" # include "KHR/khrplatform.h" diff --git a/MLX42/src/font/font.h b/MLX42/src/font/font.h index 0dc8898..5fd9c06 100644 --- a/MLX42/src/font/font.h +++ b/MLX42/src/font/font.h @@ -19,10 +19,10 @@ // GIMP RGBA C-Source image dump (font.c) static struct s_font { - uint32_t width; - uint32_t height; - uint32_t bpp; - char* pixels; + uint32_t width; + uint32_t height; + uint32_t bpp; + uint8_t pixels[1140 * 20 * 4 + 1]; } font_atlas = { 1140, 20, 4, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" diff --git a/MLX42/src/font/mlx_font.c b/MLX42/src/font/mlx_font.c index 95a00f2..c9470f1 100644 --- a/MLX42/src/font/mlx_font.c +++ b/MLX42/src/font/mlx_font.c @@ -31,7 +31,7 @@ static void mlx_draw_char(mlx_image_t* image, int32_t texoffset, int32_t imgoffs if (texoffset < 0) return; - char* pixelx; + uint8_t* pixelx; uint8_t* pixeli; for (uint32_t y = 0; y < FONT_HEIGHT; y++) { diff --git a/MLX42/src/mlx_cursor.c b/MLX42/src/mlx_cursor.c index 4c248ec..6b5b61c 100644 --- a/MLX42/src/mlx_cursor.c +++ b/MLX42/src/mlx_cursor.c @@ -3,10 +3,10 @@ /* :::::::: */ /* mlx_cursor.c :+: :+: */ /* +:+ */ -/* By: W2Wizard +#+ */ +/* By: W2Wizard +#+ */ /* +#+ */ /* Created: 2022/01/18 20:10:54 by W2Wizard #+# #+# */ -/* Updated: 2023/03/09 11:11:45 by W2Wizard ######## odam.nl */ +/* Updated: 2022/11/22 08:58:23 by jvan-hal ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ //= Public =// -mlx_win_cursor_t* mlx_create_std_cursor(cursor_t type) +void* mlx_create_std_cursor(cursor_t type) { MLX_ASSERT(type >= MLX_CURSOR_ARROW && type < MLX_CURSOR_VRESIZE, "Invalid standard cursor type"); @@ -24,7 +24,7 @@ mlx_win_cursor_t* mlx_create_std_cursor(cursor_t type) return ((void *)mlx_error(MLX_MEMFAIL)); } -mlx_win_cursor_t* mlx_create_cursor(mlx_texture_t* texture) +void* mlx_create_cursor(mlx_texture_t* texture) { MLX_NONNULL(texture); @@ -40,14 +40,7 @@ mlx_win_cursor_t* mlx_create_cursor(mlx_texture_t* texture) return ((void *)mlx_error(MLX_MEMFAIL)); } -void mlx_destroy_cursor(mlx_win_cursor_t* cursor) -{ - MLX_NONNULL(cursor); - - glfwDestroyCursor(cursor); -} - -void mlx_set_cursor(mlx_t* mlx, mlx_win_cursor_t* cursor) +void mlx_set_cursor(mlx_t* mlx, void* cursor) { MLX_NONNULL(mlx); MLX_NONNULL(cursor); diff --git a/MLX42/src/mlx_images.c b/MLX42/src/mlx_images.c index 43152f5..79cc726 100644 --- a/MLX42/src/mlx_images.c +++ b/MLX42/src/mlx_images.c @@ -3,10 +3,10 @@ /* :::::::: */ /* mlx_images.c :+: :+: */ /* +:+ */ -/* By: W2Wizard +#+ */ +/* By: W2Wizard +#+ */ /* +#+ */ -/* Created: 2021/12/28 02:29:06 by W2Wizard #+# #+# */ -/* Updated: 2023/03/30 16:36:39 by ntamayo- ######## odam.nl */ +/* Created: 2022/01/21 15:34:45 by W2Wizard #+# #+# */ +/* Updated: 2022/07/28 18:26:59 by sbos ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -229,23 +229,13 @@ bool mlx_resize_image(mlx_image_t* img, uint32_t nwidth, uint32_t nheight) return (mlx_error(MLX_INVDIM)); if (nwidth != img->width || nheight != img->height) { - uint32_t* origin = (uint32_t*)img->pixels; - float wstep = (float)img->width / nwidth; - float hstep = (float)img->height / nheight; - - uint8_t* tempbuff = calloc(nwidth * nheight, BPP); + uint8_t* tempbuff = realloc(img->pixels, (nwidth * nheight) * BPP); if (!tempbuff) return (mlx_error(MLX_MEMFAIL)); img->pixels = tempbuff; - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, nwidth, nheight, 0, GL_RGBA, GL_UNSIGNED_BYTE, img->pixels); - - uint32_t* destin = (uint32_t*)img->pixels; - for (uint32_t j = 0; j < nheight; j++) - for (uint32_t i = 0; i < nwidth; i++) - destin[j * nwidth + i] = origin[(uint32_t)(j * hstep) * img->width + (uint32_t)(i * wstep)]; (*(uint32_t*)&img->width) = nwidth; (*(uint32_t*)&img->height) = nheight; - free(origin); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, nwidth, nheight, 0, GL_RGBA, GL_UNSIGNED_BYTE, img->pixels); } return (true); } diff --git a/MLX42/src/mlx_loop.c b/MLX42/src/mlx_loop.c index 3b20f9f..ef58240 100644 --- a/MLX42/src/mlx_loop.c +++ b/MLX42/src/mlx_loop.c @@ -6,7 +6,7 @@ /* By: W2Wizard +#+ */ /* +#+ */ /* Created: 2021/12/28 01:24:36 by W2Wizard #+# #+# */ -/* Updated: 2023/03/28 16:34:17 by W2Wizard ######## odam.nl */ +/* Updated: 2023/02/13 12:26:17 by W2Wizard ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -42,11 +42,8 @@ static void mlx_render_images(mlx_t* mlx) while (imglst) { mlx_image_t* image; - if (!(image = imglst->content)) { - mlx_error(MLX_INVIMG); - return; - } - + if (!(image = imglst->content)) + return ((void)mlx_error(MLX_INVIMG)); glBindTexture(GL_TEXTURE_2D, ((mlx_image_ctx_t*)image->context)->texture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->width, image->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels); imglst = imglst->next; diff --git a/MLX42/src/textures/mlx_texture.c b/MLX42/src/textures/mlx_texture.c index 95e69f6..2a3c1bf 100644 --- a/MLX42/src/textures/mlx_texture.c +++ b/MLX42/src/textures/mlx_texture.c @@ -3,10 +3,10 @@ /* :::::::: */ /* mlx_texture.c :+: :+: */ /* +:+ */ -/* By: W2Wizard +#+ */ +/* By: W2Wizard +#+ */ /* +#+ */ /* Created: 2022/02/17 01:02:24 by W2Wizard #+# #+# */ -/* Updated: 2023/03/09 11:03:47 by W2Wizard ######## odam.nl */ +/* Updated: 2022/06/29 15:34:25 by lde-la-h ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,24 +14,66 @@ //= Public =// +mlx_image_t* mlx_texture_area_to_image(mlx_t* mlx, mlx_texture_t* texture, uint32_t xy[2], uint32_t wh[2]) +{ + MLX_NONNULL(mlx); + MLX_NONNULL(texture); + MLX_NONNULL(xy); + MLX_NONNULL(wh); + + if (wh[0] > texture->width || wh[1] > texture->height) + return ((void*)mlx_error(MLX_INVDIM)); + if (xy[0] > texture->width || xy[1] > texture->height) + return ((void*)mlx_error(MLX_INVPOS)); + + mlx_image_t* image; + if (!(image = mlx_new_image(mlx, wh[0], wh[1]))) + return ((void*)mlx_error(MLX_MEMFAIL)); + + uint8_t* pixelx; + uint8_t* pixeli; + for (uint32_t y = 0; y < wh[1]; y++) + { + pixelx = &texture->pixels[((xy[1] + y) * texture->width + xy[0]) * BPP]; + pixeli = &image->pixels[y * wh[0] * BPP]; + memmove(pixeli, pixelx, wh[0] * BPP); + } + return (image); +} + mlx_image_t* mlx_texture_to_image(mlx_t* mlx, mlx_texture_t* texture) { MLX_NONNULL(mlx); MLX_NONNULL(texture); - mlx_image_t* image = mlx_new_image(mlx, texture->width, texture->height); - if (image == NULL) + mlx_image_t* img; + const int32_t xy[] = {0, 0}; + const uint32_t wh[] = {texture->width, texture->height}; + + if (!(img = mlx_texture_area_to_image(mlx, texture, (uint32_t*)xy, (uint32_t*)wh))) return (NULL); + return (img); +} + +bool mlx_draw_texture(mlx_image_t* image, mlx_texture_t* texture, uint32_t x, uint32_t y) +{ + MLX_NONNULL(image); + MLX_NONNULL(texture); + + if (texture->width > image->width || texture->height > image->height) + return (mlx_error(MLX_INVDIM)); + if (x > image->width || y > image->height) + return (mlx_error(MLX_INVPOS)); uint8_t* pixelx; uint8_t* pixeli; for (uint32_t i = 0; i < texture->height; i++) { pixelx = &texture->pixels[(i * texture->width) * texture->bytes_per_pixel]; - pixeli = &image->pixels[(i * image->width) * texture->bytes_per_pixel]; + pixeli = &image->pixels[((i + y) * image->width + x) * texture->bytes_per_pixel]; memmove(pixeli, pixelx, texture->width * texture->bytes_per_pixel); } - return (image); + return (true); } void mlx_delete_texture(mlx_texture_t* texture) diff --git a/MLX42/tests/CMakeLists.txt b/MLX42/tests/CMakeLists.txt index d6d066a..1bb2540 100644 --- a/MLX42/tests/CMakeLists.txt +++ b/MLX42/tests/CMakeLists.txt @@ -1,12 +1,12 @@ # ----------------------------------------------------------------------------- -# Codam Coding College, Amsterdam @ 2022-2023 by Jelle van Kraaij. +# Codam Coding College, Amsterdam @ 2022-2023 by W2Wizard. # See README in the root project for more information. # ----------------------------------------------------------------------------- set(TEST_EXECUTABLE_NAME unit_tests) # Download GoogleTest -# ----------------------------------------------------------------------------- +# -------------------------------------------------- include(GoogleTest) include(FetchContent) @@ -19,7 +19,7 @@ FetchContent_Declare( FetchContent_MakeAvailable(googletest) -# ----------------------------------------------------------------------------- +# -------------------------------------------------- add_executable( ${TEST_EXECUTABLE_NAME} tests.cpp @@ -35,6 +35,6 @@ set_property(TARGET ${TEST_EXECUTABLE_NAME} PROPERTY CXX_STANDARD 14) # Add tests to CTest # Set working directory to the the testing folder so that the test can find their test files -# ----------------------------------------------------------------------------- +# -------------------------------------------------- gtest_discover_tests(${TEST_EXECUTABLE_NAME} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} TEST_PREFIX "MLX42.") enable_testing() diff --git a/MLX42/tests/WindowFixture.hpp b/MLX42/tests/WindowFixture.hpp index 9d35aec..86ede89 100644 --- a/MLX42/tests/WindowFixture.hpp +++ b/MLX42/tests/WindowFixture.hpp @@ -1,17 +1,13 @@ -// ----------------------------------------------------------------------------- -// Codam Coding College, Amsterdam @ 2022-2023 by Jelle van Kraaij. -// See README in the root project for more information. -// ----------------------------------------------------------------------------- - # pragma once # include + # include class Window : public ::testing::Test { protected: - mlx_t* mlx = nullptr; + mlx_t* mlx = NULL; static constexpr const char* name = "MLX42"; static const int32_t height = 400; diff --git a/MLX42/tests/tests.cpp b/MLX42/tests/tests.cpp index cdca6f7..1cc47ae 100644 --- a/MLX42/tests/tests.cpp +++ b/MLX42/tests/tests.cpp @@ -1,10 +1,10 @@ -// ----------------------------------------------------------------------------- -// Codam Coding College, Amsterdam @ 2022-2023 by Jelle van Kraaij. -// See README in the root project for more information. -// ----------------------------------------------------------------------------- +#include +#include +// -------------------------------------------- // If your new to gtest follow the following documentation: // http://google.github.io/googletest/primer.html +// -------------------------------------------- #include "WindowFixture.hpp" @@ -70,7 +70,7 @@ TEST_F(Window, MultipleImages) ASSERT_NE(img2, nullptr); ASSERT_EQ(mlx_errno, MLX_SUCCESS); - int32_t val1 = mlx_image_to_window(mlx, img1, Window::width / 4, Window::height / 4); + int32_t val1 = mlx_image_to_window(mlx, img1, Window::width / 4 , Window::height / 4); EXPECT_GE(val1, 0); ASSERT_EQ(mlx_errno, MLX_SUCCESS); @@ -117,7 +117,7 @@ static void ft_draw(void* param) TEST_F(Window, stringTortureTest) { - mlx_image_t *img = mlx_new_image(mlx, Window::width / 2, Window::height / 2); + mlx_image_t *img = mlx_new_image(mlx,Window::width / 2 , Window::height / 2); ASSERT_NE(img, nullptr); ASSERT_EQ(mlx_errno, MLX_SUCCESS);