You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
4.5 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [textures] example - Retrive image channel (mask)
  4. *
  5. * NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM)
  6. *
  7. * Example originally created with raylib 5.1-dev, last time updated with raylib 5.1-dev
  8. *
  9. * Example contributed by Bruno Cabral (github.com/brccabral) and reviewed by Ramon Santamaria (@raysan5)
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 2024-2024 Bruno Cabral (github.com/brccabral) and Ramon Santamaria (@raysan5)
  15. *
  16. ********************************************************************************************/
  17. #include <raylib.h>
  18. //------------------------------------------------------------------------------------
  19. // Program main entry point
  20. //------------------------------------------------------------------------------------
  21. int main(void)
  22. {
  23. // Initialization
  24. //--------------------------------------------------------------------------------------
  25. const int screenWidth = 800;
  26. const int screenHeight = 450;
  27. InitWindow(screenWidth, screenHeight, "raylib [textures] example - extract channel from image");
  28. Image fudesumiImage = LoadImage("resources/fudesumi.png");
  29. Image imageAlpha = ImageFromChannel(fudesumiImage, 3);
  30. ImageAlphaMask(&imageAlpha, imageAlpha);
  31. Image imageRed = ImageFromChannel(fudesumiImage, 0);
  32. ImageAlphaMask(&imageRed, imageAlpha);
  33. Image imageGreen = ImageFromChannel(fudesumiImage, 1);
  34. ImageAlphaMask(&imageGreen, imageAlpha);
  35. Image imageBlue = ImageFromChannel(fudesumiImage, 2);
  36. ImageAlphaMask(&imageBlue, imageAlpha);
  37. Image backgroundImage = GenImageChecked(screenWidth, screenHeight, screenWidth/20, screenHeight/20, ORANGE, YELLOW);
  38. Texture2D fudesumiTexture = LoadTextureFromImage(fudesumiImage);
  39. Texture2D textureAlpha = LoadTextureFromImage(imageAlpha);
  40. Texture2D textureRed = LoadTextureFromImage(imageRed);
  41. Texture2D textureGreen = LoadTextureFromImage(imageGreen);
  42. Texture2D textureBlue = LoadTextureFromImage(imageBlue);
  43. Texture2D backgroundTexture = LoadTextureFromImage(backgroundImage);
  44. UnloadImage(fudesumiImage);
  45. UnloadImage(imageAlpha);
  46. UnloadImage(imageRed);
  47. UnloadImage(imageGreen);
  48. UnloadImage(imageBlue);
  49. UnloadImage(backgroundImage);
  50. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  51. Rectangle fudesumiRec = {0, 0, fudesumiImage.width, fudesumiImage.height};
  52. Rectangle fudesumiPos = {50, 10, fudesumiImage.width*0.8f, fudesumiImage.height*0.8f};
  53. Rectangle redPos = { 410, 10, fudesumiPos.width / 2, fudesumiPos.height / 2 };
  54. Rectangle greenPos = { 600, 10, fudesumiPos.width / 2, fudesumiPos.height / 2 };
  55. Rectangle bluePos = { 410, 230, fudesumiPos.width / 2, fudesumiPos.height / 2 };
  56. Rectangle alphaPos = { 600, 230, fudesumiPos.width / 2, fudesumiPos.height / 2 };
  57. //--------------------------------------------------------------------------------------
  58. // Main game loop
  59. while (!WindowShouldClose()) // Detect window close button or ESC key
  60. {
  61. // Draw
  62. //----------------------------------------------------------------------------------
  63. BeginDrawing();
  64. DrawTexture(backgroundTexture, 0, 0, WHITE);
  65. DrawTexturePro(fudesumiTexture, fudesumiRec, fudesumiPos, (Vector2) {0, 0}, 0, WHITE);
  66. DrawTexturePro(textureRed, fudesumiRec, redPos, (Vector2) {0, 0}, 0, RED);
  67. DrawTexturePro(textureGreen, fudesumiRec, greenPos, (Vector2) {0, 0}, 0, GREEN);
  68. DrawTexturePro(textureBlue, fudesumiRec, bluePos, (Vector2) {0, 0}, 0, BLUE);
  69. DrawTexturePro(textureAlpha, fudesumiRec, alphaPos, (Vector2) {0, 0}, 0, WHITE);
  70. EndDrawing();
  71. //----------------------------------------------------------------------------------
  72. }
  73. // De-Initialization
  74. //--------------------------------------------------------------------------------------
  75. UnloadTexture(backgroundTexture);
  76. UnloadTexture(fudesumiTexture);
  77. UnloadTexture(textureRed);
  78. UnloadTexture(textureGreen);
  79. UnloadTexture(textureBlue);
  80. UnloadTexture(textureAlpha);
  81. CloseWindow(); // Close window and OpenGL context
  82. //--------------------------------------------------------------------------------------
  83. return 0;
  84. }