textures_image_channel.c 4.7 KB

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